mirror of
https://github.com/mackron/miniaudio.git
synced 2026-04-21 15:56:58 +02:00
audio(4): Prime the playback buffer when starting the device.
This commit is contained in:
+36
@@ -38629,6 +38629,40 @@ static void ma_device_uninit__audio4(ma_device* pDevice)
|
||||
ma_free(pDeviceStateAudio4, ma_device_get_allocation_callbacks(pDevice));
|
||||
}
|
||||
|
||||
static void ma_device_prime_playback_buffer__audio4(ma_device* pDevice)
|
||||
{
|
||||
ma_device_state_audio4* pDeviceStateAudio4 = ma_device_get_backend_state__audio4(pDevice);
|
||||
ma_device_type deviceType = ma_device_get_type(pDevice);
|
||||
ma_uint8 buffer[4096];
|
||||
ma_uint32 bpf;
|
||||
ma_uint32 framesToWrite;
|
||||
ma_uint32 framesWritten;
|
||||
|
||||
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
|
||||
bpf = ma_get_bytes_per_frame(pDevice->playback.internalFormat, pDevice->playback.internalChannels);
|
||||
framesToWrite = pDevice->playback.internalPeriodSizeInFrames * pDevice->playback.internalPeriods;
|
||||
framesWritten = 0;
|
||||
|
||||
MA_ZERO_MEMORY(buffer, sizeof(buffer));
|
||||
|
||||
while (framesWritten < framesToWrite) {
|
||||
ma_uint32 framesToWriteThisIteration = sizeof(buffer) / bpf;
|
||||
ma_uint32 framesRemaining = framesToWrite - framesWritten;
|
||||
if (framesToWriteThisIteration > framesRemaining) {
|
||||
framesToWriteThisIteration = framesRemaining;
|
||||
}
|
||||
|
||||
/* Just a guard to ensure we don't get stuck in a loop. Should never happen in practice (would require a massive channel count). */
|
||||
if (framesToWriteThisIteration == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
write(pDeviceStateAudio4->fdPlayback, buffer, framesToWriteThisIteration);
|
||||
framesWritten += framesToWriteThisIteration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static ma_result ma_device_start__audio4(ma_device* pDevice)
|
||||
{
|
||||
ma_device_state_audio4* pDeviceStateAudio4 = ma_device_get_backend_state__audio4(pDevice);
|
||||
@@ -38644,6 +38678,8 @@ static ma_result ma_device_start__audio4(ma_device* pDevice)
|
||||
if (pDeviceStateAudio4->fdPlayback == -1) {
|
||||
return MA_INVALID_ARGS;
|
||||
}
|
||||
|
||||
ma_device_prime_playback_buffer__audio4(pDevice);
|
||||
}
|
||||
|
||||
return MA_SUCCESS;
|
||||
|
||||
Reference in New Issue
Block a user