C# 如何在C语言中将音频文件读入数组#
我试图将WAV文件读入c#中的缓冲区数组,但遇到了一些问题。我正在使用文件流来管理音频文件。这是我所拥有的C# 如何在C语言中将音频文件读入数组#,c#,arrays,audio,wav,C#,Arrays,Audio,Wav,我试图将WAV文件读入c#中的缓冲区数组,但遇到了一些问题。我正在使用文件流来管理音频文件。这是我所拥有的 FileStream WAVFile = new FileStream(@"test.wav", FileMode.Open); //Buffer for the wave file... BinaryReader WAVreader = new BinaryReader(WAVFile); //Read information from
FileStream WAVFile = new FileStream(@"test.wav", FileMode.Open);
//Buffer for the wave file...
BinaryReader WAVreader = new BinaryReader(WAVFile);
//Read information from the header.
chunkID = WAVreader.ReadInt32();
chunkSize = WAVreader.ReadInt32();
RiffFormat = WAVreader.ReadInt32();
...
channels = WAVreader.ReadInt16();
samplerate = WAVreader.ReadInt32();
byteRate = WAVreader.ReadInt32();
blockAllign = WAVreader.ReadInt16();
bitsPerSample = WAVreader.ReadInt16();
dataID = WAVreader.ReadInt32();
dataSize = WAVreader.ReadInt32();
上面是从WAV文件头读取数据。那么我有这个,
musicalData = WAVreader.ReadBytes(dataSize);
…读取实际样本数据,但60秒音频仅为26字节。这是正确的吗
如何将byte[]数组转换为double[]?自从我接触WAVE文件处理以来,已经有10-15年的时间了,但与大多数人对WAVE文件的第一印象不同,WAVE文件是简单的固定大小的头文件,然后是PCM编码的音频数据,WAVE文件是更复杂的RIFF格式文件 我建议使用互操作和调用处理RIFF文件格式的API,而不是重新设计RIFF文件处理和各种情况
您可以看到如何打开和获取数据缓冲区(以及缓冲区所在的元信息)的示例。它是C++的,但是它显示了使用<代码> MMIOPENG/<代码>、<代码> MMIORAD< <代码>、<代码> MMIOORSEND/<代码>、<代码> MMIOASESTOND/<代码> API,您需要使用它来获得正确的音频缓冲器。 < P>这个代码应该能起到作用。它将一个wave文件转换为一个规范化的双数组(-1到1),但将其改为int/short数组应该很简单(删除/32768.0位,然后添加32768)。如果发现加载的wav文件是mono,则右[]数组将设置为null 我不能说它是完全防弹的(可能会有一个错误),但在创建了65536样本阵列,并创建了一个从-1到1的波之后,没有一个样本会“穿过”天花板或地板
// convert two bytes to one double in the range -1 to 1
static double bytesToDouble(byte firstByte, byte secondByte)
{
// convert two bytes to one short (little endian)
short s = (secondByte << 8) | firstByte;
// convert to range from -1 to (just below) 1
return s / 32768.0;
}
// Returns left and right double arrays. 'right' will be null if sound is mono.
public void openWav(string filename, out double[] left, out double[] right)
{
byte[] wav = File.ReadAllBytes(filename);
// Determine if mono or stereo
int channels = wav[22]; // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels
// Get past all the other sub chunks to get to the data subchunk:
int pos = 12; // First Subchunk ID from 12 to 16
// Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))
while(!(wav[pos]==100 && wav[pos+1]==97 && wav[pos+2]==116 && wav[pos+3]==97))
{
pos += 4;
int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
pos += 4 + chunkSize;
}
pos += 8;
// Pos is now positioned to start of actual sound data.
int samples = (wav.Length - pos)/2; // 2 bytes per sample (16 bit sound mono)
if (channels == 2)
{
samples /= 2; // 4 bytes per sample (16 bit stereo)
}
// Allocate memory (right will be null if only mono sound)
left = new double[samples];
if (channels == 2)
{
right = new double[samples];
}
else
{
right = null;
}
// Write to double array/s:
int i=0;
while (pos < length)
{
left[i] = bytesToDouble(wav[pos], wav[pos + 1]);
pos += 2;
if (channels == 2)
{
right[i] = bytesToDouble(wav[pos], wav[pos + 1]);
pos += 2;
}
i++;
}
}
我个人尽量避免使用第三方库。但是,如果您希望代码看起来更好、更易于处理,该选项仍然存在。检查dataID的值。它应该包含字符“fmt”。我认为当读取为32位int时,它将被反转,这意味着它将包含以下内容:0x20 0x74 0x6d 0x66。这将有助于确定您是否从正确的位置读取数据大小值。您知道我的返回26字节的原因吗?在30秒的音频中看不到这一点
using (WaveFileReader reader = new WaveFileReader("myfile.wav"))
{
Assert.AreEqual(16, reader.WaveFormat.BitsPerSample, "Only works with 16 bit audio");
byte[] buffer = new byte[reader.Length];
int read = reader.Read(buffer, 0, buffer.Length);
short[] sampleBuffer = new short[read / 2];
Buffer.BlockCopy(buffer, 0, sampleBuffer, 0, read);
}