aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2023-02-17 13:50:12 -0800
committerChris Robinson <[email protected]>2023-02-17 13:50:12 -0800
commit7e00d4a00243e417ff59d3be001e1180ff55b72b (patch)
tree454ed450839181ee1ef42527c4b62f881ada32a1
parent08bfbaa5336d183199c83a49365976da088b4bbf (diff)
Support float and ADPCM formats in alstream
-rw-r--r--examples/alstream.c229
1 files changed, 210 insertions, 19 deletions
diff --git a/examples/alstream.c b/examples/alstream.c
index 2e427a32..b561406c 100644
--- a/examples/alstream.c
+++ b/examples/alstream.c
@@ -39,20 +39,31 @@
/* Define the number of buffers and buffer size (in milliseconds) to use. 4
- * buffers with 8192 samples each gives a nice per-chunk size, and lets the
- * queue last for almost one second at 44.1khz. */
+ * buffers at 200ms each gives a nice per-chunk size, and lets the queue last
+ * for almost one second.
+ */
#define NUM_BUFFERS 4
-#define BUFFER_SAMPLES 8192
+#define BUFFER_MILLISEC 200
+
+typedef enum SampleType {
+ Int16, Float, IMA4, MSADPCM
+} SampleType;
typedef struct StreamPlayer {
- /* These are the buffers and source to play out through OpenAL with */
+ /* These are the buffers and source to play out through OpenAL with. */
ALuint buffers[NUM_BUFFERS];
ALuint source;
/* Handle for the audio file */
SNDFILE *sndfile;
SF_INFO sfinfo;
- short *membuf;
+ void *membuf;
+
+ /* The sample type and block/frame size being read for the buffer. */
+ SampleType sample_type;
+ int byteblockalign;
+ int sampleblockalign;
+ int block_count;
/* The format of the output stream (sample rate is in sfinfo) */
ALenum format;
@@ -67,7 +78,8 @@ static int UpdatePlayer(StreamPlayer *player);
/* Creates a new player object, and allocates the needed OpenAL source and
* buffer objects. Error checking is simplified for the purposes of this
- * example, and will cause an abort if needed. */
+ * example, and will cause an abort if needed.
+ */
static StreamPlayer *NewPlayer(void)
{
StreamPlayer *player;
@@ -112,7 +124,7 @@ static void DeletePlayer(StreamPlayer *player)
* it will be closed first. */
static int OpenPlayerFile(StreamPlayer *player, const char *filename)
{
- size_t frame_size;
+ int byteblockalign=0, splblockalign=0;
ClosePlayerFile(player);
@@ -124,20 +136,148 @@ static int OpenPlayerFile(StreamPlayer *player, const char *filename)
return 0;
}
- /* Get the sound format, and figure out the OpenAL format */
+ /* Detect a suitable format to load. Formats like Vorbis and Opus use float
+ * natively, so load as float to avoid clipping when possible. Formats
+ * larger than 16-bit can also use float to preserve a bit more precision.
+ */
+ switch((player->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"))
+ player->sample_type = Float;
+ break;
+ case SF_FORMAT_IMA_ADPCM:
+ /* ADPCM formats require setting a block alignment as specified in the
+ * file, which needs to be read from the wave 'fmt ' chunk manually
+ * since libsndfile doesn't provide it in a format-agnostic way.
+ */
+ if(player->sfinfo.channels <= 2
+ && (player->sfinfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
+ && alIsExtensionPresent("AL_EXT_IMA4")
+ && alIsExtensionPresent("AL_SOFT_block_alignment"))
+ player->sample_type = IMA4;
+ break;
+ case SF_FORMAT_MS_ADPCM:
+ if(player->sfinfo.channels <= 2
+ && (player->sfinfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
+ && alIsExtensionPresent("AL_SOFT_MSADPCM")
+ && alIsExtensionPresent("AL_SOFT_block_alignment"))
+ player->sample_type = MSADPCM;
+ break;
+ }
+
+ if(player->sample_type == IMA4 || player->sample_type == MSADPCM)
+ {
+ /* For ADPCM, lookup the wave file's "fmt " chunk, which is a
+ * WAVEFORMATEX-based structure for the audio format.
+ */
+ SF_CHUNK_INFO inf = { "fmt ", 4, 0, NULL };
+ SF_CHUNK_ITERATOR *iter = sf_get_chunk_iterator(player->sndfile, &inf);
+
+ /* If there's an issue getting the chunk or block alignment, load as
+ * 16-bit and have libsndfile do the conversion.
+ */
+ if(!iter || sf_get_chunk_size(iter, &inf) != SF_ERR_NO_ERROR)
+ player->sample_type = Int16;
+ else
+ {
+ ALubyte *fmtbuf = calloc(inf.datalen, 1);
+ inf.data = fmtbuf;
+ if(sf_get_chunk_data(iter, &inf) != SF_ERR_NO_ERROR)
+ player->sample_type = Int16;
+ 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(player->sample_type == IMA4)
+ {
+ splblockalign = (byteblockalign/player->sfinfo.channels - 4)/4*8 + 1;
+ if(splblockalign < 1
+ || ((splblockalign-1)/2 + 4)*player->sfinfo.channels != byteblockalign)
+ player->sample_type = Int16;
+ }
+ else
+ {
+ splblockalign = (byteblockalign/player->sfinfo.channels - 7)*2 + 2;
+ if(splblockalign < 2
+ || ((splblockalign-2)/2 + 7)*player->sfinfo.channels != byteblockalign)
+ player->sample_type = Int16;
+ }
+ }
+ free(fmtbuf);
+ }
+ }
+
+ if(player->sample_type == Int16)
+ {
+ player->sampleblockalign = 1;
+ player->byteblockalign = player->sfinfo.channels * 2;
+ }
+ else if(player->sample_type == Float)
+ {
+ player->sampleblockalign = 1;
+ player->byteblockalign = player->sfinfo.channels * 4;
+ }
+ else
+ {
+ player->sampleblockalign = splblockalign;
+ player->byteblockalign = byteblockalign;
+ }
+
+ /* Figure out the OpenAL format from the file and desired sample type. */
+ player->format = AL_NONE;
if(player->sfinfo.channels == 1)
- player->format = AL_FORMAT_MONO16;
+ {
+ if(player->sample_type == Int16)
+ player->format = AL_FORMAT_MONO16;
+ else if(player->sample_type == Float)
+ player->format = AL_FORMAT_MONO_FLOAT32;
+ else if(player->sample_type == IMA4)
+ player->format = AL_FORMAT_MONO_IMA4;
+ else if(player->sample_type == MSADPCM)
+ player->format = AL_FORMAT_MONO_MSADPCM_SOFT;
+ }
else if(player->sfinfo.channels == 2)
- player->format = AL_FORMAT_STEREO16;
+ {
+ if(player->sample_type == Int16)
+ player->format = AL_FORMAT_STEREO16;
+ else if(player->sample_type == Float)
+ player->format = AL_FORMAT_STEREO_FLOAT32;
+ else if(player->sample_type == IMA4)
+ player->format = AL_FORMAT_STEREO_IMA4;
+ else if(player->sample_type == MSADPCM)
+ player->format = AL_FORMAT_STEREO_MSADPCM_SOFT;
+ }
else if(player->sfinfo.channels == 3)
{
if(sf_command(player->sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
- player->format = AL_FORMAT_BFORMAT2D_16;
+ {
+ if(player->sample_type == Int16)
+ player->format = AL_FORMAT_BFORMAT2D_16;
+ else if(player->sample_type == Float)
+ player->format = AL_FORMAT_BFORMAT2D_FLOAT32;
+ }
}
else if(player->sfinfo.channels == 4)
{
if(sf_command(player->sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
- player->format = AL_FORMAT_BFORMAT3D_16;
+ {
+ if(player->sample_type == Int16)
+ player->format = AL_FORMAT_BFORMAT3D_16;
+ else if(player->sample_type == Float)
+ player->format = AL_FORMAT_BFORMAT3D_FLOAT32;
+ }
}
if(!player->format)
{
@@ -147,8 +287,9 @@ static int OpenPlayerFile(StreamPlayer *player, const char *filename)
return 0;
}
- frame_size = (size_t)(BUFFER_SAMPLES * player->sfinfo.channels) * sizeof(short);
- player->membuf = malloc(frame_size);
+ player->block_count = player->sfinfo.samplerate / player->sampleblockalign;
+ player->block_count = player->block_count * BUFFER_MILLISEC / 1000;
+ player->membuf = malloc((size_t)(player->block_count * player->byteblockalign));
return 1;
}
@@ -162,6 +303,15 @@ static void ClosePlayerFile(StreamPlayer *player)
free(player->membuf);
player->membuf = NULL;
+
+ if(player->sampleblockalign > 1)
+ {
+ ALsizei i;
+ for(i = 0;i < NUM_BUFFERS;i++)
+ alBufferi(player->buffers[i], AL_UNPACK_BLOCK_ALIGNMENT_SOFT, 0);
+ player->sampleblockalign = 0;
+ player->byteblockalign = 0;
+ }
}
@@ -177,11 +327,35 @@ static int StartPlayer(StreamPlayer *player)
/* Fill the buffer queue */
for(i = 0;i < NUM_BUFFERS;i++)
{
+ sf_count_t slen;
+
/* Get some data to give it to the buffer */
- sf_count_t slen = sf_readf_short(player->sndfile, player->membuf, BUFFER_SAMPLES);
- if(slen < 1) break;
+ if(player->sample_type == Int16)
+ {
+ slen = sf_readf_short(player->sndfile, player->membuf,
+ player->block_count * player->sampleblockalign);
+ if(slen < 1) break;
+ slen *= player->byteblockalign;
+ }
+ else if(player->sample_type == Float)
+ {
+ slen = sf_readf_float(player->sndfile, player->membuf,
+ player->block_count * player->sampleblockalign);
+ if(slen < 1) break;
+ slen *= player->byteblockalign;
+ }
+ else
+ {
+ slen = sf_read_raw(player->sndfile, player->membuf,
+ player->block_count * player->byteblockalign);
+ if(slen > 0) slen -= slen%player->byteblockalign;
+ if(slen < 1) break;
+ }
+
+ if(player->sampleblockalign > 1)
+ alBufferi(player->buffers[i], AL_UNPACK_BLOCK_ALIGNMENT_SOFT,
+ player->sampleblockalign);
- slen *= player->sfinfo.channels * (sf_count_t)sizeof(short);
alBufferData(player->buffers[i], player->format, player->membuf, (ALsizei)slen,
player->sfinfo.samplerate);
}
@@ -227,10 +401,27 @@ static int UpdatePlayer(StreamPlayer *player)
/* Read the next chunk of data, refill the buffer, and queue it
* back on the source */
- slen = sf_readf_short(player->sndfile, player->membuf, BUFFER_SAMPLES);
+ if(player->sample_type == Int16)
+ {
+ slen = sf_readf_short(player->sndfile, player->membuf,
+ player->block_count * player->sampleblockalign);
+ if(slen > 0) slen *= player->byteblockalign;
+ }
+ else if(player->sample_type == Float)
+ {
+ slen = sf_readf_float(player->sndfile, player->membuf,
+ player->block_count * player->sampleblockalign);
+ if(slen > 0) slen *= player->byteblockalign;
+ }
+ else
+ {
+ slen = sf_read_raw(player->sndfile, player->membuf,
+ player->block_count * player->byteblockalign);
+ if(slen > 0) slen -= slen%player->byteblockalign;
+ }
+
if(slen > 0)
{
- slen *= player->sfinfo.channels * (sf_count_t)sizeof(short);
alBufferData(bufid, player->format, player->membuf, (ALsizei)slen,
player->sfinfo.samplerate);
alSourceQueueBuffers(player->source, 1, &bufid);