我下面的微软开发者博客本教程: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
设置生成的函数的位深,很简单至今。
然后,运行程序,所有与此环境中工作。产生1Hz的频率与在16位幅度32760 /这44.1ksample是结果:
这显然是一个正确的输出。
现在我引用它:
我们使用短裤的阵列,因为我们有16位样本作为格式块中指定。如果要更改为8位音频,使用一个字节数组。如果您想使用32位音频,使用float数组。
谈shortArray
类WaveDataChunk
public class WaveDataChunk
{
public string sChunkID; // "data"
public uint dwChunkSize; // Length of header in bytes
public short[] shortArray;
然后,32位音频,改变shortArray
浮动:
public float[] shortArray;
和wBitsPerSample
至32:
wBitsPerSample = 32;
这是结果:1Hz 32bit 44.1k
实际上,频率加倍,只有一半的时间被写入。我做错了什么??我已经做什么?
当您使用IEEE浮点,范围应该是-1..1(而不是最小..最大整数)。此外,格式于是不再MS PCM(1),但IEEE FLOAT(3)。
与文章的问题是,它的块定义,不考虑IEEE浮点格式初始化。这里是我的建议,以使代码更通用(直译);组块将初始化(我造只读)在构造根据所选择的类型的字段:
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循环保存数据必须写入正确的数据类型为好。在我离开这个作为练习给你。 :)