private void WaveHeaderIN(string spath)
{
FileStream fs = new FileStream(spath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
length = (int)fs.Length - 8;
fs.Position = 22;
channels = br.ReadInt16();
fs.Position = 24;
samplerate = br.ReadInt32();
fs.Position = 34;
BitsPerSample = br.ReadInt16();
DataLength = (int)fs.Length - 44;
br.Close ();
fs.Close();
}
private void WaveHeaderOUT(string sPath)
{
FileStream fs = new FileStream(sPath, FileMode.Create, FileAccess.Write );
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(new char[4] { 'R', 'I', 'F', 'F' });
bw.Write(length);
bw.Write(new char[8] {'W','A','V','E','f','m','t',' '});
bw.Write((int)16);
bw.Write((short)1);
bw.Write(channels);
bw.Write(samplerate );
bw.Write((int)(samplerate * ((BitsPerSample * channels) / 8)));
bw.Write((short )((BitsPerSample * channels) / 8));
bw.Write(BitsPerSample);
bw.Write(new char[4] {'d','a','t','a'});
bw.Write(DataLength);
bw.Close();
fs.Close();
}
We must be careful when wrriting the header. If there is any small mistake, the merged file doesn't work, so we write "RIFF" as an array of char, not as string and use int type for storing 4 bytes and short type for storing 2 bytes.
我們必須小心寫頭。如果有任何小小的失誤,合并后的文件不工作,所以我們寫“釜山國(guó)際電影節(jié)”作為一個(gè)char數(shù)組,而不是字符串,用于存儲(chǔ)4字節(jié),存放2字節(jié)短型int類型。
Write Data of all Files in the Merged File
寫入數(shù)據(jù)合并的文件中的所有文件
public void Merge(string[] files, string outfile)
{
WaveIO wa_IN = new WaveIO();
WaveIO wa_out = new WaveIO();
wa_out.DataLength = 0;
wa_out.length = 0;
//Gather header data
foreach (string path in files)
{
wa_IN.WaveHeaderIN(@path);
wa_out.DataLength += wa_IN.DataLength;
wa_out.length += wa_IN.length;
}
//Reconstruct new header
wa_out.BitsPerSample = wa_IN.BitsPerSample;
wa_out.channels = wa_IN.channels;
wa_out.samplerate = wa_IN.samplerate;
wa_out.WaveHeaderOUT(@outfile);
foreach (string path in files)
{
FileStream fs = new FileStream(@path, FileMode.Open, FileAccess.Read);
byte[] arrfile = new byte[fs.Length - 44];
fs.Position = 44;
fs.Read(arrfile, 0, arrfile.Length);
fs.Close();
FileStream fo =
new FileStream(@outfile, FileMode.Append, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fo);
bw.Write(arrfile);
bw.Close();
fo.Close();
}
}
First we need to calculate the total length and data length of all files and then specify the channels, SampleRate and BitsPerSample of the output file.The last thing is to start reading data that is stored after byte number 44 and append it to the merged file.
首先,我們需要計(jì)算的總長(zhǎng)度和數(shù)據(jù)長(zhǎng)度的所有文件,然后指定的渠道,采樣頻率和輸出file.The bitspersample的最后一件事是開始讀取存儲(chǔ)的字節(jié)數(shù)后44和追加到文件中的數(shù)據(jù)合并。
All we need to do is call the Merge method and specify the input files and output file.
所有我們需要做的就是調(diào)用合并方法,并指定輸入文件和輸出文件。
string[] files = new string[2] { @"C:\WINDOWS\Media\Windows XP Startup.wav",
@"C:\WINDOWS\Media\Windows XP Shutdown.wav" };
WaveIO wa = new WaveIO();
wa.Merge(files,@"c:\oou.wav");
Play the Merged File
合并文件播放
Visual Studio 2005 provides a new class to play sound. Therefore, we don't need an API or anything else.
Visual Studio 2005提供一個(gè)新類播放聲音。因此,我們并不需要一個(gè)API或任何東西。
FileStream fs = new FileStream(@"c:\oou.wav", FileMode.Open,FileAccess.Read);
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(fs);sp.Play();
fs.Close();
聯(lián)系客服