Diff for /np2/sound/sound.c between versions 1.14 and 1.22

version 1.14, 2004/02/18 20:11:37 version 1.22, 2011/01/15 18:04:43
Line 1 Line 1
 #include        "compiler.h"  #include        "compiler.h"
   #include        "wavefile.h"
 #include        "dosio.h"  #include        "dosio.h"
 #include        "soundmng.h"  #include        "soundmng.h"
 #include        "cpucore.h"  #include        "cpucore.h"
Line 26  typedef struct { Line 27  typedef struct {
         UINT    samples;          UINT    samples;
         UINT    reserve;          UINT    reserve;
         UINT    remain;          UINT    remain;
   #if defined(SUPPORT_WAVEREC)
           WAVEWR  rec;
   #endif
         CBTBL   *cbreg;          CBTBL   *cbreg;
         CBTBL   cb[STREAM_CBMAX];          CBTBL   cb[STREAM_CBMAX];
 } SNDSTREAM;  } SNDSTREAM;
Line 61  static void streamprepare(UINT samples)  Line 65  static void streamprepare(UINT samples) 
 }  }
   
   
   #if defined(SUPPORT_WAVEREC)
   // ---- wave rec
   
   BOOL sound_recstart(const OEMCHAR *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];
           UINT8   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<count*2; i++) {
                           samp = buf32[i];
                           if (samp > 32767) {
                                   samp = 32767;
                           }
                           else if (samp < -32768) {
                                   samp = -32768;
                           }
                           // little endianなので satuation_s16は使えない
                           buf[i*2+0] = (UINT8)samp;
                           buf[i*2+1] = (UINT8)(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) {  BOOL sound_create(UINT rate, UINT ms) {
Line 114  scre_err1: Line 218  scre_err1:
 void sound_destroy(void) {  void sound_destroy(void) {
   
         if (sndstream.buffer) {          if (sndstream.buffer) {
   #if defined(SUPPORT_WAVEREC)
                   sound_recstop();
   #endif
                 soundmng_stop();                  soundmng_stop();
                 streamreset();                  streamreset();
                 soundmng_destroy();                  soundmng_destroy();
Line 135  void sound_reset(void) { Line 242  void sound_reset(void) {
   
 void sound_changeclock(void) {  void sound_changeclock(void) {
   
         UINT32  clock;          UINT32  clk;
         UINT    hz;          UINT    hz;
         UINT    hzmax;          UINT    hzmax;
   
Line 144  void sound_changeclock(void) { Line 251  void sound_changeclock(void) {
         }          }
   
         // とりあえず 25で割り切れる。          // とりあえず 25で割り切れる。
         clock = pccore.realclock / 25;          clk = pccore.realclock / 25;
         hz = soundcfg.rate / 25;          hz = soundcfg.rate / 25;
   
         // で、クロック数に合せて調整。(64bit演算しろよな的)          // で、クロック数に合せて調整。(64bit演算しろよな的)
         hzmax = (1 << (32 - 8)) / (clock >> 8);          hzmax = (1 << (32 - 8)) / (clk >> 8);
         while(hzmax < hz) {          while(hzmax < hz) {
                 clock = (clock + 1) >> 1;                  clk = (clk + 1) >> 1;
                 hz = (hz + 1) >> 1;                  hz = (hz + 1) >> 1;
         }          }
           TRACEOUT(("hzbase/clockbase = %d/%d", hz, clk));
         soundcfg.hzbase = hz;          soundcfg.hzbase = hz;
         soundcfg.clockbase = clock;          soundcfg.clockbase = clk;
         soundcfg.minclock = 2 * clock / hz;          soundcfg.minclock = 2 * clk / hz;
         soundcfg.lastclock = CPU_CLOCK;          soundcfg.lastclock = CPU_CLOCK;
 }  }
   
Line 191  void sound_sync(void) { Line 299  void sound_sync(void) {
                 return;                  return;
         }          }
         SNDCSEC_ENTER;          SNDCSEC_ENTER;
         streamprepare(length);  #if defined(SUPPORT_WAVEREC)
           if (sndstream.rec) {
                   streamfilewrite(length);
           }
           else
   #endif
                   streamprepare(length);
         soundcfg.lastclock += length * soundcfg.clockbase / soundcfg.hzbase;          soundcfg.lastclock += length * soundcfg.clockbase / soundcfg.hzbase;
         beep_eventreset();          beep_eventreset();
         SNDCSEC_LEAVE;          SNDCSEC_LEAVE;
Line 217  const SINT32 *ret; Line 331  const SINT32 *ret;
         ret = sndstream.buffer;          ret = sndstream.buffer;
         if (ret) {          if (ret) {
                 SNDCSEC_ENTER;                  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);                          streamprepare(sndstream.remain - sndstream.reserve);
                         soundcfg.lastclock = CPU_CLOCK + CPU_BASECLOCK - CPU_REMCLOCK;                          soundcfg.lastclock = CPU_CLOCK + CPU_BASECLOCK - CPU_REMCLOCK;
                         beep_eventreset();                          beep_eventreset();
Line 249  void sound_pcmunlock(const SINT32 *hdl)  Line 370  void sound_pcmunlock(const SINT32 *hdl) 
 }  }
   
   
 // ----  // ---- pcmmix
   
 BOOL pcmmix_regist(PMIXDAT *dat, void *datptr, UINT datsize, UINT rate) {  BRESULT pcmmix_regist(PMIXDAT *dat, void *datptr, UINT datsize, UINT rate) {
   
         GETSND  gs;          GETSND  gs;
         BYTE    tmp[256];          UINT8   tmp[256];
         UINT    size;          UINT    size;
         UINT    r;          UINT    r;
         SINT16  *buf;          SINT16  *buf;
Line 300  pmr_err1: Line 421  pmr_err1:
         return(FAILURE);          return(FAILURE);
 }  }
   
 BOOL pcmmix_regfile(PMIXDAT *dat, const char *fname, UINT rate) {  BRESULT pcmmix_regfile(PMIXDAT *dat, const OEMCHAR *fname, UINT rate) {
   
         FILEH   fh;          FILEH   fh;
         UINT    size;          UINT    size;
         BYTE    *ptr;          UINT8   *ptr;
         BOOL    r;          BRESULT r;
   
         r = FAILURE;          r = FAILURE;
         fh = file_open_rb(fname);          fh = file_open_rb(fname);
Line 316  BOOL pcmmix_regfile(PMIXDAT *dat, const  Line 437  BOOL pcmmix_regfile(PMIXDAT *dat, const 
         if (size == 0) {          if (size == 0) {
                 goto pmrf_err2;                  goto pmrf_err2;
         }          }
         ptr = (BYTE *)_MALLOC(size, fname);          ptr = (UINT8 *)_MALLOC(size, fname);
         if (ptr == NULL) {          if (ptr == NULL) {
                 goto pmrf_err2;                  goto pmrf_err2;
         }          }

Removed from v.1.14  
changed lines
  Added in v.1.22


RetroPC.NET-CVS <cvs@retropc.net>