--- np2/sound/sound.c 2004/02/18 20:11:37 1.14 +++ np2/sound/sound.c 2004/03/26 05:51:40 1.16 @@ -1,4 +1,5 @@ #include "compiler.h" +#include "wavefile.h" #include "dosio.h" #include "soundmng.h" #include "cpucore.h" @@ -26,6 +27,9 @@ typedef struct { UINT samples; UINT reserve; UINT remain; +#if defined(SUPPORT_WAVEREC) + WAVEWR rec; +#endif CBTBL *cbreg; CBTBL cb[STREAM_CBMAX]; } SNDSTREAM; @@ -61,6 +65,106 @@ static void streamprepare(UINT samples) } +#if defined(SUPPORT_WAVEREC) +// ---- wave rec + +BOOL sound_recstart(const char *filename) { + + WAVEWR rec; + + sound_recstop(); + if (sndstream.buffer == NULL) { + return(FAILURE); + } + rec = wavewr_open(filename, soundcfg.rate, 16, 2); + sndstream.rec = rec; + if (rec) { + return(SUCCESS); + } + return(FAILURE); +} + +void sound_recstop(void) { + + WAVEWR rec; + + rec = sndstream.rec; + sndstream.rec = NULL; + wavewr_close(rec); +} + +static void streamfilewrite(UINT samples) { + + CBTBL *cb; + UINT count; + SINT32 buf32[2*512]; + BYTE buf[2*2*512]; + UINT r; + UINT i; + SINT32 samp; + + while(samples) { + count = min(samples, 512); + ZeroMemory(buf32, count * 2 * sizeof(SINT32)); + cb = sndstream.cb; + while(cb < sndstream.cbreg) { + cb->cbfn(cb->hdl, buf32, count); + cb++; + } + r = min(sndstream.remain, count); + if (r) { + CopyMemory(sndstream.ptr, buf32, r * 2 * sizeof(SINT32)); + sndstream.ptr += r * 2; + sndstream.remain -= r; + } + for (i=0; i 32767) { + samp = 32767; + } + else if (samp < -32768) { + samp = -32768; + } + // little endianなので satuation_s16は使えない + buf[i*2+0] = (BYTE)samp; + buf[i*2+1] = (BYTE)(samp >> 8); + } + wavewr_write(sndstream.rec, buf, count * 4); + samples -= count; + } +} + +static void filltailsample(UINT count) { + + SINT32 *ptr; + UINT orgsize; + SINT32 sampl; + SINT32 sampr; + + count = min(sndstream.remain, count); + if (count) { + ptr = sndstream.ptr; + orgsize = (ptr - sndstream.buffer) / 2; + if (orgsize == 0) { + sampl = 0; + sampr = 0; + } + else { + sampl = *(ptr - 2); + sampr = *(ptr - 1); + } + sndstream.ptr += count * 2; + sndstream.remain -= count; + do { + ptr[0] = sampl; + ptr[1] = sampr; + ptr += 2; + } while(--count); + } +} +#endif + + // ---- BOOL sound_create(UINT rate, UINT ms) { @@ -114,6 +218,9 @@ scre_err1: void sound_destroy(void) { if (sndstream.buffer) { +#if defined(SUPPORT_WAVEREC) + sound_recstop(); +#endif soundmng_stop(); streamreset(); soundmng_destroy(); @@ -191,6 +298,12 @@ void sound_sync(void) { return; } SNDCSEC_ENTER; +#if defined(SUPPORT_WAVEREC) + if (sndstream.rec) { + streamfilewrite(length); + } + else +#endif streamprepare(length); soundcfg.lastclock += length * soundcfg.clockbase / soundcfg.hzbase; beep_eventreset(); @@ -217,7 +330,14 @@ const SINT32 *ret; ret = sndstream.buffer; if (ret) { SNDCSEC_ENTER; - if (sndstream.remain > sndstream.reserve) { + if (sndstream.remain > sndstream.reserve) +#if defined(SUPPORT_WAVEREC) + if (sndstream.rec) { + filltailsample(sndstream.remain - sndstream.reserve); + } + else +#endif + { streamprepare(sndstream.remain - sndstream.reserve); soundcfg.lastclock = CPU_CLOCK + CPU_BASECLOCK - CPU_REMCLOCK; beep_eventreset(); @@ -249,7 +369,7 @@ void sound_pcmunlock(const SINT32 *hdl) } -// ---- +// ---- pcmmix BOOL pcmmix_regist(PMIXDAT *dat, void *datptr, UINT datsize, UINT rate) {