2
votes

I want to change the bit rate of wave file.

so I searched in the net and I figure out that the wave file contain a header which is 44 bytes length , and the 25,26,27 and 28 byte are used to store the bit rate of wave file

so I take the wave and store it in an array of byte, then changes the value of bytes that used to store the bit rate of wave.

here is the code :

        private int sampleRate;
        private byte[] ByteArr;
        private MemoryStream ByteMem;
        ByteArr = null;
        ByteMem = null;
        ByteArr = File.ReadAllBytes(pathOfWav.Text);
        sampleRate = BitConverter.ToInt32(ByteArr, 24) * 2;
        Array.Copy(BitConverter.GetBytes(sampleRate), 0, ByteArr, 24, 4);
        ByteMem = new MemoryStream(ByteArr);

here I stored the Wave file location on pathOfWav.Text which is a textBox, then I stored All the bytes of wave file in ByteArr then convert the 4 byte (from 25 to 28) to Int32 and multiply it by 2 to Increase the speed of speech and stored the value in sampleRate after that I modify the previous ByteArr with the new value of Bit Rate sampleRate, then I instance a new MemoryStream .

my question is,, how to play the new Wave stream using Naudio ???

2
@Mark Heath Can You Help Please ?superuser
Perhaps if you state what you are trying to do rather than how you are trying to do it, we can provide a reasonable answer.spender
@spender I want to change the bit rate of wave file ,for example if wave file bit rate is 44100 , I want to multiply it by 2 ,then it change to 88200 , and I want to play it after this changes.superuser
You have to use methods in WaveOut class of Naudio; initialize and play. Also, you need to change other related bytes (after sample rate field). Please check below for answer.KbManu
the title of this question doesn't seem to match what you are askingMark Heath

2 Answers

0
votes

Have you solved the issue ? As per your comment, if you need only to change the sampleRate, then why are you using NAudio ? You can use default available players such as MediaPlayer/SoundPlayer. If so, you can refer to the below code. I have added a method for changing the sample rate. Though you can write the waveFormat separately or append, I have only referred to sample rate and its dependent fields. I am reading the entire file, closing and then opening the same for writing part by part.

(Original Reference for 'WaveHeader Format' in C#: http://www.codeproject.com/Articles/15187/Concatenating-Wave-Files-Using-C-2005)

public void changeSampleRate(string waveFile, int sampleRate)
{
    if (waveFile == null)
    {
        return;
    }

    /* you can add additional input validation code here */

    /* open for reading */
    FileStream fs = new FileStream(waveFile, FileMode.Open, FileAccess.Read);

    /* get the channel and bits per sample value -> required for calculation */
    BinaryReader br = new BinaryReader(fs);
    int length = (int)fs.Length - 8;
    fs.Position = 22;
    short channels = br.ReadInt16();
    fs.Position = 34;
    short BitsPerSample = br.ReadInt16();

    byte[] arrfile = new byte[fs.Length];
    fs.Position = 0;
    fs.Read(arrfile, 0, arrfile.Length); /* read entire file */
    br.Close();
    fs.Close();

    /* now open for writing */
    fs = new FileStream(waveFile, FileMode.Open, FileAccess.Write);

    BinaryWriter bw = new BinaryWriter(fs);

    bw.BaseStream.Seek(0, SeekOrigin.Begin);
    bw.Write(arrfile, 0, 24); //no change till this point 

    /* refer to waveFormat header */
    bw.Write(sampleRate);
    bw.Write((int)(sampleRate * ((BitsPerSample * channels) / 8)));
    bw.Write((short)((BitsPerSample * channels) / 8));

    /* you can keep the same data from here */
    bw.Write(arrfile, 34, arrfile.Length - 34);

    bw.Close();

    fs.Close();
}

Now you can call the above method and play the wave file with different sample rates:

    changeSampleRate(yourWaveFileToPlay, requiredSampleRate);

    MediaPlayer mp = new MediaPlayer();

    mp.Open(new Uri(yourWaveFileToPlay, UriKind.Absolute));

    mp.Play();
1
votes

To change the bitrate of a WAV file you can't just update its format chunk. You actually have to re-encode it at a new sample-rate / bit-depth (assuming it is PCM), or with a different bitrate selected for your codec if it is not PCM. I have written an article here on converting between various audio formats, including converting between different flavours of PCM. The same article will also explain what to do if you meant changing the sample rate instead of bitrate.