2009-10-11 59 views
3

我想用C#向WAV文件写入提示(即基于时间的标记,而不是类ID3标记)。似乎免费的.NET音频库,如NAudio和Bass.NET不支持这一点。如何在.NET中将线索/标记写入WAV文件

我发现了Cue Tools的来源,但它完全没有记录,也比较复杂。任何替代品?

+0

你还有一些阅读标记的代码吗? – Basj 2013-12-04 20:21:59

回答

3

下面是解释了cue块的WAV文件格式的链接:

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

因为WAV文件使用RIFF格式,你可以在cue块简单地附加的结束现有的WAV文件。要在.Net中执行此操作,您需要使用构造函数打开System.IO.FileStream对象,该构造函数采用路径和FileMode(为此目的,您将使用FileMode.Append)。然后,您将从FileStream中创建一个BinaryWriter,并使用它来编写提示块本身。

这里是一个粗略的代码示例到cue块与单个提示点追加到一个WAV文件的末尾:

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(); 

注意:我从来没有使用或测试此代码,所以我我不确定它是否正常工作。这只是为了给你一个关于如何在C#中编写这个代码的粗略想法。

+1

谢谢,+1。该方法按预期工作,除了基于一些实验,样本偏移量应该只是样本而不是字节。 – 2009-10-21 19:03:56

+0

这很有道理。我链接到的声音文章将所有东西都称为“字节偏移量”,所以我只是假定它是以字节为单位而不是样本。很高兴知道 - 我最终可能会自己使用此代码。 – MusiGenesis 2009-10-21 19:37:42

+0

你有阅读标记的一些代码吗? – Basj 2013-12-04 20:21:36