title: Replicate Wave file using Naudio - Copy/Append Latest Available bytes
body: <p>I have an Active wave recording <code>wave-file.wav</code> happening to the <code>Source</code> folder.
  I need to replicate this file to <code>Destination</code> folder with a new name <code>wave-file-copy.wav</code>.</p>
  
  <p>The recording and replication should happen in parallel. 
  I have implemented a scheduled job, which will run in every 10 minutes and copy the <code>source</code> file to <code>destination</code>.</p>
  
  <pre><code>private static void CopyWaveFile(string destinationFile, string sourceFile){
          using (var fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){
              using (var reader = new WaveFileReader(fs)){
                  using (var writer = new WaveFileWriter(destinationFile, reader.WaveFormat)){
                      reader.Position = 0;
                      var endPos = (int)reader.Length;
                      var buffer = new byte[1024];
                      while (reader.Position &lt; endPos){
                          var bytesRequired = (int)(endPos - reader.Position);
                          if (bytesRequired &lt;= 0) continue;
                          var bytesToRead = Math.Min(bytesRequired, buffer.Length);
                          var bytesRead = reader.Read(buffer, 0, bytesToRead);
                          if (bytesRead &gt; 0){
                              writer.Write(buffer, 0, bytesRead);
                          }
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>The copy operation is working fine, even though the source file is being updated continuously.</p>
  
  <p>Time taken for the copy operation is increasing in linear time, because i am copying the entire file every time.</p>
  
  <p>I am trying to implement a new function <code>ConcatenateWavFiles()</code>, which should update the content of destination file, with the latest available bytes of source recording.</p>
  
  <p>I have tried few sample codes - the approach i am using is :</p>
  
  <ol>
  <li>Read destination file meta info, and get the length.</li>
  <li>Set the <code>length</code> of destination file as <code>reader.Position</code> of source file <code>waveReader</code></li>
  <li><p>Read the source file till end, starting from position.</p>
  
  <pre><code>public static void ConcatenateWavFiles(string destinationFile, string sourceFile){  
  
      WaveFileWriter waveFileWriter = null;
      var sourceReadOffset = GetWaveFileSize(destinationFile);
  
      try{
          using (var fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
          {
              using (var reader = new WaveFileReader(fs))
              {
                  waveFileWriter = new WaveFileWriter(destinationFile, reader.WaveFormat);
                  if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat)){
                      throw new InvalidOperationException(
                          "Can't append WAV Files that don't share the same format");
                  }
  
                  var startPos = sourceReadOffset - sourceReadOffset % reader.WaveFormat.BlockAlign;
                  var endPos = (int) reader.Length;
                  reader.Position = startPos;
                  var bytesRequired = (int)(endPos - reader.Position);
                  var buffer = new byte[bytesRequired];
                  if (bytesRequired &gt; 0)
                  {
                      var bytesToRead = Math.Min(bytesRequired, buffer.Length);
                      var bytesRead = reader.Read(buffer, 0, bytesToRead);
  
                      if (bytesRead &gt; 0)
                      {
                          waveFileWriter.Write(buffer, startPos, bytesRead);
                      }
                  }
              }
          }
      }
      finally{
          if (waveFileWriter != null){
              waveFileWriter.Dispose();
          }
      }
  }
  </code></pre></li>
  </ol>
  
  <p>I was able to get the new content. </p>
  
  <p><strong>Is it possible to append the latest content to existing destination file?</strong></p>
  
  <p><strong>If possible what am I doing wrong in the code?</strong></p>
  
  <p>My code throws the following exception - <code>Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.</code></p>