title: how to play short tones with ALSA
body: <p>I'm trying to generate short tones of variable lengths through <code>ALSA</code> 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.</p>
  
  <p>I'm filling a buffer with a sine wave like so:</p>
  
  <pre><code>#define BUFFER_LEN 44100
  int freq = 700;             //audio frequency
  int fs = 44100;             //sampling frequency
  float buffer [BUFFER_LEN];
  for (k=0; k&lt;BUFFER_LEN; k++) {
     buffer[k] = (sin(2*M_PI*freq/fs*k));                                 
  }
  </code></pre>
  
  <p>Setting the pcm device parameters:</p>
  
  <pre><code>if ((err = snd_pcm_set_params(handle,
                                SND_PCM_FORMAT_FLOAT,
                                SND_PCM_ACCESS_RW_INTERLEAVED,
                                1,
                                44100,
                                1,
                                500000)) &lt; 0) {
  printf("Playback open error: %s\n", snd_strerror(err));
  exit(EXIT_FAILURE);
  }
  </code></pre>
  
  <p>Playback:</p>
  
  <pre><code>frames = snd_pcm_writei(handle, buffer, BUFFER_LEN);
  </code></pre>
  
  <p>But if I want to change it to play the tone, say a quarter of a second (i.e. changing <code>BUFFER_LEN</code> to 11025) nothing comes out of the speaker anymore.</p>
  
  <p>I've tried changing types from floats to shorts, setting the <code>PCM_FORMAT</code> to other values and trying different ways of filling the buffer with sine waves.</p>
  
  <p>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.</p>
  
  <p>I don't know if I need to work with some exact frame or buffer size multiple but I'm very open to suggestions.</p>