change format from wav to mp3 in memory stream in NAudio
Assuming you're trying to convert the output into MP3, you need something that can handle transcoding the audio. There are a number of tools available, but my personal preference is FFmpeg. It's a command line tool so you will need to take that into account, but otherwise it's very easy to use.
There's lots of information online, but you can start by checking out their documentation here.
This is a bit old now, but since you haven't accepted the answer I previously provided...
I have recently built an extension for NAudio that encapsulates the LAME library to provide simplified MP3 encoding.
Use the NuGet package manager to find NAudio.Lame
. Basic example for using it available here
.
Just wanted to post my example too using NAudio.Lame:
NuGet:
Install-Package NAudio.Lame
Code Snip: Mine obviously returns a byte[] - I have a separate save to disk method b/c I think it makes unit testing easier.
public static byte[] ConvertWavToMp3(byte[] wavFile)
{
using(var retMs = new MemoryStream())
using (var ms = new MemoryStream(wavFile))
using(var rdr = new WaveFileReader(ms))
using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 128))
{
rdr.CopyTo(wtr);
return retMs.ToArray();
}
}
You need an MP3 compressor library. I use Lame via the Yeti Lame wrapper. You can find code and a sample project here.
Steps to get this working:
Copy the following files from
MP3Compressor
to your project:AudioWriters.cs
Lame.cs
Lame_enc.dll
Mp3Writer.cs
Mp3WriterConfig.cs
WaveNative.cs
WriterConfig.cs
In the project properties for
Lame_enc.dll
set theCopy to Output
property toCopy if newer
orCopy always
.Edit
Lame.cs
and replace all instances of:[DllImport("Lame_enc.dll")]
with:
[DllImport("Lame_enc.dll", CallingConvention = CallingConvention.Cdecl)]
Add the following code to your project:
public static Byte[] WavToMP3(byte[] wavFile) { using (MemoryStream source = new MemoryStream(wavFile)) using (NAudio.Wave.WaveFileReader rdr = new NAudio.Wave.WaveFileReader(source)) { WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(rdr.WaveFormat.SampleRate, rdr.WaveFormat.BitsPerSample, rdr.WaveFormat.Channels); // convert to MP3 at 96kbit/sec... Yeti.Lame.BE_CONFIG conf = new Yeti.Lame.BE_CONFIG(fmt, 96); // Allocate a 1-second buffer int blen = rdr.WaveFormat.AverageBytesPerSecond; byte[] buffer = new byte[blen]; // Do conversion using (MemoryStream output = new MemoryStream()) { Yeti.MMedia.Mp3.Mp3Writer mp3 = new Yeti.MMedia.Mp3.Mp3Writer(output, fmt, conf); int readCount; while ((readCount = rdr.Read(buffer, 0, blen)) > 0) mp3.Write(buffer, 0, readCount); mp3.Close(); return output.ToArray(); } } }
Either add a reference to
System.Windows.Forms
to your project (if it's not there already), or editAudioWriter.cs
andWriterConfig.cs
to remove the references. Both of these have ausing System.Windows.Forms;
that you can remove, andWriterConfig.cs
has aConfigControl
declaration that needs to be removed/commented out.
Once all of that is done you should have a functional in-memory wave-file to MP3 converter that you can use to convert the WAV file that you are getting from the SpeechSynthesizer
into an MP3.