(Audio) SOme control flow changes (no functional changes) and some cleanups

This commit is contained in:
libretroadmin 2025-08-07 17:35:00 +02:00
parent e3a5c5ea46
commit 3aa8db2c08
12 changed files with 191 additions and 211 deletions

View File

@ -214,9 +214,10 @@ end:
static int alsa_thread_microphone_read(void *driver_context, void *mic_context, void *s, size_t len)
{
snd_pcm_state_t state;
size_t _len = 0;
alsa_thread_microphone_t *alsa = (alsa_thread_microphone_t*)driver_context;
alsa_thread_microphone_handle_t *mic = (alsa_thread_microphone_handle_t*)mic_context;
snd_pcm_state_t state;
if (!alsa || !mic || !s) /* If any of the parameters were invalid... */
return -1;
@ -245,28 +246,23 @@ static int alsa_thread_microphone_read(void *driver_context, void *mic_context,
if (alsa->nonblock)
{
size_t avail;
size_t write_amt;
/* "Hey, I'm gonna borrow the queue." */
slock_lock(mic->info.fifo_lock);
avail = FIFO_READ_AVAIL(mic->info.buffer);
write_amt = MIN(avail, len);
_len = MIN(avail, len);
/* "It's okay if you don't have any new samples, I'll just check in on you later." */
fifo_read(mic->info.buffer, s, write_amt);
fifo_read(mic->info.buffer, s, _len);
/* "Here, take this queue back." */
slock_unlock(mic->info.fifo_lock);
return (int)write_amt;
}
else
{
size_t read = 0;
/* Until we've read all requested samples (or we're told to stop)... */
while (read < len && !mic->info.thread_dead)
while (_len < len && !mic->info.thread_dead)
{
size_t avail;
@ -294,21 +290,20 @@ static int alsa_thread_microphone_read(void *driver_context, void *mic_context,
}
else
{
size_t read_amt = MIN(len - read, avail);
size_t read_amt = MIN(len - _len, avail);
/* "I'll just go ahead and consume all these samples..."
* (As many as will fit in s, or as many as are available.) */
fifo_read(mic->info.buffer,s + read, read_amt);
fifo_read(mic->info.buffer,s + _len, read_amt);
/* "I'm done, you can take the queue back now." */
slock_unlock(mic->info.fifo_lock);
read += read_amt;
_len += read_amt;
}
/* "I'll be right back..." */
}
return (int)read;
}
return _len;
}
static bool alsa_thread_microphone_mic_alive(const void *driver_context, const void *mic_context);
@ -475,8 +470,9 @@ static void alsa_worker_thread(void *data)
frames = snd_pcm_writei(alsa->info.pcm, buf, alsa->info.stream_info.period_frames);
if (frames == -EPIPE || frames == -EINTR ||
frames == -ESTRPIPE)
if ( frames == -EPIPE
|| frames == -EINTR
|| frames == -ESTRPIPE)
{
if (snd_pcm_recover(alsa->info.pcm, frames, false) < 0)
{
@ -625,7 +621,6 @@ static bool alsa_thread_alive(void *data)
static bool alsa_thread_stop(void *data)
{
alsa_thread_t *alsa = (alsa_thread_t*)data;
if (alsa)
alsa->is_paused = true;
return true;

View File

@ -149,15 +149,15 @@ static void audioworklet_thread_inited_cb(EMSCRIPTEN_WEBAUDIO_T context, bool su
audioworklet_data_t *audioworklet = (audioworklet_data_t*)data;
WebAudioWorkletProcessorCreateOptions opts = { "retroarch", 0 };
if (!success)
if (success)
emscripten_create_wasm_audio_worklet_processor_async(context, &opts,
audioworklet_processor_inited_cb, audioworklet);
else
{
RARCH_ERR("[AudioWorklet] Failed to init worklet thread! Is the worklet file in the right place?\n");
audioworklet->init_error = true;
audioworklet->init_done = true;
return;
}
emscripten_create_wasm_audio_worklet_processor_async(context, &opts, audioworklet_processor_inited_cb, audioworklet);
}
static void audioworklet_ctx_statechange_cb(void *data, bool state)
@ -190,9 +190,9 @@ static void audioworklet_ctx_create(void *data)
static void audioworklet_alloc_buffer(void *data)
{
size_t buffer_size;
audioworklet_data_t *audioworklet = (audioworklet_data_t*)data;
size_t buffer_size;
audioworklet->visible_buffer_size = (audioworklet->latency * audioworklet->rate * 2 * sizeof(float)) / 1000;
buffer_size = audioworklet->visible_buffer_size;
#ifdef EMSCRIPTEN_AUDIO_EXTERNAL_WRITE_BLOCK
@ -382,9 +382,8 @@ bool audioworklet_external_block(void)
{
audioworklet_data_t *audioworklet = audioworklet_static_data;
if (!audioworklet)
return false;
if (audioworklet)
{
#ifdef EMSCRIPTEN_AUDIO_FAKE_BLOCK
if (!audioworklet->block_requested)
return false;
@ -427,6 +426,7 @@ bool audioworklet_external_block(void)
platform_emscripten_exit_fake_block();
return true; /* return to RAF if needed */
#endif
}
return false;
}
#endif

View File

@ -102,17 +102,10 @@ static OSStatus coreaudio_audio_write_cb(void *userdata,
if (FIFO_READ_AVAIL(dev->buffer) < write_avail)
{
*action_flags = kAudioUnitRenderAction_OutputIsSilence;
/* Seems to be needed. */
memset(outbuf, 0, write_avail);
slock_unlock(dev->lock);
/* Technically possible to deadlock without. */
scond_signal(dev->cond);
return noErr;
}
else
fifo_read(dev->buffer, outbuf, write_avail);
slock_unlock(dev->lock);
scond_signal(dev->cond);
@ -123,7 +116,7 @@ static OSStatus coreaudio_audio_write_cb(void *userdata,
static void coreaudio_choose_output_device(coreaudio_t *dev, const char* device)
{
int i;
UInt32 deviceCount;
UInt32 device_count;
AudioObjectPropertyAddress propaddr;
AudioDeviceID *devices = NULL;
UInt32 size = 0;
@ -140,20 +133,19 @@ static void coreaudio_choose_output_device(coreaudio_t *dev, const char* device)
&propaddr, 0, 0, &size) != noErr)
return;
deviceCount = size / sizeof(AudioDeviceID);
device_count = size / sizeof(AudioDeviceID);
devices = (AudioDeviceID*)malloc(size);
if (!devices || AudioObjectGetPropertyData(kAudioObjectSystemObject,
&propaddr, 0, 0, &size, devices) != noErr)
goto done;
if (devices && AudioObjectGetPropertyData(kAudioObjectSystemObject,
&propaddr, 0, 0, &size, devices) == noErr)
{
#if HAS_MACOSX_10_12
#else
propaddr.mScope = kAudioDevicePropertyScopeOutput;
#endif
propaddr.mSelector = kAudioDevicePropertyDeviceName;
for (i = 0; i < (int)deviceCount; i ++)
for (i = 0; i < (int)device_count; i ++)
{
char device_name[1024];
device_name[0] = 0;
@ -165,11 +157,11 @@ static void coreaudio_choose_output_device(coreaudio_t *dev, const char* device)
{
AudioUnitSetProperty(dev->dev, kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global, 0, &devices[i], sizeof(AudioDeviceID));
goto done;
break;
}
}
}
done:
free(devices);
}
#endif
@ -245,9 +237,11 @@ static void *coreaudio_init(const char *device,
stream_desc.mBytesPerFrame = 2 * sizeof(float);
stream_desc.mFramesPerPacket = 1;
stream_desc.mFormatID = kAudioFormatLinearPCM;
stream_desc.mFormatFlags = kAudioFormatFlagIsFloat |
kAudioFormatFlagIsPacked | (is_little_endian() ?
0 : kAudioFormatFlagIsBigEndian);
stream_desc.mFormatFlags = kAudioFormatFlagIsFloat
| kAudioFormatFlagIsPacked;
if (!is_little_endian())
stream_desc.mFormatFlags |= kAudioFormatFlagIsBigEndian;
if (AudioUnitSetProperty(dev->dev, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 0, &stream_desc, sizeof(stream_desc)) != noErr)
@ -374,19 +368,25 @@ static bool coreaudio_alive(void *data)
static bool coreaudio_stop(void *data)
{
coreaudio_t *dev = (coreaudio_t*)data;
if (!dev)
return false;
if (dev)
{
dev->is_paused = (AudioOutputUnitStop(dev->dev) == noErr) ? true : false;
return dev->is_paused ? true : false;
if (dev->is_paused)
return true;
}
return false;
}
static bool coreaudio_start(void *data, bool is_shutdown)
{
coreaudio_t *dev = (coreaudio_t*)data;
if (!dev)
return false;
if (dev)
{
dev->is_paused = (AudioOutputUnitStart(dev->dev) == noErr) ? false : true;
return dev->is_paused ? false : true;
if (dev->is_paused)
return true;
}
return false;
}
static bool coreaudio_use_float(void *data) { return true; }

View File

@ -240,9 +240,8 @@ static bool ctr_csnd_audio_start(void *data, bool is_shutdown)
/* Prevents restarting audio when the menu
* is toggled off on shutdown */
if (is_shutdown)
return true;
if (!is_shutdown)
{
#if 0
CSND_SetPlayState(0x8, 1);
CSND_SetPlayState(0x9, 1);
@ -252,8 +251,8 @@ static bool ctr_csnd_audio_start(void *data, bool is_shutdown)
CSND_SetVol(0x9, 0x80000000, 0);
csndExecCmds(false);
ctr->playing = true;
}
return true;
}
@ -277,7 +276,6 @@ static size_t ctr_csnd_audio_write_avail(void *data)
static size_t ctr_csnd_audio_buffer_size(void *data)
{
(void)data;
return CTR_CSND_AUDIO_COUNT;
}

View File

@ -171,11 +171,11 @@ static bool ctr_dsp_audio_start(void *data, bool is_shutdown)
/* Prevents restarting audio when the menu
* is toggled off on shutdown */
if (is_shutdown)
return true;
if (!is_shutdown)
{
ndspSetMasterVol(1.0);
ctr->playing = true;
}
return true;
}
@ -198,7 +198,6 @@ static size_t ctr_dsp_audio_write_avail(void *data)
static size_t ctr_dsp_audio_buffer_size(void *data)
{
(void)data;
return CTR_DSP_AUDIO_COUNT;
}

View File

@ -149,15 +149,14 @@ static bool dsound_grab_region(dsound_t *ds, uint32_t write_ptr,
#ifdef DEBUG
RARCH_WARN("[DirectSound] %s.\n", "DSERR_BUFFERLOST");
#endif
if ((IDirectSoundBuffer_Restore(ds->dsb)) != DS_OK)
return false;
if ((IDirectSoundBuffer_Restore(ds->dsb)) == DS_OK)
if ((IDirectSoundBuffer_Lock(ds->dsb, write_ptr, CHUNK_SIZE,
&region->chunk1, &region->size1, &region->chunk2, &region->size2, 0)) != DS_OK)
return false;
&region->chunk1, &region->size1, &region->chunk2, &region->size2, 0)) == DS_OK)
return true;
}
#ifdef DEBUG
else
{
switch (res)
{
case DSERR_INVALIDCALL:
@ -172,8 +171,8 @@ static bool dsound_grab_region(dsound_t *ds, uint32_t write_ptr,
default:
break;
}
}
#endif
return false;
}

View File

@ -68,20 +68,14 @@ static size_t ja_read_deinterleaved(float *dst[2], jack_nframes_t dst_offset,
static int ja_process_cb(jack_nframes_t nframes, void *data)
{
int i;
jack_nframes_t read = 0;
jack_t *jd = (jack_t*)data;
jack_ringbuffer_data_t buf[2];
float *dst[2];
if (nframes <= 0)
if (nframes > 0)
{
#ifdef HAVE_THREADS
scond_signal(jd->cond);
#endif
return 0;
}
int i;
float *dst[2];
jack_ringbuffer_data_t buf[2];
jack_nframes_t read = 0;
for (i = 0; i < 2; i++)
dst[i] = (float *)jack_port_get_buffer(jd->ports[i], nframes);
@ -95,7 +89,7 @@ static int ja_process_cb(jack_nframes_t nframes, void *data)
for (; read < nframes; read++)
for (i = 0; i < 2; i++)
dst[i][read] = 0.0f;
}
#ifdef HAVE_THREADS
scond_signal(jd->cond);
#endif

View File

@ -230,13 +230,12 @@ static void rwebaudio_set_nonblock_state(void *data, bool state)
static size_t rwebaudio_write_avail(void *data)
{
rwebaudio_data_t *rwebaudio = (rwebaudio_data_t*)data;
size_t avail_frames;
if (!rwebaudio)
return 0;
avail_frames = RWebAudioWriteAvailFrames();
if (rwebaudio)
{
size_t avail_frames = RWebAudioWriteAvailFrames();
if (avail_frames > rwebaudio->tmpbuf_offset)
return (avail_frames - rwebaudio->tmpbuf_offset) * 2 * sizeof(float);
}
return 0;
}

View File

@ -180,13 +180,14 @@ static bool switch_audio_start(void *data, bool is_shutdown)
static bool switch_audio_alive(void *data)
{
switch_audio_t *swa = (switch_audio_t*) data;
if (!swa)
return false;
return !swa->is_paused;
return (swa && !swa->is_paused);
}
static void switch_audio_free(void *data)
{
#ifdef HAVE_LIBNX
int i;
#endif
switch_audio_t *swa = (switch_audio_t*) data;
if (!swa)
@ -197,8 +198,6 @@ static void switch_audio_free(void *data)
audoutStopAudioOut();
audoutExit();
int i;
for (i = 0; i < BUFFER_COUNT; i++)
free(swa->buffers[i].buffer);
#else
@ -208,10 +207,8 @@ static void switch_audio_free(void *data)
free(swa);
}
static bool switch_audio_use_float(void *data)
{
return false; /* force INT16 */
}
/* TODO/FIXME - implement float too? */
static bool switch_audio_use_float(void *data) { return false; /* force INT16 */ }
static size_t switch_audio_write_avail(void *data)
{

View File

@ -176,13 +176,11 @@ static int ax_audio_limit(int in)
static bool ax_audio_start(void* data, bool is_shutdown)
{
ax_audio_t* ax = (ax_audio_t*)data;
/* Prevents restarting audio when the menu
* is toggled off on shutdown */
if (is_shutdown)
return true;
if (!is_shutdown)
{
ax_audio_t* ax = (ax_audio_t*)data;
/* Set back to playing on enough buffered data */
if (ax->written > AX_AUDIO_SAMPLE_LOAD)
{
@ -190,13 +188,13 @@ static bool ax_audio_start(void* data, bool is_shutdown)
ax_audio_limit(ax->pos - ax->written));
AXSetMultiVoiceState(ax->mvoice, AX_VOICE_STATE_PLAYING);
}
}
return true;
}
static ssize_t ax_audio_write(void* data, const void* buf, size_t len)
{
uint32_t i;
size_t count_avail = 0;
ax_audio_t *ax = (ax_audio_t*)data;
const uint16_t *src = buf;
@ -236,6 +234,7 @@ static ssize_t ax_audio_write(void* data, const void* buf, size_t len)
/* make sure we have input size */
if (count > 0)
{
size_t i;
/* write in new data */
size_t start_pos = ax->pos;
int flush_p2_needed = 0;