simpleaudio: handle any audio sample rate

This commit is contained in:
Kamal Mostafa 2012-08-11 10:07:12 -07:00
parent 500208ba81
commit 22317f54fc
6 changed files with 20 additions and 20 deletions

View File

@ -285,6 +285,9 @@ main( int argc, char*argv[] )
simpleaudio * (*simpleaudio_open_system_audio)() = NULL;
unsigned int sample_rate = 48000;
unsigned int nchannels = 1; // FIXME: only works with one channel
/* configure the default system audio mechanism */
#if USE_PULSEAUDIO
simpleaudio_open_system_audio = simpleaudio_open_stream_pulseaudio;
@ -511,6 +514,7 @@ main( int argc, char*argv[] )
tx_interactive = 0;
#if USE_SNDFILE
sa_out = simpleaudio_open_stream_sndfile(SA_STREAM_PLAYBACK,
sample_rate, nchannels,
filename);
#endif
if ( ! sa_out )
@ -519,6 +523,7 @@ main( int argc, char*argv[] )
if ( ! sa_out )
sa_out = simpleaudio_open_system_audio(SA_STREAM_PLAYBACK,
sample_rate, nchannels,
program_name, "output audio");
if ( ! sa_out )
return 1;
@ -540,7 +545,9 @@ main( int argc, char*argv[] )
simpleaudio *sa = NULL;
if ( filename ) {
#if USE_SNDFILE
sa = simpleaudio_open_stream_sndfile(SA_STREAM_RECORD, filename);
sa = simpleaudio_open_stream_sndfile(SA_STREAM_RECORD,
sample_rate, nchannels,
filename);
#endif
if ( ! sa )
return 1;
@ -548,15 +555,11 @@ main( int argc, char*argv[] )
if ( ! sa )
sa = simpleaudio_open_system_audio(SA_STREAM_RECORD,
sample_rate, nchannels,
program_name, "input audio");
if ( !sa )
return 1;
unsigned int sample_rate = simpleaudio_get_rate(sa);
unsigned int nchannels = simpleaudio_get_channels(sa);
assert( nchannels == 1 );
/*
* Prepare the input sample chunk rate

View File

@ -43,7 +43,7 @@ simpleaudio_tone(simpleaudio *sa_out, float tone_freq, size_t nsamples_dur)
if ( tone_freq != 0 ) {
float wave_nsamples = 48000.0 / tone_freq; // FIXME rate
float wave_nsamples = simpleaudio_get_rate(sa_out) / tone_freq;
size_t i;
for ( i=0; i<nsamples_dur; i++ )

View File

@ -104,13 +104,10 @@ static const struct simpleaudio_backend simpleaudio_backend_alsa = {
simpleaudio *
simpleaudio_open_stream_alsa(
// unsigned int rate, unsigned int channels,
int sa_stream_direction,
unsigned int rate, unsigned int channels,
char *app_name, char *stream_name )
{
unsigned int rate = 48000;
unsigned int channels = 1;
snd_pcm_t *pcm;
int error;

View File

@ -88,8 +88,8 @@ static const struct simpleaudio_backend simpleaudio_backend_pulse = {
simpleaudio *
simpleaudio_open_stream_pulseaudio(
// unsigned int rate, unsigned int channels,
int sa_stream_direction,
unsigned int rate, unsigned int channels,
char *app_name, char *stream_name )
{
int error;
@ -100,10 +100,8 @@ simpleaudio_open_stream_pulseaudio(
/* The sample type to use */
pa_sample_spec ss = {
.format = PA_SAMPLE_FLOAT32,
// .rate = rate,
.rate = 48000,
// .channels = channels,
.channels = 1,
.rate = rate,
.channels = channels,
};
pa_buffer_attr attr = {

View File

@ -135,13 +135,14 @@ sndfile_format_from_path( const char *path )
simpleaudio *
simpleaudio_open_stream_sndfile(
int sa_stream_direction,
unsigned int rate, unsigned int channels,
char *path )
{
/* setting for SA_STREAM_PLAYBACK (file write) */
SF_INFO sfinfo = {
.format = 0,
.samplerate = 48000,
.channels = 1,
.samplerate = rate,
.channels = channels,
};
if ( sa_stream_direction == SA_STREAM_PLAYBACK )

View File

@ -38,19 +38,20 @@ enum {
simpleaudio *
simpleaudio_open_stream_pulseaudio(
// unsigned int rate, unsigned int channels,
int sa_stream_direction,
unsigned int rate, unsigned int channels,
char *app_name, char *stream_name );
simpleaudio *
simpleaudio_open_stream_alsa(
// unsigned int rate, unsigned int channels,
int sa_stream_direction,
unsigned int rate, unsigned int channels,
char *app_name, char *stream_name );
simpleaudio *
simpleaudio_open_stream_sndfile(
int sa_stream_direction,
unsigned int rate, unsigned int channels,
char *path );
/*