All of lore.kernel.org
 help / color / mirror / Atom feed
* Playback - Overwrite buffer with silence
@ 2015-04-11 15:47 Nilhcraiv
  2015-04-11 20:05 ` Clemens Ladisch
  0 siblings, 1 reply; 14+ messages in thread
From: Nilhcraiv @ 2015-04-11 15:47 UTC (permalink / raw)
  To: alsa-devel

Hello,

I am working an application and I have problems playing the audio with 
ALSA. In my application, I receive the sound data, this data must be 
decoded to be reproduce. In this process, always obtain an 
under-runerror. To fix this problem, I followed the documentation i.e. I 
use snd_pcm_sw_params_set_silence_size() and 
snd_pcm_sw_params_set_silence_threshold() to overwrite the buffer with 
silence. --> 
http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m___s_w___params.html#gaeb4a335a16981b5ea3fa671946fbdca3 
but this does not work. Does the doc is wrong?

If I obtain the boundary for ring pointers in frames at the start 
(snd_pcm_sw_params_get_boundary), the boundary is always zero. If I 
obtain the boundary each time (in the received_data() function) it is 
more than the buffer size and I get error when I put it in 
snd_pcm_sw_params_set_silence_size(), ¿Why?, Is it correct to use the 
boundary value?, ¿How I can avoid under-run by putting silence if I have 
not data at time?

Here you can see a light version of my code (without error checking):

     #define FRAMES                 240
     #define PERIOD                  1*FRAMES
     #define BUFFER_SIZE         PERIOD*2
     #define CHANNELS             1
     #define SAMPLE_RATE        48000

     snd_pcm_hw_params_t *hwparams;
     snd_pcm_sw_params_t *swparams;
     int exact_rate,dir;
     snd_pcm_uframes_t boundary=0;

     snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
     snd_pcm_hw_params_alloca(&hwparams);
     snd_pcm_sw_params_alloca(&swparams);


     /*PlayBack hw */
     snd_pcm_open(&pcm_handle, "default", stream, 0);
     snd_pcm_hw_params_any(pcm_handle, hwparams);
     snd_pcm_hw_params_set_access(pcm_handle, hwparams, 
SND_PCM_ACCESS_RW_INTERLEAVED);
     snd_pcm_hw_params_set_format(pcm_handle, hwparams, 
SND_PCM_FORMAT_S16_LE);
     exact_rate = SAMPLE_RATE;
     snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &exact_rate, 0);
     snd_pcm_hw_params_set_channels(pcm_handle, hwparams, CHANNELS);
     snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, BUFFER_SIZE);
     snd_pcm_hw_params(pcm_handle, hwparams);

     /*Avoid under-run  sw*/
     snd_pcm_sw_params_current(pcm_handle, swparams);
     snd_pcm_sw_params_get_boundary(swparams, &boundary); /*Here, 
boundary is allways ZERO*/
     snd_pcm_sw_params_set_silence_threshold(pcm_handle, swparams, 0); 
/*According to the doc.*/
     snd_pcm_sw_params_set_silence_size(pcm_handle, swparams, boundary); 
/*According to the doc.*/
     snd_pcm_sw_params(pcm_handle,swparams);
     /*END Avoid under-run*/

     snd_pcm_prepare(pcm_handle);

     void received_data(void *t_data)/*short version of the function*/
     {
         short *decoded_data = NULL;
         int len=0;

         decoded_data = (short *)malloc(PERIOD * FRAME_SIZE);
         len = decoder(&decoded_data,t_data);

         snd_pcm_sw_params_get_boundary(swparams, &boundary); /*Here, 
boundary is always a large number*/
         snd_pcm_sw_params_set_silence_size(pcm_handle, swparams, 
boundary);/*Here, boundary in frames is always a large number, and there 
are an error...*/
         snd_pcm_sw_params(pcm_handle,swparams);

         pcmreturn = snd_pcm_writei(pcm_handle, decoded_data, len);/*In 
most cases there are UNDERRUN...*/
         xrun_recovery(&pcm_handle, pcmreturn);/*Recover in case of an 
error.*/
     }

Thanks in advance!
Vicente
_______________________________________________
Alsa-devel mailing list
Alsa-devel@alsa-project.org
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-11 15:47 Playback - Overwrite buffer with silence Nilhcraiv
@ 2015-04-11 20:05 ` Clemens Ladisch
       [not found]   ` <14caab5d6d0.2759.f6cded7c48a54b1428bcde82a72ee7d7@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread
From: Clemens Ladisch @ 2015-04-11 20:05 UTC (permalink / raw)
  To: Nilhcraiv, alsa-devel

Nilhcraiv wrote:
> If I obtain the boundary for ring pointers in frames at the start (snd_pcm_sw_params_get_boundary), the boundary is always zero.

It is impossible for the boundar to be zero.

> Here you can see a light version of my code (without error checking):

Error checking would be important.


Regards,
Clemens

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
       [not found]   ` <14caab5d6d0.2759.f6cded7c48a54b1428bcde82a72ee7d7@gmail.com>
@ 2015-04-11 23:15     ` Nilhcraiv
  2015-04-12  5:58       ` Clemens Ladisch
  0 siblings, 1 reply; 14+ messages in thread
From: Nilhcraiv @ 2015-04-11 23:15 UTC (permalink / raw)
  To: Clemens Ladisch, alsa-devel

Hello Clemens,

Fisrt of all thanks for your answer.

In my code there are error checking, of course. The code of this email 
thread is only just to simplify the question.

Clemens wrote:
It is impossible for the boundar to be zero.


I am sure, if you compile this code, "boundary" variable is always zero.
But the question is, How I can put silence data in buffer if the received 
data is not available at time? (to avoid under-run) Is the doc wrong?


#define FRAMES 240
#define PERIOD 1*FRAMES
#define BUFFER_SIZE PERIOD*2
#define CHANNELS 1
#define SAMPLE_RATE 48000

snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
int exact_rate,dir;
snd_pcm_uframes_t boundary=0;

snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_sw_params_alloca(&swparams);


/*PlayBack hw */
snd_pcm_open(&pcm_handle, "default", stream, 0);
snd_pcm_hw_params_any(pcm_handle, hwparams);
snd_pcm_hw_params_set_access(pcm_handle, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(pcm_handle, hwparams,
SND_PCM_FORMAT_S16_LE);
exact_rate = SAMPLE_RATE;
snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &exact_rate, 0);
snd_pcm_hw_params_set_channels(pcm_handle, hwparams, CHANNELS);
snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, BUFFER_SIZE);
snd_pcm_hw_params(pcm_handle, hwparams);

/*Avoid under-run sw*/
snd_pcm_sw_params_current(pcm_handle, swparams);
snd_pcm_sw_params_get_boundary(swparams, &boundary); /*Here,
boundary is allways ZERO*/
snd_pcm_sw_params_set_silence_threshold(pcm_handle, swparams, 0);
/*According to the doc.*/
snd_pcm_sw_params_set_silence_size(pcm_handle, swparams, boundary);
/*According to the doc.*/
snd_pcm_sw_params(pcm_handle,swparams);
/*END Avoid under-run*/

snd_pcm_prepare(pcm_handle);


Thanks!




Nilhcraiv wrote:
> If I obtain the boundary for ring pointers in frames at the start 
> (snd_pcm_sw_params_get_boundary), the boundary is always zero.

It is impossible for the boundar to be zero.

> Here you can see a light version of my code (without error checking):

Error checking would be important.


Regards,
Clemens

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-11 23:15     ` Nilhcraiv
@ 2015-04-12  5:58       ` Clemens Ladisch
       [not found]         ` <14cb331e1c8.2759.f6cded7c48a54b1428bcde82a72ee7d7@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread
From: Clemens Ladisch @ 2015-04-12  5:58 UTC (permalink / raw)
  To: Nilhcraiv, alsa-devel

Nilhcraiv wrote:
> In my code there are error checking, of course.

But probably not enough.


Regards,
Clemens

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
       [not found]         ` <14cb331e1c8.2759.f6cded7c48a54b1428bcde82a72ee7d7@gmail.com>
@ 2015-04-13 14:45           ` Nilhcraiv
  2015-04-14 12:37             ` Clemens Ladisch
  0 siblings, 1 reply; 14+ messages in thread
From: Nilhcraiv @ 2015-04-13 14:45 UTC (permalink / raw)
  To: Clemens Ladisch, alsa-devel

Clemens wrote:
    It is impossible for the boundar to be zero.

In my code snd_pcm_sw_params_get_boundary(swparams, &boundary) function 
does not return a negative value and the boundary variable is set to zero 
then, the boundar is zero.

In any case, How I can put silence in buffer to avoid underrun? Do you know 
the answer?

Thank you in advance.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-13 14:45           ` Nilhcraiv
@ 2015-04-14 12:37             ` Clemens Ladisch
  2015-04-14 17:29               ` Nilhcraiv
  0 siblings, 1 reply; 14+ messages in thread
From: Clemens Ladisch @ 2015-04-14 12:37 UTC (permalink / raw)
  To: Nilhcraiv, alsa-devel

Nilhcraiv wrote:
> Clemens wrote:
> > It is impossible for the boundar to be zero.
>
> In my code snd_pcm_sw_params_get_boundary(swparams, &boundary)
> function does not return a negative value

But an earlier function probably did.


Regards,
Clemens

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-14 12:37             ` Clemens Ladisch
@ 2015-04-14 17:29               ` Nilhcraiv
  2015-04-14 19:46                 ` Clemens Ladisch
  2015-04-15  3:14                 ` Raymond Yau
  0 siblings, 2 replies; 14+ messages in thread
From: Nilhcraiv @ 2015-04-14 17:29 UTC (permalink / raw)
  To: Clemens Ladisch, alsa-devel

Clemens wrote:
    But an earlier function probably did.

It is the first call to " snd_pcm_sw_params_get_boundary" function and it 
return zero. If I use the function after "writei" then, the boundary value 
is a large number. What boundary means when the value is a large number?

Regards,

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-14 17:29               ` Nilhcraiv
@ 2015-04-14 19:46                 ` Clemens Ladisch
  2015-04-15  3:14                 ` Raymond Yau
  1 sibling, 0 replies; 14+ messages in thread
From: Clemens Ladisch @ 2015-04-14 19:46 UTC (permalink / raw)
  To: Nilhcraiv, alsa-devel

Nilhcraiv wrote:
> Clemens wrote:
> > But an earlier function probably did.
>
> It is the first call to " snd_pcm_sw_params_get_boundary" function and it return zero.

There are lots of other function calls.


Regards,
Clemens

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-14 17:29               ` Nilhcraiv
  2015-04-14 19:46                 ` Clemens Ladisch
@ 2015-04-15  3:14                 ` Raymond Yau
       [not found]                   ` <552EBB90.8040707@gmail.com>
  1 sibling, 1 reply; 14+ messages in thread
From: Raymond Yau @ 2015-04-15  3:14 UTC (permalink / raw)
  To: Nilhcraiv; +Cc: ALSA Development Mailing List, Clemens Ladisch

>
> Clemens wrote:
>    But an earlier function probably did.
>
> It is the first call to " snd_pcm_sw_params_get_boundary" function and it
return zero. If I use the function after "writei" then, the boundary value
is a large number. What boundary means when the value is a large number?
>

You have to add snd_pcm_dump() after snd_pcm_hw_params() to dump those
paramters accepted by your default device

Not all default device support two periods per buffer as you did not
explicitly set periods or period_size

Most functions which you used can return error

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
       [not found]                   ` <552EBB90.8040707@gmail.com>
@ 2015-04-15 19:40                     ` Clemens Ladisch
  2015-04-16 18:32                       ` Nilhcraiv
  0 siblings, 1 reply; 14+ messages in thread
From: Clemens Ladisch @ 2015-04-15 19:40 UTC (permalink / raw)
  To: Nilhcraiv; +Cc: alsa-devel

Nilhcraiv wrote:
> To set the Buffer size and Periord size I have use the
> snd_pcm_hw_params_set_period_size_near() and
> snd_pcm_hw_params_set_buffer_size_near() functions. If I do not use
> the ...near() functions, I return errors.

The allowed values of the hardware parameters depend on the hardware's
capabilities.


Regards,
Clemens

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-15 19:40                     ` Clemens Ladisch
@ 2015-04-16 18:32                       ` Nilhcraiv
  2015-04-16 20:16                         ` Clemens Ladisch
  2015-04-17  0:57                         ` Raymond Yau
  0 siblings, 2 replies; 14+ messages in thread
From: Nilhcraiv @ 2015-04-16 18:32 UTC (permalink / raw)
  To: Clemens Ladisch; +Cc: alsa-devel

Clemens wrote:
> The allowed values of the hardware parameters depend on the hardware's
> capabilities.

OK but, overwrite the buffer with silence depend of software, no? Why 
the buffer is not overwrite with silence with this pcm setup? It is as 
documentation.

PCM:

ALSA <-> PulseAudio PCM I/O Plugin
Its setup is:
   stream       : PLAYBACK
   access       : RW_INTERLEAVED
   format       : S16_LE
   subformat    : STD
   channels     : 1
   rate         : 48000
   exact rate   : 48000 (48000/1)
   msbits       : 16
   buffer_size  : 720
   period_size  : 240
   period_time  : 5000
   tstamp_mode  : NONE
   period_step  : 1
   avail_min    : 240
   period_event : 0
   start_threshold  : 240
   stop_threshold   : 720
   silence_threshold: 0
   silence_size : 6485183463413514240
   boundary     : 6485183463413514240


Regards,

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-16 18:32                       ` Nilhcraiv
@ 2015-04-16 20:16                         ` Clemens Ladisch
  2015-04-17  0:57                         ` Raymond Yau
  1 sibling, 0 replies; 14+ messages in thread
From: Clemens Ladisch @ 2015-04-16 20:16 UTC (permalink / raw)
  To: Nilhcraiv; +Cc: alsa-devel

Nilhcraiv wrote:
> Why the buffer is not overwrite with silence with this pcm setup?
>
> ALSA <-> PulseAudio PCM I/O Plugin
> Its setup is:
>   silence_threshold: 0
>   silence_size : 6485183463413514240
>   boundary     : 6485183463413514240

I guess PulseAudio does not handle the silencing correctly.


Regards,
Clemens

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-16 18:32                       ` Nilhcraiv
  2015-04-16 20:16                         ` Clemens Ladisch
@ 2015-04-17  0:57                         ` Raymond Yau
  2015-04-22 18:13                           ` Nilhcraiv
  1 sibling, 1 reply; 14+ messages in thread
From: Raymond Yau @ 2015-04-17  0:57 UTC (permalink / raw)
  To: Nilhcraiv; +Cc: ALSA Development Mailing List, Clemens Ladisch

>>
>> The allowed values of the hardware parameters depend on the hardware's
>> capabilities.
>
>
> OK but, overwrite the buffer with silence depend of software, no? Why the
buffer is not overwrite with silence with this pcm setup? It is as
documentation.
>
> PCM:
>
> ALSA <-> PulseAudio PCM I/O Plugin
> Its setup is:
>   stream       : PLAYBACK
>   access       : RW_INTERLEAVED
>   format       : S16_LE
>   subformat    : STD
>   channels     : 1
>   rate         : 48000
>   exact rate   : 48000 (48000/1)
>   msbits       : 16
>   buffer_size  : 720
>   period_size  : 240
>   period_time  : 5000
>   tstamp_mode  : NONE
>   period_step  : 1
>   avail_min    : 240
>   period_event : 0
>   start_threshold  : 240
>   stop_threshold   : 720
>   silence_threshold: 0
>   silence_size : 6485183463413514240
>   boundary     : 6485183463413514240
>
>

Pulse plugin 's minimum periods is three.

len = decoder(&decoded_data,t_data);

pcmreturn = snd_pcm_writei(pcm_handle, decoded_data, len);/*In most cases
there are UNDERRUN...*/

If  len > 720 (buffer_size), you need to write three periods, start
playback,  receive data, peform decode while waiting for the sound
card/plugin playback one period and write another period

Pulseaudio is a sound server which mix several playback streams, silence
size may has no effect since the alsa sink may use different period
size/buffer size or disable period wakeup when using timer scheduling

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Playback - Overwrite buffer with silence
  2015-04-17  0:57                         ` Raymond Yau
@ 2015-04-22 18:13                           ` Nilhcraiv
  0 siblings, 0 replies; 14+ messages in thread
From: Nilhcraiv @ 2015-04-22 18:13 UTC (permalink / raw)
  To: Raymond Yau; +Cc: ALSA Development Mailing List, Clemens Ladisch

Raymind wrote:
> Pulseaudio is a sound server which mix several playback streams, 
> silence size may has no effect since the alsa sink may use different 
> period size/buffer size or disable period wakeup when using timer 
> scheduling 
Then, How I can avoid under-run? I mean, If I attempt to write data in a 
buffer and the under-run occurs, It takes a long time to recover the pcm 
and the data must be lost. ¿how I can put silence in buffer instead of 
lost data?

I have set the snd_pcm_sw_params_set_stop_threshold() to the boundary 
value also, but the under-run occurs

My Playback PCM now:

INFO: Using "default" device.
ALSA <-> PulseAudio PCM I/O Plugin
Its setup is:
   stream       : PLAYBACK
   access       : RW_INTERLEAVED
   format       : S16_LE
   subformat    : STD
   channels     : 2
   rate         : 48000
   exact rate   : 48000 (48000/1)
   msbits       : 16
   buffer_size  : 720
   period_size  : 240
   period_time  : 5000
   tstamp_mode  : NONE
   period_step  : 1
   avail_min    : 240
   period_event : 0
   start_threshold  : 720
   stop_threshold   : 6485183463413514240
   silence_threshold: 0
   silence_size : 6485183463413514240
   boundary     : 6485183463413514240


Regards,
_______________________________________________
Alsa-devel mailing list
Alsa-devel@alsa-project.org
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2015-04-22 18:13 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-11 15:47 Playback - Overwrite buffer with silence Nilhcraiv
2015-04-11 20:05 ` Clemens Ladisch
     [not found]   ` <14caab5d6d0.2759.f6cded7c48a54b1428bcde82a72ee7d7@gmail.com>
2015-04-11 23:15     ` Nilhcraiv
2015-04-12  5:58       ` Clemens Ladisch
     [not found]         ` <14cb331e1c8.2759.f6cded7c48a54b1428bcde82a72ee7d7@gmail.com>
2015-04-13 14:45           ` Nilhcraiv
2015-04-14 12:37             ` Clemens Ladisch
2015-04-14 17:29               ` Nilhcraiv
2015-04-14 19:46                 ` Clemens Ladisch
2015-04-15  3:14                 ` Raymond Yau
     [not found]                   ` <552EBB90.8040707@gmail.com>
2015-04-15 19:40                     ` Clemens Ladisch
2015-04-16 18:32                       ` Nilhcraiv
2015-04-16 20:16                         ` Clemens Ladisch
2015-04-17  0:57                         ` Raymond Yau
2015-04-22 18:13                           ` Nilhcraiv

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.