Răsfoiți Sursa

pause on no data

Joshua Reisenauer 9 ani în urmă
părinte
comite
d6feeb14ff
2 a modificat fișierele cu 32 adăugiri și 16 ștergeri
  1. 15 16
      src/audio.c
  2. 17 0
      src/easings.h

+ 15 - 16
src/audio.c

@@ -114,11 +114,10 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType;
 //----------------------------------------------------------------------------------
 // Global Variables Definition
 //----------------------------------------------------------------------------------
-static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active
+static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS];      // What mix channels are currently active
 static bool musicEnabled = false;
 static Music currentMusic;          // Current music loaded
                                     // NOTE: Only one music file playing at a time
-
 //----------------------------------------------------------------------------------
 // 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.
-// 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.
 // 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 numberRemaining = numberElements;
     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)
     {
         ALint processed = 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 (!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
         {
             while (processed > 0)

+ 17 - 0
src/easings.h

@@ -7,6 +7,23 @@
 *   This header uses:
 *       #define EASINGS_STATIC_INLINE       // Inlines all functions code, so it runs faster.
 *                                           // 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/)
 *