]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_bsd.c
centerprint no longer counts color codes when centering lines
[divverent/darkplaces.git] / snd_bsd.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include <sys/param.h>
22 #include <sys/audioio.h>
23 #ifndef SUNOS
24 #include <sys/endian.h>
25 #endif
26 #include <sys/ioctl.h>
27
28 #include <fcntl.h>
29 #ifndef SUNOS
30 #include <paths.h>
31 #endif
32 #include <unistd.h>
33
34 #include "quakedef.h"
35 #include "snd_main.h"
36
37
38 static const int tryrates[] = {44100, 22050, 11025, 8000};
39
40 static int audio_fd = -1;
41
42 // TODO: allocate them in SNDDMA_Init, with a size depending on
43 // the sound format (enough for 0.5 sec of sound for instance)
44 #define SND_BUFF_SIZE 65536
45 static unsigned char dma_buffer [SND_BUFF_SIZE];
46 static unsigned char writebuf [SND_BUFF_SIZE];
47
48
49 qboolean SNDDMA_Init (void)
50 {
51         unsigned int i;
52         const char *snddev;
53         audio_info_t info;
54
55         memset ((void*)shm, 0, sizeof (*shm));
56
57         // Open the audio device
58 #ifdef _PATH_SOUND
59         snddev = _PATH_SOUND;
60 #else
61 #ifndef SUNOS
62         snddev = "/dev/sound";
63 #else
64         snddev = "/dev/audio";
65 #endif
66 #endif
67         audio_fd = open (snddev, O_WRONLY | O_NDELAY | O_NONBLOCK);
68         if (audio_fd < 0)
69         {
70                 Con_Printf("Can't open the sound device (%s)\n", snddev);
71                 return false;
72         }
73
74         // Look for an appropriate sound format
75         // TODO: we should also test mono/stereo and bits
76         // TODO: support "-sndspeed", "-sndbits", "-sndmono" and "-sndstereo"
77         shm->format.channels = 2;
78         shm->format.width = 2;
79         for (i = 0; i < sizeof (tryrates) / sizeof (tryrates[0]); i++)
80         {
81                 shm->format.speed = tryrates[i];
82
83                 AUDIO_INITINFO (&info);
84                 info.play.sample_rate = shm->format.speed;
85                 info.play.channels = shm->format.channels;
86                 info.play.precision = shm->format.width * 8;
87 // We only handle sound cards of the same endianess than the CPU
88 #if BYTE_ORDER == BIG_ENDIAN
89                 info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
90 #else
91 #ifndef SUNOS
92                 info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
93 #else
94                 info.play.encoding = AUDIO_ENCODING_LINEAR;
95 #endif
96 #endif
97                 if (ioctl (audio_fd, AUDIO_SETINFO, &info) == 0)
98                         break;
99         }
100         if (i == sizeof (tryrates) / sizeof (tryrates[0]))
101         {
102                 Con_Print("Can't select an appropriate sound output format\n");
103                 close (audio_fd);
104                 return false;
105         }
106
107         // Print some information
108         Con_Printf("%d bit %s sound initialized (rate: %dHz)\n",
109                                 info.play.precision,
110                                 (info.play.channels == 2) ? "stereo" : "mono",
111                                 info.play.sample_rate);
112
113         shm->sampleframes = sizeof (dma_buffer) / shm->format.width / shm->format.channels;
114         shm->samples = shm->sampleframes * shm->format.channels;
115         shm->samplepos = 0;
116         shm->buffer = dma_buffer;
117
118         return true;
119 }
120
121 int SNDDMA_GetDMAPos (void)
122 {
123         audio_info_t info;
124
125         if (!shm)
126                 return 0;
127
128         if (ioctl (audio_fd, AUDIO_GETINFO, &info) < 0)
129         {
130                 Con_Print("Error: can't get audio info\n");
131                 SNDDMA_Shutdown ();
132                 return 0;
133         }
134
135         return ((info.play.samples * shm->format.channels) % shm->samples);
136 }
137
138 void SNDDMA_Shutdown (void)
139 {
140         close (audio_fd);
141         audio_fd = -1;
142 }
143
144 /*
145 ==============
146 SNDDMA_Submit
147
148 Send sound to device if buffer isn't really the dma buffer
149 ===============
150 */
151 void SNDDMA_Submit (void)
152 {
153         int bsize;
154         int bytes, b;
155         static int wbufp = 0;
156         unsigned char *p;
157         int idx;
158         int stop = paintedtime;
159
160         if (!shm)
161                 return;
162
163         if (paintedtime < wbufp)
164                 wbufp = 0; // reset
165
166         bsize = shm->format.channels * shm->format.width;
167         bytes = (paintedtime - wbufp) * bsize;
168
169         if (!bytes)
170                 return;
171
172         if (bytes > sizeof (writebuf))
173         {
174                 bytes = sizeof (writebuf);
175                 stop = wbufp + bytes / bsize;
176         }
177
178         // Transfert the sound data from the circular dma_buffer to writebuf
179         // TODO: using 2 memcpys instead of this loop should be faster
180         p = writebuf;
181         idx = (wbufp*bsize) & (sizeof (dma_buffer) - 1);
182         for (b = bytes; b; b--)
183         {
184                 *p++ = dma_buffer[idx];
185                 idx = (idx + 1) & (sizeof (dma_buffer) - 1);
186         }
187
188         if (write (audio_fd, writebuf, bytes) < bytes)
189                 Con_Print("audio can't keep up!\n");
190
191         wbufp = stop;
192 }
193
194 void *S_LockBuffer (void)
195 {
196         return shm->buffer;
197 }
198
199 void S_UnlockBuffer (void)
200 {
201 }