1 /*
2 	snd_linux_sdl.c
3 
4 	Sound code taken from Tenebrae to work with NehQuake
5 
6 	This program is free software; you can redistribute it and/or
7 	modify it under the terms of the GNU General Public License
8 	as published by the Free Software Foundation; either version 2
9 	of the License, or (at your option) any later version.
10 
11 	This program is distributed in the hope that it will be useful,
12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15 	See the GNU General Public License for more details.
16 
17 	You should have received a copy of the GNU General Public License
18 	along with this program; if not, write to:
19 
20 		Free Software Foundation, Inc.
21 		59 Temple Place - Suite 330
22 		Boston, MA  02111-1307, USA
23 
24 	$Id: snd_sdl.c,v 1.2 2002/02/09 20:29:38 relnev Exp $
25 */
26 
27 #include <stdio.h>
28 #include "SDL.h"
29 #include "quakedef.h"
30 
31 static dma_t the_shm;
32 static int snd_inited;
33 
34 extern int desired_speed;
35 extern int desired_bits;
36 
paint_audio(void * unused,Uint8 * stream,int len)37 static void paint_audio(void *unused, Uint8 *stream, int len)
38 {
39 	if ( shm ) {
40 		shm->buffer = stream;
41 		shm->samplepos += len/(shm->samplebits/8)/2;
42 		// Check for samplepos overflow?
43 		S_PaintChannels (shm->samplepos);
44 	}
45 }
46 
SNDDMA_Init(void)47 qboolean SNDDMA_Init(void)
48 {
49 	SDL_AudioSpec desired, obtained;
50 
51 	snd_inited = 0;
52 
53 	/* Set up the desired format */
54 	if (COM_CheckParm("-sndspeed")) //Better sample rate - Eradicator
55 		desired.freq = Q_atoi(com_argv[COM_CheckParm("-sndspeed")+1]);
56         else
57              desired.freq = desired_speed;
58 
59 	switch (desired_bits) {
60 		case 8:
61 			desired.format = AUDIO_U8;
62 			break;
63 		case 16:
64 			if ( SDL_BYTEORDER == SDL_BIG_ENDIAN )
65 				desired.format = AUDIO_S16MSB;
66 			else
67 				desired.format = AUDIO_S16LSB;
68 			break;
69 		default:
70         		Con_Printf("Unknown number of audio bits: %d\n",
71 								desired_bits);
72 			return 0;
73 	}
74 
75 	if (COM_CheckParm("-sndchannels")) //Better sound channels - Eradicator
76              shm->channels = Q_atoi(com_argv[COM_CheckParm("-sndchannels")+1]);
77         else
78              desired.channels = 2;
79 
80 	desired.samples = 512;
81 	desired.callback = paint_audio;
82 
83 	/* Open the audio device */
84 	if ( SDL_OpenAudio(&desired, &obtained) < 0 ) {
85         	Con_Printf("Couldn't open SDL audio: %s\n", SDL_GetError());
86 		return 0;
87 	}
88 
89 	/* Make sure we can support the audio format */
90 	switch (obtained.format) {
91 		case AUDIO_U8:
92 			/* Supported */
93 			break;
94 		case AUDIO_S16LSB:
95 		case AUDIO_S16MSB:
96 			if ( ((obtained.format == AUDIO_S16LSB) &&
97 			     (SDL_BYTEORDER == SDL_LIL_ENDIAN)) ||
98 			     ((obtained.format == AUDIO_S16MSB) &&
99 			     (SDL_BYTEORDER == SDL_BIG_ENDIAN)) ) {
100 				/* Supported */
101 				break;
102 			}
103 			/* Unsupported, fall through */;
104 		default:
105 			/* Not supported -- force SDL to do our bidding */
106 			SDL_CloseAudio();
107 			if ( SDL_OpenAudio(&desired, NULL) < 0 ) {
108         			Con_Printf("Couldn't open SDL audio: %s\n",
109 							SDL_GetError());
110 				return 0;
111 			}
112 			memcpy(&obtained, &desired, sizeof(desired));
113 			break;
114 	}
115 	SDL_PauseAudio(0);
116 
117 	/* Fill the audio DMA information block */
118 	shm = &the_shm;
119 	shm->splitbuffer = 0;
120 	shm->samplebits = (obtained.format & 0xFF);
121         shm->speed = obtained.freq;
122         shm->channels = obtained.channels;
123 	shm->samples = obtained.samples*shm->channels;
124 	shm->samplepos = 0;
125 	shm->submission_chunk = 1;
126 	shm->buffer = NULL;
127 
128 	snd_inited = 1;
129 	return 1;
130 }
131 
SNDDMA_GetDMAPos(void)132 int SNDDMA_GetDMAPos(void)
133 {
134 	return shm->samplepos;
135 }
136 
SNDDMA_Shutdown(void)137 void SNDDMA_Shutdown(void)
138 {
139 	if (snd_inited)
140 	{
141 		SDL_CloseAudio();
142 		snd_inited = 0;
143 	}
144 }
145 
SNDDMA_Submit(void)146 void SNDDMA_Submit (void)
147 {
148 //
149 }
150 
151 
152