Removed the broken audio streaming code, other small cleanups.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 19 Mar 2015 13:34:17 -0400
changeset 9398c41dd34e4996
parent 9397 d72d2aa46341
child 9399 a684dbd185c7
Removed the broken audio streaming code, other small cleanups.
src/audio/SDL_audio.c
     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 +    /* Loop, filling the audio buffers */
   1.102 +    while (!device->shutdown) {
   1.103 +        /* Fill the current buffer with sound */
   1.104 +        if (device->convert.needed) {
   1.105 +            stream = device->convert.buf;
   1.106 +        } else if (device->enabled) {
   1.107 +            stream = current_audio.impl.GetDeviceBuf(device);
   1.108 +        } else {
   1.109 +            /* if the device isn't enabled, we still write to the
   1.110 +               fake_stream, so the app's callback will fire with
   1.111 +               a regular frequency, in case they depend on that
   1.112 +               for timing or progress. They can use hotplug
   1.113 +               now to know if the device failed. */
   1.114 +            stream = NULL;
   1.115 +        }
   1.116  
   1.117 -    /* By default do not stream */
   1.118 -    device->use_streamer = 0;
   1.119 +        if (stream == NULL) {
   1.120 +            stream = device->fake_stream;
   1.121 +        }
   1.122  
   1.123 -    if (device->convert.needed) {
   1.124 -#if 0                           /* !!! FIXME: I took len_div out of the structure. Use rate_incr instead? */
   1.125 -        /* If the result of the conversion alters the length, i.e. resampling is being used, use the streamer */
   1.126 -        if (device->convert.len_mult != 1 || device->convert.len_div != 1) {
   1.127 -            /* The streamer's maximum length should be twice whichever is larger: spec.size or len_cvt */
   1.128 -            stream_max_len = 2 * device->spec.size;
   1.129 -            if (device->convert.len_mult > device->convert.len_div) {
   1.130 -                stream_max_len *= device->convert.len_mult;
   1.131 -                stream_max_len /= device->convert.len_div;
   1.132 -            }
   1.133 -            if (SDL_StreamInit(&device->streamer, stream_max_len, silence) <
   1.134 -                0)
   1.135 -                return -1;
   1.136 -            device->use_streamer = 1;
   1.137 +        /* !!! FIXME: this should be LockDevice. */
   1.138 +        SDL_LockMutex(device->mixer_lock);
   1.139 +        if (device->paused) {
   1.140 +            SDL_memset(stream, silence, stream_len);
   1.141 +        } else {
   1.142 +            (*fill) (udata, stream, stream_len);
   1.143 +        }
   1.144 +        SDL_UnlockMutex(device->mixer_lock);
   1.145  
   1.146 -            /* istream_len should be the length of what we grab from the callback and feed to conversion,
   1.147 -               so that we get close to spec_size. I.e. we want device.spec_size = istream_len * u / d
   1.148 -             */
   1.149 -            istream_len =
   1.150 -                device->spec.size * device->convert.len_div /
   1.151 -                device->convert.len_mult;
   1.152 -        }
   1.153 -#endif
   1.154 -        stream_len = device->convert.len;
   1.155 -    } else {
   1.156 -        stream_len = device->spec.size;
   1.157 -    }
   1.158 -
   1.159 -    /* Calculate the delay while paused */
   1.160 -    delay = ((device->spec.samples * 1000) / device->spec.freq);
   1.161 -
   1.162 -    /* Determine if the streamer is necessary here */
   1.163 -#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
   1.164 -    if (device->use_streamer == 1) {
   1.165 -        /* This code is almost the same as the old code. The difference is, instead of reading
   1.166 -           directly from the callback into "stream", then converting and sending the audio off,
   1.167 -           we go: callback -> "istream" -> (conversion) -> streamer -> stream -> device.
   1.168 -           However, reading and writing with streamer are done separately:
   1.169 -           - We only call the callback and write to the streamer when the streamer does not
   1.170 -           contain enough samples to output to the device.
   1.171 -           - We only read from the streamer and tell the device to play when the streamer
   1.172 -           does have enough samples to output.
   1.173 -           This allows us to perform resampling in the conversion step, where the output of the
   1.174 -           resampling process can be any number. We will have to see what a good size for the
   1.175 -           stream's maximum length is, but I suspect 2*max(len_cvt, stream_len) is a good figure.
   1.176 -         */
   1.177 -        while (!device->shutdown) {
   1.178 -
   1.179 -            if (device->paused) {
   1.180 -                SDL_Delay(delay);
   1.181 -                continue;
   1.182 -            }
   1.183 -
   1.184 -            /* Only read in audio if the streamer doesn't have enough already (if it does not have enough samples to output) */
   1.185 -            if (SDL_StreamLength(&device->streamer) < stream_len) {
   1.186 -                /* Set up istream */
   1.187 -                if (device->convert.needed) {
   1.188 -                    if (device->convert.buf) {
   1.189 -                        istream = device->convert.buf;
   1.190 -                    } else {
   1.191 -                        continue;
   1.192 -                    }
   1.193 -                } else {
   1.194 -/* FIXME: Ryan, this is probably wrong.  I imagine we don't want to get
   1.195 - * a device buffer both here and below in the stream output.
   1.196 - */
   1.197 -                    istream = current_audio.impl.GetDeviceBuf(device);
   1.198 -                    if (istream == NULL) {
   1.199 -                        istream = device->fake_stream;
   1.200 -                    }
   1.201 -                }
   1.202 -
   1.203 -                /* Read from the callback into the _input_ stream */
   1.204 -                // !!! FIXME: this should be LockDevice.
   1.205 -                SDL_LockMutex(device->mixer_lock);
   1.206 -                (*fill) (udata, istream, istream_len);
   1.207 -                SDL_UnlockMutex(device->mixer_lock);
   1.208 -
   1.209 -                /* Convert the audio if necessary and write to the streamer */
   1.210 -                if (device->convert.needed) {
   1.211 -                    SDL_ConvertAudio(&device->convert);
   1.212 -                    if (istream == NULL) {
   1.213 -                        istream = device->fake_stream;
   1.214 -                    }
   1.215 -                    /* SDL_memcpy(istream, device->convert.buf, device->convert.len_cvt); */
   1.216 -                    SDL_StreamWrite(&device->streamer, device->convert.buf,
   1.217 -                                    device->convert.len_cvt);
   1.218 -                } else {
   1.219 -                    SDL_StreamWrite(&device->streamer, istream, istream_len);
   1.220 -                }
   1.221 -            }
   1.222 -
   1.223 -            /* Only output audio if the streamer has enough to output */
   1.224 -            if (SDL_StreamLength(&device->streamer) >= stream_len) {
   1.225 -                /* Set up the output stream */
   1.226 -                if (device->convert.needed) {
   1.227 -                    if (device->convert.buf) {
   1.228 -                        stream = device->convert.buf;
   1.229 -                    } else {
   1.230 -                        continue;
   1.231 -                    }
   1.232 -                } else {
   1.233 -                    stream = current_audio.impl.GetDeviceBuf(device);
   1.234 -                    if (stream == NULL) {
   1.235 -                        stream = device->fake_stream;
   1.236 -                    }
   1.237 -                }
   1.238 -
   1.239 -                /* Now read from the streamer */
   1.240 -                SDL_StreamRead(&device->streamer, stream, stream_len);
   1.241 -
   1.242 -                /* Ready current buffer for play and change current buffer */
   1.243 -                if (stream != device->fake_stream) {
   1.244 -                    current_audio.impl.PlayDevice(device);
   1.245 -                    /* Wait for an audio buffer to become available */
   1.246 -                    current_audio.impl.WaitDevice(device);
   1.247 -                } else {
   1.248 -                    SDL_Delay(delay);
   1.249 -                }
   1.250 -            }
   1.251 -
   1.252 -        }
   1.253 -    } else
   1.254 -#endif
   1.255 -    {
   1.256 -        /* Otherwise, do not use the streamer. This is the old code. */
   1.257 -        const int silence = (int) device->spec.silence;
   1.258 -
   1.259 -        /* Loop, filling the audio buffers */
   1.260 -        while (!device->shutdown) {
   1.261 -            /* Fill the current buffer with sound */
   1.262 -            if (device->convert.needed) {
   1.263 -                stream = device->convert.buf;
   1.264 -            } else if (device->enabled) {
   1.265 -                stream = current_audio.impl.GetDeviceBuf(device);
   1.266 -            } else {
   1.267 -                /* if the device isn't enabled, we still write to the
   1.268 -                    fake_stream, so the app's callback will fire with
   1.269 -                    a regular frequency, in case they depend on that
   1.270 -                    for timing or progress. They can use hotplug
   1.271 -                    now to know if the device failed. */
   1.272 -                stream = NULL;
   1.273 -            }
   1.274 -
   1.275 +        /* Convert the audio if necessary */
   1.276 +        if (device->enabled && device->convert.needed) {
   1.277 +            SDL_ConvertAudio(&device->convert);
   1.278 +            stream = current_audio.impl.GetDeviceBuf(device);
   1.279              if (stream == NULL) {
   1.280                  stream = device->fake_stream;
   1.281 +            } else {
   1.282 +                SDL_memcpy(stream, device->convert.buf,
   1.283 +                           device->convert.len_cvt);
   1.284              }
   1.285 +        }
   1.286  
   1.287 -            /* !!! FIXME: this should be LockDevice. */
   1.288 -            SDL_LockMutex(device->mixer_lock);
   1.289 -            if (device->paused) {
   1.290 -                SDL_memset(stream, silence, stream_len);
   1.291 -            } else {
   1.292 -                (*fill) (udata, stream, stream_len);
   1.293 -            }
   1.294 -            SDL_UnlockMutex(device->mixer_lock);
   1.295 -
   1.296 -            /* Convert the audio if necessary */
   1.297 -            if (device->enabled && device->convert.needed) {
   1.298 -                SDL_ConvertAudio(&device->convert);
   1.299 -                stream = current_audio.impl.GetDeviceBuf(device);
   1.300 -                if (stream == NULL) {
   1.301 -                    stream = device->fake_stream;
   1.302 -                } else {
   1.303 -                    SDL_memcpy(stream, device->convert.buf,
   1.304 -                               device->convert.len_cvt);
   1.305 -                }
   1.306 -            }
   1.307 -
   1.308 -            /* Ready current buffer for play and change current buffer */
   1.309 -            if (stream != device->fake_stream) {
   1.310 -                current_audio.impl.PlayDevice(device);
   1.311 -                /* Wait for an audio buffer to become available */
   1.312 -                current_audio.impl.WaitDevice(device);
   1.313 -            } else {
   1.314 -                SDL_Delay(delay);
   1.315 -            }
   1.316 +        /* Ready current buffer for play and change current buffer */
   1.317 +        if (stream == device->fake_stream) {
   1.318 +            SDL_Delay(delay);
   1.319 +        } else {
   1.320 +            current_audio.impl.PlayDevice(device);
   1.321 +            current_audio.impl.WaitDevice(device);
   1.322          }
   1.323      }
   1.324  
   1.325 -    /* Wait for the audio to drain.. */
   1.326 +    /* Wait for the audio to drain. */
   1.327      current_audio.impl.WaitDone(device);
   1.328  
   1.329 -    /* If necessary, deinit the streamer */
   1.330 -#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
   1.331 -    if (device->use_streamer == 1)
   1.332 -        SDL_StreamDeinit(&device->streamer);
   1.333 -#endif
   1.334 -
   1.335      return 0;
   1.336  }
   1.337