From bbc7ad19210a390508c22e0130d53c854e82d161 Mon Sep 17 00:00:00 2001 From: David Reid Date: Wed, 28 Jan 2026 14:47:24 +1000 Subject: [PATCH] audio(4): Make format encoding stuff more robust. --- miniaudio.h | 129 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 94 insertions(+), 35 deletions(-) diff --git a/miniaudio.h b/miniaudio.h index 4742a5ca..5cd5fb8e 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -41223,26 +41223,65 @@ static void ma_construct_device_id__audio4(char* id, size_t idSize, const char* static ma_format ma_format_from_encoding__audio4(unsigned int encoding, unsigned int precision) { - if (precision == 8 && (encoding == AUDIO_ENCODING_ULINEAR || encoding == AUDIO_ENCODING_ULINEAR || encoding == AUDIO_ENCODING_ULINEAR_LE || encoding == AUDIO_ENCODING_ULINEAR_BE)) { - return ma_format_u8; + if (precision == 8) { + switch (encoding) + { + #if defined(AUDIO_ENCODING_LINEAR8) + case AUDIO_ENCODING_LINEAR8: return ma_format_u8; + #endif + #if defined(AUDIO_ENCODING_ULINEAR) + case AUDIO_ENCODING_ULINEAR: return ma_format_u8; + #endif + #if defined(AUDIO_ENCODING_ULINEAR_LE) + case AUDIO_ENCODING_ULINEAR_LE: return ma_format_u8; + #endif + #if defined(AUDIO_ENCODING_ULINEAR_BE) + case AUDIO_ENCODING_ULINEAR_BE: return ma_format_u8; + #endif + default: return ma_format_unknown; + } } else { - if (ma_is_little_endian() && encoding == AUDIO_ENCODING_SLINEAR_LE) { - if (precision == 16) { - return ma_format_s16; - } else if (precision == 24) { - return ma_format_s24; - } else if (precision == 32) { - return ma_format_s32; - } - } else if (ma_is_big_endian() && encoding == AUDIO_ENCODING_SLINEAR_BE) { - if (precision == 16) { - return ma_format_s16; - } else if (precision == 24) { - return ma_format_s24; - } else if (precision == 32) { - return ma_format_s32; + #if defined(AUDIO_ENCODING_LINEAR) + { + if (encoding == AUDIO_ENCODING_LINEAR) { /* Assume native endian. */ + /* */ if (precision == 16) { + return ma_format_s16; + } else if (precision == 24) { + return ma_format_s24; + } else if (precision == 32) { + return ma_format_s32; + } } } + #endif + + #if defined(AUDIO_ENCODING_SLINEAR_LE) + { + if (encoding == AUDIO_ENCODING_SLINEAR_LE && ma_is_little_endian()) { /* Explicitly little-endian. Can only be supported on little-endian machines. */ + /* */ if (precision == 16) { + return ma_format_s16; + } else if (precision == 24) { + return ma_format_s24; + } else if (precision == 32) { + return ma_format_s32; + } + } + } + #endif + + #if defined(AUDIO_ENCODING_SLINEAR_BE) + { + if (encoding == AUDIO_ENCODING_SLINEAR_BE && ma_is_big_endian()) { /* Explicitly big-endian. Can only be supported on big-endian machines. */ + /* */ if (precision == 16) { + return ma_format_s16; + } else if (precision == 24) { + return ma_format_s24; + } else if (precision == 32) { + return ma_format_s32; + } + } + } + #endif } return ma_format_unknown; /* Encoding not supported. */ @@ -41253,32 +41292,52 @@ static void ma_encoding_from_format__audio4(ma_format format, unsigned int* pEnc MA_ASSERT(pEncoding != NULL); MA_ASSERT(pPrecision != NULL); + /* Encoding. */ + if (format == ma_format_u8) { + #if defined(AUDIO_ENCODING_ULINEAR) + *pEncoding = AUDIO_ENCODING_ULINEAR; + #elif defined(AUDIO_ENCODING_LINEAR8) + *pEncoding = AUDIO_ENCODING_LINEAR8; + #else + #error "Don't know how to encode format." + #endif + } else { + #if defined(AUDIO_ENCODING_LINEAR) + *pEncoding = AUDIO_ENCODING_LINEAR; + #else + { + if (ma_is_little_endian()) { + #if defined(AUDIO_ENCODING_SLINEAR_LE) + *pEncoding = AUDIO_ENCODING_SLINEAR_LE; + #else + MA_ASSERT(!"audio(4): Don't know how to set the encoding. This platform cannot use the audio(4) backend. Please submit a bug report."); + *pEncoding = 0; /* Don't know how to set the encoding... */ + #endif + } else { + #if defined(AUDIO_ENCODING_SLINEAR_LE) + *pEncoding = AUDIO_ENCODING_SLINEAR_LE; + #else + MA_ASSERT(!"audio(4): Don't know how to set the encoding. This platform cannot use the audio(4) backend. Please submit a bug report."); + *pEncoding = 0; /* Don't know how to set the encoding... */ + #endif + } + } + #endif + } + + /* Precision. */ switch (format) { - case ma_format_u8: - { - *pEncoding = AUDIO_ENCODING_ULINEAR; - *pPrecision = 8; - } break; - - case ma_format_s24: - { - *pEncoding = (ma_is_little_endian()) ? AUDIO_ENCODING_SLINEAR_LE : AUDIO_ENCODING_SLINEAR_BE; - *pPrecision = 24; - } break; - - case ma_format_s32: - { - *pEncoding = (ma_is_little_endian()) ? AUDIO_ENCODING_SLINEAR_LE : AUDIO_ENCODING_SLINEAR_BE; - *pPrecision = 32; - } break; + case ma_format_u8: *pPrecision = 8; break; + case ma_format_s24: *pPrecision = 24; break; + case ma_format_s32: *pPrecision = 32; break; + /* Default everything else to 16-bit encoding. */ case ma_format_s16: case ma_format_f32: case ma_format_unknown: default: { - *pEncoding = (ma_is_little_endian()) ? AUDIO_ENCODING_SLINEAR_LE : AUDIO_ENCODING_SLINEAR_BE; *pPrecision = 16; } break; }