All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nilhcraiv <nilhcraiv@gmail.com>
To: alsa-devel@alsa-project.org
Subject: Playback - Overwrite buffer with silence
Date: Sat, 11 Apr 2015 17:47:39 +0200	[thread overview]
Message-ID: <5529421B.7050601@gmail.com> (raw)

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

             reply	other threads:[~2015-04-11 15:47 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-11 15:47 Nilhcraiv [this message]
2015-04-11 20:05 ` Playback - Overwrite buffer with silence 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5529421B.7050601@gmail.com \
    --to=nilhcraiv@gmail.com \
    --cc=alsa-devel@alsa-project.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.