1 /*
2 	snd_sun.c
3 
4 	(description)
5 
6 	Copyright (C) 1996-1997  Id Software, Inc.
7 	Copyright (C) 1999,2000  contributors of the QuakeForge project
8 	Please see the file "AUTHORS" for a list of contributors
9 
10 	This program is free software; you can redistribute it and/or
11 	modify it under the terms of the GNU General Public License
12 	as published by the Free Software Foundation; either version 2
13 	of the License, or (at your option) any later version.
14 
15 	This program is distributed in the hope that it will be useful,
16 	but WITHOUT ANY WARRANTY; without even the implied warranty of
17 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 	See the GNU General Public License for more details.
20 
21 	You should have received a copy of the GNU General Public License
22 	along with this program; if not, write to:
23 
24 		Free Software Foundation, Inc.
25 		59 Temple Place - Suite 330
26 		Boston, MA  02111-1307, USA
27 
28 */
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 # include <string.h>
38 #endif
39 #ifdef HAVE_STRINGS_H
40 # include <strings.h>
41 #endif
42 
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <sys/audioio.h>
48 #include <sys/ioctl.h>
49 #include <sys/mman.h>
50 #include <sys/shm.h>
51 #include <sys/types.h>
52 #include <sys/wait.h>
53 
54 #include "QF/qargs.h"
55 #include "QF/qtypes.h"
56 #include "QF/sys.h"
57 
58 #include "snd_internal.h"
59 
60 static int		audio_fd;
61 static int		snd_inited;
62 static int		snd_blocked = 0;
63 
64 static unsigned int wbufp;
65 static audio_info_t info;
66 
67 #define BUFFER_SIZE		8192
68 
69 static unsigned char dma_buffer[BUFFER_SIZE];
70 static volatile dma_t sn;
71 
72 static plugin_t           plugin_info;
73 static plugin_data_t      plugin_info_data;
74 static plugin_funcs_t     plugin_info_funcs;
75 static general_data_t     plugin_info_general_data;
76 static general_funcs_t    plugin_info_general_funcs;
77 static snd_output_data_t  plugin_info_sound_data;
78 static snd_output_funcs_t plugin_info_sound_funcs;
79 
80 static void
SNDDMA_Init_Cvars(void)81 SNDDMA_Init_Cvars (void)
82 {
83 }
84 
85 static volatile dma_t *
SNDDMA_Init(void)86 SNDDMA_Init (void)
87 {
88 	if (snd_inited) {
89 		Sys_Printf ("Sound already init'd\n");
90 		return 0;
91 	}
92 
93 	audio_fd = open ("/dev/audio", O_WRONLY | O_NDELAY);
94 
95 	if (audio_fd < 0) {
96 		if (errno == EBUSY) {
97 			Sys_Printf ("Audio device is being used by another process\n");
98 		}
99 		perror ("/dev/audio");
100 		Sys_Printf ("Could not open /dev/audio\n");
101 		return (0);
102 	}
103 
104 	if (ioctl (audio_fd, AUDIO_GETINFO, &info) < 0) {
105 		perror ("/dev/audio");
106 		Sys_Printf ("Could not communicate with audio device.\n");
107 		close (audio_fd);
108 		return 0;
109 	}
110 	// set to nonblock
111 	if (fcntl (audio_fd, F_SETFL, O_NONBLOCK) < 0) {
112 		perror ("/dev/audio");
113 		close (audio_fd);
114 		return 0;
115 	}
116 
117 	AUDIO_INITINFO (&info);
118 
119 	sn.speed = 11025;
120 
121 	// try 16 bit stereo
122 	info.play.encoding = AUDIO_ENCODING_LINEAR;
123 	info.play.sample_rate = 11025;
124 	info.play.channels = 2;
125 	info.play.precision = 16;
126 
127 	if (ioctl (audio_fd, AUDIO_SETINFO, &info) < 0) {
128 		info.play.encoding = AUDIO_ENCODING_LINEAR;
129 		info.play.sample_rate = 11025;
130 		info.play.channels = 1;
131 		info.play.precision = 16;
132 		if (ioctl (audio_fd, AUDIO_SETINFO, &info) < 0) {
133 			Sys_Printf ("Incapable sound hardware.\n");
134 			close (audio_fd);
135 			return 0;
136 		}
137 		Sys_Printf ("16 bit mono sound initialized\n");
138 		sn.samplebits = 16;
139 		sn.channels = 1;
140 	} else {							// 16 bit stereo
141 		Sys_Printf ("16 bit stereo sound initialized\n");
142 		sn.samplebits = 16;
143 		sn.channels = 2;
144 	}
145 
146 	sn.frames = sizeof (dma_buffer) / (sn.samplebits / 8);
147 	sn.framepos = 0;
148 	sn.submission_chunk = 1;
149 	sn.buffer = (unsigned char *) dma_buffer;
150 
151 	snd_inited = 1;
152 
153 	return &sn;
154 }
155 
156 static int
SNDDMA_GetDMAPos(void)157 SNDDMA_GetDMAPos (void)
158 {
159 	if (!snd_inited)
160 		return (0);
161 
162 	if (ioctl (audio_fd, AUDIO_GETINFO, &info) < 0) {
163 		perror ("/dev/audio");
164 		Sys_Printf ("Could not communicate with audio device.\n");
165 		close (audio_fd);
166 		snd_inited = 0;
167 		return (0);
168 	}
169 
170 	sn.framepos = ((info.play.samples * sn.channels) % sn.frames);
171 	return sn.framepos;
172 }
173 #if 0
174 static int
175 SNDDMA_GetSamples (void)
176 {
177 	if (!snd_inited)
178 		return (0);
179 
180 	if (ioctl (audio_fd, AUDIO_GETINFO, &info) < 0) {
181 		perror ("/dev/audio");
182 		Sys_Printf ("Could not communicate with audio device.\n");
183 		close (audio_fd);
184 		snd_inited = 0;
185 		return (0);
186 	}
187 
188 	return info.play.samples;
189 }
190 #endif
191 static void
SNDDMA_Shutdown(void)192 SNDDMA_Shutdown (void)
193 {
194 	if (snd_inited) {
195 		close (audio_fd);
196 		snd_inited = 0;
197 	}
198 }
199 
200 /*
201 	SNDDMA_Submit
202 
203 	Send sound to device if buffer isn't really the dma buffer
204 */
205 static void
SNDDMA_Submit(void)206 SNDDMA_Submit (void)
207 {
208 	unsigned char *p;
209 	static unsigned char writebuf[1024];
210 	int			bsize, bytes, idx, b;
211 	int			stop = *plugin_info_sound_data.paintedtime;
212 
213 	if (snd_blocked)
214 		return;
215 
216 	if (*plugin_info_sound_data.paintedtime < wbufp)
217 		wbufp = 0;						// reset
218 
219 	bsize = sn.channels * (sn.samplebits / 8);
220 	bytes = (*plugin_info_sound_data.paintedtime - wbufp) * bsize;
221 
222 	if (!bytes)
223 		return;
224 
225 	if (bytes > (int) sizeof (writebuf)) {
226 		bytes = sizeof (writebuf);
227 		stop = wbufp + bytes / bsize;
228 	}
229 
230 	p = writebuf;
231 	idx = (wbufp * bsize) & (BUFFER_SIZE - 1);
232 
233 	for (b = bytes; b; b--) {
234 		*p++ = dma_buffer[idx];
235 		idx = (idx + 1) & (BUFFER_SIZE - 1);
236 	}
237 
238 	wbufp = stop;
239 
240 	if (write (audio_fd, writebuf, bytes) < bytes)
241 		Sys_Printf ("audio can't keep up!\n");
242 
243 }
244 
245 static void
SNDDMA_BlockSound(void)246 SNDDMA_BlockSound (void)
247 {
248 	++snd_blocked;
249 }
250 
251 static void
SNDDMA_UnblockSound(void)252 SNDDMA_UnblockSound (void)
253 {
254 	if (!snd_blocked)
255 		return;
256 	--snd_blocked;
257 }
258 
PLUGIN_INFO(snd_output,sun)259 PLUGIN_INFO(snd_output, sun)
260 {
261 	plugin_info.type = qfp_snd_output;
262 	plugin_info.api_version = QFPLUGIN_VERSION;
263 	plugin_info.plugin_version = "0.1";
264 	plugin_info.description = "SUN digital output";
265 	plugin_info.copyright = "Copyright (C) 1996-1997 id Software, Inc.\n"
266 		"Copyright (C) 1999,2000,2001  contributors of the QuakeForge "
267 		"project\n"
268 		"Please see the file \"AUTHORS\" for a list of contributors";
269 	plugin_info.functions = &plugin_info_funcs;
270 	plugin_info.data = &plugin_info_data;
271 
272 	plugin_info_data.general = &plugin_info_general_data;
273 	plugin_info_data.input = NULL;
274 	plugin_info_data.snd_output = &plugin_info_sound_data;
275 
276 	plugin_info_funcs.general = &plugin_info_general_funcs;
277 	plugin_info_funcs.input = NULL;
278 	plugin_info_funcs.snd_output = &plugin_info_sound_funcs;
279 
280 	plugin_info_general_funcs.p_Init = SNDDMA_Init_Cvars;
281 	plugin_info_general_funcs.p_Shutdown = NULL;
282 
283 	plugin_info_sound_funcs.pS_O_Init = SNDDMA_Init;
284 	plugin_info_sound_funcs.pS_O_Shutdown = SNDDMA_Shutdown;
285 	plugin_info_sound_funcs.pS_O_GetDMAPos = SNDDMA_GetDMAPos;
286 	plugin_info_sound_funcs.pS_O_Submit = SNDDMA_Submit;
287 	plugin_info_sound_funcs.pS_O_BlockSound = SNDDMA_BlockSound;
288 	plugin_info_sound_funcs.pS_O_UnblockSound = SNDDMA_UnblockSound;
289 
290 	return &plugin_info;
291 }
292