2003-04-26 20:59:02 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "RageSoundDriver_OSS.h"
|
|
|
|
|
|
|
|
|
|
#include "RageTimer.h"
|
|
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "RageSound.h"
|
2003-07-27 00:48:38 +00:00
|
|
|
#include "RageSoundManager.h"
|
2003-04-26 20:59:02 +00:00
|
|
|
#include "RageUtil.h"
|
|
|
|
|
|
|
|
|
|
#include "SDL.h"
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/soundcard.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
|
|
|
|
/* samples */
|
|
|
|
|
const int channels = 2;
|
|
|
|
|
const int samplesize = channels*2; /* 16-bit */
|
2003-06-07 14:12:47 +00:00
|
|
|
const int chunk_order = 12;
|
|
|
|
|
const int num_chunks = 4;
|
|
|
|
|
const int buffersize = num_chunks * (1 << (chunk_order-1)); /* in bytes */
|
|
|
|
|
const int buffersize_frames = buffersize/samplesize; /* in frames */
|
2003-04-26 20:59:02 +00:00
|
|
|
|
|
|
|
|
int RageSound_OSS::MixerThread_start(void *p)
|
|
|
|
|
{
|
|
|
|
|
((RageSound_OSS *) p)->MixerThread();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_OSS::MixerThread()
|
|
|
|
|
{
|
|
|
|
|
/* SOUNDMAN will be set once RageSoundManager's ctor returns and
|
|
|
|
|
* assigns it; we might get here before that happens, though. */
|
|
|
|
|
while(!SOUNDMAN && !shutdown) SDL_Delay(10);
|
|
|
|
|
|
|
|
|
|
/* We want to set a higher priority, but Unix only lets root renice
|
|
|
|
|
* < 0, which is silly. Give it a try, anyway. */
|
|
|
|
|
nice(-5);
|
|
|
|
|
|
|
|
|
|
while(!shutdown) {
|
|
|
|
|
while(GetData())
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
fd_set f;
|
|
|
|
|
FD_ZERO(&f);
|
|
|
|
|
FD_SET(fd, &f);
|
|
|
|
|
|
|
|
|
|
SDL_Delay(10);
|
|
|
|
|
|
|
|
|
|
struct timeval tv = { 0, 10000 };
|
|
|
|
|
select(fd+1, NULL, &f, NULL, &tv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RageSound_OSS::GetData()
|
|
|
|
|
{
|
|
|
|
|
LockMutex L(SOUNDMAN->lock);
|
|
|
|
|
|
|
|
|
|
/* Look for a free buffer. */
|
|
|
|
|
audio_buf_info ab;
|
|
|
|
|
if(ioctl(fd, SNDCTL_DSP_GETOSPACE, &ab) == -1)
|
|
|
|
|
RageException::Throw("ioctl(SNDCTL_DSP_GETOSPACE): %s", strerror(errno) );
|
|
|
|
|
|
|
|
|
|
if(!ab.fragments)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const int chunksize = ab.fragsize;
|
|
|
|
|
|
|
|
|
|
static Sint16 *buf = NULL;
|
|
|
|
|
if(!buf)
|
|
|
|
|
buf = new Sint16[chunksize / sizeof(Sint16)];
|
|
|
|
|
memset(buf, 0, chunksize);
|
|
|
|
|
|
|
|
|
|
/* Create a 32-bit buffer to mix sounds. */
|
|
|
|
|
static SoundMixBuffer mix;
|
2003-10-18 20:17:10 +00:00
|
|
|
mix.SetVolume( SOUNDMAN->GetMixVolume() );
|
2003-04-26 20:59:02 +00:00
|
|
|
|
|
|
|
|
for(unsigned i = 0; i < sounds.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if(sounds[i]->stopping)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* Call the callback. */
|
|
|
|
|
unsigned got = sounds[i]->snd->GetPCM((char *) buf, chunksize, last_cursor_pos);
|
|
|
|
|
|
|
|
|
|
mix.write((Sint16 *) buf, got/2);
|
|
|
|
|
|
2003-09-19 01:20:02 +00:00
|
|
|
if((int) got < chunksize)
|
2003-04-26 20:59:02 +00:00
|
|
|
{
|
|
|
|
|
/* This sound is finishing. */
|
|
|
|
|
sounds[i]->stopping = true;
|
|
|
|
|
sounds[i]->flush_pos = last_cursor_pos + (got / samplesize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mix.read(buf);
|
|
|
|
|
int wrote = write(fd, buf, chunksize);
|
|
|
|
|
if(wrote != chunksize)
|
|
|
|
|
RageException::Throw("write didn't: %i (%s)", wrote, wrote == -1? strerror(errno): "" );
|
|
|
|
|
|
|
|
|
|
/* Increment last_cursor_pos. */
|
|
|
|
|
last_cursor_pos += chunksize / samplesize;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_OSS::StartMixing(RageSound *snd)
|
|
|
|
|
{
|
|
|
|
|
sound *s = new sound;
|
|
|
|
|
s->snd = snd;
|
|
|
|
|
|
|
|
|
|
LockMutex L(SOUNDMAN->lock);
|
|
|
|
|
sounds.push_back(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_OSS::Update(float delta)
|
|
|
|
|
{
|
|
|
|
|
LockMutex L(SOUNDMAN->lock);
|
|
|
|
|
|
|
|
|
|
/* SoundStopped might erase sounds out from under us, so make a copy
|
|
|
|
|
* of the sound list. */
|
|
|
|
|
vector<sound *> snds = sounds;
|
|
|
|
|
for(unsigned i = 0; i < snds.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if(!sounds[i]->stopping) continue;
|
|
|
|
|
|
|
|
|
|
if(GetPosition(snds[i]->snd) < sounds[i]->flush_pos)
|
|
|
|
|
continue; /* stopping but still flushing */
|
|
|
|
|
|
|
|
|
|
/* This sound is done. */
|
|
|
|
|
snds[i]->snd->StopPlaying();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_OSS::StopMixing(RageSound *snd)
|
|
|
|
|
{
|
|
|
|
|
LockMutex L(SOUNDMAN->lock);
|
|
|
|
|
|
|
|
|
|
/* Find the sound. */
|
|
|
|
|
unsigned i;
|
|
|
|
|
for(i = 0; i < sounds.size(); ++i)
|
|
|
|
|
if(sounds[i]->snd == snd) break;
|
|
|
|
|
if(i == sounds.size())
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace("not stopping a sound because it's not playing");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete sounds[i];
|
|
|
|
|
sounds.erase(sounds.begin()+i, sounds.begin()+i+1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RageSound_OSS::GetPosition(const RageSound *snd) const
|
|
|
|
|
{
|
|
|
|
|
LockMutex L(SOUNDMAN->lock);
|
|
|
|
|
|
2003-10-18 20:17:10 +00:00
|
|
|
ASSERT( fd != -1 );
|
|
|
|
|
|
2003-04-26 20:59:02 +00:00
|
|
|
int delay;
|
|
|
|
|
if(ioctl(fd, SNDCTL_DSP_GETODELAY, &delay) == -1)
|
|
|
|
|
RageException::ThrowNonfatal("RageSound_OSS: ioctl(SNDCTL_DSP_GETODELAY): %s", strerror(errno));
|
|
|
|
|
|
|
|
|
|
return last_cursor_pos - (delay / samplesize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RageSound_OSS::RageSound_OSS()
|
|
|
|
|
{
|
2003-11-14 05:55:48 +00:00
|
|
|
/* If /proc/asound exists, then we're running ALSA. If we got here, we
|
|
|
|
|
* probably failed to init ALSA. The only case I've seen of this so far
|
|
|
|
|
* was not having access to /dev/snd devices.
|
|
|
|
|
*
|
|
|
|
|
* Don't run OSS under ALSA; we'll get emulation, and ALSA's OSS emulation
|
|
|
|
|
* is buggy. */
|
|
|
|
|
if( IsADirectory("/proc/asound") )
|
|
|
|
|
RageException::ThrowNonfatal( "RageSound_OSS: ALSA detected. ALSA OSS emulation is buggy; use ALSA natively.");
|
|
|
|
|
|
2003-04-26 20:59:02 +00:00
|
|
|
shutdown = false;
|
|
|
|
|
last_cursor_pos = 0;
|
|
|
|
|
|
|
|
|
|
fd = open("/dev/dsp", O_WRONLY|O_NONBLOCK);
|
|
|
|
|
if(fd == -1)
|
|
|
|
|
RageException::ThrowNonfatal("RageSound_OSS: Couldn't open /dev/dsp: %s", strerror(errno));
|
|
|
|
|
|
|
|
|
|
int i = AFMT_S16_LE;
|
|
|
|
|
if(ioctl(fd, SNDCTL_DSP_SETFMT, &i) == -1)
|
|
|
|
|
RageException::ThrowNonfatal("RageSound_OSS: ioctl(SNDCTL_DSP_SETFMT, %i): %s", i, strerror(errno));
|
|
|
|
|
if(i != AFMT_S16_LE)
|
|
|
|
|
RageException::ThrowNonfatal("RageSound_OSS: Wanted format %i, got %i instead", AFMT_S16_LE, i);
|
|
|
|
|
|
|
|
|
|
i = channels;
|
|
|
|
|
if(ioctl(fd, SNDCTL_DSP_CHANNELS, &i) == -1)
|
|
|
|
|
RageException::ThrowNonfatal("RageSound_OSS: ioctl(SNDCTL_DSP_CHANNELS, %i): %s", i, strerror(errno));
|
|
|
|
|
if(i != channels)
|
|
|
|
|
RageException::ThrowNonfatal("RageSound_OSS: Wanted %i channels, got %i instead", channels, i);
|
|
|
|
|
|
2003-06-07 14:12:47 +00:00
|
|
|
i = 44100;
|
2003-04-26 20:59:02 +00:00
|
|
|
if(ioctl(fd, SOUND_PCM_WRITE_RATE, &i) == -1 )
|
|
|
|
|
RageException::ThrowNonfatal("RageSound_OSS: ioctl(SOUND_PCM_WRITE_RATE, %i): %s", i, strerror(errno));
|
|
|
|
|
samplerate = i;
|
|
|
|
|
LOG->Trace("RageSound_OSS: sample rate %i", samplerate);
|
2003-06-07 14:12:47 +00:00
|
|
|
i = (num_chunks << 16) + chunk_order;
|
|
|
|
|
if(ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &i) == -1)
|
|
|
|
|
RageException::ThrowNonfatal("RageSound_OSS: ioctl(SNDCTL_DSP_SETFRAGMENT, %i): %s", i, strerror(errno));
|
2003-04-26 20:59:02 +00:00
|
|
|
|
2003-07-27 03:22:04 +00:00
|
|
|
MixingThread.SetName( "RageSound_OSS" );
|
|
|
|
|
MixingThread.Create( MixerThread_start, this );
|
2003-04-26 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RageSound_OSS::~RageSound_OSS()
|
|
|
|
|
{
|
|
|
|
|
/* Signal the mixing thread to quit. */
|
|
|
|
|
shutdown = true;
|
|
|
|
|
LOG->Trace("Shutting down mixer thread ...");
|
2003-07-27 03:22:04 +00:00
|
|
|
MixingThread.Wait();
|
2003-04-26 20:59:02 +00:00
|
|
|
LOG->Trace("Mixer thread shut down.");
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float RageSound_OSS::GetPlayLatency() const
|
|
|
|
|
{
|
|
|
|
|
return 0; // (1.0f / samplerate) * (buffersize_frames - chunksize_frames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2002-2003 by the person(s) listed below. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Glenn Maynard
|
|
|
|
|
*/
|