C # wav Voice file merge

Source: Internet
Author: User

Development to complete the voice broadcast products, as customers use a wide variety of playback products, when using the HDMI interface to play audio, due to the use of different sound card, the individual machine will appear too small, or not broadcast the situation. Therefore, the use of the voice file merge playback method to solve this problem.

<summary>
Merging voice streams
</summary>
<param name= "pplaystreamslist" > Voice Flow Collection </param>
public void Mergeandplaywavfile (observablecollection<stream> pplaystreamslist)
{
Try
{
if (Pplaystreamslist.count > 0)
{
int datafilebeginindex = 0;
int formatsize;
list<byte[]> binfolist = new list<byte[]> ();
list<int> filesizelist = new list<int> ();
list<int> datasizelist = new list<int> ();
foreach (Stream fs in pplaystreamslist)
{
Fs. Position = 0;
byte[] Binfo = new byte[46];
Fs. Read (binfo, 0, 46);
Formatsize = Bitconverter.toint32 (Binfo, 16);
Wave = riff_wave_chunk + format_chunk + data_chunk (RIFF)/fact_chunk (Fact)
Riff_wave_chunk (file header, 12 bytes) +
Format_chunk (sound content definition, based on the 16th to 20th byte to get the remaining chunk size, 16 or 18 bytes) +
Data_chunk (data area)
Datafilebeginindex = formatsize + 20 + 4;
Binfolist.add (Binfo);
Filesizelist.add (System.BitConverter.ToInt32 (Binfo, 4));
Datasizelist.add (System.BitConverter.ToInt32 (Binfo, Datafilebeginindex));
}

Calculate all WAV file sizes
int fileSize = 0;
foreach (int fsize in filesizelist)
{
FileSize + = fsize;
}
byte[] totalfilesize = System.BitConverter.GetBytes (fileSize);

Calculate all WAV Data sizes
int datasize = 0;
foreach (int dsize in datasizelist)
{
DataSize + = Dsize;
}
byte[] totaldatasize = System.BitConverter.GetBytes (datasize);
using (Stream tofile = new MemoryStream ())
{
Stream tofile = new MemoryStream ();
BinaryWriter bwriter = new BinaryWriter (tofile);

Get more information about the last WAV file
int lastwavefile = pplaystreamslist.count-1;

for (int i = 4, j = 0; i < 8; i++, J + +)
{
Binfolist[lastwavefile][i] = Totalfilesize[j];
}

for (int i = datafilebeginindex, j = 0; I < Datafilebeginindex + 4; i++, J + +)
{
Binfolist[lastwavefile][i] = Totaldatasize[j];
}
Bwriter.write (Binfolist[lastwavefile]);
int k = 0;
foreach (Stream fs in pplaystreamslist)
{
int readlength;
Fs. Position = Datafilebeginindex + 4;
if (K < Datasizelist.count)
{
Readlength = datasizelist[k]-40;
byte[] Dushu = new Byte[readlength];
Fs. Read (Dushu, 0, readlength);
Bwriter.write (Dushu, 0, readlength);
k++;
}
}
SoundPlayer player = new SoundPlayer ();
if (tofile! = null)
{
Player. Stream = null;
Player. Stream = ToFile;
tofile.position = 0;
Player. LoadAsync ();
for (int i = 0; i < playtimes; i++)
{player. Playsync (); }

}

Bwriter.flush ();
Bwriter.close ();

Tofile.flush ();
Tofile.close ();
Player. Dispose ();
Bwriter.dispose ();
Tofile.dispose ();
}
}
}
catch (Exception e)
{
Loginfo.savelog ("Combined voice stream error, Method Mergeandplaywavfile ():" + e.message);
}

}

C # wav Voice file merge

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.