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

空扰寡人 提交于 2019-12-18 12:06:21

问题


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.


回答1:


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



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/19166140/how-do-can-i-use-lame-to-encode-an-wav-to-an-mp3-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!