src/audio/SDL_audiotypecvt.c
changeset 10756 073957aca821
parent 10737 3406a0f8b041
child 10799 234f71894a52
     1.1 --- a/src/audio/SDL_audiotypecvt.c	Thu Jan 05 19:30:45 2017 -0500
     1.2 +++ b/src/audio/SDL_audiotypecvt.c	Thu Jan 05 19:12:20 2017 -0500
     1.3 @@ -220,14 +220,14 @@
     1.4  SDL_Upsample_Arbitrary(SDL_AudioCVT *cvt, const int channels)
     1.5  {
     1.6      const int srcsize = cvt->len_cvt - (64 * channels);
     1.7 -    const int dstsize = (int) (((double)(cvt->len_cvt/(channels*4))) * cvt->rate_incr) * (channels*4);
     1.8 +    const int dstsize = (int) ((((double)(cvt->len_cvt/(channels*4))) * cvt->rate_incr)) * (channels*4);
     1.9      register int eps = 0;
    1.10      float *dst = ((float *) (cvt->buf + dstsize)) - channels;
    1.11      const float *src = ((float *) (cvt->buf + cvt->len_cvt)) - channels;
    1.12      const float *target = ((const float *) cvt->buf);
    1.13      const size_t cpy = sizeof (float) * channels;
    1.14 +    float sample[8];
    1.15      float last_sample[8];
    1.16 -    float sample[8];
    1.17      int i;
    1.18  
    1.19  #if DEBUG_CONVERT
    1.20 @@ -236,7 +236,9 @@
    1.21  
    1.22      SDL_assert(channels <= 8);
    1.23  
    1.24 -    SDL_memcpy(sample, src, cpy);
    1.25 +    for (i = 0; i < channels; i++) {
    1.26 +        sample[i] = (float) ((((double) src[i]) + ((double) src[i - channels])) * 0.5);
    1.27 +    }
    1.28      SDL_memcpy(last_sample, src, cpy);
    1.29  
    1.30      while (dst > target) {
    1.31 @@ -244,11 +246,15 @@
    1.32          dst -= channels;
    1.33          eps += srcsize;
    1.34          if ((eps << 1) >= dstsize) {
    1.35 -            src -= channels;
    1.36 -            for (i = 0; i < channels; i++) {
    1.37 -                sample[i] = (float) ((((double) src[i]) + ((double) last_sample[i])) * 0.5);
    1.38 +            if (src > target) {
    1.39 +                src -= channels;
    1.40 +                for (i = 0; i < channels; i++) {
    1.41 +                    sample[i] = (float) ((((double) src[i]) + ((double) last_sample[i])) * 0.5);
    1.42 +                }
    1.43 +            } else {
    1.44 +
    1.45              }
    1.46 -            SDL_memcpy(last_sample, sample, cpy);
    1.47 +            SDL_memcpy(last_sample, src, cpy);
    1.48              eps -= dstsize;
    1.49          }
    1.50      }
    1.51 @@ -291,7 +297,7 @@
    1.52              for (i = 0; i < channels; i++) {
    1.53                  sample[i] = (float) ((((double) src[i]) + ((double) last_sample[i])) * 0.5);
    1.54              }
    1.55 -            SDL_memcpy(last_sample, sample, cpy);
    1.56 +            SDL_memcpy(last_sample, src, cpy);
    1.57              eps -= srcsize;
    1.58          }
    1.59      }
    1.60 @@ -303,32 +309,43 @@
    1.61  }
    1.62  
    1.63  void
    1.64 -SDL_Upsample_x2(SDL_AudioCVT *cvt, const int channels)
    1.65 +SDL_Upsample_Multiple(SDL_AudioCVT *cvt, const int channels)
    1.66  {
    1.67 -    const int dstsize = cvt->len_cvt * 2;
    1.68 -    float *dst = ((float *) (cvt->buf + dstsize)) - (channels * 2);
    1.69 +    const int multiple = (int) cvt->rate_incr;
    1.70 +    const int dstsize = cvt->len_cvt * multiple;
    1.71 +    float *buf = (float *) cvt->buf;
    1.72 +    float *dst = ((float *) (cvt->buf + dstsize)) - channels;
    1.73      const float *src = ((float *) (cvt->buf + cvt->len_cvt)) - channels;
    1.74 -    const float *target = ((const float *) cvt->buf);
    1.75 +    const float *target = buf + channels;
    1.76      const size_t cpy = sizeof (float) * channels;
    1.77      float last_sample[8];
    1.78      int i;
    1.79  
    1.80  #if DEBUG_CONVERT
    1.81 -    fprintf(stderr, "Upsample (x2), %d channels.\n", channels);
    1.82 +    fprintf(stderr, "Upsample (x%d), %d channels.\n", multiple, channels);
    1.83  #endif
    1.84  
    1.85      SDL_assert(channels <= 8);
    1.86 +
    1.87      SDL_memcpy(last_sample, src, cpy);
    1.88  
    1.89      while (dst > target) {
    1.90 +        SDL_assert(src >= buf);
    1.91 +
    1.92          for (i = 0; i < channels; i++) {
    1.93              dst[i] = (float) ((((double)src[i]) + ((double)last_sample[i])) * 0.5);
    1.94          }
    1.95          dst -= channels;
    1.96 -        SDL_memcpy(dst, src, cpy);
    1.97 -        SDL_memcpy(last_sample, src, cpy);
    1.98 +
    1.99 +        for (i = 1; i < multiple; i++) {
   1.100 +            SDL_memcpy(dst, dst + channels, cpy);
   1.101 +            dst -= channels;
   1.102 +        }
   1.103 +
   1.104          src -= channels;
   1.105 -        dst -= channels;
   1.106 +        if (src > buf) {
   1.107 +            SDL_memcpy(last_sample, src - channels, cpy);
   1.108 +        }
   1.109      }
   1.110  
   1.111      cvt->len_cvt = dstsize;
   1.112 @@ -338,51 +355,9 @@
   1.113  }
   1.114  
   1.115  void
   1.116 -SDL_Upsample_x4(SDL_AudioCVT *cvt, const int channels)
   1.117 +SDL_Downsample_Multiple(SDL_AudioCVT *cvt, const int channels)
   1.118  {
   1.119 -    const int dstsize = cvt->len_cvt * 4;
   1.120 -    float *dst = ((float *) (cvt->buf + dstsize)) - (channels * 4);
   1.121 -    const float *src = ((float *) (cvt->buf + cvt->len_cvt)) - channels;
   1.122 -    const float *target = ((const float *) cvt->buf);
   1.123 -    const size_t cpy = sizeof (float) * channels;
   1.124 -    float last_sample[8];
   1.125 -    int i;
   1.126 -
   1.127 -#if DEBUG_CONVERT
   1.128 -    fprintf(stderr, "Upsample (x4), %d channels.\n", channels);
   1.129 -#endif
   1.130 -
   1.131 -    SDL_assert(channels <= 8);
   1.132 -    SDL_memcpy(last_sample, src, cpy);
   1.133 -
   1.134 -    while (dst > target) {
   1.135 -        for (i = 0; i < channels; i++) {
   1.136 -            dst[i] = (float) ((((double) src[i]) + (3.0 * ((double) last_sample[i]))) * 0.25);
   1.137 -        }
   1.138 -        dst -= channels;
   1.139 -        for (i = 0; i < channels; i++) {
   1.140 -            dst[i] = (float) ((((double) src[i]) + ((double) last_sample[i])) * 0.25);
   1.141 -        }
   1.142 -        dst -= channels;
   1.143 -        for (i = 0; i < channels; i++) {
   1.144 -            dst[i] = (float) (((3.0 * ((double) src[i])) + ((double) last_sample[i])) * 0.25);
   1.145 -        }
   1.146 -        dst -= channels;
   1.147 -        SDL_memcpy(dst, src, cpy);
   1.148 -        dst -= channels;
   1.149 -        SDL_memcpy(last_sample, src, cpy);
   1.150 -        src -= channels;
   1.151 -    }
   1.152 -
   1.153 -    cvt->len_cvt = dstsize;
   1.154 -    if (cvt->filters[++cvt->filter_index]) {
   1.155 -        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   1.156 -    }
   1.157 -}
   1.158 -
   1.159 -void
   1.160 -SDL_Downsample_Multiple(SDL_AudioCVT *cvt, const int multiple, const int channels)
   1.161 -{
   1.162 +    const int multiple = (int) (1.0 / cvt->rate_incr);
   1.163      const int dstsize = cvt->len_cvt / multiple;
   1.164      float *dst = (float *) cvt->buf;
   1.165      const float *src = (float *) cvt->buf;