support multiple sound drivers, add sdl
[libpicofe.git] / linux / sndout_oss.c
index f9cc6c2..c0b775a 100644 (file)
@@ -1,3 +1,14 @@
+/*
+ * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
+ *
+ * This work is licensed under the terms of any of these licenses
+ * (at your option):
+ *  - GNU GPL, version 2 or later.
+ *  - GNU LGPL, version 2.1 or later.
+ *  - MAME license.
+ * See the COPYING file in the top-level directory.
+ */
+
 /* sound output via OSS */
 #include <stdio.h>
 #include <sys/types.h>
@@ -9,6 +20,8 @@
 
 #include "sndout_oss.h"
 
+int sndout_oss_frag_frames = 1;
+
 static int sounddev = -1, mixerdev = -1;
 static int can_write_safe;
 
@@ -36,7 +49,7 @@ void sndout_oss_stop(void)
        sounddev = -1;
 }
 
-int sndout_oss_start(int rate, int stereo, int frames_in_frag)
+int sndout_oss_start(int rate, int stereo)
 {
        static int s_oldrate = 0, s_oldstereo = 0;
        int frag, bsize, bits, ret;
@@ -58,10 +71,11 @@ int sndout_oss_start(int rate, int stereo, int frames_in_frag)
                }
        }
 
-       // try to fit frames_in_frag frames worth of data in fragment
+       // try to fit sndout_oss_frag_frames (video) frames
+       // worth of sound data in OSS fragment
        // ignore mono because it's unlikely to be used and
        // both GP2X and Wiz mixes mono to stereo anyway.
-       bsize = (frames_in_frag * rate / 50) * 4;
+       bsize = (sndout_oss_frag_frames * rate / 50) * 4;
 
        for (frag = 0; bsize; bsize >>= 1, frag++)
                ;
@@ -98,6 +112,42 @@ int sndout_oss_write(const void *buff, int len)
        return write(sounddev, buff, len);
 }
 
+#include "../plat.h"
+
+/* not really non-blocking, just detects if blocking occurs
+ * and starts skipping writes in case it does. */
+int sndout_oss_write_nb(const void *buff, int len)
+{
+       static int lag_counter, skip_counter;
+       unsigned int t;
+       int ret;
+
+       if (lag_counter > 2) {
+               // skip writes if audio starts blocking
+               lag_counter = 0;
+               skip_counter = FRAG_COUNT;
+       }
+
+       if (skip_counter > 0) {
+               skip_counter--;
+               return len;
+       }
+
+       t = plat_get_ticks_ms();
+       ret = sndout_oss_write(buff, len);
+       t = plat_get_ticks_ms() - t;
+       if (t > 1) {
+               // this shouldn't really happen, most likely audio is out of sync
+               lag_counter++;
+               if (lag_counter > 2)
+                       printf("audio lag %u\n", t);
+       }
+       else
+               lag_counter = 0;
+
+       return ret;
+}
+
 int sndout_oss_can_write(int bytes)
 {
        audio_buf_info bi;
@@ -117,8 +167,9 @@ int sndout_oss_can_write(int bytes)
        return bi.bytes - bi.fragsize >= bytes ? 1 : 0;
 }
 
-void sndout_oss_sync(void)
+void sndout_oss_wait(void)
 {
+       // FIXME?
        ioctl(sounddev, SOUND_PCM_SYNC, 0);
 }