Dynamically bind ALSA, so binaries compiled with ALSA will work on systems
without ALSA installed.
This commit is contained in:
@@ -3,60 +3,61 @@
|
||||
#include "RageUtil.h"
|
||||
#include "ALSA9Helpers.h"
|
||||
#include "SDL_utils.h"
|
||||
#include "ALSA9Dynamic.h"
|
||||
|
||||
/* int err; must be defined before using this macro */
|
||||
#define ALSA_CHECK(x) \
|
||||
if ( err < 0 ) { LOG->Info("ALSA9: %s: %s (%i)", x, snd_strerror(err), err); return false; }
|
||||
if ( err < 0 ) { LOG->Info("ALSA9: %s: %s (%i)", x, dsnd_strerror(err), err); return false; }
|
||||
#define ALSA_ASSERT(x) \
|
||||
if (err < 0) { LOG->Trace("RageSound_ALSA9: ASSERT %s: %s (%i)", x, snd_strerror(err), err); }
|
||||
if (err < 0) { LOG->Trace("ALSA9: %s: %s (%i)", x, dsnd_strerror(err), err); }
|
||||
|
||||
bool Alsa9Buf::SetHWParams()
|
||||
{
|
||||
int err;
|
||||
|
||||
if( snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED )
|
||||
snd_pcm_drop( pcm );
|
||||
if( dsnd_pcm_state(pcm) == SND_PCM_STATE_PREPARED )
|
||||
dsnd_pcm_drop( pcm );
|
||||
|
||||
if( snd_pcm_state(pcm) != SND_PCM_STATE_OPEN )
|
||||
if( dsnd_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");
|
||||
err = dsnd_pcm_hw_free( pcm );
|
||||
ALSA_ASSERT("dsnd_pcm_hw_free");
|
||||
}
|
||||
// RAGE_ASSERT_M( snd_pcm_state(pcm) == SND_PCM_STATE_OPEN, ssprintf("(%s)", snd_pcm_state_name(snd_pcm_state(pcm))) );
|
||||
// RAGE_ASSERT_M( dsnd_pcm_state(pcm) == SND_PCM_STATE_OPEN, ssprintf("(%s)", dsnd_pcm_state_name(dsnd_pcm_state(pcm))) );
|
||||
|
||||
/* allocate the hardware parameters structure */
|
||||
snd_pcm_hw_params_t *hwparams;
|
||||
snd_pcm_hw_params_alloca( &hwparams );
|
||||
dsnd_pcm_hw_params_alloca( &hwparams );
|
||||
|
||||
err = snd_pcm_hw_params_any(pcm, hwparams);
|
||||
ALSA_CHECK("snd_pcm_hw_params_any");
|
||||
err = dsnd_pcm_hw_params_any(pcm, hwparams);
|
||||
ALSA_CHECK("dsnd_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_CHECK("snd_pcm_hw_params_set_access");
|
||||
err = dsnd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED);
|
||||
ALSA_CHECK("dsnd_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_CHECK("snd_pcm_hw_params_set_format");
|
||||
err = dsnd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE);
|
||||
ALSA_CHECK("dsnd_pcm_hw_params_set_format");
|
||||
|
||||
/* set number of channels */
|
||||
err = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
|
||||
ALSA_CHECK("snd_pcm_hw_params_set_channels");
|
||||
err = dsnd_pcm_hw_params_set_channels(pcm, hwparams, 2);
|
||||
ALSA_CHECK("dsnd_pcm_hw_params_set_channels");
|
||||
|
||||
if( samplerate != Alsa9Buf::DYNAMIC_SAMPLERATE )
|
||||
{
|
||||
unsigned int rate = samplerate;
|
||||
err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0);
|
||||
ALSA_CHECK("snd_pcm_hw_params_set_rate_near");
|
||||
err = dsnd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0);
|
||||
ALSA_CHECK("dsnd_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_CHECK("snd_pcm_hw_params");
|
||||
err = dsnd_pcm_hw_params( pcm, hwparams );
|
||||
ALSA_CHECK("dsnd_pcm_hw_params");
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -69,52 +70,52 @@ void Alsa9Buf::GetSoundCardDebugInfo()
|
||||
done = true;
|
||||
|
||||
int card = -1;
|
||||
while( snd_card_next( &card ) >= 0 && card >= 0 )
|
||||
while( dsnd_card_next( &card ) >= 0 && card >= 0 )
|
||||
{
|
||||
const CString id = ssprintf( "hw:%d", card );
|
||||
snd_ctl_t *handle;
|
||||
int err;
|
||||
err = snd_ctl_open( &handle, id, 0 );
|
||||
ALSA_ASSERT("snd_pcm_sw_params_set_stop_threshold");
|
||||
err = dsnd_ctl_open( &handle, id, 0 );
|
||||
ALSA_ASSERT("dsnd_pcm_sw_params_set_stop_threshold");
|
||||
if ( err < 0 )
|
||||
{
|
||||
LOG->Info( "Couldn't open card #%i (\"%s\"): %s", card, id.c_str(), snd_strerror(err) );
|
||||
LOG->Info( "Couldn't open card #%i (\"%s\"): %s", card, id.c_str(), dsnd_strerror(err) );
|
||||
continue;
|
||||
}
|
||||
|
||||
snd_ctl_card_info_t *info;
|
||||
snd_ctl_card_info_alloca(&info);
|
||||
err = snd_ctl_card_info( handle, info );
|
||||
dsnd_ctl_card_info_alloca(&info);
|
||||
err = dsnd_ctl_card_info( handle, info );
|
||||
if ( err < 0 )
|
||||
{
|
||||
LOG->Info( "Couldn't get card info for card #%i (\"%s\"): %s", card, id.c_str(), snd_strerror(err) );
|
||||
snd_ctl_close( handle );
|
||||
LOG->Info( "Couldn't get card info for card #%i (\"%s\"): %s", card, id.c_str(), dsnd_strerror(err) );
|
||||
dsnd_ctl_close( handle );
|
||||
continue;
|
||||
}
|
||||
|
||||
int dev = -1;
|
||||
while ( snd_ctl_pcm_next_device( handle, &dev ) >= 0 && dev >= 0 )
|
||||
while ( dsnd_ctl_pcm_next_device( handle, &dev ) >= 0 && dev >= 0 )
|
||||
{
|
||||
snd_pcm_info_t *pcminfo;
|
||||
snd_pcm_info_alloca(&pcminfo);
|
||||
snd_pcm_info_set_device(pcminfo, dev);
|
||||
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK);
|
||||
dsnd_pcm_info_alloca(&pcminfo);
|
||||
dsnd_pcm_info_set_device(pcminfo, dev);
|
||||
dsnd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK);
|
||||
|
||||
err = snd_ctl_pcm_info(handle, pcminfo);
|
||||
err = dsnd_ctl_pcm_info(handle, pcminfo);
|
||||
if ( err < 0 )
|
||||
{
|
||||
if (err != -ENOENT)
|
||||
LOG->Info("snd_ctl_pcm_info(%i) (%s) failed: %s", card, id.c_str(), snd_strerror(err));
|
||||
LOG->Info("dsnd_ctl_pcm_info(%i) (%s) failed: %s", card, id.c_str(), dsnd_strerror(err));
|
||||
continue;
|
||||
}
|
||||
|
||||
LOG->Info( "ALSA Driver: %i: %s [%s], device %i: %s [%s], %i/%i subdevices avail",
|
||||
card, snd_ctl_card_info_get_name(info), snd_ctl_card_info_get_id(info), dev,
|
||||
snd_pcm_info_get_id(pcminfo), snd_pcm_info_get_name(pcminfo),
|
||||
snd_pcm_info_get_subdevices_avail(pcminfo),
|
||||
snd_pcm_info_get_subdevices_count(pcminfo) );
|
||||
card, dsnd_ctl_card_info_get_name(info), dsnd_ctl_card_info_get_id(info), dev,
|
||||
dsnd_pcm_info_get_id(pcminfo), dsnd_pcm_info_get_name(pcminfo),
|
||||
dsnd_pcm_info_get_subdevices_avail(pcminfo),
|
||||
dsnd_pcm_info_get_subdevices_count(pcminfo) );
|
||||
|
||||
snd_ctl_close(handle);
|
||||
dsnd_ctl_close(handle);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +132,7 @@ Alsa9Buf::Alsa9Buf( hw hardware, int channels_, int samplerate_ )
|
||||
{
|
||||
GetSoundCardDebugInfo();
|
||||
|
||||
snd_lib_error_set_handler( ErrorHandler );
|
||||
dsnd_lib_error_set_handler( ErrorHandler );
|
||||
|
||||
channels = channels_;
|
||||
samplerate = samplerate_;
|
||||
@@ -140,62 +141,56 @@ Alsa9Buf::Alsa9Buf( hw hardware, int channels_, int samplerate_ )
|
||||
|
||||
/* 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 );
|
||||
// err = dsnd_pcm_open( &pcm, "dmix", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK );
|
||||
err = dsnd_pcm_open( &pcm, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK );
|
||||
if (err < 0)
|
||||
RageException::ThrowNonfatal("snd_pcm_open: %s", snd_strerror(err));
|
||||
RageException::ThrowNonfatal("dsnd_pcm_open: %s", dsnd_strerror(err));
|
||||
|
||||
if( !SetHWParams() )
|
||||
{
|
||||
snd_pcm_close(pcm);
|
||||
CHECKPOINT;
|
||||
dsnd_pcm_close(pcm);
|
||||
CHECKPOINT;
|
||||
RageException::ThrowNonfatal( "SetHWParams failed" );
|
||||
}
|
||||
|
||||
snd_pcm_sw_params_t *swparams;
|
||||
snd_pcm_sw_params_alloca( &swparams );
|
||||
snd_pcm_sw_params_current( pcm, swparams );
|
||||
dsnd_pcm_sw_params_alloca( &swparams );
|
||||
dsnd_pcm_sw_params_current( pcm, swparams );
|
||||
|
||||
xfer_align = snd_pcm_sw_params_get_xfer_align( swparams );
|
||||
err = dsnd_pcm_sw_params_get_xfer_align( swparams, &xfer_align );
|
||||
/* If this fails, we might have bound dsnd_pcm_sw_params_get_xfer_align to
|
||||
* the old SW API. */
|
||||
ASSERT( err <= 0 );
|
||||
ALSA_ASSERT("dsnd_pcm_sw_params_get_xfer_align");
|
||||
|
||||
/* Disable SND_PCM_STATE_XRUN. */
|
||||
snd_pcm_uframes_t boundary = 0;
|
||||
err = snd_pcm_sw_params_get_boundary( swparams, &boundary );
|
||||
ALSA_ASSERT("snd_pcm_sw_params_get_boundary");
|
||||
err = dsnd_pcm_sw_params_get_boundary( swparams, &boundary );
|
||||
ALSA_ASSERT("dsnd_pcm_sw_params_get_boundary");
|
||||
if( err == 0 )
|
||||
{
|
||||
err = snd_pcm_sw_params_set_stop_threshold( pcm, swparams, boundary );
|
||||
ALSA_ASSERT("snd_pcm_sw_params_set_stop_threshold");
|
||||
err = snd_pcm_sw_params(pcm, swparams);
|
||||
ALSA_ASSERT("snd_pcm_sw_params");
|
||||
err = dsnd_pcm_sw_params_set_stop_threshold( pcm, swparams, boundary );
|
||||
ALSA_ASSERT("dsnd_pcm_sw_params_set_stop_threshold");
|
||||
err = dsnd_pcm_sw_params(pcm, swparams);
|
||||
ALSA_ASSERT("dsnd_pcm_sw_params");
|
||||
}
|
||||
|
||||
err = snd_pcm_prepare(pcm);
|
||||
ALSA_ASSERT("snd_pcm_prepare");
|
||||
err = dsnd_pcm_prepare(pcm);
|
||||
ALSA_ASSERT("dsnd_pcm_prepare");
|
||||
|
||||
|
||||
/* 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);
|
||||
total_frames = dsnd_pcm_avail_update(pcm);
|
||||
}
|
||||
|
||||
Alsa9Buf::~Alsa9Buf()
|
||||
{
|
||||
snd_pcm_close(pcm);
|
||||
dsnd_pcm_close(pcm);
|
||||
}
|
||||
|
||||
|
||||
int Alsa9Buf::GetNumFramesToFill( int writeahead )
|
||||
{
|
||||
snd_pcm_sframes_t avail_frames = snd_pcm_avail_update(pcm);
|
||||
snd_pcm_sframes_t avail_frames = dsnd_pcm_avail_update(pcm);
|
||||
|
||||
if( avail_frames > total_frames )
|
||||
{
|
||||
@@ -212,16 +207,16 @@ int Alsa9Buf::GetNumFramesToFill( int writeahead )
|
||||
/* It's a large skip. Catch up. If we fall too far behind, the sound thread will
|
||||
* be decoding as fast as it can, which will steal too many cycles from the rendering
|
||||
* thread. */
|
||||
snd_pcm_forward( pcm, size );
|
||||
dsnd_pcm_forward( pcm, size );
|
||||
}
|
||||
}
|
||||
|
||||
if( avail_frames < 0 && Recover(avail_frames) )
|
||||
avail_frames = snd_pcm_avail_update(pcm);
|
||||
avail_frames = dsnd_pcm_avail_update(pcm);
|
||||
|
||||
if( avail_frames < 0 )
|
||||
{
|
||||
LOG->Trace( "RageSoundDriver_ALSA9::GetData: snd_pcm_avail_update: %s", snd_strerror(avail_frames) );
|
||||
LOG->Trace( "RageSoundDriver_ALSA9::GetData: dsnd_pcm_avail_update: %s", dsnd_strerror(avail_frames) );
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -236,10 +231,10 @@ int Alsa9Buf::GetNumFramesToFill( int writeahead )
|
||||
void Alsa9Buf::Write( const Sint16 *buffer, int frames )
|
||||
{
|
||||
/* We should be able to write it all. If we don't, treat it as an error. */
|
||||
int wrote = snd_pcm_mmap_writei( pcm, (const char *) buffer, frames );
|
||||
int wrote = dsnd_pcm_mmap_writei( pcm, (const char *) buffer, frames );
|
||||
if( wrote < 0 )
|
||||
{
|
||||
LOG->Trace( "RageSoundDriver_ALSA9::GetData: snd_pcm_mmap_writei: %s (%i)", snd_strerror(wrote), wrote );
|
||||
LOG->Trace( "RageSoundDriver_ALSA9::GetData: dsnd_pcm_mmap_writei: %s (%i)", dsnd_strerror(wrote), wrote );
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -259,8 +254,8 @@ bool Alsa9Buf::Recover( int r )
|
||||
if( r == -EPIPE )
|
||||
{
|
||||
LOG->Trace("RageSound_ALSA9::Recover (prepare)");
|
||||
int err = snd_pcm_prepare(pcm);
|
||||
ALSA_ASSERT("snd_pcm_prepare (Recover)");
|
||||
int err = dsnd_pcm_prepare(pcm);
|
||||
ALSA_ASSERT("dsnd_pcm_prepare (Recover)");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -268,10 +263,10 @@ bool Alsa9Buf::Recover( int r )
|
||||
{
|
||||
LOG->Trace("RageSound_ALSA9::Recover (resume)");
|
||||
int err;
|
||||
while ((err = snd_pcm_resume(pcm)) == -EAGAIN)
|
||||
while ((err = dsnd_pcm_resume(pcm)) == -EAGAIN)
|
||||
SDL_Delay(10);
|
||||
|
||||
ALSA_ASSERT("snd_pcm_resume (Recover)");
|
||||
ALSA_ASSERT("dsnd_pcm_resume (Recover)");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -280,17 +275,17 @@ bool Alsa9Buf::Recover( int r )
|
||||
|
||||
int Alsa9Buf::GetPosition() const
|
||||
{
|
||||
if( snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED )
|
||||
if( dsnd_pcm_state(pcm) == SND_PCM_STATE_PREPARED )
|
||||
{
|
||||
LOG->Trace("???");
|
||||
return 0;
|
||||
}
|
||||
|
||||
snd_pcm_hwsync( pcm );
|
||||
dsnd_pcm_hwsync( pcm );
|
||||
|
||||
/* delay is returned in frames */
|
||||
snd_pcm_sframes_t delay;
|
||||
int err = snd_pcm_delay( pcm, &delay );
|
||||
int err = dsnd_pcm_delay( pcm, &delay );
|
||||
|
||||
return last_cursor_pos - delay;
|
||||
}
|
||||
@@ -309,8 +304,8 @@ void Alsa9Buf::Play()
|
||||
|
||||
void Alsa9Buf::Stop()
|
||||
{
|
||||
snd_pcm_drop( pcm );
|
||||
snd_pcm_prepare( pcm );
|
||||
dsnd_pcm_drop( pcm );
|
||||
dsnd_pcm_prepare( pcm );
|
||||
}
|
||||
|
||||
void Alsa9Buf::SetSampleRate(int hz)
|
||||
@@ -319,6 +314,6 @@ void Alsa9Buf::SetSampleRate(int hz)
|
||||
|
||||
SetHWParams();
|
||||
|
||||
snd_pcm_prepare( pcm );
|
||||
dsnd_pcm_prepare( pcm );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user