我需要将 wav 文件的所有样本放入一个数组(或者两个,如果您需要这样做以保持立体声),以便我可以对它们进行一些修改。我想知道这是否容易完成(最好没有外部库)。我没有阅读声音文件的经验,所以我对这个主题了解不多。
这段代码应该可以解决问题。它将波形文件转换为标准化双精度数组(-1 到 1),但将其改为 int/short 数组应该很简单(删除/32768.0
位并添加 32768 代替)。这right[]
如果发现加载的 wav 文件是单声道的,数组将被设置为 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++;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)