fixed by preloading samples. the audio callback is being called from the
[dos_auplay] / src / main.c
index 9a1b262..4395af6 100644 (file)
@@ -1,16 +1,22 @@
 #include <stdio.h>\r
+#include <stdlib.h>\r
 #include <string.h>\r
 #include "aufile.h"\r
 #include "audio.h"\r
 \r
+#define DBG_PRELOAD\r
+\r
 static int play_file(const char *fname);\r
+static int cbfunc(void *buf, int size, void *cls);\r
 static void print_usage(const char *argv0);\r
 \r
 int main(int argc, char **argv)\r
 {\r
        int i;\r
 \r
-       play_file("namarie.wav");       /* TODO remove */\r
+       if(audio_init() == -1) {\r
+               return 1;\r
+       }\r
 \r
        for(i=1; i<argc; i++) {\r
                if(argv[i][0] == '-') {\r
@@ -28,6 +34,9 @@ int main(int argc, char **argv)
        return 0;\r
 }\r
 \r
+static unsigned char *dbg_samples;\r
+static int dbg_cur_offs;\r
+\r
 static int play_file(const char *fname)\r
 {\r
        struct au_file *au;\r
@@ -36,13 +45,56 @@ static int play_file(const char *fname)
                return -1;\r
        }\r
 \r
+       audio_set_callback(cbfunc, au);\r
+\r
+#ifdef DBG_PRELOAD\r
+       if(!(dbg_samples = malloc(au->size))) {\r
+               perror("failed to preload samples into memory");\r
+               return -1;\r
+       }\r
+       dbg_cur_offs = 0;\r
+       if(au_read(au, dbg_samples, au->size) < au->size) {\r
+               perror("failed to preload samples into memory");\r
+               return -1;\r
+       }\r
+#endif\r
+\r
        audio_play(au->rate, au->bits, au->chan);\r
        while(audio_isplaying());\r
 \r
        au_close(au);\r
+#ifdef DBG_PRELOAD\r
+       free(dbg_samples);\r
+#endif\r
        return 0;\r
 }\r
 \r
+#ifdef DBG_PRELOAD\r
+static int cbfunc(void *buf, int size, void *cls)\r
+{\r
+       struct au_file *au = cls;\r
+\r
+       if(dbg_cur_offs + size > au->size) {\r
+               size = au->size - dbg_cur_offs;\r
+       }\r
+       if(size <= 0) return 0;\r
+\r
+       memcpy(buf, dbg_samples + dbg_cur_offs, size);\r
+       dbg_cur_offs += size;\r
+       return size;\r
+}\r
+#else\r
+static int cbfunc(void *buf, int size, void *cls)\r
+{\r
+       int rd;\r
+\r
+       if((rd = au_read(cls, buf, size)) <= 0) {\r
+               return 0;\r
+       }\r
+       return rd;\r
+}\r
+#endif\r
+\r
 static void print_usage(const char *argv0)\r
 {\r
        printf("Usage: %s [options] <file1> <file2> ... <filen>\n", argv0);\r