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

时间:2012-01-06 06:13:17

标签: c# file-io audio

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

7 个答案:

答案 0 :(得分:29)

这段代码可以解决问题。它将波形文件转换为规范化的双数组(-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 (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++;
    }
}

答案 1 :(得分:23)

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

答案 2 :(得分:13)

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

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

答案 3 :(得分:11)

在撰写本文时,没有人处理过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;
}

答案 4 :(得分:3)

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

只需下载控件,它就非常适合WAV文件操作。

答案 5 :(得分:2)

要将wav文件放入数组中,您可以执行以下操作:

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

但是像Fletch说的那样你需要从头文件中隔离数据。它应该只是一个简单的偏移。

答案 6 :(得分:2)

尝试Play audio data from array

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