aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoshua Reisenauer <kd7tck@msn.com>2016-05-02 14:37:00 -0700
committerJoshua Reisenauer <kd7tck@msn.com>2016-05-02 14:37:00 -0700
commit4636e3367cdf3d2e1adab3a08667c34631f94837 (patch)
treeadd80db5bf4c84b61316916c62eed0185b48f4d0 /src
parent9ef0240e99e7e6a626fdb8fee4f8a81eea21f3e2 (diff)
downloadraylib-4636e3367cdf3d2e1adab3a08667c34631f94837.tar.gz
raylib-4636e3367cdf3d2e1adab3a08667c34631f94837.zip
number remaining buffer transfer for updateAudioContext
updateAudioContext is almost done
Diffstat (limited to 'src')
-rw-r--r--src/audio.c39
-rw-r--r--src/audio.h2
-rw-r--r--src/raylib.h2
3 files changed, 33 insertions, 10 deletions
diff --git a/src/audio.c b/src/audio.c
index d935b6c7..ff4f2858 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 d723ef1b..2b36b3f0 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 3fa20116..5c727b61 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