2003-02-23 08:39:29 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "RageSoundDriver_ALSA9.h"
|
|
|
|
|
|
|
|
|
|
#include "RageTimer.h"
|
|
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "RageSound.h"
|
2003-07-27 03:35:59 +00:00
|
|
|
#include "RageSoundManager.h"
|
2003-02-23 08:39:29 +00:00
|
|
|
#include "RageUtil.h"
|
|
|
|
|
|
|
|
|
|
#include "SDL_utils.h"
|
|
|
|
|
|
|
|
|
|
const int channels = 2;
|
|
|
|
|
const int samplerate = 44100;
|
2003-03-01 08:09:45 +00:00
|
|
|
|
|
|
|
|
const int samples_per_frame = channels;
|
2003-10-21 09:50:03 +00:00
|
|
|
const int bytes_per_frame = sizeof(Sint16) * samples_per_frame;
|
2003-03-01 08:09:45 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
const unsigned max_writeahead = 1024*8;
|
2003-03-01 08:09:45 +00:00
|
|
|
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-07-27 06:43:50 +00:00
|
|
|
/* int err; must be defined before using this macro */
|
2003-02-23 08:39:29 +00:00
|
|
|
#define ALSA_ASSERT(x) \
|
|
|
|
|
if (err < 0) \
|
|
|
|
|
{ \
|
|
|
|
|
LOG->Trace("RageSound_ALSA9: ASSERT %s: %s", \
|
|
|
|
|
x, snd_strerror(err)); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int RageSound_ALSA9::MixerThread_start(void *p)
|
|
|
|
|
{
|
|
|
|
|
((RageSound_ALSA9 *) p)->MixerThread();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_ALSA9::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);
|
|
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
while(!shutdown)
|
|
|
|
|
{
|
|
|
|
|
unsigned int frames_read = GetData();
|
|
|
|
|
const float delay_ms = 1000 * float(max_writeahead) / samplerate;
|
|
|
|
|
SDL_Delay( int(delay_ms) / 2);
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-27 06:43:50 +00:00
|
|
|
/* Returns the number of frames processed */
|
2003-10-21 09:50:03 +00:00
|
|
|
int RageSound_ALSA9::GetData()
|
2003-02-23 08:39:29 +00:00
|
|
|
{
|
|
|
|
|
LockMutex L(SOUNDMAN->lock);
|
|
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
snd_pcm_sframes_t avail_frames = snd_pcm_avail_update(pcm);
|
|
|
|
|
if( avail_frames == -EPIPE )
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace("underrun");
|
|
|
|
|
int err = snd_pcm_prepare(pcm);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_prepare (Recover)");
|
|
|
|
|
avail_frames = snd_pcm_avail_update(pcm);
|
|
|
|
|
}
|
|
|
|
|
if( avail_frames < 0 )
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace( "RageSoundDriver_ALSA9::GetData: snd_pcm_avail_update: %s", snd_strerror(avail_frames) );
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const snd_pcm_sframes_t filled_frames = total_frames - avail_frames;
|
|
|
|
|
const snd_pcm_sframes_t frames_to_fill = (max_writeahead - filled_frames) & ~3;
|
|
|
|
|
ASSERT( frames_to_fill >= 0 );
|
|
|
|
|
ASSERT( frames_to_fill <= (int)max_writeahead );
|
|
|
|
|
|
|
|
|
|
LOG->Trace("%li avail, %li filled, %li to", avail_frames, filled_frames, frames_to_fill);
|
|
|
|
|
if( frames_to_fill <= 0 )
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace("skip");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-01 08:09:45 +00:00
|
|
|
/* Sint16 represents a single sample
|
|
|
|
|
* each frame contains one sample per channel
|
|
|
|
|
*/
|
2003-10-21 09:50:03 +00:00
|
|
|
static Sint16 *buf = NULL;
|
2003-02-23 08:39:29 +00:00
|
|
|
if (!buf)
|
2003-10-21 09:50:03 +00:00
|
|
|
buf = new Sint16[max_writeahead*samples_per_frame*4];
|
|
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
SoundMixBuffer mix;
|
2003-10-21 09:50:03 +00:00
|
|
|
mix.SetVolume( SOUNDMAN->GetMixVolume() );
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
for(unsigned i = 0; i < sounds.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if(sounds[i]->stopping)
|
|
|
|
|
continue;
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* Call the callback.
|
2003-03-01 08:09:45 +00:00
|
|
|
* Get the units straight,
|
|
|
|
|
* <bytes> = GetPCM(<bytes*>, <bytes>, <frames>)
|
|
|
|
|
*/
|
2003-10-21 09:50:03 +00:00
|
|
|
unsigned got = sounds[i]->snd->GetPCM( (char *) buf, frames_to_fill*bytes_per_frame, last_cursor_pos ) / sizeof(Sint16);
|
|
|
|
|
mix.write((Sint16 *) buf, got);
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
if( int(got) < frames_to_fill )
|
2003-03-17 20:12:00 +00:00
|
|
|
{
|
|
|
|
|
/* This sound is finishing. */
|
|
|
|
|
sounds[i]->stopping = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
memset( buf, 0, sizeof(Sint16) * frames_to_fill * samples_per_frame );
|
|
|
|
|
mix.read((Sint16*)buf);
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
/* We should be able to write it all. If we don't, treat it as an error. */
|
|
|
|
|
int wrote = snd_pcm_mmap_writei( pcm, buf, frames_to_fill );
|
|
|
|
|
if( wrote == -EPIPE )
|
2003-03-17 20:12:00 +00:00
|
|
|
{
|
2003-10-21 09:50:03 +00:00
|
|
|
int err = snd_pcm_prepare(pcm);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_prepare (Recover)");
|
|
|
|
|
wrote = snd_pcm_mmap_writei( pcm, buf, frames_to_fill );
|
2003-03-17 20:12:00 +00:00
|
|
|
}
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
if( wrote <= 0 )
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace( "RageSoundDriver_ALSA9::GetData: snd_pcm_mmap_writei: %s", snd_strerror(wrote) );
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
last_cursor_pos += wrote;
|
|
|
|
|
if( wrote < frames_to_fill )
|
|
|
|
|
LOG->Trace("Couldn't write whole buffer? (%i < %li)\n", wrote, frames_to_fill );
|
|
|
|
|
|
|
|
|
|
return frames_to_fill;
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When the play buffer underruns, subsequent writes to the buffer
|
|
|
|
|
* return -EPIPE. When this happens, call Recover() to restart
|
|
|
|
|
* playback.
|
|
|
|
|
*/
|
|
|
|
|
void RageSound_ALSA9::Recover(int r)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (r == -EPIPE)
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace("RageSound_ALSA9::Recover (prepare)");
|
|
|
|
|
err = snd_pcm_prepare(pcm);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_prepare (Recover)");
|
|
|
|
|
}
|
|
|
|
|
else /* r == -ESTRPIPE */
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace("RageSound_ALSA9::Recover (resume)");
|
|
|
|
|
while ((err = snd_pcm_resume(pcm)) == -EAGAIN)
|
|
|
|
|
SDL_Delay(10);
|
|
|
|
|
|
|
|
|
|
ALSA_ASSERT("snd_pcm_resume (Recover)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RageSound_ALSA9::StartMixing(RageSound *snd)
|
|
|
|
|
{
|
|
|
|
|
sound *s = new sound;
|
|
|
|
|
s->snd = snd;
|
|
|
|
|
|
2003-04-12 18:10:50 +00:00
|
|
|
LockMutex L(SOUNDMAN->lock);
|
2003-02-23 08:39:29 +00:00
|
|
|
sounds.push_back(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_ALSA9::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;
|
|
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
if(GetPosition(snds[i]->snd) < sounds[i]->flush_pos)
|
|
|
|
|
continue; /* stopping but still flushing */
|
2003-02-23 08:39:29 +00:00
|
|
|
|
|
|
|
|
/* This sound is done. */
|
|
|
|
|
snds[i]->snd->StopPlaying();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_ALSA9::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_ALSA9::GetPosition(const RageSound *snd) const
|
|
|
|
|
{
|
2003-03-17 20:12:00 +00:00
|
|
|
LockMutex L(SOUNDMAN->lock);
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
snd_pcm_status_t *status;
|
|
|
|
|
snd_pcm_status_alloca(&status);
|
2003-10-21 09:50:03 +00:00
|
|
|
|
|
|
|
|
int err = snd_pcm_status( pcm, status );
|
2003-03-17 20:12:00 +00:00
|
|
|
|
2003-02-23 08:39:29 +00:00
|
|
|
ALSA_ASSERT("snd_pcm_status");
|
|
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
snd_pcm_state_t state = snd_pcm_status_get_state( status );
|
|
|
|
|
if ( state == SND_PCM_STATE_PREPARED )
|
|
|
|
|
return 0;
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
/* XXX snd_pcm_hwsync */
|
2003-03-01 08:09:45 +00:00
|
|
|
/* delay is returned in frames */
|
2003-10-21 09:50:03 +00:00
|
|
|
snd_pcm_sframes_t delay = snd_pcm_status_get_delay(status);
|
2003-02-23 08:39:29 +00:00
|
|
|
|
|
|
|
|
return last_cursor_pos - delay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RageSound_ALSA9::RageSound_ALSA9()
|
|
|
|
|
{
|
|
|
|
|
shutdown = false;
|
|
|
|
|
last_cursor_pos = 0;
|
|
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* open the device */
|
|
|
|
|
// if we instead use plughw: then our requested format WILL be provided
|
2003-10-21 09:50:03 +00:00
|
|
|
int err;
|
|
|
|
|
err = snd_pcm_open( &pcm, "hw:0,0", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK );
|
2003-07-27 06:43:50 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
RageException::ThrowNonfatal("snd_pcm_open: %s", snd_strerror(err));
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* allocate the hardware parameters structure */
|
2003-10-21 09:50:03 +00:00
|
|
|
snd_pcm_hw_params_t *hwparams;
|
2003-03-17 20:12:00 +00:00
|
|
|
err = snd_pcm_hw_params_malloc(&hwparams);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_hw_params_malloc");
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* not exactly sure what this does */
|
|
|
|
|
err = snd_pcm_hw_params_any(pcm, hwparams);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_hw_params_any");
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* set to mmap mode (with channels interleaved) */
|
|
|
|
|
err = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_hw_params_set_access");
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* set PCM format (signed 16bit, little endian) */
|
|
|
|
|
err = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_hw_params_set_format");
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* set number of channels */
|
|
|
|
|
err = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_hw_params_set_channels");
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
unsigned int rate = samplerate;
|
|
|
|
|
err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0);
|
|
|
|
|
// check if we got the rate we desire
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* write the hardware parameters to the device */
|
|
|
|
|
err = snd_pcm_hw_params(pcm, hwparams);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_hw_params");
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
snd_pcm_hw_params_free(hwparams);
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* prepare the device to reveice data */
|
|
|
|
|
err = snd_pcm_prepare(pcm);
|
|
|
|
|
ALSA_ASSERT("snd_pcm_prepare");
|
2003-02-23 08:39:29 +00:00
|
|
|
|
|
|
|
|
//XXX should RageException::ThrowNonfatal if something went wrong
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* prepare a snd_output_t for use with LOG->Trace */
|
2003-10-21 09:50:03 +00:00
|
|
|
snd_output_t *errout = NULL;
|
2003-02-23 08:39:29 +00:00
|
|
|
snd_output_buffer_open(&errout);
|
|
|
|
|
snd_pcm_dump(pcm, errout);
|
2003-10-21 09:50:03 +00:00
|
|
|
snd_output_flush(errout);
|
|
|
|
|
|
2003-02-23 08:39:29 +00:00
|
|
|
char *errstring;
|
|
|
|
|
snd_output_buffer_string(errout, &errstring);
|
|
|
|
|
LOG->Trace("%s", errstring);
|
2003-10-21 09:50:03 +00:00
|
|
|
snd_output_close( errout );
|
|
|
|
|
|
|
|
|
|
total_frames = snd_pcm_avail_update(pcm);
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-07-27 03:35:59 +00:00
|
|
|
MixingThread.SetName( "RageSound_ALSA9" );
|
|
|
|
|
MixingThread.Create( MixerThread_start, this );
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageSound_ALSA9::~RageSound_ALSA9()
|
|
|
|
|
{
|
|
|
|
|
/* Signal the mixing thread to quit. */
|
|
|
|
|
shutdown = true;
|
|
|
|
|
LOG->Trace("Shutting down mixer thread ...");
|
2003-07-27 03:35:59 +00:00
|
|
|
MixingThread.Wait();
|
2003-02-23 08:39:29 +00:00
|
|
|
LOG->Trace("Mixer thread shut down.");
|
|
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
snd_pcm_close(pcm);
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float RageSound_ALSA9::GetPlayLatency() const
|
|
|
|
|
{
|
2003-10-21 09:50:03 +00:00
|
|
|
return float(max_writeahead)/samplerate;
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2002 by the person(s) listed below. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Glenn Maynard (RageSoundDriver_WaveOut)
|
|
|
|
|
*/
|