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

ann*_*sly 29 c# audio file-io

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

Dan*_*n W 30

这段代码应该可以解决问题.它将波形文件转换为规范化的双数组(-1到1),但是要使它成为一个int/short数组(删除该/32768.0位并添加32768)应该是微不足道的.所述right[]如果加载的wav文件被发现是单阵列将被设置为空.

我不能声称它完全是防弹(潜在的一个一个错误),但是在创建一个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++;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @MartinBerger 在大多数情况下,字节会自动提升为 int,包括移位。 (2认同)
  • @MartinBerger是的,请参阅http://msdn.microsoft.com/en-us/library/aa691330(v=vs.71).aspx,特别是最后一条规则:`否则,两个操作数都转换为int类型. . (2认同)

Mar*_*ath 26

假设您的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);
}
Run Code Online (Sandbox Code Playgroud)

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


Fle*_*tch 13

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

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

  • 这个链接是一个很好的介绍,但请注意.今天许多wavs的标头长度超过44个字节.46字节标头特别常见,并且介绍不讨论额外数据解析的可能性. (2认同)

P i*_*P i 12

在撰写本文时,没有人提到过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<samps; i++ ) {
                    L[i] = asFloat[s++];
                    R[i] = asFloat[s++];
                }
                return true;
            default:
                return false;
            }
        }
    }
    catch {
            Debug.Log( "...Failed to load note: " + filename );
            return false;
            //left = new float[ 1 ]{ 0f };
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 不应该L和R是长度(samps/2),因为asFloat的大小是samps? (2认同)
  • 伟大的!-- 阅读此答案和本文,以更好地了解发生了什么:http://soundfile.sapp.org/doc/WaveFormat/ (2认同)