X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=snd_oss.c;h=c3a67aaf7be3397e8adb38cdf0531c984a28874d;hb=8113139cb204c7e10f35bb748bc223a6200af2fa;hp=ca17c0516bcf26874a147ab6bf59248b5dde82e6;hpb=5a62228b228e2a329d2ba7fef9dacd12de4d9033;p=xonotic%2Fdarkplaces.git diff --git a/snd_oss.c b/snd_oss.c index ca17c051..c3a67aaf 100644 --- a/snd_oss.c +++ b/snd_oss.c @@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // OSS module, used by Linux and FreeBSD +#include #include #include #include @@ -89,9 +90,9 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) } 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 / 5; + 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); @@ -101,6 +102,8 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) 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) @@ -160,12 +163,10 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) SndSys_Shutdown(); return false; } - -#ifdef __linux__ - alsaspeakerlayout = true; -#else - alsaspeakerlayout = false; -#endif + + // 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; @@ -186,7 +187,7 @@ void SndSys_Shutdown (void) // Stop the sound and close the device if (audio_fd >= 0) { - ioctl(audio_fd, SNDCTL_DSP_RESET, 0); + ioctl(audio_fd, SNDCTL_DSP_RESET, NULL); close(audio_fd); audio_fd = -1; } @@ -200,6 +201,42 @@ void SndSys_Shutdown (void) } +/* +==================== +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 @@ -222,42 +259,15 @@ void SndSys_Submit (void) nbframes = snd_renderbuffer->endframe - snd_renderbuffer->startframe; if (nbframes > limit) { - written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], limit * factor); - if (written < 0) - { - Con_Printf("SndSys_Submit: audio write returned %d!\n", written); - return; - } - - if (written % factor != 0) - Sys_Error("SndSys_Submit: nb of bytes written (%d) isn't aligned to a frame sample!\n", written); - - snd_renderbuffer->startframe += written / factor; - - if ((unsigned int)written < limit * factor) - { - Con_Printf("SndSys_Submit: audio can't keep up! (%u < %u)\n", written, limit * factor); + written = SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], limit * factor); + if (written < 0 || (unsigned int)written < limit * factor) return; - } nbframes -= limit; startoffset = 0; } - written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], nbframes * factor); - if (written < 0) - { - Con_Printf("SndSys_Submit: audio write returned %d!\n", written); - return; - } - - if (written % factor != 0) - Sys_Error("SndSys_Submit: nb of bytes written (%d) isn't aligned to a frame sample!\n", written); - - snd_renderbuffer->startframe += written / factor; - - if ((unsigned int)written < nbframes * factor) - Con_Printf("SndSys_Submit: audio can't keep up! (%u < %u)\n", written, nbframes * factor); + SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], nbframes * factor); } @@ -274,7 +284,6 @@ unsigned int SndSys_GetSoundTime (void) int new_osstime; unsigned int timediff; - // TODO: use SNDCTL_DSP_GETODELAY instead if (ioctl (audio_fd, SNDCTL_DSP_GETOPTR, &count) == -1) { Con_Print ("SndSys_GetSoundTimeDiff: can't ioctl (SNDCTL_DSP_GETOPTR)\n");