19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Lesser General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2.1 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Lesser General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Lesser General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free Software 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Modified in Oct 2004 by Hannu Savolainen 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hannu@opensound.com 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */ 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h> /* For perror() */ 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string.h> /* For strerror() */ 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h> 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h> 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h> 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h> 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h> 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h> 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/stat.h> 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is installed on some systems */ 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <soundcard.h> 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is recommended by OSS */ 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/soundcard.h> 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h" 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h" 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h" 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h" 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h" 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dspaudio.h" 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The tag name used by DSP audio */ 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DSP_DRIVER_NAME "dsp" 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Open the audio device for playback, and don't block if busy */ 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define OPEN_FLAGS (O_WRONLY|O_NONBLOCK) 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */ 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec); 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_WaitAudio(_THIS); 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_PlayAudio(_THIS); 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DSP_GetAudioBuf(_THIS); 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_CloseAudio(_THIS); 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */ 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void) 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int fd; 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int available; 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall available = 0; 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0); 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( fd >= 0 ) { 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall available = 1; 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(fd); 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(available); 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device) 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(device->hidden); 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(device); 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex) 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_AudioDevice *this; 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Initialize all variables that we clean on shutdown */ 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( this ) { 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(this, 0, (sizeof *this)); 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->hidden = (struct SDL_PrivateAudioData *) 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_malloc((sizeof *this->hidden)); 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (this == NULL) || (this->hidden == NULL) ) { 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_OutOfMemory(); 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( this ) { 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(this); 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd = -1; 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the function pointers */ 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->OpenAudio = DSP_OpenAudio; 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->WaitAudio = DSP_WaitAudio; 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->PlayAudio = DSP_PlayAudio; 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->GetAudioBuf = DSP_GetAudioBuf; 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->CloseAudio = DSP_CloseAudio; 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->free = Audio_DeleteDevice; 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return this; 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap DSP_bootstrap = { 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_DRIVER_NAME, "OSS /dev/dsp standard audio", 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Audio_Available, Audio_CreateDevice 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */ 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_WaitAudio(_THIS) 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Not needed at all since OSS handles waiting automagically */ 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_PlayAudio(_THIS) 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (write(audio_fd, mixbuf, mixlen)==-1) 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall perror("Audio write"); 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->enabled = 0; 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen); 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DSP_GetAudioBuf(_THIS) 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(mixbuf); 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_CloseAudio(_THIS) 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( mixbuf != NULL ) { 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_FreeAudioMem(mixbuf); 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall mixbuf = NULL; 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( audio_fd >= 0 ) { 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(audio_fd); 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd = -1; 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec) 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall char audiodev[1024]; 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int format; 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int value; 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int frag_spec; 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint16 test_format; 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Make sure fragment size stays a power of 2, or OSS fails. */ 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* I don't know which of these are actually legal values, though... */ 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (spec->channels > 8) 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->channels = 8; 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else if (spec->channels > 4) 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->channels = 4; 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else if (spec->channels > 2) 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->channels = 2; 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Open the audio device */ 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( audio_fd < 0 ) { 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall mixbuf = NULL; 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Make the file descriptor use blocking writes with fcntl() */ 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { long flags; 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall flags = fcntl(audio_fd, F_GETFL); 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall flags &= ~O_NONBLOCK; 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) { 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't set audio blocking mode"); 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_CloseAudio(this); 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get a list of supported hardware formats */ 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) { 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall perror("SNDCTL_DSP_GETFMTS"); 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't get audio format list"); 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_CloseAudio(this); 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Try for a closest match on audio format */ 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = 0; 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( test_format = SDL_FirstAudioFormat(spec->format); 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ! format && test_format; ) { 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Trying format 0x%4.4x\n", test_format); 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch ( test_format ) { 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_U8: 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_U8 ) { 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_U8; 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_S16LSB: 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_S16_LE ) { 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_S16_LE; 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_S16MSB: 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_S16_BE ) { 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_S16_BE; 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * These formats are not used by any real life systems so they are not 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * needed here. 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_S8: 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_S8 ) { 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_S8; 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_U16LSB: 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_U16_LE ) { 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_U16_LE; 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_U16MSB: 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_U16_BE ) { 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_U16_BE; 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = 0; 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ! format ) { 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall test_format = SDL_NextAudioFormat(); 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( format == 0 ) { 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't find any hardware audio formats"); 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_CloseAudio(this); 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->format = test_format; 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the audio format */ 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = format; 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (value != format) ) { 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall perror("SNDCTL_DSP_SETFMT"); 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't set audio format"); 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_CloseAudio(this); 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the number of channels of output */ 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = spec->channels; 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) { 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall perror("SNDCTL_DSP_CHANNELS"); 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Cannot set the number of channels"); 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_CloseAudio(this); 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->channels = value; 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the DSP frequency */ 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = spec->freq; 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) { 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall perror("SNDCTL_DSP_SPEED"); 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't set audio frequency"); 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_CloseAudio(this); 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->freq = value; 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Calculate the final parameters for this audio specification */ 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_CalculateAudioSpec(spec); 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Determine the power of two of the fragment size */ 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( frag_spec = 0; (0x01U<<frag_spec) < spec->size; ++frag_spec ); 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (0x01U<<frag_spec) != spec->size ) { 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Fragment size must be a power of two"); 3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_CloseAudio(this); 3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall frag_spec |= 0x00020000; /* two fragments, for low latency */ 3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the audio buffering parameters */ 3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Requesting %d fragments of size %d\n", 3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (frag_spec >> 16), 1<<(frag_spec&0xFFFF)); 3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) { 3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall perror("SNDCTL_DSP_SETFRAGMENT"); 3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { audio_buf_info info; 3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info); 3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "fragments = %d\n", info.fragments); 3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "fragstotal = %d\n", info.fragstotal); 3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "fragsize = %d\n", info.fragsize); 3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "bytes = %d\n", info.bytes); 3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Allocate mixing buffer */ 3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall mixlen = spec->size; 3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); 3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( mixbuf == NULL ) { 3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DSP_CloseAudio(this); 3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(mixbuf, spec->silence, spec->size); 3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get the parent process id (we're the parent of the audio thread) */ 3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall parent = getpid(); 3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* We're ready to rock and roll. :-) */ 3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 341