Question

Here in this code, I am using NAudio and Lame to convert wav file to mp3 stream and I am getting mp3 stream output without any issues. But apart from this I also want to set volume to the final mp3 stream. Any help is greatly appreciated.

 public byte[] ConvertWavToMP3(byte[] bt, uint bitrate)
 {         

     MemoryStream ms = new MemoryStream(bt);
     ms.Seek(0, SeekOrigin.Begin);
     var ws = new WaveFileReader(ms);

     byte[] wavdata = null;
     using (MemoryStream wavstrm = new MemoryStream())
     using (WaveFileWriter wavwri = new WaveFileWriter(wavstrm, ws.WaveFormat))
     {
        ws.CopyTo(wavwri);
        wavdata = wavstrm.ToArray();
     }

     WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(ws.WaveFormat.SampleRate, ws.WaveFormat.BitsPerSample, ws.WaveFormat.Channels);

     Yeti.Lame.BE_CONFIG beconf = new Yeti.Lame.BE_CONFIG(fmt, bitrate);

     using (MemoryStream mp3strm = new MemoryStream())
     using (Mp3Writer mp3wri = new Mp3Writer(mp3strm, fmt, beconf))
     {               

        mp3wri.Write(wavdata, 0, wavdata.Length);
        byte[] mp3data = mp3strm.ToArray();
        return mp3data;
     }

    }
Was it helpful?

Solution

You can use AudioFileReader which will give you a Volume property you can adjust (1.0 is full scale). It will also turn the audio into an ISampleProvider with IEEE float samples, but I think LAME accepts IEEE float, otherwise use SampleToWaveProvider16 to get back down to 16 bit integer samples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top