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

本文关键字:数据 读取 数组 wav 文件 | 更新日期: 2023-09-27 18:19:31

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

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

这段代码应该可以完成任务。它将wave文件转换为标准化的双数组(-1到1),但将其改为int/short数组(删除/32768.0位并添加32768)应该是微不足道的。如果发现加载的wav文件为mono,则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 (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++;
    }
}

假设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个字节这样的方法。

注意:根据Daniel Moller的评论,请参阅http://soundfile.sapp.org/doc/WaveFormat/理解代码。(并对评论投赞成票)。

在撰写本文时,没有人寻址32位或64位编码的WAV。

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

static bool readWav( string filename, out float[] L, out float[] R )
{
    L = R = null;
    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 (expect 16 or 18)
            // 16 bytes coming...
            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 nValues = bytes / bytesForSamp;

            float[] asFloat = null;
            switch( bitDepth ) {
                case 64:
                    double[] 
                        asDouble = new double[nValues];  
                    Buffer.BlockCopy(byteArray, 0, asDouble, 0, bytes);
                    asFloat = Array.ConvertAll( asDouble, e => (float)e );
                    break;
                case 32:
                    asFloat = new float[nValues];   
                    Buffer.BlockCopy(byteArray, 0, asFloat, 0, bytes);
                    break;
                case 16:
                    Int16 [] 
                        asInt16 = new Int16[nValues];   
                    Buffer.BlockCopy(byteArray, 0, asInt16, 0, bytes);
                    asFloat = Array.ConvertAll( asInt16, e => e / (float)(Int16.MaxValue+1) );
                    break;
                default:
                    return false;
            }
            switch( channels ) {
            case 1:
                L = asFloat;
                R = null;
                return true;
            case 2:
                // de-interleave
                int nSamps = nValues / 2;
                L = new float[nSamps];
                R = new float[nSamps];
                for( int s=0, v=0; s<nSamps; s++ ) {
                    L[s] = asFloat[v++];
                    R[s] = asFloat[v++];
                }
                return true;
            default:
                return false;
            }
        }
    }
    catch {
            Debug.Log( "...Failed to load: " + filename );
            return false;
    }
    return false;
}

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

这里有一个很好的参考:https://ccrma.stanford.edu/courses/422/projects/WaveFormat/(镜像)

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

只要下载控件,它就可以很好地处理WAV文件。

要将wav文件放入数组,只需执行以下操作:

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

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

尝试从阵列播放音频数据

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