How to write cues/markers to a WAV file in .NET

2019-04-11 11:23发布

I'd like write cues (i.e. time-based markers, not ID3-like tags) to a WAV file with C#. It seems that the free .NET audio libraries such as NAudio and Bass.NET don't support this.

I've found the source of Cue Tools, but it's entirely undocumented and relatively complex. Any alternatives?

标签: c# .net audio wav
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-11 12:02

Here's a link that explains the format of a cue chunk in a WAV file:

http://www.sonicspot.com/guide/wavefiles.html#cue

Because a WAV file uses the RIFF format, you can simply append the cue chunk to the end of an existing WAV file. To do this in .Net, you would open a System.IO.FileStream object, using the constructor that takes a path and a FileMode (you would use FileMode.Append for this purpose). You would then create a BinaryWriter from your FileStream, and use it to write the cue chunk itself.

Here is a rough code sample to append a cue chunk with a single cue point to the end of a WAV file:

System.IO.FileStream fs = 
    new System.IO.FileStream(@"c:\sample.wav", 
    System.IO.FileMode.Append);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
char[] cue = new char[] { 'c', 'u', 'e', ' ' };
bw.Write(cue, 0, 4); // "cue "
bw.Write((int)28); // chunk size = 4 + (24 * # of cues)
bw.Write((int)1); // # of cues
// first cue point
bw.Write((int)0); // unique ID of first cue
bw.Write((int)0); // position
char[] data = new char[] { 'd', 'a', 't', 'a' };
bw.Write(data, 0, 4); // RIFF ID = "data"
bw.Write((int)0); // chunk start
bw.Write((int)0); // block start
bw.Write((int)500); // sample offset - in a mono, 16-bits-per-sample WAV
// file, this would be the 250th sample from the start of the block
bw.Close();
fs.Dispose();

Note: I have never used or tested this code, so I am not sure if it works quite right. It is just intended to give you a rough idea of how to write this in C#.

查看更多
登录 后发表回答