private IntPtr InsertSilence(int countSilentSamples, IntPtr waveData, int lengthWave, ref Avi.AVISTREAMINFO streamInfo)
{
//initialize silence
int lengthSilence = countSilentSamples * streamInfo.dwSampleSize;
byte[] silence = new byte[lengthSilence];
//initialize new sound
int lengthNewStream = lengthSilence + lengthWave;
IntPtr newWaveData = Marshal.AllocHGlobal(lengthNewStream);
//copy silence
Marshal.Copy(silence, 0, newWaveData, lengthSilence);
//copy sound
byte[] sound = new byte[lengthWave];
Marshal.Copy(waveData, sound, 0, lengthWave);
IntPtr startOfSound = new IntPtr(newWaveData.ToInt32() + lengthSilence);
Marshal.Copy(sound, 0, startOfSound, lengthWave);
Marshal.FreeHGlobal(newWaveData);
streamInfo.dwLength = lengthNewStream;
return newWaveData;
}