在c#中将.wav文件转换为.aiff

时间:2012-11-11 21:28:27

标签: c# audio file-conversion

我正在尝试编写一个将.wav文件转换为.aiff文件的类作为项目的一部分。

我遇到过几个图书馆Alvas.Audio(http://alvas.net/alvas.audio,overview.aspx)和NAudio(http://naudio.codeplex.com)

我想知道是否有人对他们中的任何一个有任何经验,因为我真的很难找到如何使用任何一个库编写aiff格式的文件。

到目前为止,我有以下代码,但我无法弄清楚如何将outfile定义为aiff:

Alvas

string inFile = textBox1.Text; 
WaveReader mr = new WaveReader(File.OpenRead(inFile));
IntPtr mrFormat = mr.ReadFormat();
IntPtr wwFormat = AudioCompressionManager.GetCompatibleFormat(mrFormat, AudioCompressionManager.PcmFormatTag);
string outFile = inFile + ".aif";
WaveWriter ww = new WaveWriter(File.Create(outFile), AudioCompressionManager.FormatBytes(wwFormat));
AudioCompressionManager.Convert(mr, ww, false);
mr.Close();
ww.Close();

n音讯

string inFile = textBox1.Text;
string outFile = inFile + ".aif";

using (WaveFileReader reader = new WaveFileReader(inFile))
{
   using (WaveFileWriter writer = new WaveFileWriter(outFile, reader.WaveFormat))
   {
       byte[] buffer = new byte[4096];
       int bytesRead = 0;
       do
       {
           bytesRead = reader.Read(buffer, 0, buffer.Length);
           writer.Write(buffer, 0, bytesRead);
       } while (bytesRead > 0);
   }
}

任何帮助都会被非常接受:)

2 个答案:

答案 0 :(得分:1)

有关Alvas.Audio的最新版本,请参阅以下代码:How to convert .wav to .aiff?

static void Wav2Aiff(string inFile)
{
    WaveReader wr = new WaveReader(File.OpenRead(inFile));
    IntPtr inFormat = wr.ReadFormat();
    IntPtr outFormat = AudioCompressionManager.GetCompatibleFormat(inFormat, 
        AudioCompressionManager.PcmFormatTag);
    string outFile = inFile + ".aif";
    AiffWriter aw = new AiffWriter(File.Create(outFile), outFormat);
    byte[] outData = AudioCompressionManager.Convert(inFormat, outFormat, wr.ReadData(), false);
    aw.WriteData(outData);
    wr.Close();
    aw.Close();
}

答案 1 :(得分:0)

Alvas的WavWriter和NAudio的WaveFileWriter都是为了创建WAV文件,而不是AIFF文件。 NAudio没有包含AiffFileWriter,我不知道Alvas,但AIFF文件在Windows平台上并不常用。它们使用大端字节排序(WAV使用little-endian),AIFF文件格式对WAV文件有不同的“块”定义。

基本答案是您可能需要创建自己的AIFF编写代码。您可以阅读AIFF specification here。您基本上需要创建一个FORM块,其中包含一个SSND(声音数据)块后面的COMM(公共)块。规范解释了要放在这些块中的内容(这是相当简单的)。在Windows上你需要记住的主要事情是交换字节顺序。

相关问题