src/audio/SDL_audio.c
changeset 9398 c41dd34e4996
parent 9397 d72d2aa46341
child 9399 a684dbd185c7
     1.1 --- a/src/audio/SDL_audio.c	Thu Mar 19 13:27:10 2015 -0400
     1.2 +++ b/src/audio/SDL_audio.c	Thu Mar 19 13:34:17 2015 -0400
     1.3 @@ -286,67 +286,6 @@
     1.4  #undef FILL_STUB
     1.5  }
     1.6  
     1.7 -#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
     1.8 -/* Streaming functions (for when the input and output buffer sizes are different) */
     1.9 -/* Write [length] bytes from buf into the streamer */
    1.10 -static void
    1.11 -SDL_StreamWrite(SDL_AudioStreamer * stream, Uint8 * buf, int length)
    1.12 -{
    1.13 -    int i;
    1.14 -
    1.15 -    for (i = 0; i < length; ++i) {
    1.16 -        stream->buffer[stream->write_pos] = buf[i];
    1.17 -        ++stream->write_pos;
    1.18 -    }
    1.19 -}
    1.20 -
    1.21 -/* Read [length] bytes out of the streamer into buf */
    1.22 -static void
    1.23 -SDL_StreamRead(SDL_AudioStreamer * stream, Uint8 * buf, int length)
    1.24 -{
    1.25 -    int i;
    1.26 -
    1.27 -    for (i = 0; i < length; ++i) {
    1.28 -        buf[i] = stream->buffer[stream->read_pos];
    1.29 -        ++stream->read_pos;
    1.30 -    }
    1.31 -}
    1.32 -
    1.33 -static int
    1.34 -SDL_StreamLength(SDL_AudioStreamer * stream)
    1.35 -{
    1.36 -    return (stream->write_pos - stream->read_pos) % stream->max_len;
    1.37 -}
    1.38 -
    1.39 -/* Initialize the stream by allocating the buffer and setting the read/write heads to the beginning */
    1.40 -#if 0
    1.41 -static int
    1.42 -SDL_StreamInit(SDL_AudioStreamer * stream, int max_len, Uint8 silence)
    1.43 -{
    1.44 -    /* First try to allocate the buffer */
    1.45 -    stream->buffer = (Uint8 *) SDL_malloc(max_len);
    1.46 -    if (stream->buffer == NULL) {
    1.47 -        return -1;
    1.48 -    }
    1.49 -
    1.50 -    stream->max_len = max_len;
    1.51 -    stream->read_pos = 0;
    1.52 -    stream->write_pos = 0;
    1.53 -
    1.54 -    /* Zero out the buffer */
    1.55 -    SDL_memset(stream->buffer, silence, max_len);
    1.56 -
    1.57 -    return 0;
    1.58 -}
    1.59 -#endif
    1.60 -
    1.61 -/* Deinitialize the stream simply by freeing the buffer */
    1.62 -static void
    1.63 -SDL_StreamDeinit(SDL_AudioStreamer * stream)
    1.64 -{
    1.65 -    SDL_free(stream->buffer);
    1.66 -}
    1.67 -#endif
    1.68  
    1.69  /* device hotplug support... */
    1.70  
    1.71 @@ -655,17 +594,12 @@
    1.72  SDL_RunAudio(void *devicep)
    1.73  {
    1.74      SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
    1.75 +    const int silence = (int) device->spec.silence;
    1.76 +    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
    1.77 +    const int stream_len = (device->convert.needed) ? device->convert.len : device->spec.size;
    1.78      Uint8 *stream;
    1.79 -    int stream_len;
    1.80 -    void *udata;
    1.81 -    void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
    1.82 -    Uint32 delay;
    1.83 -
    1.84 -#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
    1.85 -    /* For streaming when the buffer sizes don't match up */
    1.86 -    Uint8 *istream;
    1.87 -    int istream_len = 0;
    1.88 -#endif
    1.89 +    void *udata = device->spec.userdata;
    1.90 +    void (SDLCALL *fill) (void *, Uint8 *, int) = device->spec.callback;
    1.91  
    1.92      /* The audio mixing is always a high priority thread */
    1.93      SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
    1.94 @@ -674,202 +608,59 @@
    1.95      device->threadid = SDL_ThreadID();
    1.96      current_audio.impl.ThreadInit(device);
    1.97  
    1.98 -    /* Set up the mixing function */
    1.99 -    fill = device->spec.callback;
   1.100 -    udata = device->spec.userdata;
   1.101 -
   1.102 -    /* By default do not stream */
   1.103 -    device->use_streamer = 0;
   1.104 -
   1.105 -    if (device->convert.needed) {
   1.106 -#if 0                           /* !!! FIXME: I took len_div out of the structure. Use rate_incr instead? */
   1.107 -        /* If the result of the conversion alters the length, i.e. resampling is being used, use the streamer */
   1.108 -        if (device->convert.len_mult != 1 || device->convert.len_div != 1) {
   1.109 -            /* The streamer's maximum length should be twice whichever is larger: spec.size or len_cvt */
   1.110 -            stream_max_len = 2 * device->spec.size;
   1.111 -            if (device->convert.len_mult > device->convert.len_div) {
   1.112 -                stream_max_len *= device->convert.len_mult;
   1.113 -                stream_max_len /= device->convert.len_div;
   1.114 -            }
   1.115 -            if (SDL_StreamInit(&device->streamer, stream_max_len, silence) <
   1.116 -                0)
   1.117 -                return -1;
   1.118 -            device->use_streamer = 1;
   1.119 -
   1.120 -            /* istream_len should be the length of what we grab from the callback and feed to conversion,
   1.121 -               so that we get close to spec_size. I.e. we want device.spec_size = istream_len * u / d
   1.122 -             */
   1.123 -            istream_len =
   1.124 -                device->spec.size * device->convert.len_div /
   1.125 -                device->convert.len_mult;
   1.126 +    /* Loop, filling the audio buffers */
   1.127 +    while (!device->shutdown) {
   1.128 +        /* Fill the current buffer with sound */
   1.129 +        if (device->convert.needed) {
   1.130 +            stream = device->convert.buf;
   1.131 +        } else if (device->enabled) {
   1.132 +            stream = current_audio.impl.GetDeviceBuf(device);
   1.133 +        } else {
   1.134 +            /* if the device isn't enabled, we still write to the
   1.135 +               fake_stream, so the app's callback will fire with
   1.136 +               a regular frequency, in case they depend on that
   1.137 +               for timing or progress. They can use hotplug
   1.138 +               now to know if the device failed. */
   1.139 +            stream = NULL;
   1.140          }
   1.141 -#endif
   1.142 -        stream_len = device->convert.len;
   1.143 -    } else {
   1.144 -        stream_len = device->spec.size;
   1.145 -    }
   1.146 -
   1.147 -    /* Calculate the delay while paused */
   1.148 -    delay = ((device->spec.samples * 1000) / device->spec.freq);
   1.149 -
   1.150 -    /* Determine if the streamer is necessary here */
   1.151 -#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
   1.152 -    if (device->use_streamer == 1) {
   1.153 -        /* This code is almost the same as the old code. The difference is, instead of reading
   1.154 -           directly from the callback into "stream", then converting and sending the audio off,
   1.155 -           we go: callback -> "istream" -> (conversion) -> streamer -> stream -> device.
   1.156 -           However, reading and writing with streamer are done separately:
   1.157 -           - We only call the callback and write to the streamer when the streamer does not
   1.158 -           contain enough samples to output to the device.
   1.159 -           - We only read from the streamer and tell the device to play when the streamer
   1.160 -           does have enough samples to output.
   1.161 -           This allows us to perform resampling in the conversion step, where the output of the
   1.162 -           resampling process can be any number. We will have to see what a good size for the
   1.163 -           stream's maximum length is, but I suspect 2*max(len_cvt, stream_len) is a good figure.
   1.164 -         */
   1.165 -        while (!device->shutdown) {
   1.166 -
   1.167 -            if (device->paused) {
   1.168 -                SDL_Delay(delay);
   1.169 -                continue;
   1.170 -            }
   1.171  
   1.172 -            /* Only read in audio if the streamer doesn't have enough already (if it does not have enough samples to output) */
   1.173 -            if (SDL_StreamLength(&device->streamer) < stream_len) {
   1.174 -                /* Set up istream */
   1.175 -                if (device->convert.needed) {
   1.176 -                    if (device->convert.buf) {
   1.177 -                        istream = device->convert.buf;
   1.178 -                    } else {
   1.179 -                        continue;
   1.180 -                    }
   1.181 -                } else {
   1.182 -/* FIXME: Ryan, this is probably wrong.  I imagine we don't want to get
   1.183 - * a device buffer both here and below in the stream output.
   1.184 - */
   1.185 -                    istream = current_audio.impl.GetDeviceBuf(device);
   1.186 -                    if (istream == NULL) {
   1.187 -                        istream = device->fake_stream;
   1.188 -                    }
   1.189 -                }
   1.190 -
   1.191 -                /* Read from the callback into the _input_ stream */
   1.192 -                // !!! FIXME: this should be LockDevice.
   1.193 -                SDL_LockMutex(device->mixer_lock);
   1.194 -                (*fill) (udata, istream, istream_len);
   1.195 -                SDL_UnlockMutex(device->mixer_lock);
   1.196 -
   1.197 -                /* Convert the audio if necessary and write to the streamer */
   1.198 -                if (device->convert.needed) {
   1.199 -                    SDL_ConvertAudio(&device->convert);
   1.200 -                    if (istream == NULL) {
   1.201 -                        istream = device->fake_stream;
   1.202 -                    }
   1.203 -                    /* SDL_memcpy(istream, device->convert.buf, device->convert.len_cvt); */
   1.204 -                    SDL_StreamWrite(&device->streamer, device->convert.buf,
   1.205 -                                    device->convert.len_cvt);
   1.206 -                } else {
   1.207 -                    SDL_StreamWrite(&device->streamer, istream, istream_len);
   1.208 -                }
   1.209 -            }
   1.210 +        if (stream == NULL) {
   1.211 +            stream = device->fake_stream;
   1.212 +        }
   1.213  
   1.214 -            /* Only output audio if the streamer has enough to output */
   1.215 -            if (SDL_StreamLength(&device->streamer) >= stream_len) {
   1.216 -                /* Set up the output stream */
   1.217 -                if (device->convert.needed) {
   1.218 -                    if (device->convert.buf) {
   1.219 -                        stream = device->convert.buf;
   1.220 -                    } else {
   1.221 -                        continue;
   1.222 -                    }
   1.223 -                } else {
   1.224 -                    stream = current_audio.impl.GetDeviceBuf(device);
   1.225 -                    if (stream == NULL) {
   1.226 -                        stream = device->fake_stream;
   1.227 -                    }
   1.228 -                }
   1.229 -
   1.230 -                /* Now read from the streamer */
   1.231 -                SDL_StreamRead(&device->streamer, stream, stream_len);
   1.232 +        /* !!! FIXME: this should be LockDevice. */
   1.233 +        SDL_LockMutex(device->mixer_lock);
   1.234 +        if (device->paused) {
   1.235 +            SDL_memset(stream, silence, stream_len);
   1.236 +        } else {
   1.237 +            (*fill) (udata, stream, stream_len);
   1.238 +        }
   1.239 +        SDL_UnlockMutex(device->mixer_lock);
   1.240  
   1.241 -                /* Ready current buffer for play and change current buffer */
   1.242 -                if (stream != device->fake_stream) {
   1.243 -                    current_audio.impl.PlayDevice(device);
   1.244 -                    /* Wait for an audio buffer to become available */
   1.245 -                    current_audio.impl.WaitDevice(device);
   1.246 -                } else {
   1.247 -                    SDL_Delay(delay);
   1.248 -                }
   1.249 -            }
   1.250 -
   1.251 -        }
   1.252 -    } else
   1.253 -#endif
   1.254 -    {
   1.255 -        /* Otherwise, do not use the streamer. This is the old code. */
   1.256 -        const int silence = (int) device->spec.silence;
   1.257 -
   1.258 -        /* Loop, filling the audio buffers */
   1.259 -        while (!device->shutdown) {
   1.260 -            /* Fill the current buffer with sound */
   1.261 -            if (device->convert.needed) {
   1.262 -                stream = device->convert.buf;
   1.263 -            } else if (device->enabled) {
   1.264 -                stream = current_audio.impl.GetDeviceBuf(device);
   1.265 -            } else {
   1.266 -                /* if the device isn't enabled, we still write to the
   1.267 -                    fake_stream, so the app's callback will fire with
   1.268 -                    a regular frequency, in case they depend on that
   1.269 -                    for timing or progress. They can use hotplug
   1.270 -                    now to know if the device failed. */
   1.271 -                stream = NULL;
   1.272 -            }
   1.273 -
   1.274 +        /* Convert the audio if necessary */
   1.275 +        if (device->enabled && device->convert.needed) {
   1.276 +            SDL_ConvertAudio(&device->convert);
   1.277 +            stream = current_audio.impl.GetDeviceBuf(device);
   1.278              if (stream == NULL) {
   1.279                  stream = device->fake_stream;
   1.280 +            } else {
   1.281 +                SDL_memcpy(stream, device->convert.buf,
   1.282 +                           device->convert.len_cvt);
   1.283              }
   1.284 -
   1.285 -            /* !!! FIXME: this should be LockDevice. */
   1.286 -            SDL_LockMutex(device->mixer_lock);
   1.287 -            if (device->paused) {
   1.288 -                SDL_memset(stream, silence, stream_len);
   1.289 -            } else {
   1.290 -                (*fill) (udata, stream, stream_len);
   1.291 -            }
   1.292 -            SDL_UnlockMutex(device->mixer_lock);
   1.293 +        }
   1.294  
   1.295 -            /* Convert the audio if necessary */
   1.296 -            if (device->enabled && device->convert.needed) {
   1.297 -                SDL_ConvertAudio(&device->convert);
   1.298 -                stream = current_audio.impl.GetDeviceBuf(device);
   1.299 -                if (stream == NULL) {
   1.300 -                    stream = device->fake_stream;
   1.301 -                } else {
   1.302 -                    SDL_memcpy(stream, device->convert.buf,
   1.303 -                               device->convert.len_cvt);
   1.304 -                }
   1.305 -            }
   1.306 -
   1.307 -            /* Ready current buffer for play and change current buffer */
   1.308 -            if (stream != device->fake_stream) {
   1.309 -                current_audio.impl.PlayDevice(device);
   1.310 -                /* Wait for an audio buffer to become available */
   1.311 -                current_audio.impl.WaitDevice(device);
   1.312 -            } else {
   1.313 -                SDL_Delay(delay);
   1.314 -            }
   1.315 +        /* Ready current buffer for play and change current buffer */
   1.316 +        if (stream == device->fake_stream) {
   1.317 +            SDL_Delay(delay);
   1.318 +        } else {
   1.319 +            current_audio.impl.PlayDevice(device);
   1.320 +            current_audio.impl.WaitDevice(device);
   1.321          }
   1.322      }
   1.323  
   1.324 -    /* Wait for the audio to drain.. */
   1.325 +    /* Wait for the audio to drain. */
   1.326      current_audio.impl.WaitDone(device);
   1.327  
   1.328 -    /* If necessary, deinit the streamer */
   1.329 -#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
   1.330 -    if (device->use_streamer == 1)
   1.331 -        SDL_StreamDeinit(&device->streamer);
   1.332 -#endif
   1.333 -
   1.334      return 0;
   1.335  }
   1.336