Attualmente sto usando NAudio per catturare il suono e crea solo un file wav. Sto cercando un modo per codificarlo su un mp3 prima di salvare il file. Ho trovato LAME ma quando provo ad aggiungere il file lame_enc.dll dice "Non è stato possibile aggiungere un riferimento. Assicurati che il file sia accessibile e che sia un assembly valido o un componente COM". Qualsiasi aiuto sarebbe apprezzato.Come posso usare LAME per codificare un wav con un mp3 C#
risposta
Basta posizionare il file lame_enc.dll nella cartella bin e non provare ad aggiungerlo al riferimento. Dopo di ciò prova questo codice. Qui è anche possibile impostare il bit rate come 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;
}
}
Il file lame_enc.dll
è una DLL non gestita, il che significa che non si può semplicemente aggiungere un riferimento ad esso nella vostra applicazione .NET. È necessario un wrapper per definire quali sono i punti di ingresso e come vengono chiamati. Per lame_enc.dll
Io uso il Yeti
involucro, che può essere trovato nel codice allegato this CodeProject article.
ho postato uno step-by-step su come utilizzare questo per la codifica MP3 in risposta alla domanda: change format from wav to mp3 in memory stream in NAudio. Questo dovrebbe iniziare.
modo più semplice in .Net 4.0:
Utilizzare il Visual Studio Nuget Package Manager Console:
Install-Package NAudio.Lame
Codice Snip: Invia discorso a un flusso di memoria, quindi salvare come 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);
}
}
retMs non fa nulla in questo esempio? –
http://www.codeproject.com/Articles/501521/How-to-convert-between-most-audio-formats-in-NET – MichaC