1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// OSS module, used by Linux and FreeBSD
#include "quakedef.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <unistd.h>
#include "snd_main.h"
#define NB_FRAGMENTS 4
static int audio_fd = -1;
static int old_osstime = 0;
static unsigned int osssoundtime;
/*
====================
SndSys_Init
Create "snd_renderbuffer" with the proper sound format if the call is successful
May return a suggested format if the requested format isn't available
====================
*/
qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
{
int flags, ioctl_param, prev_value;
unsigned int fragmentsize;
Con_DPrint("SndSys_Init: using the OSS module\n");
// Check the requested sound format
if (requested->width < 1 || requested->width > 2)
{
Con_Printf("SndSys_Init: invalid sound width (%hu)\n",
requested->width);
if (suggested != NULL)
{
memcpy(suggested, requested, sizeof(*suggested));
if (requested->width < 1)
suggested->width = 1;
else
suggested->width = 2;
}
return false;
}
// Open /dev/dsp
audio_fd = open("/dev/dsp", O_WRONLY);
if (audio_fd < 0)
{
perror("/dev/dsp");
Con_Print("SndSys_Init: could not open /dev/dsp\n");
return false;
}
// Use non-blocking IOs if possible
flags = fcntl(audio_fd, F_GETFL);
if (flags != -1)
{
if (fcntl(audio_fd, F_SETFL, flags | O_NONBLOCK) == -1)
Con_Print("SndSys_Init : fcntl(F_SETFL, O_NONBLOCK) failed!\n");
}
else
Con_Print("SndSys_Init: fcntl(F_GETFL) failed!\n");
// Set the fragment size (up to "NB_FRAGMENTS" fragments of "fragmentsize" bytes)
fragmentsize = requested->speed * requested->channels * requested->width / 10;
fragmentsize = (unsigned int)ceilf((float)fragmentsize / (float)NB_FRAGMENTS);
fragmentsize = CeilPowerOf2(fragmentsize);
ioctl_param = (NB_FRAGMENTS << 16) | log2i(fragmentsize);
if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &ioctl_param) == -1)
{
Con_Print ("SndSys_Init: could not set the fragment size\n");
SndSys_Shutdown ();
return false;
}
Con_Printf ("SndSys_Init: using %u fragments of %u bytes\n",
ioctl_param >> 16, 1 << (ioctl_param & 0xFFFF));
// Set the sound width
if (requested->width == 1)
ioctl_param = AFMT_U8;
else
ioctl_param = AFMT_S16_NE;
prev_value = ioctl_param;
if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &ioctl_param) == -1 ||
ioctl_param != prev_value)
{
if (ioctl_param != prev_value && suggested != NULL)
{
memcpy(suggested, requested, sizeof(*suggested));
if (ioctl_param == AFMT_S16_NE)
suggested->width = 2;
else
suggested->width = 1;
}
Con_Printf("SndSys_Init: could not set the sound width to %hu\n",
requested->width);
SndSys_Shutdown();
return false;
}
// Set the sound channels
ioctl_param = requested->channels;
if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &ioctl_param) == -1 ||
ioctl_param != requested->channels)
{
if (ioctl_param != requested->channels && suggested != NULL)
{
memcpy(suggested, requested, sizeof(*suggested));
suggested->channels = ioctl_param;
}
Con_Printf("SndSys_Init: could not set the number of channels to %hu\n",
requested->channels);
SndSys_Shutdown();
return false;
}
// Set the sound speed
ioctl_param = requested->speed;
if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &ioctl_param) == -1 ||
(unsigned int)ioctl_param != requested->speed)
{
if ((unsigned int)ioctl_param != requested->speed && suggested != NULL)
{
memcpy(suggested, requested, sizeof(*suggested));
suggested->speed = ioctl_param;
}
Con_Printf("SndSys_Init: could not set the sound speed to %u\n",
requested->speed);
SndSys_Shutdown();
return false;
}
// TOCHECK: I'm not sure which channel layout OSS uses for 5.1 and 7.1
if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO)
Cvar_SetValueQuick (&snd_channellayout, SND_CHANNELLAYOUT_ALSA);
old_osstime = 0;
osssoundtime = 0;
snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
return true;
}
/*
====================
SndSys_Shutdown
Stop the sound card, delete "snd_renderbuffer" and free its other resources
====================
*/
void SndSys_Shutdown (void)
{
// Stop the sound and close the device
if (audio_fd >= 0)
{
ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
close(audio_fd);
audio_fd = -1;
}
if (snd_renderbuffer != NULL)
{
Mem_Free(snd_renderbuffer->ring);
Mem_Free(snd_renderbuffer);
snd_renderbuffer = NULL;
}
}
/*
====================
SndSys_Write
====================
*/
static int SndSys_Write (const unsigned char* buffer, unsigned int nb_bytes)
{
int written;
unsigned int factor;
written = write (audio_fd, buffer, nb_bytes);
if (written < 0)
{
if (errno != EAGAIN)
Con_Printf ("SndSys_Write: audio write returned %d! (errno= %d)\n",
written, errno);
return written;
}
factor = snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
if (written % factor != 0)
Sys_Error ("SndSys_Write: nb of bytes written (%d) isn't aligned to a frame sample!\n",
written);
snd_renderbuffer->startframe += written / factor;
if ((unsigned int)written < nb_bytes)
{
Con_DPrintf("SndSys_Submit: audio can't keep up! (%u < %u)\n",
written, nb_bytes);
}
return written;
}
/*
====================
SndSys_Submit
Submit the contents of "snd_renderbuffer" to the sound card
====================
*/
void SndSys_Submit (void)
{
unsigned int startoffset, factor, limit, nbframes;
int written;
if (audio_fd < 0 ||
snd_renderbuffer->startframe == snd_renderbuffer->endframe)
return;
startoffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
factor = snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
limit = snd_renderbuffer->maxframes - startoffset;
nbframes = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
if (nbframes > limit)
{
written = SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], limit * factor);
if (written < 0 || (unsigned int)written < limit * factor)
return;
nbframes -= limit;
startoffset = 0;
}
SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], nbframes * factor);
}
/*
====================
SndSys_GetSoundTime
Returns the number of sample frames consumed since the sound started
====================
*/
unsigned int SndSys_GetSoundTime (void)
{
struct count_info count;
int new_osstime;
unsigned int timediff;
if (ioctl (audio_fd, SNDCTL_DSP_GETOPTR, &count) == -1)
{
Con_Print ("SndSys_GetSoundTimeDiff: can't ioctl (SNDCTL_DSP_GETOPTR)\n");
return 0;
}
new_osstime = count.bytes / (snd_renderbuffer->format.width * snd_renderbuffer->format.channels);
if (new_osstime >= old_osstime)
timediff = new_osstime - old_osstime;
else
{
Con_Print ("SndSys_GetSoundTime: osstime wrapped\n");
timediff = 0;
}
old_osstime = new_osstime;
osssoundtime += timediff;
return osssoundtime;
}
/*
====================
SndSys_LockRenderBuffer
Get the exclusive lock on "snd_renderbuffer"
====================
*/
qboolean SndSys_LockRenderBuffer (void)
{
// Nothing to do
return true;
}
/*
====================
SndSys_UnlockRenderBuffer
Release the exclusive lock on "snd_renderbuffer"
====================
*/
void SndSys_UnlockRenderBuffer (void)
{
// Nothing to do
}
/*
====================
SndSys_SendKeyEvents
Send keyboard events originating from the sound system (e.g. MIDI)
====================
*/
void SndSys_SendKeyEvents(void)
{
// not supported
}
|