title: can't configure hardware parameters on ALSA raspberry pi c appliction
body: <p>I trying to write ALSA application for recording audio, and when I try to set some parameters and then print them to the screen I getting some default numbers that i cant change</p>
  
  <pre><code>#include &lt;alsa/asoundlib.h&gt;
  
  using namespace std;
  
  
  typedef struct {  
    int audio;
    int recording;
    void *cons;
    snd_pcm_t *inhandle;
    snd_pcm_t *outhandle;
    unsigned long sampleIndex;
    unsigned long inlen;
    unsigned long sampleRate;
  } audio_t;
  static audio_t aud;
  
  
  void aboutAlsa(snd_pcm_t *handle,snd_pcm_hw_params_t *params) {
    unsigned int val, val2;
    snd_pcm_format_t val3;
    int dir;
    snd_pcm_uframes_t frames;
  
    printf("ALSA library version: %s\n",SND_LIB_VERSION_STR);
    printf("PCM handle name = '%s'\n",snd_pcm_name(handle));
    printf("PCM state = %s\n",snd_pcm_state_name(snd_pcm_state(handle)));
    snd_pcm_hw_params_get_access(params,(snd_pcm_access_t *) &amp;val);
    printf("access type = %s\n",snd_pcm_access_name((snd_pcm_access_t)val));
    snd_pcm_hw_params_get_format(params, &amp;val3);
    printf("format = '%s' (%s)\n",snd_pcm_format_name(val3),
      snd_pcm_format_description(val3));
    snd_pcm_hw_params_get_subformat(params,(snd_pcm_subformat_t *)&amp;val);
    printf("subformat = '%s' (%s)\n",snd_pcm_subformat_name((snd_pcm_subformat_t)val),
      snd_pcm_subformat_description((snd_pcm_subformat_t)val));
    snd_pcm_hw_params_get_channels(params, &amp;val);
    printf("channels = %d\n", val);
    snd_pcm_hw_params_get_rate(params, &amp;val, &amp;dir);
    printf("rate = %d bps\n", val);
    snd_pcm_hw_params_get_period_time(params,&amp;val, &amp;dir);
    printf("period time = %d us\n", val);
    snd_pcm_hw_params_get_period_size(params,&amp;frames, &amp;dir);
    printf("period size = %d frames\n", (int)frames);
    snd_pcm_hw_params_get_buffer_time(params,&amp;val, &amp;dir);
    printf("buffer time = %d us\n", val);
    snd_pcm_hw_params_get_buffer_size(params,(snd_pcm_uframes_t *) &amp;val);
    printf("buffer size = %d frames\n", val);
    snd_pcm_hw_params_get_periods(params, &amp;val, &amp;dir);
    printf("periods per buffer = %d frames\n", val);
    snd_pcm_hw_params_get_rate_numden(params,&amp;val, &amp;val2);
    printf("exact rate = %d/%d bps\n", val, val2);
    val = snd_pcm_hw_params_get_sbits(params);
    printf("significant bits = %d\n", val);
    return;
  }
  
  static int openKnownAudio(int record) {
    int rc;
    int SAMPLERATE = 16000;
    unsigned int val;
    int dir=0;
    snd_pcm_t *handle;
    snd_pcm_hw_params_t *hw_params=NULL;
    snd_pcm_uframes_t frames;
    size_t esz = 256;
    char err[esz];
    /* Open PCM device for recording (capture). */ 
    if (record) {
      if ((rc=snd_pcm_open(&amp;aud.inhandle, "default",SND_PCM_STREAM_CAPTURE, 0))&lt;0) {
        snprintf(err, esz, "unable to open pcm device for recording: %s\n",snd_strerror(rc));
      }
      handle=aud.inhandle;
    } else {
      if ((rc=snd_pcm_open(&amp;aud.outhandle, "default",SND_PCM_STREAM_PLAYBACK, 0))&lt;0) {
        snprintf(err, esz, "unable to open pcm device for playback: %s\n",snd_strerror(rc));
      }
      handle=aud.outhandle;
    }
  
    /* Configure hardware parameters  */
    if((rc=snd_pcm_hw_params_malloc(&amp;hw_params)) &lt; 0) {
      snprintf(err, esz, "unable to malloc hw_params: %s\n",snd_strerror(rc));
    }
    if((rc=snd_pcm_hw_params_any(handle, hw_params))&lt;0) {
      snprintf(err, esz, "unable to setup hw_params: %s\n",snd_strerror(rc));
    }
    if((rc=snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED))&lt;0) {
      snprintf(err, esz, "unable to set access mode: %s\n",snd_strerror(rc));
    }
    if((rc=snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_S16_LE))&lt;0) {
      snprintf(err, esz, "unable to set format: %s\n",snd_strerror(rc));
    }
    if((rc=snd_pcm_hw_params_set_channels(handle, hw_params, 1))&lt;0) {
      snprintf(err, esz, "unable to set channels: %s\n",snd_strerror(rc));
    }
    val = SAMPLERATE;
    dir = 0;
    if((rc=snd_pcm_hw_params_set_rate(handle, hw_params, SAMPLERATE,0))&lt;0) {
      snprintf(err, esz, "unable to set samplerate: %s\n",snd_strerror(rc));
    }
    if (val!=SAMPLERATE) {
      snprintf(err, esz, "unable to set requested samplerate: requested=%i got=%i\n",SAMPLERATE,val);
    }
    frames = 64; 
    if ((rc=snd_pcm_hw_params_set_period_size_near(handle,hw_params, &amp;frames, &amp;dir))&lt;0) {
      snprintf(err, esz, "unable to set period size: %s\n",snd_strerror(rc));
    }
    frames = 4096; 
    if ((rc=snd_pcm_hw_params_set_buffer_size_near(handle,hw_params, &amp;frames))&lt;0) {
      snprintf(err, esz, "unable to set buffer size: %s\n",snd_strerror(rc));
    }
  
    if ((rc = snd_pcm_hw_params(handle, hw_params))&lt;0) {
      snprintf(err, esz, "unable to set hw parameters: %s\n",snd_strerror(rc));
    }
  
    aboutAlsa(handle,hw_params);
  
    snd_pcm_hw_params_free(hw_params);
    aud.recording = (record)? 1:0;
    aud.audio=1;
    return 1;
  }
  </code></pre>
  
  <p>This what I get on raspberry pi when I run it:</p>
  
  <pre><code>ALSA library version: 1.0.28
  PCM handle name = 'default'
  PCM state = PREPARED
  access type = RW_INTERLEAVED
  format = 'S16_LE' (Signed 16 bit Little Endian)
  subformat = 'STD' (Standard)
  channels = 1
  rate = 16000 bps
  period time = 21333 us
  period size = 341 frames
  buffer time = 256000 us
  buffer size = 4096 frames
  periods per buffer = 4096 frames
  exact rate = 16000/1 bps
  significant bits = 16
  </code></pre>
  
  <p>And this is what I get when I run it on desktop pc:</p>
  
  <pre><code>ALSA library version: 1.0.28
  PCM handle name = 'default'
  PCM state = PREPARED
  access type = RW_INTERLEAVED
  format = 'S16_LE' (Signed 16 bit Little Endian)
  subformat = 'STD' (Standard)
  channels = 1
  rate = 16000 bps
  period time = 4000 us
  period size = 64 frames
  buffer time = 256000 us
  buffer size = 4096 frames
  periods per buffer = 64 frames
  exact rate = 16000/1 bps
  significant bits = 16
  </code></pre>
  
  <p>As you can see I'm trying to set the period size to 64 and getting back 341, this value only changes when I change the rate, lets say I set the rate to 44100 and this what I getting back:</p>
  
  <pre><code>rate = 44100 bps
  period time = 21333 us
  period size = 940 frames
  buffer time = 85328 us
  buffer size = 3763 frames
  periods per buffer = 3763 frames
  </code></pre>
  
  <p>On desktop pc this doesn't happens I tried to trace down this functions in alsa-lib but I getting lost there also tried different sound cards and still getting same result .</p>