mirror of
https://github.com/love2d/megasource.git
synced 2026-08-17 19:24:09 +02:00
Update OpenAL-soft to 1.23.1-bc7cb17.
This commit is contained in:
@@ -37,22 +37,35 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
|
||||
/* 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. */
|
||||
#define NUM_BUFFERS 4
|
||||
#define BUFFER_SAMPLES 8192
|
||||
* buffers at 200ms each gives a nice per-chunk size, and lets the queue last
|
||||
* for almost one second.
|
||||
*/
|
||||
enum { NumBuffers = 4 };
|
||||
enum { BufferMillisec = 200 };
|
||||
|
||||
typedef enum SampleType {
|
||||
Int16, Float, IMA4, MSADPCM
|
||||
} SampleType;
|
||||
|
||||
typedef struct StreamPlayer {
|
||||
/* These are the buffers and source to play out through OpenAL with */
|
||||
ALuint buffers[NUM_BUFFERS];
|
||||
/* These are the buffers and source to play out through OpenAL with. */
|
||||
ALuint buffers[NumBuffers];
|
||||
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 +80,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;
|
||||
@@ -76,7 +90,7 @@ static StreamPlayer *NewPlayer(void)
|
||||
assert(player != NULL);
|
||||
|
||||
/* Generate the buffers and source */
|
||||
alGenBuffers(NUM_BUFFERS, player->buffers);
|
||||
alGenBuffers(NumBuffers, player->buffers);
|
||||
assert(alGetError() == AL_NO_ERROR && "Could not create buffers");
|
||||
|
||||
alGenSources(1, &player->source);
|
||||
@@ -99,11 +113,11 @@ static void DeletePlayer(StreamPlayer *player)
|
||||
ClosePlayerFile(player);
|
||||
|
||||
alDeleteSources(1, &player->source);
|
||||
alDeleteBuffers(NUM_BUFFERS, player->buffers);
|
||||
alDeleteBuffers(NumBuffers, player->buffers);
|
||||
if(alGetError() != AL_NO_ERROR)
|
||||
fprintf(stderr, "Failed to delete object IDs\n");
|
||||
|
||||
memset(player, 0, sizeof(*player));
|
||||
memset(player, 0, sizeof(*player)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
|
||||
free(player);
|
||||
}
|
||||
|
||||
@@ -112,7 +126,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 +138,151 @@ 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_ALAC_20:
|
||||
case SF_FORMAT_ALAC_24:
|
||||
case SF_FORMAT_ALAC_32:
|
||||
case 0x0080/*SF_FORMAT_MPEG_LAYER_I*/:
|
||||
case 0x0081/*SF_FORMAT_MPEG_LAYER_II*/:
|
||||
case 0x0082/*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 || inf.datalen < 14)
|
||||
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 +292,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 * BufferMillisec / 1000;
|
||||
player->membuf = malloc((size_t)player->block_count * (size_t)player->byteblockalign);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -162,6 +308,15 @@ static void ClosePlayerFile(StreamPlayer *player)
|
||||
|
||||
free(player->membuf);
|
||||
player->membuf = NULL;
|
||||
|
||||
if(player->sampleblockalign > 1)
|
||||
{
|
||||
ALsizei i;
|
||||
for(i = 0;i < NumBuffers;i++)
|
||||
alBufferi(player->buffers[i], AL_UNPACK_BLOCK_ALIGNMENT_SOFT, 0);
|
||||
player->sampleblockalign = 0;
|
||||
player->byteblockalign = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -175,13 +330,37 @@ static int StartPlayer(StreamPlayer *player)
|
||||
alSourcei(player->source, AL_BUFFER, 0);
|
||||
|
||||
/* Fill the buffer queue */
|
||||
for(i = 0;i < NUM_BUFFERS;i++)
|
||||
for(i = 0;i < NumBuffers;i++)
|
||||
{
|
||||
/* 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;
|
||||
sf_count_t slen;
|
||||
|
||||
/* Get some data to give it to the buffer */
|
||||
if(player->sample_type == Int16)
|
||||
{
|
||||
slen = sf_readf_short(player->sndfile, player->membuf,
|
||||
(sf_count_t)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,
|
||||
(sf_count_t)player->block_count * player->sampleblockalign);
|
||||
if(slen < 1) break;
|
||||
slen *= player->byteblockalign;
|
||||
}
|
||||
else
|
||||
{
|
||||
slen = sf_read_raw(player->sndfile, player->membuf,
|
||||
(sf_count_t)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 +406,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,
|
||||
(sf_count_t)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,
|
||||
(sf_count_t)player->block_count * player->sampleblockalign);
|
||||
if(slen > 0) slen *= player->byteblockalign;
|
||||
}
|
||||
else
|
||||
{
|
||||
slen = sf_read_raw(player->sndfile, player->membuf,
|
||||
(sf_count_t)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);
|
||||
@@ -292,10 +488,9 @@ int main(int argc, char **argv)
|
||||
|
||||
/* Get the name portion, without the path, for display. */
|
||||
namepart = strrchr(argv[i], '/');
|
||||
if(namepart || (namepart=strrchr(argv[i], '\\')))
|
||||
namepart++;
|
||||
else
|
||||
namepart = argv[i];
|
||||
if(!namepart) namepart = strrchr(argv[i], '\\');
|
||||
if(!namepart) namepart = argv[i];
|
||||
else namepart++;
|
||||
|
||||
printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format),
|
||||
player->sfinfo.samplerate);
|
||||
|
||||
Reference in New Issue
Block a user