From 4636e3367cdf3d2e1adab3a08667c34631f94837 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Mon, 2 May 2016 14:37:00 -0700 Subject: [PATCH] number remaining buffer transfer for updateAudioContext updateAudioContext is almost done --- src/audio.c | 39 +++++++++++++++++++++++++++++++-------- src/audio.h | 2 +- src/raylib.h | 2 +- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/audio.c b/src/audio.c index d935b6c74..ff4f2858e 100644 --- a/src/audio.c +++ b/src/audio.c @@ -273,17 +273,20 @@ void CloseAudioContext(AudioContext ctx) // 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 -// Returns true if data was pushed onto queue, otherwise if queue is full then no data is added and false is returned -bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) +// Returns number of floats that where processed +unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) { + unsigned short numberProcessed = 0; + unsigned short numberRemaining = dataLength; AudioContext_t *context = (AudioContext_t*)ctx; + if (context && mixChannelsActive_g[context->mixChannel] == context) { ALint processed = 0; ALuint buffer = 0; alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) - if(!processed) return false;//nothing to process, queue is still full + if(!processed) return 0;//nothing to process, queue is still full if (!data || !dataLength)// play silence while (processed > 0) @@ -292,17 +295,37 @@ bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength FillAlBufferWithSilence(context, buffer); alSourceQueueBuffers(context->alSource, 1, &buffer); processed--; + numberProcessed+=MUSIC_BUFFER_SIZE; + } + if(numberRemaining)// buffer data stream in increments of MUSIC_BUFFER_SIZE + while (processed > 0) + { + alSourceUnqueueBuffers(context->alSource, 1, &buffer); + if(numberRemaining >= MUSIC_BUFFER_SIZE) + { + float pcm[MUSIC_BUFFER_SIZE]; + memcpy(pcm, &data[numberProcessed], MUSIC_BUFFER_SIZE); + alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), context->sampleRate); + alSourceQueueBuffers(context->alSource, 1, &buffer); + numberProcessed+=MUSIC_BUFFER_SIZE; + numberRemaining-=MUSIC_BUFFER_SIZE; + } + else // less than MUSIC_BUFFER_SIZE number of samples left to buffer, the remaining data is padded with zeros + { + float pcm[MUSIC_BUFFER_SIZE] = {0.f}; // pad with zeros + } + + processed--; } - return true; } - return false; + return numberProcessed; } // fill buffer with zeros -static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer) +static void FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer) { float pcm[MUSIC_BUFFER_SIZE] = {0.f}; - alBufferData(buffer, ac->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), ac->sampleRate); + alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), context->sampleRate); } // example usage: @@ -322,7 +345,7 @@ static void ResampleShortToFloat(short *shorts, float *floats, unsigned short le // example usage: // char ch[3] = {1,2,3};float fl[3]; -// ResampleShortToFloat(ch,fl,3); +// ResampleByteToFloat(ch,fl,3); static void ResampleByteToFloat(char *chars, float *floats, unsigned short len) { int x; diff --git a/src/audio.h b/src/audio.h index d723ef1b0..2b36b3f04 100644 --- a/src/audio.h +++ b/src/audio.h @@ -88,7 +88,7 @@ bool IsAudioDeviceReady(void); // True if call // all samples are floating point by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels); void CloseAudioContext(AudioContext ctx); // Frees audio context -bool 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, float *data, unsigned short dataLength); // 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 LoadSoundFromWave(Wave wave); // Load sound to memory from wave data diff --git a/src/raylib.h b/src/raylib.h index 3fa20116b..5c727b615 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -874,7 +874,7 @@ bool IsAudioDeviceReady(void); // True if call // all samples are floating point by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels); void CloseAudioContext(AudioContext ctx); // Frees audio context -bool 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, float *data, unsigned short dataLength); // 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 LoadSoundFromWave(Wave wave); // Load sound to memory from wave data