/[glob2]/glob2/src/SoundMixer.cpp
ViewVC logotype

Diff of /glob2/src/SoundMixer.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.10 by nct, Sat May 29 22:54:23 2004 UTC revision 1.11 by nct, Sat Jun 26 15:18:36 2004 UTC
# Line 25  Line 25 
25  #include <iostream>  #include <iostream>
26  #include <SDL_endian.h>  #include <SDL_endian.h>
27    
28  #define SAMPLE_COUNT_PER_SLICE 8192  #define SAMPLE_COUNT_PER_SLICE 8192*8
29    #define INTERPOLATION_RANGE 65535
30    #define INTERPOLATION_BITS 16
31    
32  #if SDL_BYTEORDER == SDL_LIL_ENDIAN  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
33  #define OGG_BYTEORDER 0  #define OGG_BYTEORDER 0
# Line 38  static int interpolationTable[SAMPLE_COU Line 40  static int interpolationTable[SAMPLE_COU
40  static void initInterpolationTable(void)  static void initInterpolationTable(void)
41  {  {
42          double l = static_cast<double>(SAMPLE_COUNT_PER_SLICE-1);          double l = static_cast<double>(SAMPLE_COUNT_PER_SLICE-1);
43          double m = 65535;          double m = INTERPOLATION_RANGE;
44          double a = - (2) / (l * l * l);          double a = - (2) / (l * l * l);
45          double b = (3) / (l * l);          double b = (3) / (l * l);
46          for (unsigned i=0; i<SAMPLE_COUNT_PER_SLICE; i++)          for (unsigned i=0; i<SAMPLE_COUNT_PER_SLICE; i++)
# Line 70  void mixaudio(void *voidMixer, Uint8 *st Line 72  void mixaudio(void *voidMixer, Uint8 *st
72                  // read first ogg                  // read first ogg
73                  rest = len;                  rest = len;
74                  p = reinterpret_cast<char *>(track0);                  p = reinterpret_cast<char *>(track0);
75                    ogg_int64_t firstPos = ov_pcm_tell(mixer->tracks[mixer->actTrack]);
76                  while(rest > 0)                  while(rest > 0)
77                  {                  {
78                          int bs;                          int bs;
# Line 89  void mixaudio(void *voidMixer, Uint8 *st Line 92  void mixaudio(void *voidMixer, Uint8 *st
92                  }                  }
93    
94                  // read second ogg                  // read second ogg
                 ogg_int64_t firstPos = ov_pcm_tell(mixer->tracks[mixer->actTrack]);  
95                  ov_pcm_seek(mixer->tracks[mixer->nextTrack], firstPos);                  ov_pcm_seek(mixer->tracks[mixer->nextTrack], firstPos);
96                  rest = len;                  rest = len;
97                  p = reinterpret_cast<char *>(track1);                  p = reinterpret_cast<char *>(track1);
# Line 114  void mixaudio(void *voidMixer, Uint8 *st Line 116  void mixaudio(void *voidMixer, Uint8 *st
116                  // mix                  // mix
117                  for (unsigned i=0; i<nsamples; i++)                  for (unsigned i=0; i<nsamples; i++)
118                  {                  {
119                          Sint16 t0 = track0[i];                          int t0 = track0[i];
120                          Sint16 t1 = track1[i];                          int t1 = track1[i];
121                          int intI = interpolationTable[i];                          int intI = interpolationTable[i];
122                          int val = (intI*t0+((65535-intI)*t1))>>16;                          int val = (intI*t1+((INTERPOLATION_RANGE-intI)*t0))>>INTERPOLATION_BITS;
123                          mix[i] = (val * vol)>>8;                          mix[i] = (val * vol)>>8;
124                  }                  }
125    
# Line 152  void mixaudio(void *voidMixer, Uint8 *st Line 154  void mixaudio(void *voidMixer, Uint8 *st
154                  // volume & fading                  // volume & fading
155                  if (mixer->mode == SoundMixer::MODE_NORMAL)                  if (mixer->mode == SoundMixer::MODE_NORMAL)
156                  {                  {
157                          for (unsigned i=0; i<nsamples; i++)                          if (vol != 255)
158                          {                                  for (unsigned i=0; i<nsamples; i++)
159                                  int t = mix[i];                                  {
160                                  mix[i] = (t * vol) >> 8;                                          int t = mix[i];
161                          }                                          mix[i] = (t * vol) >> 8;
162                                    }
163                  }                  }
164                  else if (mixer->mode == SoundMixer::MODE_START)                  else if (mixer->mode == SoundMixer::MODE_START)
165                  {                  {
166                          for (unsigned i=0; i<nsamples; i++)                          for (unsigned i=0; i<nsamples; i++)
167                          {                          {
168                                  int t = mix[i];                                  int t = mix[i];
169                                  t = (interpolationTable[i]*t) >> 16;                                  t = (interpolationTable[i]*t) >> INTERPOLATION_BITS;
170                                  mix[i] = (t * vol) >> 8;                                  mix[i] = (t * vol) >> 8;
171                          }                          }
172                          mixer->mode = SoundMixer::MODE_NORMAL;                          mixer->mode = SoundMixer::MODE_NORMAL;
# Line 174  void mixaudio(void *voidMixer, Uint8 *st Line 177  void mixaudio(void *voidMixer, Uint8 *st
177                          {                          {
178                                  int t = mix[i];                                  int t = mix[i];
179                                  int intI = interpolationTable[i];                                  int intI = interpolationTable[i];
180                                  t = ((65535-intI)*t) >> 16;                                  t = ((INTERPOLATION_RANGE-intI)*t) >> INTERPOLATION_BITS;
181                                  mix[i] = (t * vol) >> 8;                                  mix[i] = (t * vol) >> 8;
182                          }                          }
183                          mixer->mode = SoundMixer::MODE_STOPPED;                          mixer->mode = SoundMixer::MODE_STOPPED;
# Line 262  int SoundMixer::loadTrack(const char *na Line 265  int SoundMixer::loadTrack(const char *na
265                  return -2;                  return -2;
266          }          }
267    
268            SDL_LockAudio();
269          tracks.push_back(oggFile);          tracks.push_back(oggFile);
270            SDL_UnlockAudio();
271            
272          return (int)tracks.size()-1;          return (int)tracks.size()-1;
273  }  }
274    
# Line 270  void SoundMixer::setNextTrack(unsigned i Line 276  void SoundMixer::setNextTrack(unsigned i
276  {  {
277          if ((soundEnabled) && (volume) && (i<tracks.size()))          if ((soundEnabled) && (volume) && (i<tracks.size()))
278          {          {
279                    SDL_LockAudio();
280                    
281                  // Select next tracks                  // Select next tracks
282                  if (actTrack >= 0)                  if (actTrack >= 0)
283                          nextTrack = i;                          nextTrack = i;
# Line 286  void SoundMixer::setNextTrack(unsigned i Line 294  void SoundMixer::setNextTrack(unsigned i
294                  {                  {
295                          mode = MODE_EARLY_CHANGE;                          mode = MODE_EARLY_CHANGE;
296                  }                  }
297                    
298                    SDL_UnlockAudio();
299          }          }
300  }  }
301    

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26