2

我有以下 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();
        }
    }
4

0 回答 0