share/gui: don't use less of widget width for truncation than available
[neverball] / share / audio.c
index 47c668b..2eeebe7 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
-#include "text.h"
 #include "config.h"
 #include "audio.h"
 #include "common.h"
+#include "fs.h"
+#include "fs_ov.h"
 
 /*---------------------------------------------------------------------------*/
 
@@ -53,6 +54,10 @@ static struct voice *queue  = NULL;
 static struct voice *voices = NULL;
 static short        *buffer = NULL;
 
+static ov_callbacks callbacks = {
+    fs_ov_read, fs_ov_seek, fs_ov_close, fs_ov_tell
+};
+
 /*---------------------------------------------------------------------------*/
 
 #define MIX(d, s) {                           \
@@ -141,7 +146,7 @@ static int voice_step(struct voice *V, float volume, Uint8 *stream, int length)
 static struct voice *voice_init(const char *filename, float a)
 {
     struct voice *V;
-    FILE        *fp;
+    fs_file      fp;
 
     /* Allocate and initialize a new voice structure. */
 
@@ -153,9 +158,9 @@ static struct voice *voice_init(const char *filename, float a)
 
         /* Attempt to open the named Ogg stream. */
 
-        if ((fp = fopen(config_data(filename), FMODE_RB)))
+        if ((fp = fs_open(filename, "r")))
         {
-            if (ov_open(fp, &V->vf, NULL, 0) == 0)
+            if (ov_open_callbacks(fp, &V->vf, NULL, 0, callbacks) == 0)
             {
                 vorbis_info *info = ov_info(&V->vf, -1);
 
@@ -172,7 +177,7 @@ static struct voice *voice_init(const char *filename, float a)
 
                 /* The file will be closed when the Ogg is cleared. */
             }
-            else fclose(fp);
+            else fs_close(fp);
         }
     }
     return V;
@@ -297,18 +302,23 @@ void audio_play(const char *filename, float a)
 
         /* If we're already playing this sound, preempt the running copy. */
 
-        for (V = voices; V; V = V->next)
-            if (strcmp(V->name, filename) == 0)
-            {
-                ov_raw_seek(&V->vf, 0);
+        SDL_LockAudio();
+        {
+            for (V = voices; V; V = V->next)
+                if (strcmp(V->name, filename) == 0)
+                {
+                    ov_raw_seek(&V->vf, 0);
 
-                V->amp = a;
+                    V->amp = a;
 
-                if (V->amp > 1.0) V->amp = 1.0;
-                if (V->amp < 0.0) V->amp = 0.0;
+                    if (V->amp > 1.0) V->amp = 1.0;
+                    if (V->amp < 0.0) V->amp = 0.0;
 
-                return;
-            }
+                    SDL_UnlockAudio();
+                    return;
+                }
+        }
+        SDL_UnlockAudio();
 
         /* Create a new voice structure. */
 
@@ -407,7 +417,22 @@ void audio_music_fade_to(float t, const char *filename)
             audio_music_fade_out(t);
             audio_music_queue(filename, t);
         }
-        else audio_music_fade_in(t);
+        else
+        {
+            /*
+             * We're fading to the current track.  Chances are,
+             * whatever track is still in the queue, we don't want to
+             * hear it anymore.
+             */
+
+            if (queue)
+            {
+                voice_free(queue);
+                queue = NULL;
+            }
+
+            audio_music_fade_in(t);
+        }
     }
     else
     {