How do can I use LAME to encode an wav to an mp3 c#
Asked Answered
M

3

6

I am currently using NAudio to capture the sound and it only creates a wav file. I am looking for a way to encode it to an mp3 before saving the file. I found LAME but when ever i try to add the lame_enc.dll file it says "A reference could not be added. Please make sure the file is accessible, and that is a valid assembly or COM component". Any help would be appreciated.

Macomber answered 3/10, 2013 at 18:8 Comment(1)
codeproject.com/Articles/501521/…Overweigh
P
11

Easiest way in .Net 4.0:

Use the visual studio Nuget Package manager console:

Install-Package NAudio.Lame

Code Snip: Send speech to a memory stream, then save as mp3:

//reference System.Speech
using System.Speech.Synthesis; 
using System.Speech.AudioFormat;

//reference Nuget Package NAudio.Lame
using NAudio.Wave;
using NAudio.Lame; 


using (SpeechSynthesizer reader = new SpeechSynthesizer()) {
    //set some settings
    reader.Volume = 100;
    reader.Rate = 0; //medium

    //save to memory stream
    MemoryStream ms = new MemoryStream();
    reader.SetOutputToWaveStream(ms);

    //do speaking
    reader.Speak("This is a test mp3");

    //now convert to mp3 using LameEncoder or shell out to audiograbber
    ConvertWavStreamToMp3File(ref ms, "mytest.mp3");
}

public static void ConvertWavStreamToMp3File(ref MemoryStream ms, string savetofilename) {
    //rewind to beginning of stream
    ms.Seek(0, SeekOrigin.Begin);

    using (var retMs = new MemoryStream())
    using (var rdr = new WaveFileReader(ms))
    using (var wtr = new LameMP3FileWriter(savetofilename, rdr.WaveFormat, LAMEPreset.VBR_90)) {
        rdr.CopyTo(wtr);
    }
}
Palaeontology answered 30/5, 2014 at 4:36 Comment(1)
retMs does nothing in this example?Woolgrower
J
2

The file lame_enc.dll is an unmanaged DLL, meaning that you can't just add a reference to it in your .NET application. You need a wrapper to define what the entry points are and how they're called . For lame_enc.dll I use the Yeti wrapper, which can be found in the code attached to this CodeProject article.

I posted a step-by-step on how to use this for MP3 encoding in response the question: change format from wav to mp3 in memory stream in NAudio. That should get you started.

Jaredjarek answered 5/10, 2013 at 21:38 Comment(0)
G
1

Just place the lame_enc.dll in bin folder and don't try to add it to reference. After that try this code. Here you can also set bit rate like 64,128,.....

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

        byte[] srm = null;

        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;
        }
       }
Gametangium answered 4/10, 2013 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.