4

I'm trying to generate short tones of variable lengths through ALSA in a small C program. A few examples I've tried work fine when playing one second worth of sound but anything shorter than that just doesn't produce any sound at all.

I'm filling a buffer with a sine wave like so:

#define BUFFER_LEN 44100
int freq = 700;             //audio frequency
int fs = 44100;             //sampling frequency
float buffer [BUFFER_LEN];
for (k=0; k<BUFFER_LEN; k++) {
   buffer[k] = (sin(2*M_PI*freq/fs*k));                                 
}

Setting the pcm device parameters:

if ((err = snd_pcm_set_params(handle,
                              SND_PCM_FORMAT_FLOAT,
                              SND_PCM_ACCESS_RW_INTERLEAVED,
                              1,
                              44100,
                              1,
                              500000)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}

Playback:

frames = snd_pcm_writei(handle, buffer, BUFFER_LEN);

But if I want to change it to play the tone, say a quarter of a second (i.e. changing BUFFER_LEN to 11025) nothing comes out of the speaker anymore.

I've tried changing types from floats to shorts, setting the PCM_FORMAT to other values and trying different ways of filling the buffer with sine waves.

If anything, I hear a little 'bip' sound in the speakers but just not what I expect. The program doesn't segfaults or crash but I'm just puzzled how to make ALSA play shorter samples.

I don't know if I need to work with some exact frame or buffer size multiple but I'm very open to suggestions.

5
  • Wild guess: it's due to having latency == 500000us longer than the playback length of 250000us. What happenbs with latency == 0? Commented Nov 3, 2016 at 10:28
  • What is your program doing after the last write call?
    – CL.
    Commented Nov 3, 2016 at 10:40
  • @Tom Goodfellow Wow, that worked! This is for a small morse-code application. I can write some beeps but if I run the program for the second time it doesn't always play. I think I need to flush the buffer or reset something.. not sure.. but it looks promising. Thanks.
    – captcha
    Commented Nov 3, 2016 at 10:43
  • @CL After the call I immediately do a snd_pcm_close(handle); Looks like the latency argument was the one that made it work. Can I just safely set that to zero?
    – captcha
    Commented Nov 3, 2016 at 10:44
  • The latency parameter controls the size of the buffer. Zero is not a valid value, and too-small values increase the risk of an underrun. If you know the amount of data you're going to write, try setting the latency to that amount of time.
    – CL.
    Commented Nov 3, 2016 at 11:48

1 Answer 1

3

Your application writing samples to the buffer, and the hardware reading samples from the buffer and playing them are two different processes that run asynchronously.

If there is not enough free space in the buffer for the amount of samples you're trying to write, then snd_pcm_writei() will wait until enough space is available. But when snd_pcm_writei() returns, up to a full buffer of samples might not yet have been played.

To wait until the buffer is empty, use snd_pcm_drain().

2
  • So would I use snd_pcm_drain() at the beginning of every write or at the end? Anyway, I have made some headway and by setting the latency to 10000 I am getting good playback. Although now I notice that any printf() commands to the console are blocked until I close the snd device..
    – captcha
    Commented Nov 4, 2016 at 10:39
  • 1
    You would use it when you want to wait for the buffer to empty.
    – CL.
    Commented Nov 4, 2016 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.