]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_bsd.c
went through most of todo and cleaned things up, not finished labeling them by catego...
[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 #include <sys/endian.h>
24 #include <sys/ioctl.h>
25
26 #include <fcntl.h>
27 #include <paths.h>
28 #include <unistd.h>
29
30 #include "quakedef.h"
31
32
33 static const int tryrates[] = {44100, 22050, 11025, 8000};
34
35 static int audio_fd = -1;
36 static qboolean snd_inited = false;
37
38 // TODO: allocate them in SNDDMA_Init, with a size depending on
39 // the sound format (enough for 0.5 sec of sound for instance)
40 #define SND_BUFF_SIZE 65536
41 static qbyte dma_buffer [SND_BUFF_SIZE];
42 static qbyte writebuf [SND_BUFF_SIZE];
43
44
45 qboolean SNDDMA_Init (void)
46 {
47         unsigned int i;
48         const char *snddev;
49         audio_info_t info;
50
51         memset ((void*)shm, 0, sizeof (*shm));
52
53         // Open the audio device
54 #ifdef _PATH_SOUND
55         snddev = _PATH_SOUND;
56 #else
57         snddev = "/dev/sound";
58 #endif
59         audio_fd = open (snddev, O_WRONLY | O_NDELAY | O_NONBLOCK);
60         if (audio_fd < 0)
61         {
62                 Con_Printf("Can't open the sound device (%s)\n", snddev);
63                 return false;
64         }
65
66         // Look for an appropriate sound format
67         // TODO: we should also test mono/stereo and bits
68         // TODO: support "-sndspeed", "-sndbits", "-sndmono" and "-sndstereo"
69         shm->format.channels = 2;
70         shm->format.width = 2;
71         for (i = 0; i < sizeof (tryrates) / sizeof (tryrates[0]); i++)
72         {
73                 shm->format.speed = tryrates[i];
74
75                 AUDIO_INITINFO (&info);
76                 info.play.sample_rate = shm->format.speed;
77                 info.play.channels = shm->format.channels;
78                 info.play.precision = shm->format.width * 8;
79 // We only handle sound cards of the same endianess than the CPU
80 #if BYTE_ORDER == BIG_ENDIAN
81                 info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
82 #else
83                 info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
84 #endif
85                 if (ioctl (audio_fd, AUDIO_SETINFO, &info) == 0)
86                         break;
87         }
88         if (i == sizeof (tryrates) / sizeof (tryrates[0]))
89         {
90                 Con_Print("Can't select an appropriate sound output format\n");
91                 close (audio_fd);
92                 return false;
93         }
94
95         // Print some information
96         Con_Printf("%d bit %s sound initialized (rate: %dHz)\n",
97                                 info.play.precision,
98                                 (info.play.channels == 2) ? "stereo" : "mono",
99                                 info.play.sample_rate);
100
101         shm->samples = sizeof (dma_buffer) / shm->format.width;
102         shm->samplepos = 0;
103         shm->buffer = dma_buffer;
104
105         snd_inited = true;
106         return true;
107 }
108
109 int SNDDMA_GetDMAPos (void)
110 {
111         audio_info_t info;
112
113         if (!snd_inited)
114                 return 0;
115
116         if (ioctl (audio_fd, AUDIO_GETINFO, &info) < 0)
117         {
118                 Con_Print("Error: can't get audio info\n");
119                 SNDDMA_Shutdown ();
120                 return 0;
121         }
122
123         return ((info.play.samples * shm->format.channels) % shm->samples);
124 }
125
126 void SNDDMA_Shutdown (void)
127 {
128         if (snd_inited)
129         {
130                 close (audio_fd);
131                 audio_fd = -1;
132                 snd_inited = false;
133         }
134 }
135
136 /*
137 ==============
138 SNDDMA_Submit
139
140 Send sound to device if buffer isn't really the dma buffer
141 ===============
142 */
143 void SNDDMA_Submit (void)
144 {
145         int bsize;
146         int bytes, b;
147         static int wbufp = 0;
148         unsigned char *p;
149         int idx;
150         int stop = paintedtime;
151
152         if (!snd_inited)
153                 return;
154
155         if (paintedtime < wbufp)
156                 wbufp = 0; // reset
157
158         bsize = shm->format.channels * shm->format.width;
159         bytes = (paintedtime - wbufp) * bsize;
160
161         if (!bytes)
162                 return;
163
164         if (bytes > sizeof (writebuf))
165         {
166                 bytes = sizeof (writebuf);
167                 stop = wbufp + bytes / bsize;
168         }
169
170         // Transfert the sound data from the circular dma_buffer to writebuf
171         // TODO: using 2 memcpys instead of this loop should be faster
172         p = writebuf;
173         idx = (wbufp*bsize) & (sizeof (dma_buffer) - 1);
174         for (b = bytes; b; b--)
175         {
176                 *p++ = dma_buffer[idx];
177                 idx = (idx + 1) & (sizeof (dma_buffer) - 1);
178         }
179
180         if (write (audio_fd, writebuf, bytes) < bytes)
181                 Con_Print("audio can't keep up!\n");
182
183         wbufp = stop;
184 }
185
186 void *S_LockBuffer (void)
187 {
188         return shm->buffer;
189 }
190
191 void S_UnlockBuffer (void)
192 {
193 }