new boolean floatingPoint option

Now floating point is either on or off. This simplifies the use of 16bit
vs float.
This commit is contained in:
Joshua Reisenauer 2016-05-02 21:59:55 -07:00
parent c3208c5cd6
commit 9d09ada33b
3 changed files with 88 additions and 58 deletions

View file

@ -59,15 +59,17 @@
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#define MUSIC_STREAM_BUFFERS 2 #define MAX_STREAM_BUFFERS 2
#define MAX_AUDIO_CONTEXTS 4 #define MAX_AUDIO_CONTEXTS 4 // Number of open AL sources
#if defined(PLATFORM_RPI) || defined(PLATFORM_ANDROID) #if defined(PLATFORM_RPI) || defined(PLATFORM_ANDROID)
// NOTE: On RPI and Android should be lower to avoid frame-stalls // NOTE: On RPI and Android should be lower to avoid frame-stalls
#define MUSIC_BUFFER_SIZE 4096*2 // PCM data buffer (short) - 16Kb (RPI) #define MUSIC_BUFFER_SIZE_SHORT 4096*2 // PCM data buffer (short) - 16Kb (RPI)
#define MUSIC_BUFFER_SIZE_FLOAT 4096 // PCM data buffer (float) - 16Kb (RPI)
#else #else
// NOTE: On HTML5 (emscripten) this is allocated on heap, by default it's only 16MB!...just take care... // NOTE: On HTML5 (emscripten) this is allocated on heap, by default it's only 16MB!...just take care...
#define MUSIC_BUFFER_SIZE 4096*8 // PCM data buffer (short) - 64Kb #define MUSIC_BUFFER_SIZE_SHORT 4096*8 // PCM data buffer (short) - 64Kb
#define MUSIC_BUFFER_SIZE_FLOAT 4096*4 // PCM data buffer (float) - 64Kb
#endif #endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -80,7 +82,7 @@ typedef struct Music {
stb_vorbis *stream; stb_vorbis *stream;
jar_xm_context_t *chipctx; // Stores jar_xm context jar_xm_context_t *chipctx; // Stores jar_xm context
ALuint buffers[MUSIC_STREAM_BUFFERS]; ALuint buffers[MAX_STREAM_BUFFERS];
ALuint source; ALuint source;
ALenum format; ALenum format;
@ -96,12 +98,13 @@ typedef struct Music {
// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to // no more than 4 concurrent audio contexts in use. This is due to each active context being tied to
// a dedicated mix channel. All audio is 32bit floating point in stereo. // a dedicated mix channel. All audio is 32bit floating point in stereo.
typedef struct AudioContext_t { typedef struct AudioContext_t {
unsigned short sampleRate; // default is 48000 unsigned short sampleRate; // default is 48000
unsigned char channels; // 1=mono,2=stereo unsigned char channels; // 1=mono,2=stereo
unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream
ALenum alFormat; // openAL format specifier bool floatingPoint; // if false then the short datatype is used instead
ALuint alSource; // openAL source ALenum alFormat; // openAL format specifier
ALuint alBuffer[2]; // openAL sample buffer ALuint alSource; // openAL source
ALuint alBuffer[MAX_STREAM_BUFFERS]; // openAL sample buffer
} AudioContext_t; } AudioContext_t;
#if defined(AUDIO_STANDALONE) #if defined(AUDIO_STANDALONE)
@ -126,7 +129,7 @@ static void UnloadWave(Wave wave); // Unload wave data
static bool BufferMusicStream(ALuint buffer); // Fill music buffers with data static bool BufferMusicStream(ALuint buffer); // Fill music buffers with data
static void EmptyMusicStream(void); // Empty music buffers static void EmptyMusicStream(void); // Empty music buffers
static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer);// fill buffer with zeros static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer);// fill buffer with zeros, returns number processed
static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in
static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // pass two arrays of same length in static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // pass two arrays of same length in
@ -201,11 +204,10 @@ bool IsAudioDeviceReady(void)
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time.
// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo // exmple usage is InitAudioContext(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point
// all samples are floating point by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint)
AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels)
{ {
if(mixChannel > MAX_AUDIO_CONTEXTS) return NULL; if(mixChannel >= MAX_AUDIO_CONTEXTS) return NULL;
if(!IsAudioDeviceReady()) InitAudioDevice(); if(!IsAudioDeviceReady()) InitAudioDevice();
else StopMusicStream(); else StopMusicStream();
@ -214,13 +216,24 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChanne
ac->sampleRate = sampleRate; ac->sampleRate = sampleRate;
ac->channels = channels; ac->channels = channels;
ac->mixChannel = mixChannel; ac->mixChannel = mixChannel;
ac->floatingPoint = floatingPoint;
mixChannelsActive_g[mixChannel] = ac; mixChannelsActive_g[mixChannel] = ac;
// setup openAL format // setup openAL format
if(channels == 1) if(channels == 1)
ac->alFormat = AL_FORMAT_MONO_FLOAT32; {
else if(floatingPoint)
ac->alFormat = AL_FORMAT_STEREO_FLOAT32; ac->alFormat = AL_FORMAT_MONO_FLOAT32;
else
ac->alFormat = AL_FORMAT_MONO16;
}
else if(channels == 2)
{
if(floatingPoint)
ac->alFormat = AL_FORMAT_STEREO_FLOAT32;
else
ac->alFormat = AL_FORMAT_STEREO16;
}
// Create an audio source // Create an audio source
alGenSources(1, &ac->alSource); alGenSources(1, &ac->alSource);
@ -230,16 +243,17 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChanne
alSource3f(ac->alSource, AL_VELOCITY, 0, 0, 0); alSource3f(ac->alSource, AL_VELOCITY, 0, 0, 0);
// Create Buffer // Create Buffer
alGenBuffers(2, ac->alBuffer); alGenBuffers(MAX_STREAM_BUFFERS, ac->alBuffer);
//fill buffers //fill buffers
FillAlBufferWithSilence(ac, ac->alBuffer[0]); int x;
FillAlBufferWithSilence(ac, ac->alBuffer[1]); for(x=0;x<MAX_STREAM_BUFFERS;x++)
alSourceQueueBuffers(ac->alSource, 2, ac->alBuffer); FillAlBufferWithSilence(ac, ac->alBuffer[x]);
alSourceQueueBuffers(ac->alSource, MAX_STREAM_BUFFERS, ac->alBuffer);
alSourcei(ac->alSource, AL_LOOPING, AL_FALSE); // this could cause errors alSourcei(ac->alSource, AL_LOOPING, AL_FALSE); // this could cause errors
alSourcePlay(ac->alSource); alSourcePlay(ac->alSource);
return ac; return ac;
} }
return NULL; return NULL;
@ -264,20 +278,22 @@ void CloseAudioContext(AudioContext ctx)
//delete source and buffers //delete source and buffers
alDeleteSources(1, &context->alSource); alDeleteSources(1, &context->alSource);
alDeleteBuffers(2, context->alBuffer); alDeleteBuffers(MAX_STREAM_BUFFERS, context->alBuffer);
mixChannelsActive_g[context->mixChannel] = NULL; mixChannelsActive_g[context->mixChannel] = NULL;
free(context); free(context);
ctx = NULL; ctx = NULL;
} }
} }
// Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in.
// Call "UpdateAudioContext(ctx, NULL, 0)" every game tick if you want to pause the audio // Call "UpdateAudioContext(ctx, NULL, 0)" every game tick if you want to pause the audio.
// Returns number of floats that where processed // @Returns number of samples that where processed.
unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) // All data streams should be of a length that is evenly divisible by MUSIC_BUFFER_SIZE,
// otherwise the remaining data will not be pushed.
unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements)
{ {
unsigned short numberProcessed = 0; unsigned short numberProcessed = 0;
unsigned short numberRemaining = dataLength; unsigned short numberRemaining = numberElements;
AudioContext_t *context = (AudioContext_t*)ctx; AudioContext_t *context = (AudioContext_t*)ctx;
if (context && mixChannelsActive_g[context->mixChannel] == context) if (context && mixChannelsActive_g[context->mixChannel] == context)
@ -288,44 +304,60 @@ unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short
if(!processed) return 0;//nothing to process, queue is still full if(!processed) return 0;//nothing to process, queue is still full
if (!data || !dataLength)// play silence if (!data || !numberElements)// play silence
{
while (processed > 0) while (processed > 0)
{ {
alSourceUnqueueBuffers(context->alSource, 1, &buffer); alSourceUnqueueBuffers(context->alSource, 1, &buffer);
FillAlBufferWithSilence(context, buffer); numberProcessed += FillAlBufferWithSilence(context, buffer);
alSourceQueueBuffers(context->alSource, 1, &buffer); alSourceQueueBuffers(context->alSource, 1, &buffer);
processed--; processed--;
numberProcessed+=MUSIC_BUFFER_SIZE;
} }
}
if(numberRemaining)// buffer data stream in increments of MUSIC_BUFFER_SIZE if(numberRemaining)// buffer data stream in increments of MUSIC_BUFFER_SIZE
{
while (processed > 0) while (processed > 0)
{ {
alSourceUnqueueBuffers(context->alSource, 1, &buffer); if(context->floatingPoint && numberRemaining >= MUSIC_BUFFER_SIZE_FLOAT) // process float buffers
if(numberRemaining >= MUSIC_BUFFER_SIZE)
{ {
float pcm[MUSIC_BUFFER_SIZE]; float *ptr = (float*)data;
memcpy(pcm, &data[numberProcessed], MUSIC_BUFFER_SIZE); alSourceUnqueueBuffers(context->alSource, 1, &buffer);
alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), context->sampleRate); alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_FLOAT*sizeof(float), context->sampleRate);
alSourceQueueBuffers(context->alSource, 1, &buffer); alSourceQueueBuffers(context->alSource, 1, &buffer);
numberProcessed+=MUSIC_BUFFER_SIZE; numberProcessed+=MUSIC_BUFFER_SIZE_FLOAT;
numberRemaining-=MUSIC_BUFFER_SIZE; numberRemaining-=MUSIC_BUFFER_SIZE_FLOAT;
} }
else // less than MUSIC_BUFFER_SIZE number of samples left to buffer, the remaining data is padded with zeros else if(!context->floatingPoint && numberRemaining >= MUSIC_BUFFER_SIZE_SHORT) // process short buffers
{ {
float pcm[MUSIC_BUFFER_SIZE] = {0.f}; // pad with zeros short *ptr = (short*)data;
alSourceUnqueueBuffers(context->alSource, 1, &buffer);
alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_SHORT*sizeof(short), context->sampleRate);
alSourceQueueBuffers(context->alSource, 1, &buffer);
numberProcessed+=MUSIC_BUFFER_SIZE_SHORT;
numberRemaining-=MUSIC_BUFFER_SIZE_SHORT;
} }
processed--; processed--;
} }
}
} }
return numberProcessed; return numberProcessed;
} }
// fill buffer with zeros // fill buffer with zeros, returns number processed
static void FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer) static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer)
{ {
float pcm[MUSIC_BUFFER_SIZE] = {0.f}; if(context->floatingPoint){
alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), context->sampleRate); float pcm[MUSIC_BUFFER_SIZE_FLOAT] = {0.f};
alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE_FLOAT*sizeof(float), context->sampleRate);
return MUSIC_BUFFER_SIZE_FLOAT;
}
else
{
short pcm[MUSIC_BUFFER_SIZE_SHORT] = {0};
alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE_SHORT*sizeof(short), context->sampleRate);
return MUSIC_BUFFER_SIZE_SHORT;
}
} }
// example usage: // example usage:
@ -920,7 +952,7 @@ float GetMusicTimePlayed(void)
// Fill music buffers with new data from music stream // Fill music buffers with new data from music stream
static bool BufferMusicStream(ALuint buffer) static bool BufferMusicStream(ALuint buffer)
{ {
short pcm[MUSIC_BUFFER_SIZE]; short pcm[MUSIC_BUFFER_SIZE_SHORT];
int size = 0; // Total size of data steamed (in bytes) int size = 0; // Total size of data steamed (in bytes)
int streamedBytes = 0; // samples of data obtained, channels are not included in calculation int streamedBytes = 0; // samples of data obtained, channels are not included in calculation
@ -930,15 +962,15 @@ static bool BufferMusicStream(ALuint buffer)
{ {
if (currentMusic.chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. if (currentMusic.chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes.
{ {
int readlen = MUSIC_BUFFER_SIZE / 2; int readlen = MUSIC_BUFFER_SIZE_SHORT / 2;
jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location
size += readlen * currentMusic.channels; // Not sure if this is what it needs size += readlen * currentMusic.channels; // Not sure if this is what it needs
} }
else else
{ {
while (size < MUSIC_BUFFER_SIZE) while (size < MUSIC_BUFFER_SIZE_SHORT)
{ {
streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE - size); streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE_SHORT - size);
if (streamedBytes > 0) size += (streamedBytes*currentMusic.channels); if (streamedBytes > 0) size += (streamedBytes*currentMusic.channels);
else break; else break;
} }

View file

@ -84,11 +84,10 @@ bool IsAudioDeviceReady(void); // True if call
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time.
// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo // exmple usage is InitAudioContext(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point
// all samples are floating point by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint);
AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels);
void CloseAudioContext(AudioContext ctx); // Frees audio context void CloseAudioContext(AudioContext ctx); // Frees audio context
unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played
Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSound(char *fileName); // Load sound to memory
Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data

View file

@ -870,11 +870,10 @@ bool IsAudioDeviceReady(void); // True if call
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time.
// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo // exmple usage is InitAudioContext(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point
// all samples are floating point by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint);
AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels);
void CloseAudioContext(AudioContext ctx); // Frees audio context void CloseAudioContext(AudioContext ctx); // Frees audio context
unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played
Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSound(char *fileName); // Load sound to memory
Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data