Files
itgmania212121/stepmania/src/arch/Sound/ALSA9Helpers.cpp
T

305 lines
7.9 KiB
C++
Raw Normal View History

2003-10-21 22:51:25 +00:00
#include "global.h"
#include "RageLog.h"
2003-10-22 02:34:44 +00:00
#include "RageUtil.h"
2003-10-21 22:51:25 +00:00
#include "ALSA9Helpers.h"
#include "SDL_utils.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; }
2003-10-21 22:51:25 +00:00
#define ALSA_ASSERT(x) \
2003-10-22 02:34:44 +00:00
if (err < 0) { LOG->Trace("RageSound_ALSA9: ASSERT %s: %s (%i)", x, snd_strerror(err), err); }
2003-10-21 22:51:25 +00:00
bool Alsa9Buf::SetHWParams()
2003-10-21 22:51:25 +00:00
{
int err;
2003-10-22 02:34:44 +00:00
if( snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED )
snd_pcm_drop( pcm );
2003-10-22 02:34:44 +00:00
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))) );
2003-10-21 22:51:25 +00:00
/* allocate the hardware parameters structure */
snd_pcm_hw_params_t *hwparams;
2003-10-22 02:34:44 +00:00
snd_pcm_hw_params_alloca( &hwparams );
2003-10-21 22:51:25 +00:00
err = snd_pcm_hw_params_any(pcm, hwparams);
ALSA_CHECK("snd_pcm_hw_params_any");
2003-10-21 22:51:25 +00:00
/* 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");
2003-10-21 22:51:25 +00:00
/* 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");
2003-10-21 22:51:25 +00:00
/* set number of channels */
err = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
ALSA_CHECK("snd_pcm_hw_params_set_channels");
2003-10-21 22:51:25 +00:00
2003-10-22 02:34:44 +00:00
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");
2003-10-22 02:34:44 +00:00
if( (int) rate != samplerate )
LOG->Warn("Alsa9Buf::SetHWParams: Couldn't get %ihz (got %ihz instead)", samplerate, rate);
}
2003-10-21 22:51:25 +00:00
/* write the hardware parameters to the device */
err = snd_pcm_hw_params(pcm, hwparams);
ALSA_CHECK("snd_pcm_hw_params");
return true;
}
void Alsa9Buf::GetSoundCardDebugInfo()
{
static bool done = false;
if( done )
return;
done = true;
int card = -1;
while( snd_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");
if ( err < 0 )
{
LOG->Info( "Couldn't open card #%i (\"%s\"): %s", card, id.c_str(), snd_strerror(err) );
continue;
}
snd_ctl_card_info_t *info;
snd_ctl_card_info_alloca(&info);
err = snd_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 );
continue;
}
int dev = -1;
while ( snd_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);
err = snd_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));
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) );
snd_ctl_close(handle);
}
}
if( card == 0 )
LOG->Info( "No ALSA sound cards were found.");
2003-10-22 02:34:44 +00:00
}
2003-10-21 22:51:25 +00:00
2003-10-22 02:34:44 +00:00
Alsa9Buf::Alsa9Buf( hw hardware, int channels_, int samplerate_ )
{
GetSoundCardDebugInfo();
2003-10-22 02:34:44 +00:00
channels = channels_;
samplerate = samplerate_;
samplebits = 16;
last_cursor_pos = 0;
/* Open the device. */
int err;
2003-10-22 02:35:56 +00:00
// 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 );
2003-10-22 02:34:44 +00:00
if (err < 0)
RageException::ThrowNonfatal("snd_pcm_open: %s", snd_strerror(err));
if( !SetHWParams() )
{
snd_pcm_close(pcm);
RageException::ThrowNonfatal( "SetHWParams failed" );
}
2003-10-22 02:34:44 +00:00
snd_pcm_sw_params_t *swparams;
snd_pcm_sw_params_alloca( &swparams );
snd_pcm_sw_params_current( pcm, swparams );
2003-10-22 02:34:44 +00:00
/* 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");
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");
}
2003-10-21 22:51:25 +00:00
err = snd_pcm_prepare(pcm);
ALSA_ASSERT("snd_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);
}
Alsa9Buf::~Alsa9Buf()
{
snd_pcm_close(pcm);
}
2003-10-22 02:34:44 +00:00
int Alsa9Buf::GetNumFramesToFill( int writeahead )
2003-10-21 22:51:25 +00:00
{
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;
2003-10-22 02:34:44 +00:00
RAGE_ASSERT_M( filled_frames >= 0, ssprintf("total %i, %i avail", total_frames, avail_frames) );
2003-10-21 22:51:25 +00:00
const snd_pcm_sframes_t frames_to_fill = (writeahead - filled_frames) & ~3;
2003-10-22 02:34:44 +00:00
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);
2003-10-21 22:51:25 +00:00
// LOG->Trace("%li avail, %li filled, %li to", avail_frames, filled_frames, frames_to_fill);
return max( 0l, frames_to_fill );
}
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 );
if( wrote < 0 && Recover(wrote) )
wrote = snd_pcm_mmap_writei( pcm, (const char *) buffer, frames );
if( wrote < 0 )
{
2003-10-22 02:34:44 +00:00
LOG->Trace( "RageSoundDriver_ALSA9::GetData: snd_pcm_mmap_writei: %s (%i)", snd_strerror(wrote), wrote );
2003-10-21 22:51:25 +00:00
return;
}
last_cursor_pos += wrote;
if( wrote < frames )
LOG->Trace("Couldn't write whole buffer? (%i < %i)\n", wrote, frames );
}
/*
* When the play buffer underruns, subsequent writes to the buffer
* return -EPIPE. When this happens, call Recover() to restart playback.
*/
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)");
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;
}
int Alsa9Buf::GetPosition() const
{
2003-10-22 02:34:44 +00:00
if( snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED )
{
LOG->Trace("???");
2003-10-21 22:51:25 +00:00
return 0;
2003-10-22 02:34:44 +00:00
}
2003-10-21 22:51:25 +00:00
snd_pcm_hwsync( pcm );
2003-10-22 02:34:44 +00:00
2003-10-21 22:51:25 +00:00
/* delay is returned in frames */
2003-10-22 02:34:44 +00:00
snd_pcm_sframes_t delay;
int err = snd_pcm_delay( pcm, &delay );
2003-10-21 22:51:25 +00:00
return last_cursor_pos - delay;
}
2003-10-22 02:34:44 +00:00
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 );
}