c# - Adding values from 2 byte arrays -


i want add signals 2 wav files , have been converting each byte short , dividing 2 avoid overflow, add 2 short values , reconvert byte before writing output file.

the wav files in question human speech files - not pure sinusoidal waves.

i making sure bytes same channel added together. bytes in 0th position 1 file mixed bytes numbered position second file , odd positioned elements added odd pistioned elements other file.

my difficulty when use 1 wav silence (0 sound), process works perfectly, , output sound audible when both wav files have sound, resultant becomes loud hisss.

questions:

  1. why amplitude of signal increase though averaging out ?
  2. why signal sampled @ same rate input stay audible when mixed 0 values totally messed when mixed other audible signal.?

i want entirely in c# , not want use naudio/ nyquist/ or other libraries

can ?

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 position within filestreams  // header ends , data read fs.position = 44; fsnext.position = 44; // read first stream arrfile, second 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 same size overlap previous array.resize<byte> (ref arrfileout, thisarrovlpwithprev);  byte[] ampadjmergedbytearr= { }; short[] sharr = { }; short[] sharrnext = { }; // both overlapping areas // first file, begins @ overlap prev + part without overlap  // size of overlap overlap next buffer.blockcopy (arrfile, thisarrovlpwithprev+thisarrnoovlp, sharr,0,thisarrovlpwithnext); buffer.blockcopy (arrfilenext, 0, sharrnext, 0, thisarrovlpwithnext);  array.resize<byte> (ref ampadjmergedbytearr, thisarrovlpwithnext);  (idx=0; idx<thisarrovlpwithnext; idx++) {     ampadjmergedbytearr[idx] = (byte) (sharr[idx] /2 + sharrnext[idx]/2); }  // set size of output file , write output buffer.blockcopy (ampadjmergedbytearr, 0, arrfileout, 0, ampadjmergedbytearr.length); bw.write (arrfileout); 

#endregion


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -