title: snd_pcm_hw_params_set_period_size_near doesn't return the good value
body: <p>I'm trying to encalpsulate alsa inside a class.
  The problem is when i try to use :</p>
  
  <pre><code>snd_pcm_hw_params_get_period_size(this-&gt;__params, &amp;period_size, NULL);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>snd_pcm_hw_params_get_period_time(this-&gt;__params, &amp;time_period, NULL);
  </code></pre>
  
  <p>inside <code>void alsa_control::record_to_file(std::string filename, int duration_in_us);</code></p>
  
  <p>in the cpp it send 0 in period and seems to be unmodified for period_time</p>
  
  <p>The .h</p>
  
  <pre><code>#ifndef ALSA_RECORDING_H
  #define ALSA_RECORDING_H
  
  #include &lt;iostream&gt;
  #include &lt;alsa/asoundlib.h&gt;
  #include &lt;wav_functions.h&gt;
  
  #define STEREO 2
  #define MONO 1
  
  using std::cout;
  using std::endl;
  
  class alsa_control {
  public:
  
      void record_to_file(std::string filename, int duration_in_us);
  
      alsa_control(unsigned int rate, unsigned long frames, int bits, unsigned int stereo_mode);
      ~alsa_control();
  
  private:
      unsigned int __rate;
      unsigned int __stereo_mode;
      int __bits;
      snd_pcm_uframes_t __frames;
      snd_pcm_hw_params_t *__params;
      snd_pcm_t *__handle;
  
      void open_pcm_device();
      void set_parameters_ALSA();
  
      alsa_control()=delete;
      alsa_control(const alsa_control&amp;)=delete;
  
  };
  
  #endif /* ALSA_RECORDING_H */
  </code></pre>
  
  <p>and the .cpp</p>
  
  <pre><code>#include &lt;alsa_control.h&gt;
  
  alsa_control::alsa_control(unsigned int rate, unsigned long frames, int bits, unsigned int stereo_mode)
          : __rate(rate),
            __stereo_mode(stereo_mode),
            __bits(bits),
            __frames(frames) {
  
      this-&gt;open_pcm_device();
      snd_pcm_hw_params_alloca(&amp;this-&gt;__params);
      this-&gt;set_parameters_ALSA();
  }
  
  alsa_control::~alsa_control() {
      snd_pcm_drain(this-&gt;__handle);
      snd_pcm_close(this-&gt;__handle);
  }
  
  void alsa_control::open_pcm_device() {
      int rc = snd_pcm_open(&amp;this-&gt;__handle, "default", SND_PCM_STREAM_CAPTURE, 0);
  
      if (rc &lt; 0) {
          cout &lt;&lt; "ERROR :  unable to open pcm device: " &lt;&lt; snd_strerror(rc) &lt;&lt; endl;
          exit(1);
      }
  }
  
  void alsa_control::set_parameters_ALSA() {
      snd_pcm_hw_params_any(this-&gt;__handle, this-&gt;__params); // def values
      snd_pcm_hw_params_set_access(this-&gt;__handle, this-&gt;__params, SND_PCM_ACCESS_RW_NONINTERLEAVED); //non interleaved
      snd_pcm_hw_params_set_format(this-&gt;__handle, this-&gt;__params, SND_PCM_FORMAT_S16_LE); //16bits little-endian
      snd_pcm_hw_params_set_channels(this-&gt;__handle, this-&gt;__params, this-&gt;__stereo_mode); // stereo ou mono
  
  
      snd_pcm_hw_params_set_rate_near(this-&gt;__handle, this-&gt;__params, &amp;this-&gt;__rate, NULL); // sample rate (freq echantillonage)
      auto ret = snd_pcm_hw_params_set_period_size_near(this-&gt;__handle, this-&gt;__params, &amp;this-&gt;__frames, NULL); //frames pour une période
  
      int rc = snd_pcm_hw_params(this-&gt;__handle, this-&gt;__params);
      if (rc &lt; 0) {
          cout &lt;&lt; "ERROR - unable to set hw parameters: " &lt;&lt; snd_strerror(rc) &lt;&lt; endl;
          exit(1);
      }
  }
  
  void alsa_control::record_to_file(std::string filename, int duration_in_us) {
      std::ofstream f;
      int rc;
      int nb_ech = 0;
      snd_pcm_uframes_t period_size;
      unsigned int time_period;
  
  
      filename += ".wav";
      f.open(filename, std::ios::binary);
      write_header_wav(f, this-&gt;__rate, (short) this-&gt;__bits, (short) this-&gt;__stereo_mode, 10000); //10000 is a constant because we don't know the size of the recording
  
      snd_pcm_hw_params_get_period_size(this-&gt;__params, &amp;period_size, NULL);
      period_size = 2048;
      snd_pcm_uframes_t size = period_size * 2; /* 2 bytes/sample, 1 channels */
      char *buffer = (char *) malloc(size);
  
  
      snd_pcm_hw_params_get_period_time(this-&gt;__params, &amp;time_period, NULL);
      time_period = 128000;
      long loops = duration_in_us / time_period;
  
      while (loops-- &gt; 0) {
          rc = (int) snd_pcm_readi(this-&gt;__handle, buffer, period_size);
          if (rc == -EPIPE) {
              cout &lt;&lt; "ERROR - overrun occurred" &lt;&lt; endl;
              snd_pcm_prepare(this-&gt;__handle);
          } else if (rc &lt; 0) {
              cout &lt;&lt; "ERROR - error from read: " &lt;&lt; snd_strerror(rc) &lt;&lt; endl;
          } else if (rc != (int) period_size) {
              cout &lt;&lt; "ERROR - short read, read " &lt;&lt; rc &lt;&lt; " frames" &lt;&lt; endl;
          }
  
          if (!(loops % 10))cout &lt;&lt; loops &lt;&lt; endl;
  
          f.write(buffer, rc * 2);
          nb_ech += rc;
      }
  
      f.close();
      f.open(filename, std::ios::binary | std::ios::in);
      write_header_wav(f, this-&gt;__rate, (short) this-&gt;__bits, (short) this-&gt;__stereo_mode, nb_ech);
      f.close();
      free(buffer);
  }
  </code></pre>