title: RtAudio - Playing samples from wav file
body: <p>I am currently trying to learn audio programming. My goal is to open a wav file, extract everything and play the samples with RtAudio.</p>
  
  <p>I made a WaveLoader class which let's me extract the samples and meta data. I used <a href="http://soundfile.sapp.org/doc/WaveFormat/" rel="nofollow noreferrer">this</a> guide to do that and I checked that everything is correct with 010 editor. Here is a snapshot of 010 editor showing the structure and data.</p>
  
  <p><a href="https://i.stack.imgur.com/APWao.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/APWao.png" alt="010 Editor"></a></p>
  
  <p>And this is how i store the raw samples inside WaveLoader class:</p>
  
  <pre><code>        data = new short[wave_data.payloadSize]; // - Allocates memory size of chunk size
  
          if (!fread(data, 1, wave_data.payloadSize, sound_file))
          {
              throw ("Could not read wav data");
          }
  </code></pre>
  
  <p>If i print out each sample I get : 1, -3, 4, -5 ... which seems ok.</p>
  
  <p>The problem is that I am not sure how I can play them. This is what I've done:</p>
  
  <pre><code>/*
   * Using PortAudio to play samples
   */
  bool Player::Play() 
  {
      ShowDevices();
      rt.showWarnings(true);
  
      RtAudio::StreamParameters oParameters; //, iParameters;
      oParameters.deviceId = rt.getDefaultOutputDevice();
      oParameters.firstChannel = 0;
      oParameters.nChannels = mAudio.channels;
  
      //iParameters.deviceId = rt.getDefaultInputDevice();
      //iParameters.nChannels = 2;
  
      unsigned int sampleRate = mAudio.sampleRate;
  
      // Use a buffer of 512, we need to feed callback with 512 bytes everytime!
      unsigned int nBufferFrames = 512;
  
      RtAudio::StreamOptions options;
      options.flags = RTAUDIO_SCHEDULE_REALTIME;
      options.flags = RTAUDIO_NONINTERLEAVED;
  
      //&amp;parameters, NULL, RTAUDIO_FLOAT64,sampleRate, &amp;bufferFrames, &amp;mCallback, (void *)&amp;rawData
  
      try {
          rt.openStream(&amp;oParameters, NULL, RTAUDIO_SINT16, sampleRate, &amp;nBufferFrames, &amp;mCallback, (void*) &amp;mAudio);
          rt.startStream();
      }
      catch (RtAudioError&amp; e) {
          std::cout &lt;&lt; e.getMessage() &lt;&lt; std::endl;
          return false;
      }
      return true;
  }
  
  /*
  * RtAudio Callback
  *
  */
  int mCallback(void * outputBuffer, void * inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void * userData)
  {
      unsigned int i = 0;
      short *out = static_cast&lt;short*&gt;(outputBuffer);
      auto *data = static_cast&lt;Player::AUDIO_DATA*&gt;(userData);
  
      // if i is more than our data size, we are done!
      if (i &gt; data-&gt;dataSize) return 1;
  
      // First time callback is called data-&gt;ptr is 0, this means that the offset is 0
      // Second time data-&gt;ptr is 1, this means offset = nBufferFrames (512) * 1 = 512
      unsigned int offset = nBufferFrames * data-&gt;ptr++;
  
      printf("Offset: %i\n", offset);
      // First time callback is called offset is 0, we are starting from 0 and looping nBufferFrames (512) times, this gives us 512 bytes
      // Second time, the offset is 1, we are starting from 512 bytes and looping to 512 + 512 = 1024 
      for (i = offset; i &lt; offset + nBufferFrames; ++i)
      {
          short sample = data-&gt;rawData[i]; // Get raw sample from our struct
          *out++ = sample;                // Pass to output buffer for playback
  
          printf("Current sample value: %i\n", sample);       // this is showing 1, -3, 4, -5 check 010 editor
      }
  
      printf("Current time: %f\n", streamTime);
      return 0;
  }
  </code></pre>
  
  <p>Inside callback function, when I print out sample values I get exactly like 010 editor? Why isnt rtaudio playing them. What is wrong here? Do I need to normalize sample values to between -1 and 1?</p>
  
  <p>Edit:
  The wav file I am trying to play:</p>
  
  <ul>
  <li>Chunksize: 16</li>
  <li>Format: 1</li>
  <li>Channel: 1</li>
  <li>SampleRate: 48000</li>
  <li>ByteRate: 96000</li>
  <li>BlockAlign: 2</li>
  <li>BitPerSample: 16</li>
  <li>Size of raw samples total: 2217044 bytes</li>
  </ul>