title: Creating WAV file from data in real time - need assist
body: <p>I am quite new to audio processing and my C++ programming skills are also on basic level. I have to create recording function and I receive data signal from device in form of callback. I would like someone to assist me with basic theory how should I manage WAV memory because I am lost. For now my code looks like this:</p>
  
  <p>Functions I am currently using:</p>
  
  <pre><code>template &lt;typename T&gt;
  void write(std::ofstream&amp; stream, const T&amp; t) {
    stream.write((const char*)&amp;t, sizeof(T));
  }
  
  template &lt;typename T&gt;
  void writeFormat(std::ofstream&amp; stream) {
    write&lt;short&gt;(stream, 1);
  }
  
  template &lt;&gt;
  void writeFormat&lt;float&gt;(std::ofstream&amp; stream) {
    write&lt;short&gt;(stream, 3);
  }
  
  template &lt;typename SampleType&gt;
  void writeWAVData(
    char const* outFile,
    SampleType* buf,
    size_t bufSize,
    int sampleRate,
    short channels)
  {
    std::ofstream stream(outFile, std::ios::binary|ios::app|ios::ate);
  
    stream.write("RIFF", 4);                                        // Start writting RIFF
    write&lt;int&gt;(stream, 0);                                          // (file-size)-8 - FOR NOW IGNORED
    stream.write("WAVE", 4);                                        // File type
  
    stream.write("fmt ", 4);                                        // Start Writting format chunk "fmt"
    write&lt;int&gt;(stream, 16);                                         // Chunk Data Size 16 + extra format bytes
    writeFormat&lt;SampleType&gt;(stream);                                // Format (compression code)
    write&lt;short&gt;(stream, channels);                                 // Channels
    write&lt;int&gt;(stream, sampleRate);                                 // Sample Rate
    write&lt;int&gt;(stream, sampleRate * channels * sizeof(SampleType)); // Byterate (byte/per sec)
    write&lt;short&gt;(stream, channels * sizeof(SampleType));            // Frame size (block align)
    write&lt;short&gt;(stream, 8 * sizeof(SampleType));                   // Bits per sample 
  
    stream.write("data", 4);                                        // Start writting chunk for extra format bytes
    stream.write((const char*)&amp;bufSize, 0);                         //  - FOR NOW IGNORED
    stream.write((const char*)buf, 0);                              //  - FOR NOW IGNORED
  }
  </code></pre>
  
  <p>General idea what I want to do is:</p>
  
  <ol>
  <li>Create *.wav file.</li>
  <li>Write header chunk.</li>
  <li>Add new data at the end of file as long user doesn't stop it.</li>
  <li>When user clicks stop, go back to the header memory and edit field under "RIFF" and "data" chunks with right value.</li>
  </ol>
  
  <p>First Question: So I have buffer storing 256 samples so in loop, every 256 I try to write buffer with new data to the file like in code below. After reading *.wav documentaiton <a href="http://www.sonicspot.com/guide/wavefiles.html" rel="nofollow">http://www.sonicspot.com/guide/wavefiles.html</a> I understand that *.wav file should have RIFF header at start of each chunk. But if I write buffer with 256 samples and place RIFF chunk before it what's difference if I would enter 512 samples instead? Does every sample need RIFF chunk before it?</p>
  
  <pre><code>for (int j = 0 ; j &lt; 256 ; j++)
  {
      if (j == 255) 
          writeWAVData("mySound0.wav", (int*)bufferInfos[0].buffers[doubleBufferIndex], buffSize, 44100, 1);
  }
  </code></pre>
  
  <p>Where:</p>
  
  <ul>
  <li>bufferInfos[] - holds table of various buffers but I want to save data from only one now</li>
  <li>bufferInfos[0].buffers[doubleBufferIndex] - is a pointer to first buffer data block</li>
  <li>doubleBufferIndex - index of pan, takes values 0-1, left, right, left, right etc</li>
  </ul>
  
  <p>Second question: is about channel parameter because it's a bit confusing. My signal consists of values received in turns: left pan, right pan, left pan, right pan... so that would mean I have 2 channels - left and right?</p>
  
  <p>Third question: My signal from device is 24bit... shouldn't I just write in the bits per sample 24 then or how should it include this information.</p>