如何将wav文件中的数据读取到数组中

我需要将一个wav文件的所有样本放到一个数组中(如果你需要这样做两个以保持立体声),这样我就可以对它们进行一些修改。 我想知道这是否容易完成(最好没有外部库)。 我没有阅读声音文件的经验,所以我对这个主题知之甚少。

WAV文件(至少是未压缩的文件)非常简单。 有一个标题,然后数据跟着它。

这是一个很好的参考: https//ccrma.stanford.edu/courses/422/projects/WaveFormat/ ( mirror )

这段代码应该可以解决问题。 它将波形文件转换为规范化的双数组(-1到1),但是要使它成为一个int / short数组(删除/32768.0位并添加32768)应该是微不足道的。 如果发现加载的wav文件是单声道,则right[]数组将设置为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 (ie 64 61 74 61 ...... (ie 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++; } } 

假设您的WAV文件包含16位PCM(这是最常见的),您可以使用NAudio将其读取到字节数组中,然后将其复制到16位整数数组中以方便使用。 如果是立体声,则样本将左右交错。

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

我知道你想避免第三方库,但如果你想确保用额外的块处理WAV文件,我建议避免像在文件中寻找44个字节的方法。

在撰写本文时,没有人解决过32位或64位编码的WAV问题。

以下代码处理16/32/64位和单声道/立体声:

 static bool readWav( string filename, out float[] L, out float[] R ) { L = R = null; //float [] left = new float[1]; //float [] right; try { using (FileStream fs = File.Open(filename,FileMode.Open)) { BinaryReader reader = new BinaryReader(fs); // chunk 0 int chunkID = reader.ReadInt32(); int fileSize = reader.ReadInt32(); int riffType = reader.ReadInt32(); // chunk 1 int fmtID = reader.ReadInt32(); int fmtSize = reader.ReadInt32(); // bytes for this chunk int fmtCode = reader.ReadInt16(); int channels = reader.ReadInt16(); int sampleRate = reader.ReadInt32(); int byteRate = reader.ReadInt32(); int fmtBlockAlign = reader.ReadInt16(); int bitDepth = reader.ReadInt16(); if (fmtSize == 18) { // Read any extra values int fmtExtraSize = reader.ReadInt16(); reader.ReadBytes(fmtExtraSize); } // chunk 2 int dataID = reader.ReadInt32(); int bytes = reader.ReadInt32(); // DATA! byte[] byteArray = reader.ReadBytes(bytes); int bytesForSamp = bitDepth/8; int samps = bytes / bytesForSamp; float[] asFloat = null; switch( bitDepth ) { case 64: double[] asDouble = new double[samps]; Buffer.BlockCopy(byteArray, 0, asDouble, 0, bytes); asFloat = Array.ConvertAll( asDouble, e => (float)e ); break; case 32: asFloat = new float[samps]; Buffer.BlockCopy(byteArray, 0, asFloat, 0, bytes); break; case 16: Int16 [] asInt16 = new Int16[samps]; Buffer.BlockCopy(byteArray, 0, asInt16, 0, bytes); asFloat = Array.ConvertAll( asInt16, e => e / (float)Int16.MaxValue ); break; default: return false; } switch( channels ) { case 1: L = asFloat; R = null; return true; case 2: L = new float[samps]; R = new float[samps]; for( int i=0, s=0; i 

http://hourlyapps.blogspot.com/2008/07/open-source-wave-graph-c-net-control.html
这是一个控件,显示Wav文件的频谱,它还提供解码的Wav文件的字节[],您可以在其中播放和/或更改其值。

只需下载Control,它对WAV文件操作非常有用。

要将wav文件放入数组,您可以这样做:

byte [] data = File.ReadAllBytes(“FilePath”);

但是像Fletch说的那样你需要将数据与标题隔离开来。 它应该只是一个简单的偏移。

尝试从arrays播放音频数据

 PlayerEx pl = new PlayerEx(); private static void PlayArray(PlayerEx pl) { double fs = 8000; // sample freq double freq = 1000; // desired tone short[] mySound = new short[4000]; for (int i = 0; i < 4000; i++) { double t = (double)i / fs; // current time mySound[i] = (short)(Math.Cos(t * freq) * (short.MaxValue)); } IntPtr format = AudioCompressionManager.GetPcmFormat(1, 16, (int)fs); pl.OpenPlayer(format); byte[] mySoundByte = new byte[mySound.Length * 2]; Buffer.BlockCopy(mySound, 0, mySoundByte, 0, mySoundByte.Length); pl.AddData(mySoundByte); pl.StartPlay(); }