Navigation Menu

Skip to content

Commit

Permalink
audio: rename fake_stream to work_buffer.
Browse files Browse the repository at this point in the history
It's more than an alternative for when the OS can't provide a DMA buffer, now.
  • Loading branch information
icculus committed Jan 6, 2017
1 parent 992124d commit b3e8db8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
33 changes: 16 additions & 17 deletions src/audio/SDL_audio.c
Expand Up @@ -568,16 +568,16 @@ SDL_RunAudio(void *devicep)
stream = current_audio.impl.GetDeviceBuf(device);
} else {
/* if the device isn't enabled, we still write to the
fake_stream, so the app's callback will fire with
work_buffer, so the app's callback will fire with
a regular frequency, in case they depend on that
for timing or progress. They can use hotplug
now to know if the device failed.
Streaming playback uses fake_stream as a work buffer, too. */
Streaming playback uses work_buffer, too. */
stream = NULL;
}

if (stream == NULL) {
stream = device->fake_stream;
stream = device->work_buffer;
}

if ( SDL_AtomicGet(&device->enabled) ) {
Expand Down Expand Up @@ -614,7 +614,7 @@ SDL_RunAudio(void *devicep)
current_audio.impl.WaitDevice(device);
}
}
} else if (stream == device->fake_stream) {
} else if (stream == device->work_buffer) {
/* nothing to do; pause like we queued a buffer to play. */
SDL_Delay(delay);
} else { /* writing directly to the device. */
Expand Down Expand Up @@ -670,8 +670,8 @@ SDL_CaptureAudio(void *devicep)
/* Fill the current buffer with sound */
still_need = stream_len;

/* just use the "fake" stream to hold data read from the device. */
stream = device->fake_stream;
/* Use the work_buffer to hold data read from the device. */
stream = device->work_buffer;
SDL_assert(stream != NULL);

ptr = stream;
Expand Down Expand Up @@ -702,16 +702,16 @@ SDL_CaptureAudio(void *devicep)
SDL_AudioStreamPut(device->stream, stream, stream_len);

while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->callbackspec.size)) {
const int got = SDL_AudioStreamGet(device->stream, device->fake_stream, device->callbackspec.size);
const int got = SDL_AudioStreamGet(device->stream, device->work_buffer, device->callbackspec.size);
SDL_assert((got < 0) || (got == device->callbackspec.size));
if (got != device->callbackspec.size) {
SDL_memset(device->fake_stream, device->spec.silence, device->callbackspec.size);
SDL_memset(device->work_buffer, device->spec.silence, device->callbackspec.size);
}

/* !!! FIXME: this should be LockDevice. */
SDL_LockMutex(device->mixer_lock);
if (!SDL_AtomicGet(&device->paused)) {
callback(udata, device->fake_stream, device->callbackspec.size);
callback(udata, device->work_buffer, device->callbackspec.size);
}
SDL_UnlockMutex(device->mixer_lock);
}
Expand Down Expand Up @@ -958,7 +958,7 @@ close_audio_device(SDL_AudioDevice * device)
SDL_DestroyMutex(device->mixer_lock);
}

SDL_free(device->fake_stream);
SDL_free(device->work_buffer);
SDL_FreeAudioStream(device->stream);

if (device->hidden != NULL) {
Expand Down Expand Up @@ -1243,16 +1243,15 @@ open_audio_device(const char *devname, int iscapture,
device->spec.userdata = device;
}

/* !!! FIXME: rename this from fake_stream */
/* Allocate a scratch audio buffer */
device->fake_stream_len = build_stream ? device->callbackspec.size : 0;
if (device->spec.size > device->fake_stream_len) {
device->fake_stream_len = device->spec.size;
device->work_buffer_len = build_stream ? device->callbackspec.size : 0;
if (device->spec.size > device->work_buffer_len) {
device->work_buffer_len = device->spec.size;
}
SDL_assert(device->fake_stream_len > 0);
SDL_assert(device->work_buffer_len > 0);

device->fake_stream = (Uint8 *) SDL_malloc(device->fake_stream_len);
if (device->fake_stream == NULL) {
device->work_buffer = (Uint8 *) SDL_malloc(device->work_buffer_len);
if (device->work_buffer == NULL) {
close_audio_device(device);
SDL_OutOfMemory();
return 0;
Expand Down
8 changes: 4 additions & 4 deletions src/audio/SDL_sysaudio.h
Expand Up @@ -146,11 +146,11 @@ struct SDL_AudioDevice
SDL_atomic_t paused;
SDL_bool iscapture;

/* Fake audio buffer for when the audio hardware is busy */
Uint8 *fake_stream;
/* Scratch buffer used in the bridge between SDL and the user callback. */
Uint8 *work_buffer;

/* Size, in bytes, of fake_stream. */
Uint32 fake_stream_len;
/* Size, in bytes, of work_buffer. */
Uint32 work_buffer_len;

/* A mutex for locking the mixing buffers */
SDL_mutex *mixer_lock;
Expand Down
24 changes: 12 additions & 12 deletions src/audio/emscripten/SDL_emscriptenaudio.c
Expand Up @@ -65,26 +65,26 @@ HandleAudioProcess(_THIS)

if (this->stream == NULL) { /* no conversion necessary. */
SDL_assert(this->spec.size == stream_len);
callback(this->spec.userdata, this->fake_stream, stream_len);
callback(this->spec.userdata, this->work_buffer, stream_len);
} else { /* streaming/converting */
int got;
while (SDL_AudioStreamAvailable(this->stream) < ((int) this->spec.size)) {
callback(this->spec.userdata, this->fake_stream, stream_len);
if (SDL_AudioStreamPut(this->stream, this->fake_stream, stream_len) == -1) {
callback(this->spec.userdata, this->work_buffer, stream_len);
if (SDL_AudioStreamPut(this->stream, this->work_buffer, stream_len) == -1) {
SDL_AudioStreamClear(this->stream);
SDL_AtomicSet(&this->enabled, 0);
break;
}
}

got = SDL_AudioStreamGet(this->stream, this->fake_stream, this->spec.size);
got = SDL_AudioStreamGet(this->stream, this->work_buffer, this->spec.size);
SDL_assert((got < 0) || (got == this->spec.size));
if (got != this->spec.size) {
SDL_memset(this->fake_stream, this->spec.silence, this->spec.size);
SDL_memset(this->work_buffer, this->spec.silence, this->spec.size);
}
}

FeedAudioDevice(this, this->fake_stream, this->spec.size);
FeedAudioDevice(this, this->work_buffer, this->spec.size);
}

static void
Expand Down Expand Up @@ -117,25 +117,25 @@ HandleCaptureProcess(_THIS)
}
}
}
}, this->fake_stream, (this->spec.size / sizeof (float)) / this->spec.channels);
}, this->work_buffer, (this->spec.size / sizeof (float)) / this->spec.channels);

/* okay, we've got an interleaved float32 array in C now. */

if (this->stream == NULL) { /* no conversion necessary. */
SDL_assert(this->spec.size == stream_len);
callback(this->spec.userdata, this->fake_stream, stream_len);
callback(this->spec.userdata, this->work_buffer, stream_len);
} else { /* streaming/converting */
if (SDL_AudioStreamPut(this->stream, this->fake_stream, this->spec.size) == -1) {
if (SDL_AudioStreamPut(this->stream, this->work_buffer, this->spec.size) == -1) {
SDL_AtomicSet(&this->enabled, 0);
}

while (SDL_AudioStreamAvailable(this->stream) >= stream_len) {
const int got = SDL_AudioStreamGet(this->stream, this->fake_stream, stream_len);
const int got = SDL_AudioStreamGet(this->stream, this->work_buffer, stream_len);
SDL_assert((got < 0) || (got == stream_len));
if (got != stream_len) {
SDL_memset(this->fake_stream, this->callbackspec.silence, stream_len);
SDL_memset(this->work_buffer, this->callbackspec.silence, stream_len);
}
callback(this->spec.userdata, this->fake_stream, stream_len); /* Send it to the app. */
callback(this->spec.userdata, this->work_buffer, stream_len); /* Send it to the app. */
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/audio/haiku/SDL_haikuaudio.cc
Expand Up @@ -69,8 +69,8 @@ FillSound(void *device, void *stream, size_t len,
const int stream_len = audio->callbackspec.size;
const int ilen = (int) len;
while (SDL_AudioStreamAvailable(audio->stream) < ilen) {
callback(audio->spec.userdata, audio->fake_stream, stream_len);
if (SDL_AudioStreamPut(audio->stream, audio->fake_stream, stream_len) == -1) {
callback(audio->spec.userdata, audio->work_buffer, stream_len);
if (SDL_AudioStreamPut(audio->stream, audio->work_buffer, stream_len) == -1) {
SDL_AudioStreamClear(audio->stream);
SDL_AtomicSet(&audio->enabled, 0);
break;
Expand Down
4 changes: 2 additions & 2 deletions src/audio/nacl/SDL_naclaudio.c
Expand Up @@ -70,8 +70,8 @@ static void nacl_audio_callback(void* stream, uint32_t buffer_size, PP_TimeDelta
} else { /* streaming/converting */
const int stream_len = _this->callbackspec.size;
while (SDL_AudioStreamAvailable(_this->stream) < len) {
callback(_this->spec.userdata, _this->fake_stream, stream_len);
if (SDL_AudioStreamPut(_this->stream, _this->fake_stream, stream_len) == -1) {
callback(_this->spec.userdata, _this->work_buffer, stream_len);
if (SDL_AudioStreamPut(_this->stream, _this->work_buffer, stream_len) == -1) {
SDL_AudioStreamClear(_this->stream);
SDL_AtomicSet(&_this->enabled, 0);
break;
Expand Down

0 comments on commit b3e8db8

Please sign in to comment.