diff options
author | Chris Robinson <[email protected]> | 2023-02-16 16:32:26 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-02-16 16:32:26 -0800 |
commit | 4e53db83877ba3f38af3516e188c5c7509d6b016 (patch) | |
tree | 1a444bca07b637eb9777ba1a06f8143986af22b4 /examples | |
parent | f424d7f7a623f610c4d87b3d161057ed4118316e (diff) |
Support loading as float or ADPCM in alplay
Diffstat (limited to 'examples')
-rw-r--r-- | examples/alplay.c | 176 |
1 files changed, 164 insertions, 12 deletions
diff --git a/examples/alplay.c b/examples/alplay.c index 56d5434b..e1b7c5f0 100644 --- a/examples/alplay.c +++ b/examples/alplay.c @@ -38,18 +38,28 @@ #include "common/alhelpers.h" +enum FormatType { + Int16, + Float, + IMA4, + MSADPCM +}; + /* LoadBuffer loads the named audio file into an OpenAL buffer object, and * returns the new buffer ID. */ static ALuint LoadSound(const char *filename) { + enum FormatType sample_format = Int16; + ALint byteblockalign = 0; + ALint splblockalign = 0; + sf_count_t num_frames; ALenum err, format; - ALuint buffer; + ALsizei num_bytes; SNDFILE *sndfile; SF_INFO sfinfo; - short *membuf; - sf_count_t num_frames; - ALsizei num_bytes; + ALuint buffer; + void *membuf; /* Open the audio file and check that it's usable. */ sndfile = sf_open(filename, SFM_READ, &sfinfo); @@ -58,28 +68,148 @@ static ALuint LoadSound(const char *filename) fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile)); return 0; } - if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels) + if(sfinfo.frames < 1) { fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames); sf_close(sndfile); return 0; } + /* Detect a suitable format to load. Formats like Vorbis and Opus use float + * natively, so load as float to avoid clipping. Formats larger than 16-bit + * can also use float to preserve a bit more precision. + */ + switch((sfinfo.format&SF_FORMAT_SUBMASK)) + { + case SF_FORMAT_PCM_24: + case SF_FORMAT_PCM_32: + case SF_FORMAT_FLOAT: + case SF_FORMAT_DOUBLE: + case SF_FORMAT_VORBIS: + case SF_FORMAT_OPUS: + case SF_FORMAT_MPEG_LAYER_I: + case SF_FORMAT_MPEG_LAYER_II: + case SF_FORMAT_MPEG_LAYER_III: + if(alIsExtensionPresent("AL_EXT_FLOAT32")) + sample_format = Float; + break; + case SF_FORMAT_IMA_ADPCM: + /* ADPCM formats require setting a block alignment, which libsndfile + * doesn't explicitly provide and needs to be read from the wave 'fmt ' + * chunk manually. + */ + if(sfinfo.channels <= 2 && (sfinfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV + && alIsExtensionPresent("AL_EXT_IMA4") + && alIsExtensionPresent("AL_SOFT_block_alignment")) + sample_format = IMA4; + break; + case SF_FORMAT_MS_ADPCM: + if(sfinfo.channels <= 2 && (sfinfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV + && alIsExtensionPresent("AL_SOFT_MSADPCM") + && alIsExtensionPresent("AL_SOFT_block_alignment")) + sample_format = MSADPCM; + break; + } + + if(sample_format == IMA4 || sample_format == MSADPCM) + { + SF_CHUNK_INFO inf = { "fmt ", 4, 0, NULL }; + SF_CHUNK_ITERATOR *iter = sf_get_chunk_iterator(sndfile, &inf); + if(iter) + { + if(sf_get_chunk_size(iter, &inf) != SF_ERR_NO_ERROR) + iter = NULL; + else + { + ALubyte *fmtbuf = calloc(inf.datalen, 1); + inf.data = fmtbuf; + if(sf_get_chunk_data(iter, &inf) != SF_ERR_NO_ERROR) + iter = NULL; + else + { + /* Read the nBlockAlign field, and convert from bytes- to + * samples-per-block (verifying it's valid by converting + * back and comparing to the original value). + */ + byteblockalign = fmtbuf[12] | (fmtbuf[13]<<8); + if(sample_format == IMA4) + { + splblockalign = (byteblockalign/sfinfo.channels - 4)/4*8 + 1; + if(((splblockalign-1)/2 + 4)*sfinfo.channels != byteblockalign) + iter = NULL; + } + else + { + splblockalign = (byteblockalign/sfinfo.channels - 7)*2 + 2; + if(((splblockalign-2)/2 + 7)*sfinfo.channels != byteblockalign) + iter = NULL; + } + } + free(fmtbuf); + } + } + + /* If there was an issue getting the block alignment, have libsndfile + * do the conversion and load as 16-bit. + */ + if(!iter) + sample_format = Int16; + } + + if(sample_format == Int16) + { + splblockalign = 1; + byteblockalign = sfinfo.channels * 2; + } + else if(sample_format == Float) + { + splblockalign = 1; + byteblockalign = sfinfo.channels * 4; + } + /* Get the sound format, and figure out the OpenAL format */ format = AL_NONE; if(sfinfo.channels == 1) - format = AL_FORMAT_MONO16; + { + if(sample_format == Int16) + format = AL_FORMAT_MONO16; + else if(sample_format == Float) + format = AL_FORMAT_MONO_FLOAT32; + else if(sample_format == IMA4) + format = AL_FORMAT_MONO_IMA4; + else if(sample_format == MSADPCM) + format = AL_FORMAT_MONO_MSADPCM_SOFT; + } else if(sfinfo.channels == 2) - format = AL_FORMAT_STEREO16; + { + if(sample_format == Int16) + format = AL_FORMAT_STEREO16; + else if(sample_format == Float) + format = AL_FORMAT_STEREO_FLOAT32; + else if(sample_format == IMA4) + format = AL_FORMAT_STEREO_IMA4; + else if(sample_format == MSADPCM) + format = AL_FORMAT_STEREO_MSADPCM_SOFT; + } else if(sfinfo.channels == 3) { if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT) - format = AL_FORMAT_BFORMAT2D_16; + { + if(sample_format == Int16) + format = AL_FORMAT_BFORMAT2D_16; + else if(sample_format == Float) + format = AL_FORMAT_BFORMAT2D_FLOAT32; + } } else if(sfinfo.channels == 4) { if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT) - format = AL_FORMAT_BFORMAT3D_16; + { + if(sample_format == Int16) + format = AL_FORMAT_BFORMAT3D_16; + else if(sample_format == Float) + format = AL_FORMAT_BFORMAT3D_FLOAT32; + } } if(!format) { @@ -88,10 +218,27 @@ static ALuint LoadSound(const char *filename) return 0; } + if(sfinfo.frames/splblockalign > (sf_count_t)(INT_MAX/byteblockalign)) + { + fprintf(stderr, "Too many samples in %s (%" PRId64 ")\n", filename, sfinfo.frames); + sf_close(sndfile); + return 0; + } + /* Decode the whole audio file to a buffer. */ - membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short)); + membuf = malloc((size_t)(sfinfo.frames / splblockalign * byteblockalign)); - num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames); + if(sample_format == Int16) + num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames); + else if(sample_format == Float) + num_frames = sf_readf_float(sndfile, membuf, sfinfo.frames); + else + { + sf_count_t count = sfinfo.frames / splblockalign * byteblockalign; + num_frames = sf_read_raw(sndfile, membuf, count); + if(num_frames > 0) + num_frames = num_frames / byteblockalign * splblockalign; + } if(num_frames < 1) { free(membuf); @@ -99,13 +246,18 @@ static ALuint LoadSound(const char *filename) fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames); return 0; } - num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short); + num_bytes = (ALsizei)(num_frames / splblockalign * byteblockalign); + + printf("Loading: %s (%s, %dhz)\n", filename, FormatName(format), sfinfo.samplerate); + fflush(stdout); /* Buffer the audio data into a new buffer object, then free the data and * close the file. */ buffer = 0; alGenBuffers(1, &buffer); + if(splblockalign > 1) + alBufferi(buffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, splblockalign); alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate); free(membuf); |