split out ALSA stream code

This commit is contained in:
Glenn Maynard
2003-10-21 22:51:25 +00:00
parent a47708b0b5
commit dd6191bdf1
4 changed files with 239 additions and 185 deletions
@@ -7,8 +7,6 @@
#include "RageSoundManager.h"
#include "RageUtil.h"
#include "SDL_utils.h"
const int channels = 2;
const int samplerate = 44100;
@@ -17,17 +15,6 @@ const int bytes_per_frame = sizeof(Sint16) * samples_per_frame;
const unsigned max_writeahead = 1024*8;
/* int err; must be defined before using this macro */
#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();
@@ -42,35 +29,18 @@ void RageSound_ALSA9::MixerThread()
while(!shutdown)
{
unsigned int frames_read = GetData();
GetData();
const float delay_ms = 1000 * float(max_writeahead) / samplerate;
SDL_Delay( int(delay_ms) / 2);
}
}
/* Returns the number of frames processed */
int RageSound_ALSA9::GetData()
void RageSound_ALSA9::GetData()
{
LockMutex L(SOUNDMAN->lock);
snd_pcm_sframes_t avail_frames = snd_pcm_avail_update(pcm);
if( avail_frames < 0 && Recover(avail_frames) )
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);
const int frames_to_fill = pcm->GetNumFramesToFill();
if( frames_to_fill <= 0 )
return 0;
return;
/* Sint16 represents a single sample
* each frame contains one sample per channel
@@ -82,6 +52,7 @@ int RageSound_ALSA9::GetData()
static SoundMixBuffer mix;
mix.SetVolume( SOUNDMAN->GetMixVolume() );
LockMutex L(SOUNDMAN->lock);
for(unsigned i = 0; i < sounds.size(); ++i)
{
if(sounds[i]->stopping)
@@ -91,7 +62,7 @@ int RageSound_ALSA9::GetData()
* Get the units straight,
* <bytes> = GetPCM(<bytes*>, <bytes>, <frames>)
*/
unsigned got = sounds[i]->snd->GetPCM( (char *) buf, frames_to_fill*bytes_per_frame, last_cursor_pos ) / sizeof(Sint16);
unsigned got = sounds[i]->snd->GetPCM( (char *) buf, frames_to_fill*bytes_per_frame, pcm->GetPlayPos() ) / sizeof(Sint16);
mix.write((Sint16 *) buf, got);
if( int(got) < frames_to_fill )
@@ -100,55 +71,12 @@ int RageSound_ALSA9::GetData()
sounds[i]->stopping = true;
}
}
L.Unlock();
memset( buf, 0, sizeof(Sint16) * frames_to_fill * samples_per_frame );
mix.read((Sint16*)buf);
mix.read( buf );
/* 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 < 0 && Recover(wrote) )
wrote = snd_pcm_mmap_writei( pcm, buf, frames_to_fill );
if( wrote < 0 )
{
LOG->Trace( "RageSoundDriver_ALSA9::GetData: snd_pcm_mmap_writei: %s", snd_strerror(wrote) );
return -1;
}
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;
}
/**
* When the play buffer underruns, subsequent writes to the buffer
* return -EPIPE. When this happens, call Recover() to restart
* playback.
*/
bool RageSound_ALSA9::Recover(int r)
{
if( r == -EPIPE )
{
LOG->Trace("RageSound_ALSA9::Recover (prepare)");
int err = snd_pcm_prepare(pcm);
ALSA_ASSERT("snd_pcm_prepare (Recover)");
return true;
}
if( r == -ESTRPIPE )
{
LOG->Trace("RageSound_ALSA9::Recover (resume)");
int err;
while ((err = snd_pcm_resume(pcm)) == -EAGAIN)
SDL_Delay(10);
ALSA_ASSERT("snd_pcm_resume (Recover)");
return true;
}
return false;
pcm->Write( buf, frames_to_fill );
}
@@ -201,90 +129,16 @@ void RageSound_ALSA9::StopMixing(RageSound *snd)
int RageSound_ALSA9::GetPosition(const RageSound *snd) const
{
LockMutex L(SOUNDMAN->lock);
snd_pcm_status_t *status;
snd_pcm_status_alloca(&status);
int err = snd_pcm_status( pcm, status );
ALSA_ASSERT("snd_pcm_status");
snd_pcm_state_t state = snd_pcm_status_get_state( status );
if ( state == SND_PCM_STATE_PREPARED )
return 0;
snd_pcm_hwsync( pcm );
/* delay is returned in frames */
snd_pcm_sframes_t delay = snd_pcm_status_get_delay(status);
return last_cursor_pos - delay;
return pcm->GetPosition();
}
RageSound_ALSA9::RageSound_ALSA9()
{
shutdown = false;
last_cursor_pos = 0;
/* open the device */
// if we instead use plughw: then our requested format WILL be provided
int err;
err = snd_pcm_open( &pcm, "hw:0,0", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK );
if (err < 0)
RageException::ThrowNonfatal("snd_pcm_open: %s", snd_strerror(err));
/* allocate the hardware parameters structure */
snd_pcm_hw_params_t *hwparams;
err = snd_pcm_hw_params_malloc(&hwparams);
ALSA_ASSERT("snd_pcm_hw_params_malloc");
/* not exactly sure what this does */
err = snd_pcm_hw_params_any(pcm, hwparams);
ALSA_ASSERT("snd_pcm_hw_params_any");
/* 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");
/* 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");
/* set number of channels */
err = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
ALSA_ASSERT("snd_pcm_hw_params_set_channels");
unsigned int rate = samplerate;
err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0);
// check if we got the rate we desire
/* write the hardware parameters to the device */
err = snd_pcm_hw_params(pcm, hwparams);
ALSA_ASSERT("snd_pcm_hw_params");
snd_pcm_hw_params_free(hwparams);
/* prepare the device to reveice data */
err = snd_pcm_prepare(pcm);
ALSA_ASSERT("snd_pcm_prepare");
//XXX should RageException::ThrowNonfatal if something went wrong
/* prepare a snd_output_t for use with LOG->Trace */
snd_output_t *errout = NULL;
snd_output_buffer_open(&errout);
snd_pcm_dump(pcm, errout);
snd_output_flush(errout);
char *errstring;
snd_output_buffer_string(errout, &errstring);
LOG->Trace("%s", errstring);
snd_output_close( errout );
total_frames = snd_pcm_avail_update(pcm);
pcm = new Alsa9Buf( Alsa9Buf::HW_DONT_CARE, channels, samplerate, max_writeahead );
MixingThread.SetName( "RageSound_ALSA9" );
MixingThread.Create( MixerThread_start, this );
}
@@ -297,7 +151,7 @@ RageSound_ALSA9::~RageSound_ALSA9()
MixingThread.Wait();
LOG->Trace("Mixer thread shut down.");
snd_pcm_close(pcm);
delete pcm;
}
float RageSound_ALSA9::GetPlayLatency() const