title: ALSA - samplerate conversion
body: <p>I have a text-to-speech application, that generate an audio-stream (raw-data) with a samplerate of 22kHz.</p>
  
  <p>I have a USB-SoundCard that support only 44kHz.</p>
  
  <p>With my asound.conf I can play wav-files that contains 22kHz and 44kHz audio-stream without problems in aplay.</p>
  
  <p>My Application use the alsa-libs and set the samplerate of the device.
  In this case only 44kHz will succeed, because the hardware supports only this samplerate. But now, when i write the generated audio-stream to alsa, it sounds wrong, because the samplerates dosn't match. The audio-stream (raw-data) dosn't contain any header information, so I think alsa don't use any plugin zu convert the samplerate. Alsa don't know that the stream has a different samplerate</p>
  
  <p>My question is now, what is the right way to tell alsa, that the generated audio-stream have a different samplerate, so the alsa-plugin convert it.</p>
  
  <p>The following code works on the USB SoundCard only with <em>sampleRate = 44100</em>, otherwise an error occured (-22, invalid parameters).</p>
  
  <pre><code>void initAlsa()
  {
      const char* name = "default";
  
      alsaAudio = true;
      writeRiffAtClose = false;
      int err = snd_pcm_open (&amp;alsaPlaybackHandle, name, SND_PCM_STREAM_PLAYBACK, 0);
      if (err &lt; 0)
          throw TtsException({"Alsa: cannot open playback audio device ", name, " (" , snd_strerror (err), ")"}, 0);
  
      sampleRate = 44100;
      err = snd_pcm_set_params(alsaPlaybackHandle,                 // pcm PCM handle
                               SND_PCM_FORMAT_S16_LE,                 // format required PCM format
                               SND_PCM_ACCESS_RW_INTERLEAVED,      // access required PCM access
                               2,                                  // channels required PCM channels (Stereo)
                               sampleRate,                         // rate required sample rate in Hz
                               1,                                  // soft_resample 0 = disallow alsa-lib resample stream, 1 = allow resampling
                               250000);          /* 0.25sec */     // latency required overall latency in us
      if (err &lt; 0)
          throw TtsException({"Alsa: cannot set parameters (" , err, " = " , snd_strerror(err), ") on ", name}, 0);
  
      LOG_DEBUG("Alsa audio initialized");
  }
  </code></pre>
  
  <p>Other way are, I manuelly convert the sample rate, before i put it to alsa, but i think: why not use the alsa-plugin.</p>
  
  <p>I don't have the option to get 44kHz audio-stream from the tts-engine (it's another software).</p>
  
  <p>Or exist another way, that I don't see?</p>
  
  <p>Best regards.</p>