0

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;
     }

    }
sainath sagar
  • 499
  • 2
  • 10
  • 28

1 Answers1

0

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.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • But @Mark AudioFileReader accepts only filename as parameter. You can see my above code where after converting to mp3 I need to set volume. Is it possible to set volume at that level? – sainath sagar Oct 04 '13 at 05:41
  • AudioFileReader uses the SampleChannel class to convert IWaveProvider to ISampleProvider and add a VolumeProperty. – Mark Heath Oct 04 '13 at 06:46
  • Hi @Mark i need to pass only stream but AudioFileReader has file name as parameter which is not my requirement. – sainath sagar Oct 04 '13 at 07:43