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#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */ 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h> 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string.h> /* For strerror() */ 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h> 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h> 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h> 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h> 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h> 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h> 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h> 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/stat.h> 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/mman.h> 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is installed on some systems */ 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <soundcard.h> 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is recommended by OSS */ 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/soundcard.h> 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef MAP_FAILED 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAP_FAILED ((Uint8 *)-1) 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h" 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h" 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h" 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h" 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dmaaudio.h" 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The tag name used by DMA audio */ 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DMA_DRIVER_NAME "dma" 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Open the audio device for playback, and don't block if busy */ 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define OPEN_FLAGS (O_RDWR|O_NONBLOCK) 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */ 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec); 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DMA_WaitAudio(_THIS); 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DMA_PlayAudio(_THIS); 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DMA_GetAudioBuf(_THIS); 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DMA_CloseAudio(_THIS); 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */ 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void) 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int available; 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int fd; 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall available = 0; 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0); 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( fd >= 0 ) { 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int caps; 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct audio_buf_info info; 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps) == 0) && 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) && 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) == 0) ) { 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall available = 1; 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(fd); 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(available); 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device) 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(device->hidden); 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(device); 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex) 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_AudioDevice *this; 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Initialize all variables that we clean on shutdown */ 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( this ) { 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(this, 0, (sizeof *this)); 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->hidden = (struct SDL_PrivateAudioData *) 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_malloc((sizeof *this->hidden)); 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (this == NULL) || (this->hidden == NULL) ) { 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_OutOfMemory(); 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( this ) { 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(this); 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd = -1; 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the function pointers */ 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->OpenAudio = DMA_OpenAudio; 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->WaitAudio = DMA_WaitAudio; 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->PlayAudio = DMA_PlayAudio; 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->GetAudioBuf = DMA_GetAudioBuf; 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->CloseAudio = DMA_CloseAudio; 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->free = Audio_DeleteDevice; 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return this; 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap DMA_bootstrap = { 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DMA_DRIVER_NAME, "OSS /dev/dsp DMA audio", 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Audio_Available, Audio_CreateDevice 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */ 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DMA_WaitAudio(_THIS) 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fd_set fdset; 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Check to see if the thread-parent process is still alive */ 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { static int cnt = 0; 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Note that this only works with thread implementations 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall that use a different process id for each thread. 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */ 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( kill(parent, 0) < 0 ) { 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->enabled = 0; 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* See if we need to use timed audio synchronization */ 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( frame_ticks ) { 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Use timer for general audio synchronization */ 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 ticks; 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS; 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ticks > 0 ) { 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Delay(ticks); 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Use select() for audio synchronization */ 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct timeval timeout; 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall FD_ZERO(&fdset); 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall FD_SET(audio_fd, &fdset); 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall timeout.tv_sec = 10; 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall timeout.tv_usec = 0; 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Waiting for audio to get ready\n"); 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) { 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const char *message = 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef AUDIO_OSPACE_HACK 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall "Audio timeout - buggy audio driver? (trying ospace)"; 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall "Audio timeout - buggy audio driver? (disabled)"; 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* In general we should never print to the screen, 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but in this case we have no other way of letting 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall the user know what happened. 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "SDL: %s\n", message); 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef AUDIO_OSPACE_HACK 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* We may be able to use GET_OSPACE trick */ 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall frame_ticks = (float)(this->spec->samples*1000) / 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->spec->freq; 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall next_frame = SDL_GetTicks()+frame_ticks; 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->enabled = 0; 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Don't try to close - may hang */ 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd = -1; 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Done disabling audio\n"); 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* AUDIO_OSPACE_HACK */ 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Ready!\n"); 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DMA_PlayAudio(_THIS) 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* If timer synchronization is enabled, set the next write frame */ 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( frame_ticks ) { 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall next_frame += frame_ticks; 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DMA_GetAudioBuf(_THIS) 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall count_info info; 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int playing; 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int filling; 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get number of blocks, looping if we're not using select() */ 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall do { 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &info) < 0 ) { 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Uh oh... */ 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->enabled = 0; 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(NULL); 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } while ( frame_ticks && (info.blocks < 1) ); 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( info.blocks > 1 ) { 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Warning: audio underflow (%d frags)\n", info.blocks-1); 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall playing = info.ptr / this->spec.size; 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall filling = (playing + 1)%num_buffers; 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (dma_buf + (filling * this->spec.size)); 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DMA_CloseAudio(_THIS) 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( dma_buf != NULL ) { 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall munmap(dma_buf, dma_len); 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dma_buf = NULL; 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( audio_fd >= 0 ) { 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(audio_fd); 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd = -1; 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DMA_ReopenAudio(_THIS, const char *audiodev, int format, int stereo, 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_AudioSpec *spec) 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int frag_spec; 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int value; 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Close and then reopen the audio device */ 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(audio_fd); 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd = open(audiodev, O_RDWR, 0); 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( audio_fd < 0 ) { 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Calculate the final parameters for this audio specification */ 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_CalculateAudioSpec(spec); 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Determine the power of two of the fragment size */ 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec ); 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (0x01<<frag_spec) != spec->size ) { 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Fragment size must be a power of two"); 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the audio buffering parameters */ 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) { 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't set audio fragment spec"); 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the audio format */ 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = format; 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (value != format) ) { 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't set audio format"); 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set mono or stereo audio */ 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = (spec->channels > 1); 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) || 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (value != stereo) ) { 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't set audio channels"); 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the DSP frequency */ 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = spec->freq; 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) { 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't set audio frequency"); 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->freq = value; 3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* We successfully re-opened the audio */ 3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec) 3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall char audiodev[1024]; 3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int format; 3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int stereo; 3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int value; 3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint16 test_format; 3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct audio_buf_info info; 3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Reset the timer synchronization flag */ 3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall frame_ticks = 0.0; 3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Open the audio device */ 3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); 3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( audio_fd < 0 ) { 3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); 3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dma_buf = NULL; 3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ioctl(audio_fd, SNDCTL_DSP_RESET, 0); 3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get a list of supported hardware formats */ 3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) { 3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't get audio format list"); 3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Try for a closest match on audio format */ 3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = 0; 3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( test_format = SDL_FirstAudioFormat(spec->format); 3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ! format && test_format; ) { 3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO 3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Trying format 0x%4.4x\n", test_format); 3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch ( test_format ) { 3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_U8: 3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_U8 ) { 3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_U8; 3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_S8: 3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_S8 ) { 3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_S8; 3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_S16LSB: 3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_S16_LE ) { 3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_S16_LE; 3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_S16MSB: 3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_S16_BE ) { 3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_S16_BE; 3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_U16LSB: 3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_U16_LE ) { 3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_U16_LE; 3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_U16MSB: 3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( value & AFMT_U16_BE ) { 3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = AFMT_U16_BE; 3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = 0; 3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ! format ) { 3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall test_format = SDL_NextAudioFormat(); 3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( format == 0 ) { 3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't find any hardware audio formats"); 3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->format = test_format; 3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the audio format */ 3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = format; 3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || 3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (value != format) ) { 3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't set audio format"); 3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set mono or stereo audio (currently only two channels supported) */ 3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall stereo = (spec->channels > 1); 3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo); 3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( stereo ) { 3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->channels = 2; 4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->channels = 1; 4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Because some drivers don't allow setting the buffer size 4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall after setting the format, we must re-open the audio device 4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall once we know what format and channels are supported 4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( DMA_ReopenAudio(this, audiodev, format, stereo, spec) < 0 ) { 4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Error is set by DMA_ReopenAudio() */ 4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Memory map the audio buffer */ 4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info) < 0 ) { 4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't get OSPACE parameters"); 4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->size = info.fragsize; 4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->samples = spec->size / ((spec->format & 0xFF) / 8); 4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->samples /= spec->channels; 4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall num_buffers = info.fragstotal; 4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dma_len = num_buffers*spec->size; 4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dma_buf = (Uint8 *)mmap(NULL, dma_len, PROT_WRITE, MAP_SHARED, 4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall audio_fd, 0); 4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( dma_buf == MAP_FAILED ) { 4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("DMA memory map failed"); 4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dma_buf = NULL; 4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(dma_buf, spec->silence, dma_len); 4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Check to see if we need to use select() workaround */ 4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { char *workaround; 4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall workaround = SDL_getenv("SDL_DSP_NOSELECT"); 4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( workaround ) { 4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall frame_ticks = (float)(spec->samples*1000)/spec->freq; 4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall next_frame = SDL_GetTicks()+frame_ticks; 4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Trigger audio playback */ 4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = 0; 4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value); 4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall value = PCM_ENABLE_OUTPUT; 4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value) < 0 ) { 4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't trigger audio output"); 4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get the parent process id (we're the parent of the audio thread) */ 4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall parent = getpid(); 4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* We're ready to rock and roll. :-) */ 4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 456