I have a RAW audio file which is made up of several concatenated, smaller WAV files. I can open and play this file as 48,000, 8 bit PCM, signed, mono, in Sound Forge.
What I would like to do, in C#, is programmatically overwrite each individual WAV within the file with new data of equal or smaller length, which I think I'm fairly close to being able to do. I can do a File.ReadAllBytes on the RAW file, loop through the byte array, and determine the start and end location of each WAV. I then pick the new WAV files on my PC, remove the headers, and write the remaining data into my RAW file. If the new WAV is smaller than it's destination, I pad out the difference with 0's.
However when I open the new file in Sound Forge at the settings mentioned above, all of my WAVs look more like solid blocks, than the normal "christmas tree" shape, and they all play loud and distorted. After experiencing this for the first time, it dawned on me that all of my new source WAVs were in 41,000, 16 bit PCM, signed, stereo format. I opened my newly created RAW file in this format, and it looks and plays fine.
I figured I must need to convert all of my new WAVs to 48,000, 8 bit PCM, signed, mono, first, then run the application to copy them into the RAW file. I wrote some code using NAudio to do the conversion on the files before copying the data into my RAW file, and I'm still left with the same problem. I also tried converting each of my new WAVs manually in Sound Forge first, and still have the same problem.
What am I missing here? Thanks.
This code does what you describe (without the padding). It opens the mainFile an then inserts another file at a specified position. If both files have the same format it will work just fine.
public void InsertWave(string mainFile, string insertFile, long position)
{
byte[] data = File.ReadAllBytes(insertFile);
using (FileStream main = File.OpenWrite(mainFile))
{
main.Seek(position, SeekOrigin.Begin);
main.Write(data, 0, data.Length);
main.Close();
}
}
If both files are 48,000 Hz, 8 bit PCM, signed, mono you can't do anything wrong. However, if one file is mono and the other is stereo or if one is 8 bit and the other is 16 bit your output will be distorted.
It doesn't even matter what format the files are actually using. As long as both have the same format and your position is a multiple of the sample size you will always end up with a valid wave file.
So if your output is distorted it is very likely that the files have different formats. If you convert stereo to mono you must also be aware that you cannot just mix both channels. You must reduve the level by 6dB to keep the same volume.
来源:https://stackoverflow.com/questions/11711689/remove-the-headers-from-several-wav-files-and-then-concatenate-the-remaining-da