49b8a8deab18a3e90dceb66138fa952432a4acee
[dos_sbtest] / src / audio.c
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY, without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include "audio.h"
20 #include "au_sb.h"
21
22 struct audrv {
23         void *(*get_buffer)(int *size);
24         void (*start)(int rate, int nchan);
25         void (*pause)(void);
26         void (*cont)(void);
27         void (*stop)(void);
28         void (*volume)(int vol);
29 };
30
31 static struct audrv drv;
32
33 static audio_callback_func cbfunc;
34 static void *cbcls;
35
36 void audio_init(void)
37 {
38         if(sb_detect()) {
39                 drv.get_buffer = sb_buffer;
40                 drv.start = sb_start;
41                 drv.pause = sb_pause;
42                 drv.cont = sb_continue;
43                 drv.stop = sb_stop;
44                 drv.volume = sb_volume;
45                 return;
46         }
47
48         printf("No supported audio device detected\n");
49 }
50
51 void audio_set_callback(audio_callback_func func, void *cls)
52 {
53         cbfunc = func;
54         cbcls = cls;
55 }
56
57 int audio_callback(void *buf, int sz)
58 {
59         if(!cbfunc) {
60                 return 0;
61         }
62         return cbfunc(buf, sz, cbcls);
63 }
64
65 void audio_play(int rate, int nchan)
66 {
67         drv.start(rate, nchan);
68 }
69
70 void audio_pause(void)
71 {
72         drv.pause();
73 }
74
75 void audio_resume(void)
76 {
77         drv.cont();
78 }
79
80 void audio_stop(void)
81 {
82         drv.stop();
83 }
84
85 void audio_volume(int vol)
86 {
87         drv.volume(vol);
88 }