Add ALSA hardware mixing

This commit is contained in:
Glenn Maynard
2003-10-22 02:34:44 +00:00
parent b101cba858
commit 0d2671bb01
9 changed files with 508 additions and 116 deletions
+90 -36
View File
@@ -1,35 +1,31 @@
#include "global.h"
#include "RageLog.h"
#include "RageUtil.h"
#include "ALSA9Helpers.h"
#include "SDL_utils.h"
/* 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)); }
if (err < 0) { LOG->Trace("RageSound_ALSA9: ASSERT %s: %s (%i)", x, snd_strerror(err), err); }
Alsa9Buf::Alsa9Buf( hw hardware, int channels_, int samplerate_, int writeahead_ )
void Alsa9Buf::SetHWParams()
{
channels = channels_;
samplerate = samplerate_;
samplebits = 16;
writeahead = writeahead_;
// buffer_locked = false;
last_cursor_pos = 0;
//write_cursor = LastPosition = buffer_bytes_filled = 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));
if( snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED )
snd_pcm_drop( pcm );
if( snd_pcm_state(pcm) != SND_PCM_STATE_OPEN )
{
/* Reset the stream to SND_PCM_STATE_OPEN. */
err = snd_pcm_hw_free( pcm );
ALSA_ASSERT("snd_pcm_hw_free");
}
// RAGE_ASSERT_M( snd_pcm_state(pcm) == SND_PCM_STATE_OPEN, ssprintf("(%s)", snd_pcm_state_name(snd_pcm_state(pcm))) );
/* 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");
snd_pcm_hw_params_alloca( &hwparams );
/* not exactly sure what this does */
err = snd_pcm_hw_params_any(pcm, hwparams);
ALSA_ASSERT("snd_pcm_hw_params_any");
@@ -45,17 +41,47 @@ Alsa9Buf::Alsa9Buf( hw hardware, int channels_, int samplerate_, int writeahead_
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
if( samplerate != Alsa9Buf::DYNAMIC_SAMPLERATE )
{
unsigned int rate = samplerate;
err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0);
ALSA_ASSERT("snd_pcm_hw_params_set_rate_near");
if( (int) rate != samplerate )
LOG->Warn("Alsa9Buf::SetHWParams: Couldn't get %ihz (got %ihz instead)", samplerate, rate);
}
/* 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);
Alsa9Buf::Alsa9Buf( hw hardware, int channels_, int samplerate_ )
{
channels = channels_;
samplerate = samplerate_;
samplebits = 16;
last_cursor_pos = 0;
/* Open the device. */
int err;
err = snd_pcm_open( &pcm, "dmix", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK );
// err = snd_pcm_open( &pcm, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK );
if (err < 0)
RageException::ThrowNonfatal("snd_pcm_open: %s", snd_strerror(err));
SetHWParams();
snd_pcm_sw_params_t *swparams;
snd_pcm_sw_params_alloca( &swparams );
/* Disable SND_PCM_STATE_XRUN. */
err = snd_pcm_sw_params_set_stop_threshold( pcm, swparams, 10000000 );
ALSA_ASSERT("snd_pcm_sw_params_set_stop_threshold");
err = snd_pcm_sw_params(pcm, swparams);
ALSA_ASSERT("snd_pcm_sw_params");
/* prepare the device to reveice data */
err = snd_pcm_prepare(pcm);
ALSA_ASSERT("snd_pcm_prepare");
@@ -84,7 +110,7 @@ Alsa9Buf::~Alsa9Buf()
}
int Alsa9Buf::GetNumFramesToFill()
int Alsa9Buf::GetNumFramesToFill( int writeahead )
{
snd_pcm_sframes_t avail_frames = snd_pcm_avail_update(pcm);
if( avail_frames < 0 && Recover(avail_frames) )
@@ -97,8 +123,11 @@ int Alsa9Buf::GetNumFramesToFill()
}
const snd_pcm_sframes_t filled_frames = total_frames - avail_frames;
RAGE_ASSERT_M( filled_frames >= 0, ssprintf("total %i, %i avail", total_frames, avail_frames) );
const snd_pcm_sframes_t frames_to_fill = (writeahead - filled_frames) & ~3;
ASSERT( frames_to_fill <= (int)writeahead );
RAGE_ASSERT_M( frames_to_fill <= (int)writeahead, ssprintf("writeahead %i, %i filled, %i to fill", writeahead, filled_frames, frames_to_fill) );
// LOG->Trace("%li/%li fr avail, %li", avail_frames, total_frames, frames_to_fill);
// LOG->Trace("%li avail, %li filled, %li to", avail_frames, filled_frames, frames_to_fill);
return max( 0l, frames_to_fill );
@@ -113,7 +142,7 @@ void Alsa9Buf::Write( const Sint16 *buffer, int frames )
if( wrote < 0 )
{
LOG->Trace( "RageSoundDriver_ALSA9::GetData: snd_pcm_mmap_writei: %s", snd_strerror(wrote) );
LOG->Trace( "RageSoundDriver_ALSA9::GetData: snd_pcm_mmap_writei: %s (%i)", snd_strerror(wrote), wrote );
return;
}
@@ -154,21 +183,46 @@ bool Alsa9Buf::Recover( int r )
int Alsa9Buf::GetPosition() const
{
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 )
if( snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED )
{
LOG->Trace("???");
return 0;
}
snd_pcm_hwsync( pcm );
/* delay is returned in frames */
snd_pcm_sframes_t delay = snd_pcm_status_get_delay(status);
snd_pcm_sframes_t delay;
int err = snd_pcm_delay( pcm, &delay );
LOG->Trace("last %i, del %i", last_cursor_pos, delay);
return last_cursor_pos - delay;
}
void Alsa9Buf::Reset()
{
/* Nothing is playing. Reset the sample count; this is just to
* prevent eventual overflow. */
last_cursor_pos = 0;
}
void Alsa9Buf::Play()
{
/* NOP. It'll start playing when it gets some data. */
}
void Alsa9Buf::Stop()
{
snd_pcm_drop( pcm );
snd_pcm_prepare( pcm );
}
void Alsa9Buf::SetSampleRate(int hz)
{
samplerate = hz;
SetHWParams();
snd_pcm_prepare( pcm );
}