Skip to content

Commit 7cb349f

Browse files
committed
audio(4): Refactoring for the new backend architecture.
1 parent 8c735da commit 7cb349f

File tree

1 file changed

+108
-9
lines changed

1 file changed

+108
-9
lines changed

miniaudio.h

Lines changed: 108 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38195,6 +38195,8 @@ typedef struct ma_device_state_audio4
3819538195
{
3819638196
int fdPlayback;
3819738197
int fdCapture;
38198+
void* pIntermediaryBufferPlayback;
38199+
void* pIntermediaryBufferCapture;
3819838200
} ma_device_state_audio4;
3819938201

3820038202

@@ -38573,7 +38575,7 @@ static ma_result ma_context_enumerate_devices__audio4(ma_context* pContext, ma_e
3857338575
return MA_SUCCESS;
3857438576
}
3857538577

38576-
static ma_result ma_device_init_fd__audio4(ma_device* pDevice, ma_device_descriptor* pDescriptor, ma_device_type deviceType, int* pFD)
38578+
static ma_result ma_device_init_fd__audio4(ma_device* pDevice, ma_device_descriptor* pDescriptor, ma_device_type deviceType, int* pFD, void** ppIntermediaryBuffer)
3857738579
{
3857838580
const char* pDefaultDeviceNames[] = {
3857938581
"/dev/audio",
@@ -38588,10 +38590,12 @@ static ma_result ma_device_init_fd__audio4(ma_device* pDevice, ma_device_descrip
3858838590
ma_uint32 internalSampleRate;
3858938591
ma_uint32 internalPeriodSizeInFrames;
3859038592
ma_uint32 internalPeriods;
38593+
void* pIntermediaryBuffer;
3859138594

3859238595
MA_ASSERT(deviceType != ma_device_type_duplex);
3859338596
MA_ASSERT(pDevice != NULL);
3859438597
MA_ASSERT(pFD != NULL);
38598+
MA_ASSERT(ppIntermediaryBuffer != NULL);
3859538599

3859638600
/* The first thing to do is open the file. */
3859738601
if (deviceType == ma_device_type_capture) {
@@ -38830,15 +38834,23 @@ static ma_result ma_device_init_fd__audio4(ma_device* pDevice, ma_device_descrip
3883038834
return MA_FORMAT_NOT_SUPPORTED;
3883138835
}
3883238836

38833-
*pFD = fd;
38834-
3883538837
pDescriptor->format = internalFormat;
3883638838
pDescriptor->channels = internalChannels;
3883738839
pDescriptor->sampleRate = internalSampleRate;
3883838840
ma_channel_map_init_standard(ma_standard_channel_map_sound4, pDescriptor->channelMap, ma_countof(pDescriptor->channelMap), internalChannels);
3883938841
pDescriptor->periodSizeInFrames = internalPeriodSizeInFrames;
3884038842
pDescriptor->periodCount = internalPeriods;
3884138843

38844+
pIntermediaryBuffer = ma_malloc(ma_get_bytes_per_frame(pDescriptor->format, pDescriptor->channels) * pDescriptor->periodSizeInFrames, ma_device_get_allocation_callbacks(pDevice));
38845+
if (pIntermediaryBuffer == NULL) {
38846+
close(fd);
38847+
ma_log_post(ma_device_get_log(pDevice), MA_LOG_LEVEL_ERROR, "[OSS] Failed to allocate memory for intermediary buffer.");
38848+
return MA_OUT_OF_MEMORY;
38849+
}
38850+
38851+
*pFD = fd;
38852+
*ppIntermediaryBuffer = pIntermediaryBuffer;
38853+
3884238854
return MA_SUCCESS;
3884338855
}
3884438856

@@ -38882,20 +38894,22 @@ static ma_result ma_device_init__audio4(ma_device* pDevice, const void* pDeviceB
3888238894
#endif
3888338895

3888438896
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
38885-
ma_result result = ma_device_init_fd__audio4(pDevice, pDescriptorCapture, ma_device_type_capture, &pDeviceStateAudio4->fdCapture);
38897+
ma_result result = ma_device_init_fd__audio4(pDevice, pDescriptorCapture, ma_device_type_capture, &pDeviceStateAudio4->fdCapture, &pDeviceStateAudio4->pIntermediaryBufferCapture);
3888638898
if (result != MA_SUCCESS) {
3888738899
ma_free(pDeviceStateAudio4, ma_device_get_allocation_callbacks(pDevice));
3888838900
return result;
3888938901
}
3889038902
}
3889138903

3889238904
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
38893-
ma_result result = ma_device_init_fd__audio4(pDevice, pDescriptorPlayback, ma_device_type_playback, &pDeviceStateAudio4->fdPlayback);
38905+
ma_result result = ma_device_init_fd__audio4(pDevice, pDescriptorPlayback, ma_device_type_playback, &pDeviceStateAudio4->fdPlayback, &pDeviceStateAudio4->pIntermediaryBufferPlayback);
3889438906
if (result != MA_SUCCESS) {
3889538907
if (deviceType == ma_device_type_duplex) {
38896-
ma_free(pDeviceStateAudio4, ma_device_get_allocation_callbacks(pDevice));
3889738908
close(pDeviceStateAudio4->fdCapture);
38909+
ma_free(pDeviceStateAudio4->pIntermediaryBufferCapture, ma_device_get_allocation_callbacks(pDevice));
3889838910
}
38911+
38912+
ma_free(pDeviceStateAudio4, ma_device_get_allocation_callbacks(pDevice));
3889938913
return result;
3890038914
}
3890138915
}
@@ -38912,10 +38926,12 @@ static void ma_device_uninit__audio4(ma_device* pDevice)
3891238926

3891338927
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
3891438928
close(pDeviceStateAudio4->fdCapture);
38929+
ma_free(pDeviceStateAudio4->pIntermediaryBufferCapture, ma_device_get_allocation_callbacks(pDevice));
3891538930
}
3891638931

3891738932
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
3891838933
close(pDeviceStateAudio4->fdPlayback);
38934+
ma_free(pDeviceStateAudio4->pIntermediaryBufferPlayback, ma_device_get_allocation_callbacks(pDevice));
3891938935
}
3892038936

3892138937
ma_free(pDeviceStateAudio4, ma_device_get_allocation_callbacks(pDevice));
@@ -39038,6 +39054,88 @@ static ma_result ma_device_read__audio4(ma_device* pDevice, void* pPCMFrames, ma
3903839054
return MA_SUCCESS;
3903939055
}
3904039056

39057+
static ma_result ma_device_step__audio4(ma_device* pDevice, ma_blocking_mode blockingMode)
39058+
{
39059+
ma_device_state_audio4* pDeviceStateAudio4 = ma_device_get_backend_state__audio4(pDevice);
39060+
ma_device_type deviceType = ma_device_get_type(pDevice);
39061+
struct timeval tv;
39062+
struct timeval* pTimeout = NULL;
39063+
fd_set fds;
39064+
ma_result result;
39065+
39066+
if (blockingMode == MA_BLOCKING_MODE_NON_BLOCKING) {
39067+
tv.tv_sec = 0;
39068+
tv.tv_usec = 0;
39069+
pTimeout = &tv;
39070+
}
39071+
39072+
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
39073+
int retval;
39074+
39075+
do
39076+
{
39077+
FD_ZERO(&fds);
39078+
FD_SET(pDeviceStateAudio4->fdCapture, &fds);
39079+
39080+
retval = select(pDeviceStateAudio4->fdCapture + 1, &fds, NULL, NULL, pTimeout);
39081+
} while (retval < 0 && errno == EINTR);
39082+
39083+
if (!ma_device_is_started(pDevice)) {
39084+
return MA_DEVICE_NOT_STARTED;
39085+
}
39086+
39087+
if (FD_ISSET(pDeviceStateAudio4->fdCapture, &fds)) {
39088+
ma_uint32 framesRead;
39089+
39090+
result = ma_device_read__audio4(pDevice, pDeviceStateAudio4->pIntermediaryBufferCapture, pDevice->capture.internalPeriodSizeInFrames, &framesRead);
39091+
if (result != MA_SUCCESS) {
39092+
return result;
39093+
}
39094+
39095+
ma_device_handle_backend_data_callback(pDevice, NULL, pDeviceStateAudio4->pIntermediaryBufferCapture, framesRead);
39096+
}
39097+
}
39098+
39099+
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
39100+
int retval;
39101+
39102+
do
39103+
{
39104+
FD_ZERO(&fds);
39105+
FD_SET(pDeviceStateAudio4->fdPlayback, &fds);
39106+
39107+
retval = select(pDeviceStateAudio4->fdPlayback + 1, NULL, &fds, NULL, pTimeout);
39108+
} while (retval < 0 && errno == EINTR);
39109+
39110+
if (!ma_device_is_started(pDevice)) {
39111+
return MA_DEVICE_NOT_STARTED;
39112+
}
39113+
39114+
if (FD_ISSET(pDeviceStateAudio4->fdPlayback, &fds)) {
39115+
ma_uint32 framesWritten;
39116+
39117+
result = ma_device_write__audio4(pDevice, pDeviceStateAudio4->pIntermediaryBufferPlayback, pDevice->playback.internalPeriodSizeInFrames, &framesWritten);
39118+
if (result != MA_SUCCESS) {
39119+
return result;
39120+
}
39121+
39122+
ma_device_handle_backend_data_callback(pDevice, pDeviceStateAudio4->pIntermediaryBufferPlayback, NULL, framesWritten);
39123+
}
39124+
}
39125+
39126+
return MA_SUCCESS;
39127+
}
39128+
39129+
static void ma_device_loop__audio4(ma_device* pDevice)
39130+
{
39131+
while (ma_device_is_started(pDevice)) {
39132+
ma_result result = ma_device_step__audio4(pDevice, MA_BLOCKING_MODE_BLOCKING);
39133+
if (result != MA_SUCCESS) {
39134+
break;
39135+
}
39136+
}
39137+
}
39138+
3904139139
static ma_device_backend_vtable ma_gDeviceBackendVTable_Audio4 =
3904239140
{
3904339141
ma_backend_info__audio4,
@@ -39048,9 +39146,9 @@ static ma_device_backend_vtable ma_gDeviceBackendVTable_Audio4 =
3904839146
ma_device_uninit__audio4,
3904939147
ma_device_start__audio4,
3905039148
ma_device_stop__audio4,
39051-
ma_device_read__audio4,
39052-
ma_device_write__audio4,
39053-
NULL, /* onDeviceLoop */
39149+
NULL,
39150+
NULL,
39151+
ma_device_loop__audio4,
3905439152
NULL /* onDeviceWakeup */
3905539153
};
3905639154

@@ -39652,6 +39750,7 @@ static ma_result ma_device_init_fd__oss(ma_device* pDevice, const ma_device_conf
3965239750
MA_ASSERT(pDevice != NULL);
3965339751
MA_ASSERT(pDeviceConfigOSS != NULL);
3965439752
MA_ASSERT(pFD != NULL);
39753+
MA_ASSERT(ppIntermediaryBuffer != NULL);
3965539754
MA_ASSERT(deviceType != ma_device_type_duplex);
3965639755

3965739756
pDeviceID = pDescriptor->pDeviceID;

0 commit comments

Comments
 (0)