pause on no data

This commit is contained in:
Joshua Reisenauer 2016-05-03 02:52:45 -07:00
parent 9d09ada33b
commit d6feeb14ff
2 changed files with 32 additions and 16 deletions

View File

@ -118,7 +118,6 @@ static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix chan
static bool musicEnabled = false; static bool musicEnabled = false;
static Music currentMusic; // Current music loaded static Music currentMusic; // Current music loaded
// NOTE: Only one music file playing at a time // NOTE: Only one music file playing at a time
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module specific Functions Declaration // Module specific Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -286,34 +285,34 @@ void CloseAudioContext(AudioContext ctx)
} }
// 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)" if you want to pause the audio.
// @Returns number of samples that where processed. // @Returns number of samples that where processed.
// All data streams should be of a length that is evenly divisible by MUSIC_BUFFER_SIZE, // All data streams should be of a length that is evenly divisible by MUSIC_BUFFER_SIZE,
// otherwise the remaining data will not be pushed. // otherwise the remaining data will not be pushed.
unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements) unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements)
{ {
unsigned short numberProcessed = 0;
unsigned short numberRemaining = numberElements;
AudioContext_t *context = (AudioContext_t*)ctx; AudioContext_t *context = (AudioContext_t*)ctx;
if(context && context->channels == 2 && numberElements % 2 != 0) return 0; // when there is two channels there must be an even number of samples
if (!data || !numberElements) alSourcePause(context->alSource); // pauses audio until data is given
else{ // restart audio otherwise
ALint state;
alGetSourcei(context->alSource, AL_SOURCE_STATE, &state);
if (state != AL_PLAYING) alSourcePlay(context->alSource);
}
if (context && mixChannelsActive_g[context->mixChannel] == context) if (context && mixChannelsActive_g[context->mixChannel] == context)
{ {
ALint processed = 0; ALint processed = 0;
ALuint buffer = 0; ALuint buffer = 0;
alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) unsigned short numberProcessed = 0;
unsigned short numberRemaining = numberElements;
alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any)
if(!processed) return 0;//nothing to process, queue is still full if(!processed) return 0;//nothing to process, queue is still full
if (!data || !numberElements)// play silence
{
while (processed > 0)
{
alSourceUnqueueBuffers(context->alSource, 1, &buffer);
numberProcessed += FillAlBufferWithSilence(context, buffer);
alSourceQueueBuffers(context->alSource, 1, &buffer);
processed--;
}
}
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)

View File

@ -7,6 +7,23 @@
* This header uses: * This header uses:
* #define EASINGS_STATIC_INLINE // Inlines all functions code, so it runs faster. * #define EASINGS_STATIC_INLINE // Inlines all functions code, so it runs faster.
* // This requires lots of memory on system. * // This requires lots of memory on system.
* How to use:
* The four inputs t,b,c,d are defined as follows:
* t = current time in milliseconds
* b = starting position in only one dimension [X || Y || Z] your choice
* c = the total change in value of b that needs to occur
* d = total time it should take to complete
*
* Example:
* float speed = 1.f;
* float currentTime = 0.f;
* float currentPos[2] = {0,0};
* float newPos[2] = {1,1};
* float tempPosition[2] = currentPos;//x,y positions
* while(currentPos[0] < newPos[0])
* currentPos[0] = EaseSineIn(currentTime, tempPosition[0], tempPosition[0]-newPos[0], speed);
* currentPos[1] = EaseSineIn(currentTime, tempPosition[1], tempPosition[1]-newPos[0], speed);
* currentTime += diffTime();
* *
* A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/) * A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/)
* *