Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding values from 2 byte arrays
    text
    copied!<p>I want to add signals from 2 wav files and have been converting each byte to short , dividing by 2 to avoid overflow, then add the 2 short values and then reconvert back to byte before writing to the output file. </p> <p>The wav files in question are human speech files - not pure sinusoidal waves. </p> <p>I am making sure that the bytes from same channel get added together. So bytes in 0th position from one file get mixed with bytes from even numbered position from second file and odd positioned elements get added to odd pistioned elements from other file.</p> <p>My difficulty is that when I use one wav with silence (0 sound), the process works perfectly, and output sound is perfectly audible but when both wav files have some sound, the resultant becomes just a loud hisss.</p> <p>Questions:</p> <ol> <li>why does the amplitude of signal increase suddenly even though I am averaging it out ?</li> <li>why does the signal sampled at same rate as the input stay audible when mixed with 0 values but get totally messed up when mixed with other audible signal.?</li> </ol> <p>I want to do this entirely in c# and do not want to use NAudio/ Nyquist/ or other libraries</p> <p>Can anyone help ?</p> <h2>CODE</h2> <pre><code>int idx=0; int OverlapPCT=20; byte[] arrfile = {}; byte[] arrfileNext = {}; byte[] arrfileOut = {}; string TmpOutFile = Application.StartupPath + "\\" + "tmp.wav"; if (File.Exists (TmpOutFile)){File.Delete (TmpOutFile); } FileStream fo = new FileStream (@TmpOutFile, FileMode.Append, FileAccess.Write); BinaryWriter bw = new BinaryWriter (fo); //header part of output file written here - // set the position within the filestreams // from where header ends and data is to be read fs.Position = 44; fsNext.Position = 44; // read first stream into the arrfile, second into arrfileNext fs.Read (arrfile, 0, fs.Length-44); fsNext.Read (arrfileNext, 0, fsNext.Length-44); // --------------------------------- ThisArrOvlpWithPrev = 0; ThisArrOvlpWithNext = arrfile.Length (OverlapPCT/100); ThisArrNoOvlp = arrfile.Length - (ThisArrOvlpWithPrev+ThisArrOvlpWithNext); Array.Clear (arrfileOut, 0, arrfileOut.Length); // make sure arrfileOut is same size as overlap with previous Array.Resize&lt;byte&gt; (ref arrfileOut, ThisArrOvlpWithPrev); byte[] AmpAdjMergedByteArr= { }; short[] shArr = { }; short[] shArrNext = { }; // get both overlapping areas // for first file, it begins at overlap with prev + part without overlap // size of the overlap is overlap with next Buffer.BlockCopy (arrfile, ThisArrOvlpWithPrev+ThisArrNoOvlp, shArr,0,ThisArrOvlpWithNext); Buffer.BlockCopy (arrfileNext, 0, shArrNext, 0, ThisArrOvlpWithNext); Array.Resize&lt;byte&gt; (ref AmpAdjMergedByteArr, ThisArrOvlpWithNext); for (idx=0; idx&lt;ThisArrOvlpWithNext; idx++) { AmpAdjMergedByteArr[idx] = (byte) (shArr[idx] /2 + shArrNext[idx]/2); } // now set the size of output file and write this to output Buffer.BlockCopy (AmpAdjMergedByteArr, 0, arrfileOut, 0, AmpAdjMergedByteArr.Length); bw.Write (arrfileOut); </code></pre> <h2> #endregion</h2>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload