alsabat: add buffer size and period size setup support
PATCH 1/1 alsabat: add buffer size and period size setup support
From: "vivian,zhang" <[email protected]>
Signed-off-by Zhang Vivian <[email protected]>
--- bat/alsa.c | 95 +++++++++++++++++++++++++++++++++++++++----------------- bat/bat.c | 16 ++++++++-- bat/common.h | 2 ++ bat/tinyalsa.c | 5 ++- 4 files changed, 86 insertions(+), 32 deletions(-)
bat/alsa.c +66 -29
@@ -74,6 +74,8 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)snd_pcm_format_t format;unsigned int buffer_time =0 ;unsigned int period_time =0 ; +snd_pcm_uframes_t buffer_size =0 ; +snd_pcm_uframes_t period_size =0 ;unsigned int rate;int err;const char *device_name =snd_pcm_name (sndpcm->handle);
@@ -145,39 +147,71 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)return -EINVAL ; } -if (snd_pcm_hw_params_get_buffer_time_max (params, - &buffer_time,0 ) <0 ) { -fprintf (bat->err,_ ("Get parameter from device error: " )); -fprintf (bat->err,_ ("buffer time: %d %s: %s(%d)\n" ), - buffer_time, - device_name,snd_strerror (err), err); -return -EINVAL ; - } +if (bat->buffer_size >0 ) { +if (bat->period_size ==0 ) + bat->period_size = bat->buffer_size /DIV_BUFFERTIME ; -if (buffer_time >MAX_BUFFERTIME ) - buffer_time =MAX_BUFFERTIME ; + buffer_size = bat->buffer_size; + period_size = bat->period_size; - period_time = buffer_time /DIV_BUFFERTIME ; +fprintf (bat->log,_ ("Set period size: %d buffer size: %d\n" ), + (int ) period_size, (int ) buffer_size); -/* Set buffer time and period time */ - err =snd_pcm_hw_params_set_buffer_time_near (sndpcm->handle, params, - &buffer_time,0 ); -if (err <0 ) { -fprintf (bat->err,_ ("Set parameter to device error: " )); -fprintf (bat->err,_ ("buffer time: %d %s: %s(%d)\n" ), - buffer_time, - device_name,snd_strerror (err), err); -return err; - } + err =snd_pcm_hw_params_set_buffer_size_near (sndpcm->handle, + params, &buffer_size); +if (err <0 ) { +fprintf (bat->err,_ ("Set parameter to device error: " )); +fprintf (bat->err,_ ("buffer size: %d %s: %s(%d)\n" ), + (int ) buffer_size, + device_name,snd_strerror (err), err); +return err; + } - err =snd_pcm_hw_params_set_period_time_near (sndpcm->handle, params, - &period_time,0 ); -if (err <0 ) { -fprintf (bat->err,_ ("Set parameter to device error: " )); -fprintf (bat->err,_ ("period time: %d %s: %s(%d)\n" ), - period_time, - device_name,snd_strerror (err), err); -return err; + err =snd_pcm_hw_params_set_period_size_near (sndpcm->handle, + params, &period_size,0 ); +if (err <0 ) { +fprintf (bat->err,_ ("Set parameter to device error: " )); +fprintf (bat->err,_ ("period size: %d %s: %s(%d)\n" ), + (int ) period_size, + device_name,snd_strerror (err), err); +return err; + } + }else { +if (snd_pcm_hw_params_get_buffer_time_max (params, + &buffer_time,0 ) <0 ) { +fprintf (bat->err, +_ ("Get parameter from device error: " )); +fprintf (bat->err,_ ("buffer time: %d %s: %s(%d)\n" ), + buffer_time, + device_name,snd_strerror (err), err); +return -EINVAL ; + } + +if (buffer_time >MAX_BUFFERTIME ) + buffer_time =MAX_BUFFERTIME ; + + period_time = buffer_time /DIV_BUFFERTIME ; + +/* Set buffer time and period time */ + err =snd_pcm_hw_params_set_buffer_time_near (sndpcm->handle, + params, &buffer_time,0 ); +if (err <0 ) { +fprintf (bat->err,_ ("Set parameter to device error: " )); +fprintf (bat->err,_ ("buffer time: %d %s: %s(%d)\n" ), + buffer_time, + device_name,snd_strerror (err), err); +return err; + } + + err =snd_pcm_hw_params_set_period_time_near (sndpcm->handle, + params, &period_time,0 ); +if (err <0 ) { +fprintf (bat->err,_ ("Set parameter to device error: " )); +fprintf (bat->err,_ ("period time: %d %s: %s(%d)\n" ), + period_time, + device_name,snd_strerror (err), err); +return err; + } }/* Write the parameters to the driver */
@@ -214,6 +248,9 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)return -EINVAL ; } +fprintf (bat->log,_ ("Get period size: %d buffer size: %d\n" ), + (int ) sndpcm->period_size, (int ) sndpcm->buffer_size); + err =snd_pcm_format_physical_width (format);if (err <0 ) {fprintf (bat->err,_ ("Invalid parameters: " ));
bat/bat.c +14 -2
@@ -292,6 +292,8 @@ _("Usage: alsabat [-options]...\n"" -k parameter for frequency detecting threshold\n" " -F target frequency\n" " -p total number of periods to play/capture\n" +" -B buffer size in frames\n" +" -E period size in frames\n" " --log=# file that both stdout and strerr redirecting to\n" " --file=# file for playback\n" " --saveplay=# file that storing playback content, for debug\n"
@@ -324,6 +326,8 @@ static void set_defaults(struct bat *bat) bat->capture.device =NULL ; bat->buf =NULL ; bat->local = false; + bat->buffer_size =0 ; + bat->period_size =0 ;#ifdef HAVE_LIBTINYALSA bat->channels =2 ; bat->playback.fct = &playback_tinyalsa;
@@ -342,8 +346,8 @@ static void set_defaults(struct bat *bat)static void parse_arguments (struct bat *bat,int argc,char *argv[]) { -int c, option_index; -static const char short_options[] ="D:P:C:f:n:F:c:r:s:k:p:lth" ; +int c, option_index, err; +static const char short_options[] ="D:P:C:f:n:F:c:r:s:k:p:B:E:lth" ;static const struct option long_options[] = { {"help" ,0 ,0 ,'h' }, {"log" ,1 ,0 ,OPT_LOG },
@@ -414,6 +418,14 @@ static void parse_arguments(struct bat *bat, int argc, char *argv[]) bat->periods_total =atoi (optarg); bat->period_is_limited = true;break ; +case 'B' : + err =atoi (optarg); + bat->buffer_size = err >=32 && err <200000 ? err :0 ; +break ; +case 'E' : + err =atoi (optarg); + bat->period_size = err >=32 && err <200000 ? err :0 ; +break ;case 'h' :default :usage (bat);
bat/common.h +2 -0
@@ -152,6 +152,8 @@ struct bat {int frame_size;/* size of frame */ int sample_size;/* size of sample */ enum _bat_pcm_format format;/* PCM format */ +int buffer_size;/* buffer size in frames */ +int period_size;/* period size in frames */ float sigma_k;/* threshold for peak detection */ float target_freq[MAX_CHANNELS ];
bat/tinyalsa.c +4 -1
@@ -57,7 +57,10 @@ static int init_config(struct bat *bat, struct pcm_config *config) { config->channels = bat->channels; config->rate = bat->rate; - config->period_size =1024 ; +if (bat->period_size >0 ) + config->period_size = bat->period_size; +else + config->period_size =1024 ; config->period_count =4 ; config->start_threshold =0 ; config->stop_threshold =0 ;
Signature · 1 line
1.7.9.5
Quoted 2 lines · On Mon, 2016-05-30 at 17:07 +0800, [email protected] wrote:
From: "vivian,zhang" <[email protected]>
Commit message ?
Quoted 25 lines
Signed-off-by Zhang Vivian <[email protected]>--- bat/alsa.c | 95 +++++++++++++++++++++++++++++++++++++++----------------- bat/bat.c | 16 ++++++++-- bat/common.h | 2 ++ bat/tinyalsa.c | 5 ++- 4 files changed, 86 insertions(+), 32 deletions(-)bat/alsa.c +2
-0@@ -74,6 +74,8 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)snd_pcm_format_t format;unsigned int buffer_time =0 ;unsigned int period_time =0 ; +snd_pcm_uframes_t buffer_size =0 ; +snd_pcm_uframes_t period_size =0 ;unsigned int rate;int err;const char *device_name =snd_pcm_name (sndpcm->handle);@@ -145,39 +147,71 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)return -EINVAL ; }
bat/alsa.c static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm) · line 151
Quoted 10 lines
bat/alsa.c +2
-8-if (snd_pcm_hw_params_get_buffer_time_max (params, - &buffer_time,0 ) <0 ) { -fprintf (bat->err,_ ("Get parameter from device error: " )); -fprintf (bat->err,_ ("buffer time: %d %s: %s(%d)\n" ), - buffer_time, - device_name,snd_strerror (err), err); -return -EINVAL ; - } +if (bat->buffer_size >0 ) { +if (bat->period_size ==0 )
It might be possible to if (a > 0 && b == 0) to save a level of indentation here.
Quoted 131 lines
bat/alsa.c +62
-21+ bat->period_size = bat->buffer_size /DIV_BUFFERTIME ; -if (buffer_time >MAX_BUFFERTIME ) - buffer_time =MAX_BUFFERTIME ; + buffer_size = bat->buffer_size; + period_size = bat->period_size; - period_time = buffer_time /DIV_BUFFERTIME ; +fprintf (bat->log,_ ("Set period size: %d buffer size: %d\n" ), + (int ) period_size, (int ) buffer_size); -/* Set buffer time and period time */ - err =snd_pcm_hw_params_set_buffer_time_near (sndpcm->handle, params, - &buffer_time,0 ); -if (err <0 ) { -fprintf (bat->err,_ ("Set parameter to device error: " )); -fprintf (bat->err,_ ("buffer time: %d %s: %s(%d)\n" ), - buffer_time, - device_name,snd_strerror (err), err); -return err; - } + err =snd_pcm_hw_params_set_buffer_size_near (sndpcm->handle, + params, &buffer_size); +if (err <0 ) { +fprintf (bat->err,_ ("Set parameter to device error: " )); +fprintf (bat->err,_ ("buffer size: %d %s: %s(%d)\n" ), + (int ) buffer_size, + device_name,snd_strerror (err), err); +return err; + } - err =snd_pcm_hw_params_set_period_time_near (sndpcm->handle, params, - &period_time,0 ); -if (err <0 ) { -fprintf (bat->err,_ ("Set parameter to device error: " )); -fprintf (bat->err,_ ("period time: %d %s: %s(%d)\n" ), - period_time, - device_name,snd_strerror (err), err); -return err; + err =snd_pcm_hw_params_set_period_size_near (sndpcm->handle, + params, &period_size,0 ); +if (err <0 ) { +fprintf (bat->err,_ ("Set parameter to device error: " )); +fprintf (bat->err,_ ("period size: %d %s: %s(%d)\n" ), + (int ) period_size, + device_name,snd_strerror (err), err); +return err; + } + }else { +if (snd_pcm_hw_params_get_buffer_time_max (params, + &buffer_time,0 ) <0 ) { +fprintf (bat->err, +_ ("Get parameter from device error: " )); +fprintf (bat->err,_ ("buffer time: %d %s: %s(%d)\n" ), + buffer_time, + device_name,snd_strerror (err), err); +return -EINVAL ; + } + +if (buffer_time >MAX_BUFFERTIME ) + buffer_time =MAX_BUFFERTIME ; + + period_time = buffer_time /DIV_BUFFERTIME ; + +/* Set buffer time and period time */ + err =snd_pcm_hw_params_set_buffer_time_near (sndpcm->handle, + params, &buffer_time,0 ); +if (err <0 ) { +fprintf (bat->err,_ ("Set parameter to device error: " )); +fprintf (bat->err,_ ("buffer time: %d %s: %s(%d)\n" ), + buffer_time, + device_name,snd_strerror (err), err); +return err; + } + + err =snd_pcm_hw_params_set_period_time_near (sndpcm->handle, + params, &period_time,0 ); +if (err <0 ) { +fprintf (bat->err,_ ("Set parameter to device error: " )); +fprintf (bat->err,_ ("period time: %d %s: %s(%d)\n" ), + period_time, + device_name,snd_strerror (err), err); +return err; + } }/* Write the parameters to the driver */ @@ -214,6 +248,9 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)return -EINVAL ; } +fprintf (bat->log,_ ("Get period size: %d buffer size: %d\n" ), + (int ) sndpcm->period_size, (int ) sndpcm->buffer_size); + err =snd_pcm_format_physical_width (format);if (err <0 ) {fprintf (bat->err,_ ("Invalid parameters: " ));bat/bat.c +6
-2@@ -292,6 +292,8 @@ _("Usage: alsabat [-options]...\n"" -k parameter for frequency detecting threshold\n" " -F target frequency\n" " -p total number of periods to play/capture\n" +" -B buffer size in frames\n" +" -E period size in frames\n" " --log=# file that both stdout and strerr redirecting to\n" " --file=# file for playback\n" " --saveplay=# file that storing playback content, for debug\n" @@ -324,6 +326,8 @@ static void set_defaults(struct bat *bat) bat->capture.device =NULL ; bat->buf =NULL ; bat->local = false; + bat->buffer_size =0 ; + bat->period_size =0 ;#ifdef HAVE_LIBTINYALSA bat->channels =2 ; bat->playback.fct = &playback_tinyalsa;@@ -342,8 +346,8 @@ static void set_defaults(struct bat *bat)static void parse_arguments (struct bat *bat,int argc,char *argv[]) { -int c, option_index; -static const char short_options[] ="D:P:C:f:n:F:c:r:s:k:p:lth" ; +int c, option_index, err; +static const char short_options[] ="D:P:C:f:n:F:c:r:s:k:p:B:E:lth" ;static const struct option long_options[] = { {"help" ,0 ,0 ,'h' }, {"log" ,1 ,0 ,OPT_LOG },
bat/bat.c static void parse_arguments(struct bat *bat, int argc, char *argv[]) · line 423
Quoted 6 lines
bat/bat.c +3
-0@@ -414,6 +418,14 @@ static void parse_arguments(struct bat *bat, int argc, char *argv[]) bat->periods_total =atoi (optarg); bat->period_is_limited = true;break ; +case 'B' : + err =atoi (optarg); + bat->buffer_size = err >=32 && err <200000 ? err :0 ;
Probably best to have a macro for min and max values here and below.
Quoted 26 lines
bat/bat.c +5
-0+break ; +case 'E' : + err =atoi (optarg); + bat->period_size = err >=32 && err <200000 ? err :0 ; +break ;case 'h' :default :usage (bat);bat/common.h +2
-0@@ -152,6 +152,8 @@ struct bat {int frame_size;/* size of frame */ int sample_size;/* size of sample */ enum _bat_pcm_format format;/* PCM format */ +int buffer_size;/* buffer size in frames */ +int period_size;/* period size in frames */ float sigma_k;/* threshold for peak detection */ float target_freq[MAX_CHANNELS ];bat/tinyalsa.c +0
-0
bat/tinyalsa.c static int init_config(struct bat *bat, struct pcm_config *config) · line 63
Quoted 8 lines
bat/tinyalsa.c +4
-1@@ -57,7 +57,10 @@ static int init_config(struct bat *bat, struct pcm_config *config) { config->channels = bat->channels; config->rate = bat->rate; - config->period_size =1024 ; +if (bat->period_size >0 ) + config->period_size = bat->period_size; +else + config->period_size =1024 ;
Probably best to also have a macro for default period size too.
bat/tinyalsa.c static int init_config(struct bat *bat, struct pcm_config *config) · line 66
Quoted 3 lines
bat/tinyalsa.c +0
-0config->period_count =4 ; config->start_threshold =0 ; config->stop_threshold =0 ;
Liam