Creating a .wav File in C#

前端 未结 4 1311
青春惊慌失措
青春惊慌失措 2021-02-05 15:25

As an excuse to learn C#, I have been trying to code a simple project: creating audio files. To start, I want to make sure that I can write files that meet the WAVE format. I ha

4条回答
  •  鱼传尺愫
    2021-02-05 15:50

    I lack the proper WAV data, but try replacing the part of your code where you generate the header with this code (replace appropriately):

    wr.Write(Encoding.ASCII.GetBytes("RIFF"));
    wr.Write(0);
    wr.Write(Encoding.ASCII.GetBytes("WAVE"));
    wr.Write(Encoding.ASCII.GetBytes("fmt "));
    wr.Write(18 + (int)(numsamples * samplelength));
    wr.Write((short)1); // Encoding
    wr.Write((short)numchannels); // Channels
    wr.Write((int)(samplerate)); // Sample rate
    wr.Write((int)(samplerate * samplelength * numchannels)); // Average bytes per second
    wr.Write((short)(samplelength * numchannels)); // block align
    wr.Write((short)(8 * samplelength)); // bits per sample
    wr.Write((short)(numsamples * samplelength)); // Extra size
    wr.Write("data");
    

提交回复
热议问题