--- np2/sound/beepg.c 2003/10/20 12:08:01 1.3 +++ np2/sound/beepg.c 2004/07/04 08:30:16 1.6 @@ -1,86 +1,147 @@ #include "compiler.h" -#include "pccore.h" #include "sound.h" #include "beep.h" extern BEEPCFG beepcfg; - static void oneshot(BEEP bp, SINT32 *pcm, UINT count) { - SINT32 vol; - SINT32 samp; - SINT32 remain; - BPEVENT *bev; + SINT32 vol; +const BPEVENT *bev; + SINT32 clock; + int event; + SINT32 remain; + SINT32 samp; vol = beepcfg.vol; bev = bp->event; - + if (bp->events) { + bp->events--; + clock = bev->clock; + event = bev->enable; + bev++; + } + else { + clock = 0x40000000; + event = bp->lastenable; + } do { remain = (1 << 16); samp = 0; - while(remain >= bev->clock) { - if (--bp->events) { - remain -= bev->clock; - if (bp->lastenable) { - samp += bev->clock; - } - bp->lastenable = bev->enable; + while(remain >= clock) { + remain -= clock; + if (bp->lastenable) { + samp += clock; + } + bp->lastenable = event; + if (bp->events) { + bp->events--; + clock = bev->clock; + event = bev->enable; bev++; } - else { // サンプルがぽなくなり - if (bp->lastenable) { - samp += remain; - samp *= vol; - samp >>= (16 - 11); - do { - pcm[0] += samp; - pcm[1] += samp; - pcm += 2; - samp >>= 1; - } while(--count); - } - else { - samp *= vol; - samp >>= (16 - 11); - pcm[0] += samp; - pcm[1] += samp; - } - bp->lastenable = bev->enable; - return; + else { + clock = 0x40000000; } } - bev->clock -= remain; + clock -= remain; if (bp->lastenable) { samp += remain; } samp *= vol; - samp >>= (16 - 11); + samp >>= (16 - 10); pcm[0] += samp; pcm[1] += samp; pcm += 2; } while(--count); + bp->lastenable = event; + bp->events = 0; } -static void rategenerate(BEEP bp, SINT32 *pcm, UINT count) { +static void rategenerator(BEEP bp, SINT32 *pcm, UINT count) { - UINT vol; - SINT32 samp; - UINT r; + SINT32 vol; +const BPEVENT *bev; + SINT32 samp; + SINT32 remain; + SINT32 clock; + int event; + UINT r; - vol = beepcfg.vol << 8; + vol = beepcfg.vol; + bev = bp->event; + if (bp->events) { + bp->events--; + clock = bev->clock; + event = bev->enable; + bev++; + } + else { + clock = 0x40000000; + event = bp->lastenable; + } do { - samp = 0; - r = 4; - do { - bp->cnt += bp->hz; - samp += vol * ((bp->cnt & 0x8000)?1:-1); - } while(--r); - pcm[0] += samp; - pcm[1] += samp; - pcm += 2; - } while(--count); + if (clock >= (1 << 16)) { + r = clock >> 16; + r = min(r, count); + clock -= r << 16; + count -= r; + if (bp->lastenable) { + do { + samp = (bp->cnt & 0x8000)?1:-1; + bp->cnt += bp->hz; + samp += (bp->cnt & 0x8000)?1:-1; + bp->cnt += bp->hz; + samp += (bp->cnt & 0x8000)?1:-1; + bp->cnt += bp->hz; + samp += (bp->cnt & 0x8000)?1:-1; + bp->cnt += bp->hz; + samp *= vol; + samp <<= (10 - 2); + pcm[0] += samp; + pcm[1] += samp; + pcm += 2; + } while(--r); + } + else { + pcm += 2 * r; + } + } + else { + remain = (1 << 16); + samp = 0; + while(remain >= clock) { + remain -= clock; + if (bp->lastenable) { + samp += clock; + } + bp->lastenable = event; + bp->cnt = 0; + if (bp->events) { + bp->events--; + clock = bev->clock; + event = bev->enable; + bev++; + } + else { + clock = 0x40000000; + } + } + clock -= remain; + if (bp->lastenable) { + samp += remain; + } + samp *= vol; + samp >>= (16 - 10); + pcm[0] += samp; + pcm[1] += samp; + pcm += 2; + count--; + } + } while(count); + bp->lastenable = event; + bp->events = 0; } void SOUNDCALL beep_getpcm(BEEP bp, SINT32 *pcm, UINT count) { @@ -92,13 +153,7 @@ void SOUNDCALL beep_getpcm(BEEP bp, SINT } } else if (bp->mode == 1) { - if (bp->buz) { - if (bp->hz) { - rategenerate(bp, pcm, count); - } - else { - } - } + rategenerator(bp, pcm, count); } } }