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

主宰稳场 提交于 2019-12-09 19:24:38

问题


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?


回答1:


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#.



来源:https://stackoverflow.com/questions/1550819/how-to-write-cues-markers-to-a-wav-file-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!