--- np2/sound/sound.c 2003/10/20 06:36:33 1.4 +++ np2/sound/sound.c 2004/01/08 16:29:56 1.11 @@ -1,13 +1,12 @@ #include "compiler.h" #include "soundmng.h" -#include "i286.h" +#include "cpucore.h" #include "pccore.h" #include "iocore.h" #include "sound.h" #include "sndcsec.h" #include "beep.h" - SOUNDCFG soundcfg; @@ -22,6 +21,7 @@ typedef struct { SINT32 *buffer; SINT32 *ptr; UINT samples; + UINT reserve; UINT remain; CBTBL *cbreg; CBTBL cb[STREAM_CBMAX]; @@ -32,9 +32,11 @@ static SNDSTREAM sndstream; static void streamreset(void) { + SNDCSEC_ENTER; sndstream.ptr = sndstream.buffer; - sndstream.remain = sndstream.samples; + sndstream.remain = sndstream.samples + sndstream.reserve; sndstream.cbreg = sndstream.cb; + SNDCSEC_LEAVE; } static void streamprepare(UINT samples) { @@ -61,6 +63,7 @@ static void streamprepare(UINT samples) BOOL sound_create(UINT rate, UINT ms) { UINT samples; + UINT reserve; ZeroMemory(&sndstream, sizeof(sndstream)); switch(rate) { @@ -81,15 +84,21 @@ BOOL sound_create(UINT rate, UINT ms) { soundcfg.rate = rate; sound_changeclock(); - sndstream.buffer = (SINT32 *)_MALLOC(samples * 2 * sizeof(SINT32), - "stream"); +#if defined(SOUNDRESERVE) + reserve = rate * SOUNDRESERVE / 1000; +#else + reserve = 0; +#endif + sndstream.buffer = (SINT32 *)_MALLOC((samples + reserve) * 2 + * sizeof(SINT32), "stream"); if (sndstream.buffer == NULL) { goto scre_err2; } sndstream.samples = samples; - streamreset(); + sndstream.reserve = reserve; SNDCSEC_INIT; + streamreset(); return(SUCCESS); scre_err2: @@ -102,10 +111,10 @@ scre_err1: void sound_destroy(void) { if (sndstream.buffer) { - SNDCSEC_TERM; - soundmng_stop(); + streamreset(); soundmng_destroy(); + SNDCSEC_TERM; _MFREE(sndstream.buffer); sndstream.buffer = NULL; } @@ -116,7 +125,7 @@ void sound_reset(void) { if (sndstream.buffer) { soundmng_reset(); streamreset(); - soundcfg.lastclock = I286_CLOCK; + soundcfg.lastclock = CPU_CLOCK; beep_eventreset(); } } @@ -144,7 +153,7 @@ void sound_changeclock(void) { soundcfg.hzbase = hz; soundcfg.clockbase = clock; soundcfg.minclock = 2 * clock / hz; - soundcfg.lastclock = I286_CLOCK; + soundcfg.lastclock = CPU_CLOCK; } void sound_streamregist(void *hdl, SOUNDCB cbfn) { @@ -170,7 +179,7 @@ void sound_sync(void) { return; } - length = I286_CLOCK + I286_BASECLOCK - I286_REMCLOCK - soundcfg.lastclock; + length = CPU_CLOCK + CPU_BASECLOCK - CPU_REMCLOCK - soundcfg.lastclock; if (length < soundcfg.minclock) { return; } @@ -180,39 +189,59 @@ void sound_sync(void) { } SNDCSEC_ENTER; streamprepare(length); - SNDCSEC_LEAVE; - soundcfg.writecount += length; soundcfg.lastclock += length * soundcfg.clockbase / soundcfg.hzbase; beep_eventreset(); + SNDCSEC_LEAVE; + soundcfg.writecount += length; if (soundcfg.writecount >= 100) { soundcfg.writecount = 0; soundmng_sync(); } } +static volatile int locks = 0; + const SINT32 *sound_pcmlock(void) { const SINT32 *ret; + if (locks) { + TRACEOUT(("sound pcm lock: already locked")); + return(NULL); + } + locks++; ret = sndstream.buffer; if (ret) { SNDCSEC_ENTER; - if (sndstream.remain) { - streamprepare(sndstream.remain); - soundcfg.lastclock = I286_CLOCK + I286_BASECLOCK - I286_REMCLOCK; + if (sndstream.remain > sndstream.reserve) { + streamprepare(sndstream.remain - sndstream.reserve); + soundcfg.lastclock = CPU_CLOCK + CPU_BASECLOCK - CPU_REMCLOCK; beep_eventreset(); } } + else { + locks--; + } return(ret); } void sound_pcmunlock(const SINT32 *hdl) { + int leng; + if (hdl) { - sndstream.ptr = sndstream.buffer; - sndstream.remain = sndstream.samples; + leng = sndstream.reserve - sndstream.remain; + if (leng > 0) { + CopyMemory(sndstream.buffer, + sndstream.buffer + (sndstream.samples * 2), + leng * 2 * sizeof(SINT32)); + } + sndstream.ptr = sndstream.buffer + (leng * 2); + sndstream.remain = sndstream.samples + sndstream.reserve - leng; +// sndstream.remain += sndstream.samples; SNDCSEC_LEAVE; + locks--; } }