【问题标题】:Converting .wav files to .aiff in c#在 C# 中将 .wav 文件转换为 .aiff
【发布时间】:2012-10-31 09:36:48
【问题描述】:

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

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

我想知道是否有人对其中任何一个有任何经验,因为我真的很难弄清楚如何使用这两个库以 aiff 格式编写文件。

到目前为止,我有以下代码,但我不知道如何将 outfile 定义为 aiff:

阿尔瓦斯

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

音频

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

任何帮助都会被极大地接受:)

【问题讨论】:

    标签: c# audio file-conversion


    【解决方案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();
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • Alvas 7.1 也不支持 AIFF。
      • 是的,这就是我今天得出的结论。这在理论上听起来很简单,尽管我在实践中发现它并不是那么简单。 Aumplib 是另一个库,尽管它只是一个包装器,它似乎完全符合我的需要(它有一个 AiffWriter),尽管它似乎不再能很好地 PInvoke DLL。我很惊讶没有我可以支付的图书馆来满足这种需求!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2016-06-19
      • 2011-06-19
      相关资源
      最近更新 更多