src/audio/SDL_audiocvt.c
changeset 10817 efc103e60c5b
parent 10814 938218064f67
child 10830 92013fad89d1
equal deleted inserted replaced
10816:c8d44526965e 10817:efc103e60c5b
   194 static int
   194 static int
   195 SDL_ResampleAudioSimple(const int chans, const double rate_incr,
   195 SDL_ResampleAudioSimple(const int chans, const double rate_incr,
   196                         float *last_sample, const float *inbuf,
   196                         float *last_sample, const float *inbuf,
   197                         const int inbuflen, float *outbuf, const int outbuflen)
   197                         const int inbuflen, float *outbuf, const int outbuflen)
   198 {
   198 {
   199     const int framelen = chans * sizeof (float);
   199     const int framelen = chans * (int)sizeof (float);
   200     const int total = (inbuflen / framelen);
   200     const int total = (inbuflen / framelen);
   201     const int finalpos = total - chans;
   201     const int finalpos = (total * chans) - chans;
       
   202     const int dest_samples = (int)(((double)total) * rate_incr);
   202     const double src_incr = 1.0 / rate_incr;
   203     const double src_incr = 1.0 / rate_incr;
       
   204     float *dst = outbuf;
       
   205     float *target = (dst + (dest_samples * chans));
   203     double idx = 0.0;
   206     double idx = 0.0;
   204     float *dst = outbuf;
       
   205     int consumed = 0;
       
   206     int i;
   207     int i;
   207 
   208 
       
   209     SDL_assert((dest_samples * framelen) <= outbuflen);
   208     SDL_assert((inbuflen % framelen) == 0);
   210     SDL_assert((inbuflen % framelen) == 0);
   209 
   211 
   210     while (consumed < total) {
   212     while(dst < target) {
   211         const int pos = ((int)idx) * chans;
   213         const int pos = ((int)idx) * chans;
   212         const float *src = &inbuf[(pos >= finalpos) ? finalpos : pos];
   214         const float *src = &inbuf[(pos >= finalpos) ? finalpos : pos];
   213         SDL_assert(dst < (outbuf + (outbuflen / framelen)));
       
   214         for (i = 0; i < chans; i++) {
   215         for (i = 0; i < chans; i++) {
   215             const float val = *(src++);
   216             const float val = *(src++);
   216             *(dst++) = (val + last_sample[i]) * 0.5f;
   217             *(dst++) = (val + last_sample[i]) * 0.5f;
   217             last_sample[i] = val;
   218             last_sample[i] = val;
   218         }
   219         }
   219         consumed = pos + chans;
       
   220         idx += src_incr;
   220         idx += src_incr;
   221     }
   221     }
   222 
   222 
   223     return (int) ((dst - outbuf) * sizeof (float));
   223     return (int) ((dst - outbuf) * (int)sizeof(float));
   224 }
   224 }
   225 
   225 
   226 
   226 
   227 int
   227 int
   228 SDL_ConvertAudio(SDL_AudioCVT * cvt)
   228 SDL_ConvertAudio(SDL_AudioCVT * cvt)