C#创建24 / 32bit声音WAV文件

时间:2019-02-10 23:15:16

标签: c# wav 32-bit

我正在Microsoft Developer Blog上关注本教程:https://blogs.msdn.microsoft.com/dawate/2009/06/24/intro-to-audio-programming-part-3-synthesizing-simple-wave-audio-using-c/

该教程称为“音频编程简介”,我遵循了所有步骤,但是可能错过了一些东西。

这是从WaveFormatChunk类的同音异义方法中提取的一个配置文件:

    sChunkID = "fmt ";
    dwChunkSize = 16;
    wFormatTag = 1;
    wChannels = 2;
    dwSamplesPerSec = 44100;
    wBitsPerSample = 16;
    wBlockAlign = (ushort)(wChannels * (wBitsPerSample / 8));
    dwAvgBytesPerSec = dwSamplesPerSec * wBlockAlign;

wBitsPerSample设置所生成函数的位深度,直到现在还很简单。

然后,运行程序,所有人都在使用此设置。产生16比特/44.1ksample的振幅为32760的1Hz频率,结果如下:

1Hz 16bit 44.1k

这显然是正确的输出。

现在我引用它:

  

我们使用短裤数组,因为我们在format块中指定了16位样本。如果要更改为8位音频,请使用字节数组。如果要使用32位音频,请使用浮点数组。

shortArray类中讨论WaveDataChunk

public class WaveDataChunk
{
    public string sChunkID;     // "data"
    public uint dwChunkSize;    // Length of header in bytes
    public short[] shortArray;

然后,对于32位音频,将shortArray更改为float:

public float[] shortArray;

wBitsPerSample至32:

wBitsPerSample = 32;

这是结果: 1Hz 32bit 44.1k

实际上,频率加倍,只有一半的时间被写入。我做错了什么??我该怎么办?

1 个答案:

答案 0 :(得分:0)

使用IEEE浮点时,范围应为-1..1(而不是min..max整数)。而且,格式不再是MS PCM(1),而是IEEE FLOAT(3)。

本文的问题在于,在不考虑IEEE FLOAT格式的情况下初始化了其块定义。这是我的一个建议,使代码更通用(从字面上看);块将根据选择的类型初始化构造函数中的字段(我将其设为只读):

public class WaveFormatChunk<T> where T: struct, IConvertible
{
    public readonly string sChunkID;         // Four bytes: "fmt "
    public readonly uint dwChunkSize;        // Length of chunk in bytes
    public readonly ushort wFormatTag;       // 1 (MS PCM)
    public readonly ushort wChannels;        // Number of channels
    public readonly uint dwSamplesPerSec;    // Frequency of the audio in Hz... 44100
    public readonly uint dwAvgBytesPerSec;   // for estimating RAM allocation
    public readonly ushort wBlockAlign;      // sample frame size, in bytes
    public readonly ushort wBitsPerSample;    // bits per sample

    /// <summary>
    /// Initializes a format chunk. Supported data types: byte, short, float
    /// </summary>
    public WaveFormatChunk(short channels, uint samplesPerSec)
    {
        sChunkID = "fmt ";
        dwChunkSize = 16;
        wFormatTag = typeof(T) == typeof(float) || typeof(T) == typeof(double) ? 3 : 1;
        wChannels = channels;
        dwSamplesPerSec = samplesPerSec;
        wBitsPerSample = (ushort)(Marshal.SizeOf<T>() * 8);
        wBlockAlign = (ushort)(wChannels * ((wBitsPerSample + 7) / 8));
        dwAvgBytesPerSec = dwSamplesPerSec * wBlockAlign;            
    }
}

public class WaveDataChunk<T> where T: struct, IConvertible
{
    public readonly string sChunkID;     // "data"
    public readonly uint dwChunkSize;    // Length of data chunk in bytes
    public readonly T[] dataArray;  // 8-bit audio

    /// <summary>
    /// Initializes a new data chunk with a specified capacity.
    /// </summary>
    public WaveDataChunk(uint capacity)
    {
        dataArray = new T[capacity];
        dwChunkSize = (uint)(Marshal.SizeOf<T>() * capacity);
        sChunkID = "data";
    }   
}

public void FloatWaveGenerator(WaveExampleType type)
{          
    // Init chunks
    header = new WaveHeader();
    format = new WaveFormatChunk<float>(2, 44100);
    data = new WaveDataChunk<float>(format.dwSamplesPerSec * format.wChannels);            

    // Fill the data array with sample data
    switch (type)
    {
        case WaveExampleType.ExampleSineWave:
            double freq = 440.0f;   // Concert A: 440Hz

            // The "angle" used in the function, adjusted for the number of channels and sample rate.
            // This value is like the period of the wave.
            double t = (Math.PI * 2 * freq) / format.dwSamplesPerSec;

            for (uint i = 0; i < format.dwSamplesPerSec - 1; i++)
            {
                // Fill with a simple sine wave at max amplitude
                for (int channel = 0; channel < format.wChannels; channel++)
                {
                    data.dataArray[i * format.wChannels + channel] = (float)Math.Sin(t * i);
                }                        
            }

            break;
    }          
}

请注意,您需要调整FloatWaveGenerator使用的变量,并且保存数据的foreach循环也必须写入正确的数据类型。我将此作为练习留给您。 :)