support multiple sound drivers
[picodrive.git] / platform / common / emu.c
index 76765a4..e0c50af 100644 (file)
@@ -16,6 +16,7 @@
 #include "../libpicofe/posix.h"\r
 #include "../libpicofe/input.h"\r
 #include "../libpicofe/fonts.h"\r
+#include "../libpicofe/sndout.h"\r
 #include "../libpicofe/lprintf.h"\r
 #include "../libpicofe/plat.h"\r
 #include "emu.h"\r
@@ -45,6 +46,8 @@ int pico_pen_x = 320/2, pico_pen_y = 240/2;
 int pico_inp_mode = 0;\r
 int engineState = PGS_Menu;\r
 \r
+static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
+\r
 /* tmp buff to reduce stack usage for plats with small stack */\r
 static char static_buff[512];\r
 const char *rom_fname_reload;\r
@@ -1418,6 +1421,8 @@ void emu_init(void)
        PicoMessage = plat_status_msg_busy_next;\r
        PicoMCDopenTray = emu_tray_open;\r
        PicoMCDcloseTray = emu_tray_close;\r
+\r
+       sndout_init();\r
 }\r
 \r
 void emu_finish(void)\r
@@ -1440,6 +1445,42 @@ void emu_finish(void)
        pprof_finish();\r
 \r
        PicoExit();\r
+       sndout_exit();\r
+}\r
+\r
+static void snd_write_nonblocking(int len)\r
+{\r
+       sndout_write_nb(PsndOut, len);\r
+}\r
+\r
+void emu_sound_start(void)\r
+{\r
+       PsndOut = NULL;\r
+\r
+       if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
+       {\r
+               int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
+\r
+               PsndRerate(Pico.m.frame_count ? 1 : 0);\r
+\r
+               printf("starting audio: %i len: %i stereo: %i, pal: %i\n",\r
+                       PsndRate, PsndLen, is_stereo, Pico.m.pal);\r
+               sndout_start(PsndRate, is_stereo);\r
+               PicoWriteSound = snd_write_nonblocking;\r
+               plat_update_volume(0, 0);\r
+               memset(sndBuffer, 0, sizeof(sndBuffer));\r
+               PsndOut = sndBuffer;\r
+       }\r
+}\r
+\r
+void emu_sound_stop(void)\r
+{\r
+       sndout_stop();\r
+}\r
+\r
+void emu_sound_wait(void)\r
+{\r
+       sndout_wait();\r
 }\r
 \r
 static void skip_frame(int do_audio)\r
@@ -1477,6 +1518,7 @@ void emu_loop(void)
 \r
        plat_video_loop_prepare();\r
        pemu_loop_prep();\r
+       pemu_sound_start();\r
 \r
        /* number of ticks per frame */\r
        if (Pico.m.pal) {\r
@@ -1641,6 +1683,7 @@ void emu_loop(void)
        }\r
 \r
        pemu_loop_end();\r
+       emu_sound_stop();\r
 \r
        // pemu_loop_end() might want to do 1 frame for bg image,\r
        // so free CD buffer here\r