tags:

views:

334

answers:

1

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  A: 

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

MusiGenesis
Thanks, +1. This method worked as expected, except that based on some experimentation the sample offset should just be written in as samples, not bytes.
Ville Koskinen
That makes sense. The sonic spot article that I linked to called everything a "byte offset", so I just assumed it was in bytes rather than samples. It's good to know - I may end up using this code myself.
MusiGenesis