使用c#编辑Wav文件样本以增加幅度

时间:2014-12-24 07:50:28

标签: c# amplitude

我有以下c#代码,它将波形文件读入字节数组,然后将其写入另一个数组,创建一个新的波形文件。我在网上发现它并且对它没有太多了解。

我想更改正在写入新数组的值,以便我的幅度增加,假设生成新波形文件的值为'x'。在哪里以及应该做出哪些更改?

    private void ReadWavfiles(string fileName)
    {
        byte[] fa = File.ReadAllBytes(fileName);

        int startByte = 0;

        // look for data header
        for (var x = 0; x < fa.Length; x++)
            if (fa[x] == 'd' && fa[x + 1] == 'a' && fa[x + 2] == 't' && fa[x + 3] == 'a')
            {
                    startByte = x + 8;
                    break;
            }


        var buff = new byte[fa.Length / 2];
        var y = 0;
        var length = fa.Length;

        for (int s = startByte; s < length; s = s + 2)
            buff[y++] = (byte)(fa[s + 1] * 0x100 + fa[s]);

        write(buff, "D:\\as1.wav", "D:\\us1.wav");

        WaveOut obj = new WaveOut();
        obj.Init(new WaveFileReader("D:\\as1.wav"));
        obj.Play();
    }

    private void write(byte[] buffer, string fileOut, string fileIn)
    {
        WaveFileReader reader = new WaveFileReader(fileIn);
        int numChannels = reader.WaveFormat.Channels, sampleRate = reader.WaveFormat.SampleRate;
        int BUFFER_SIZE = 1024 * 16;

        using (WaveFileWriter writer = new WaveFileWriter(fileOut, new WaveFormat(sampleRate, 16, numChannels)))
        {
            int bytesRead;

            while (true)
            {
                bytesRead = reader.Read(buffer, 0, BUFFER_SIZE);
                if (bytesRead != 0)
                    writer.Write(buffer, 0, bytesRead);
                else break;
            }

            writer.Close();
        }
    }

0 个答案:

没有答案