summaryrefslogtreecommitdiff
path: root/apps/plugins/sdl/progs/quake/snd_sdl.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/sdl/progs/quake/snd_sdl.c')
-rw-r--r--apps/plugins/sdl/progs/quake/snd_sdl.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/apps/plugins/sdl/progs/quake/snd_sdl.c b/apps/plugins/sdl/progs/quake/snd_sdl.c
new file mode 100644
index 0000000000..644d4e1acf
--- /dev/null
+++ b/apps/plugins/sdl/progs/quake/snd_sdl.c
@@ -0,0 +1,113 @@
1
2#include <stdio.h>
3#include "SDL_audio.h"
4#include "SDL_byteorder.h"
5#include "quakedef.h"
6
7static dma_t the_shm;
8static int snd_inited;
9
10extern int desired_speed;
11extern int desired_bits;
12
13// SDL hereby demands `len' samples in stream, *NOW*!
14static void paint_audio(void *unused, Uint8 *stream, int len)
15{
16 if ( shm ) {
17 shm->buffer = stream;
18 shm->samplepos += len/(shm->samplebits/8)/2;
19 // Check for samplepos overflow?
20 S_PaintChannels (shm->samplepos);
21 }
22}
23
24qboolean SNDDMA_Init(void)
25{
26 SDL_AudioSpec desired, obtained;
27
28 snd_inited = 0;
29
30 /* Set up the desired format */
31 desired.freq = desired_speed;
32 switch (desired_bits) {
33 case 8:
34 desired.format = AUDIO_U8;
35 break;
36 case 16:
37 if ( SDL_BYTEORDER == SDL_BIG_ENDIAN )
38 desired.format = AUDIO_S16MSB;
39 else
40 desired.format = AUDIO_S16LSB;
41 break;
42 default:
43 Con_Printf("Unknown number of audio bits: %d\n",
44 desired_bits);
45 return 0;
46 }
47 desired.channels = 2;
48 desired.samples = 1024;
49 desired.callback = paint_audio;
50
51 /* Open the audio device */
52 if ( SDL_OpenAudio(&desired, &obtained) < 0 ) {
53 Con_Printf("Couldn't open SDL audio: %s\n", SDL_GetError());
54 return 0;
55 }
56
57 /* Make sure we can support the audio format */
58 switch (obtained.format) {
59 case AUDIO_U8:
60 /* Supported */
61 break;
62 case AUDIO_S16LSB:
63 case AUDIO_S16MSB:
64 if ( ((obtained.format == AUDIO_S16LSB) &&
65 (SDL_BYTEORDER == SDL_LIL_ENDIAN)) ||
66 ((obtained.format == AUDIO_S16MSB) &&
67 (SDL_BYTEORDER == SDL_BIG_ENDIAN)) ) {
68 /* Supported */
69 break;
70 }
71 /* Unsupported, fall through */;
72 default:
73 /* Not supported -- force SDL to do our bidding */
74 SDL_CloseAudio();
75 if ( SDL_OpenAudio(&desired, NULL) < 0 ) {
76 Con_Printf("Couldn't open SDL audio: %s\n",
77 SDL_GetError());
78 return 0;
79 }
80 memcpy(&obtained, &desired, sizeof(desired));
81 break;
82 }
83 SDL_PauseAudio(0);
84
85 /* Fill the audio DMA information block */
86 shm = &the_shm;
87 shm->splitbuffer = 0;
88 shm->samplebits = (obtained.format & 0xFF);
89 shm->speed = obtained.freq;
90 shm->channels = obtained.channels;
91 shm->samples = obtained.samples*shm->channels;
92 shm->samplepos = 0;
93 shm->submission_chunk = 1;
94 shm->buffer = NULL;
95
96 snd_inited = 1;
97 return 1;
98}
99
100int SNDDMA_GetDMAPos(void)
101{
102 return shm->samplepos;
103}
104
105void SNDDMA_Shutdown(void)
106{
107 if (snd_inited)
108 {
109 SDL_CloseAudio();
110 snd_inited = 0;
111 }
112}
113