audio: Simplified GetFirstAudioFormat/GetNextAudioFormat.

Now it just returns an iterable array and needs no global state.
This commit is contained in:
Ryan C. Gordon 2023-05-02 18:35:42 -04:00
parent b6ca360228
commit e191bc8491
17 changed files with 80 additions and 125 deletions

View File

@ -1520,38 +1520,26 @@ void SDL_QuitAudio(void)
}
#define NUM_FORMATS 8
static int format_idx; /* !!! FIXME: whoa, why are there globals in use here?! */
static int format_idx_sub;
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
{ SDL_AUDIO_U8, SDL_AUDIO_S8, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB },
{ SDL_AUDIO_S8, SDL_AUDIO_U8, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB },
{ SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_U8, SDL_AUDIO_S8 },
{ SDL_AUDIO_S16MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_U8, SDL_AUDIO_S8 },
{ SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_U8, SDL_AUDIO_S8 },
{ SDL_AUDIO_S32MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_U8, SDL_AUDIO_S8 },
{ SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_U8, SDL_AUDIO_S8 },
{ SDL_AUDIO_F32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_U8, SDL_AUDIO_S8 },
static const SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS + 1] = {
{ SDL_AUDIO_U8, SDL_AUDIO_S8, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, 0 },
{ SDL_AUDIO_S8, SDL_AUDIO_U8, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, 0 },
{ SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_U8, SDL_AUDIO_S8, 0 },
{ SDL_AUDIO_S16MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_U8, SDL_AUDIO_S8, 0 },
{ SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_U8, SDL_AUDIO_S8, 0 },
{ SDL_AUDIO_S32MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_U8, SDL_AUDIO_S8, 0 },
{ SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_U8, SDL_AUDIO_S8, 0 },
{ SDL_AUDIO_F32MSB, SDL_AUDIO_F32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_S32LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S16LSB, SDL_AUDIO_U8, SDL_AUDIO_S8, 0 },
};
SDL_AudioFormat
SDL_GetFirstAudioFormat(SDL_AudioFormat format)
const SDL_AudioFormat *SDL_ClosestAudioFormats(SDL_AudioFormat format)
{
for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
if (format_list[format_idx][0] == format) {
break;
int i;
for (i = 0; i < NUM_FORMATS; i++) {
if (format_list[i][0] == format) {
return &format_list[i][0];
}
}
format_idx_sub = 0;
return SDL_GetNextAudioFormat();
}
SDL_AudioFormat
SDL_GetNextAudioFormat(void)
{
if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
return 0;
}
return format_list[format_idx][format_idx_sub++];
return &format_list[0][NUM_FORMATS]; /* not found; return what looks like a list with only a zero in it. */
}
Uint8 SDL_GetSilenceValueForFormat(const SDL_AudioFormat format)

View File

@ -35,9 +35,8 @@
/* Functions and variables exported from SDL_audio.c for SDL_sysaudio.c */
/* Functions to get a list of "close" audio formats */
extern SDL_AudioFormat SDL_GetFirstAudioFormat(SDL_AudioFormat format);
extern SDL_AudioFormat SDL_GetNextAudioFormat(void);
/* Function to get a list of audio formats, ordered most similar to `format` to least, 0-terminated. Don't free results. */
const SDL_AudioFormat *SDL_ClosestAudioFormats(SDL_AudioFormat format);
/* Function to calculate the size and silence for a SDL_AudioSpec */
extern Uint8 SDL_GetSilenceValueForFormat(const SDL_AudioFormat format);

View File

@ -527,6 +527,7 @@ static int ALSA_OpenDevice(_THIS, const char *devname)
snd_pcm_sw_params_t *swparams = NULL;
snd_pcm_format_t format = 0;
SDL_AudioFormat test_format = 0;
const SDL_AudioFormat *closefmts;
unsigned int rate = 0;
unsigned int channels = 0;
#ifdef SND_CHMAP_API_VERSION
@ -569,7 +570,8 @@ static int ALSA_OpenDevice(_THIS, const char *devname)
}
/* Try for a closest match on audio format */
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
switch (test_format) {
case SDL_AUDIO_U8:
format = SND_PCM_FORMAT_U8;

View File

@ -38,6 +38,7 @@ static SDL_AudioDevice *captureDevice = NULL;
static int ANDROIDAUDIO_OpenDevice(_THIS, const char *devname)
{
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
SDL_bool iscapture = this->iscapture;
if (iscapture) {
@ -63,7 +64,8 @@ static int ANDROIDAUDIO_OpenDevice(_THIS, const char *devname)
return SDL_OutOfMemory();
}
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
if ((test_format == SDL_AUDIO_U8) ||
(test_format == SDL_AUDIO_S16) ||
(test_format == SDL_AUDIO_F32)) {

View File

@ -1008,6 +1008,7 @@ static int audioqueue_thread(void *arg)
static int COREAUDIO_OpenDevice(_THIS, const char *devname)
{
AudioStreamBasicDescription *strdesc;
const SDL_AudioFormat *closefmts;
SDL_AudioFormat test_format;
SDL_bool iscapture = this->iscapture;
SDL_AudioDevice **new_open_devices;
@ -1065,7 +1066,8 @@ static int COREAUDIO_OpenDevice(_THIS, const char *devname)
strdesc->mSampleRate = this->spec.freq;
strdesc->mFramesPerPacket = 1;
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
/* CoreAudio handles most of SDL's formats natively. */
switch (test_format) {
case SDL_AUDIO_U8:

View File

@ -485,6 +485,7 @@ static int DSOUND_OpenDevice(_THIS, const char *devname)
SDL_bool tried_format = SDL_FALSE;
SDL_bool iscapture = this->iscapture;
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
LPGUID guid = (LPGUID)this->handle;
DWORD bufsize;
@ -514,7 +515,8 @@ static int DSOUND_OpenDevice(_THIS, const char *devname)
}
}
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
switch (test_format) {
case SDL_AUDIO_U8:
case SDL_AUDIO_S16:

View File

@ -58,10 +58,11 @@ static int DSP_OpenDevice(_THIS, const char *devname)
{
SDL_bool iscapture = this->iscapture;
const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
int format;
int format = 0;
int value;
int frag_spec;
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
/* We don't care what the devname is...we'll try to open anything. */
/* ...but default to first name in the list... */
@ -112,9 +113,8 @@ static int DSP_OpenDevice(_THIS, const char *devname)
}
/* Try for a closest match on audio format */
format = 0;
for (test_format = SDL_GetFirstAudioFormat(this->spec.format);
!format && test_format;) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
#ifdef DEBUG_AUDIO
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
#endif
@ -146,12 +146,9 @@ static int DSP_OpenDevice(_THIS, const char *devname)
break;
#endif
default:
format = 0;
break;
}
if (!format) {
test_format = SDL_GetNextAudioFormat();
continue;
}
break;
}
if (format == 0) {
return SDL_SetError("Couldn't find any hardware audio formats");

View File

@ -199,6 +199,7 @@ static void EMSCRIPTENAUDIO_CloseDevice(_THIS)
static int EMSCRIPTENAUDIO_OpenDevice(_THIS, const char *devname)
{
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
SDL_bool iscapture = this->iscapture;
int result;
@ -235,7 +236,8 @@ static int EMSCRIPTENAUDIO_OpenDevice(_THIS, const char *devname)
return SDL_SetError("Web Audio API is not available!");
}
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
switch (test_format) {
case SDL_AUDIO_F32: /* web audio only supports floats */
break;

View File

@ -119,6 +119,7 @@ static int HAIKUAUDIO_OpenDevice(_THIS, const char *devname)
{
media_raw_audio_format format;
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
/* Initialize all variables that we clean on shutdown */
_this->hidden = new SDL_PrivateAudioData;
@ -132,7 +133,9 @@ static int HAIKUAUDIO_OpenDevice(_THIS, const char *devname)
format.byte_order = B_MEDIA_LITTLE_ENDIAN;
format.frame_rate = (float) _this->spec.freq;
format.channel_count = _this->spec.channels; /* !!! FIXME: support > 2? */
for (test_format = SDL_GetFirstAudioFormat(_this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(_this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
switch (test_format) {
case SDL_AUDIO_S8:
format.format = media_raw_audio_format::B_AUDIO_CHAR;

View File

@ -310,10 +310,9 @@ static void FreePrivateData(_THIS)
static int FindAudioFormat(_THIS)
{
SDL_bool found_valid_format = SDL_FALSE;
Uint16 test_format = SDL_GetFirstAudioFormat(this->spec.format);
while (!found_valid_format && test_format) {
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
this->spec.format = test_format;
switch (test_format) {
case SDL_AUDIO_S8:
@ -321,22 +320,17 @@ static int FindAudioFormat(_THIS)
this->hidden->format = (this->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM8 : NDSP_FORMAT_MONO_PCM8;
this->hidden->isSigned = 1;
this->hidden->bytePerSample = this->spec.channels;
found_valid_format = SDL_TRUE;
break;
return 0;
case SDL_AUDIO_S16:
/* Signed 16-bit audio supported */
this->hidden->format = (this->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16;
this->hidden->isSigned = 1;
this->hidden->bytePerSample = this->spec.channels * 2;
found_valid_format = SDL_TRUE;
break;
default:
test_format = SDL_GetNextAudioFormat();
break;
return 0;
}
}
return found_valid_format ? 0 : -1;
return -1;
}
#endif /* SDL_AUDIO_DRIVER_N3DS */

View File

@ -194,6 +194,7 @@ static int NETBSDAUDIO_OpenDevice(_THIS, const char *devname)
{
SDL_bool iscapture = this->iscapture;
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
int encoding = AUDIO_ENCODING_NONE;
audio_info_t info, hwinfo;
struct audio_prinfo *prinfo = iscapture ? &info.record : &info.play;
@ -235,7 +236,8 @@ static int NETBSDAUDIO_OpenDevice(_THIS, const char *devname)
prinfo->sample_rate = this->spec.freq;
prinfo->channels = this->spec.channels;
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
switch (test_format) {
case SDL_AUDIO_U8:
encoding = AUDIO_ENCODING_ULINEAR;

View File

@ -417,8 +417,9 @@ static int openslES_CreatePCMPlayer(_THIS)
https://developer.android.com/ndk/guides/audio/opensl/android-extensions.html#floating-point
*/
if (SDL_GetAndroidSDKVersion() >= 21) {
const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(this->spec.format);
SDL_AudioFormat test_format;
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
while ((test_format = *(closefmts++)) != 0) {
if (SDL_AUDIO_ISSIGNED(test_format)) {
break;
}

View File

@ -519,6 +519,7 @@ static int PULSEAUDIO_OpenDevice(_THIS, const char *devname)
{
struct SDL_PrivateAudioData *h = NULL;
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
pa_sample_spec paspec;
pa_buffer_attr paattr;
pa_channel_map pacmap;
@ -536,7 +537,8 @@ static int PULSEAUDIO_OpenDevice(_THIS, const char *devname)
SDL_zerop(this->hidden);
/* Try for a closest match on audio format */
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
#ifdef DEBUG_AUDIO
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
#endif

View File

@ -269,7 +269,7 @@ QSA_OpenDevice(_THIS, const char *devname)
int status = 0;
int format = 0;
SDL_AudioFormat test_format = 0;
int found = 0;
const SDL_AudioFormat *closefmts;
snd_pcm_channel_setup_t csetup;
snd_pcm_channel_params_t cparams;
SDL_bool iscapture = this->iscapture;
@ -312,70 +312,23 @@ QSA_OpenDevice(_THIS, const char *devname)
}
/* Try for a closest match on audio format */
format = 0;
/* can't use format as SND_PCM_SFMT_U8 = 0 in qsa */
found = 0;
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); !found;) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
/* if match found set format to equivalent QSA format */
switch (test_format) {
case SDL_AUDIO_U8:
{
format = SND_PCM_SFMT_U8;
found = 1;
}
break;
case SDL_AUDIO_S8:
{
format = SND_PCM_SFMT_S8;
found = 1;
}
break;
case SDL_AUDIO_S16LSB:
{
format = SND_PCM_SFMT_S16_LE;
found = 1;
}
break;
case SDL_AUDIO_S16MSB:
{
format = SND_PCM_SFMT_S16_BE;
found = 1;
}
break;
case SDL_AUDIO_S32LSB:
{
format = SND_PCM_SFMT_S32_LE;
found = 1;
}
break;
case SDL_AUDIO_S32MSB:
{
format = SND_PCM_SFMT_S32_BE;
found = 1;
}
break;
case SDL_AUDIO_F32LSB:
{
format = SND_PCM_SFMT_FLOAT_LE;
found = 1;
}
break;
case SDL_AUDIO_F32MSB:
{
format = SND_PCM_SFMT_FLOAT_BE;
found = 1;
}
break;
default:
{
break;
}
}
if (!found) {
test_format = SDL_GetNextAudioFormat();
#define CHECKFMT(sdlfmt, qsafmt) case SDL_AUDIO_##sdlfmt: format = SND_PCM_SFMT_##qsafmt; break
CHECKFMT(U8, U8);
CHECKFMT(S8, S8);
CHECKFMT(S16LSB, S16_LE);
CHECKFMT(S16MSB, S16_BE);
CHECKFMT(S32LSB, S32_LE);
CHECKFMT(S32MSB, S32_BE);
CHECKFMT(F32LSB, FLOAT_LE);
CHECKFMT(F32MSB, FLOAT_BE);
#undef CHECKFMT
default: continue;
}
break;
}
/* assumes test_format not 0 on success */

View File

@ -223,6 +223,7 @@ static void SNDIO_CloseDevice(_THIS)
static int SNDIO_OpenDevice(_THIS, const char *devname)
{
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
struct sio_par par;
SDL_bool iscapture = this->iscapture;
@ -258,7 +259,8 @@ static int SNDIO_OpenDevice(_THIS, const char *devname)
par.appbufsz = par.round * 2;
/* Try for a closest match on audio format */
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
if (!SDL_AUDIO_ISFLOAT(test_format)) {
par.le = SDL_AUDIO_ISLITTLEENDIAN(test_format) ? 1 : 0;
par.sig = SDL_AUDIO_ISSIGNED(test_format) ? 1 : 0;

View File

@ -61,6 +61,7 @@ static int VITAAUD_OpenDevice(_THIS, const char *devname)
int format, mixlen, i, port = SCE_AUDIO_OUT_PORT_TYPE_MAIN;
int vols[2] = { SCE_AUDIO_MAX_VOLUME, SCE_AUDIO_MAX_VOLUME };
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc(sizeof(*this->hidden));
@ -69,7 +70,8 @@ static int VITAAUD_OpenDevice(_THIS, const char *devname)
}
SDL_memset(this->hidden, 0, sizeof(*this->hidden));
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
if (test_format == SDL_AUDIO_S16LSB) {
this->spec.format = test_format;
break;

View File

@ -397,6 +397,7 @@ int WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
WAVEFORMATEX *waveformat = NULL;
SDL_AudioFormat test_format;
SDL_AudioFormat wasapi_format = 0;
const SDL_AudioFormat *closefmts;
HRESULT ret = S_OK;
DWORD streamflags = 0;
@ -425,7 +426,8 @@ int WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
/* Make sure we have a valid format that we can convert to whatever WASAPI wants. */
wasapi_format = WaveFormatToSDLFormat(waveformat);
for (test_format = SDL_GetFirstAudioFormat(this->spec.format); test_format; test_format = SDL_GetNextAudioFormat()) {
closefmts = SDL_ClosestAudioFormats(this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
if (test_format == wasapi_format) {
this->spec.format = test_format;
break;