public static byte[] WavToMP3(byte[] wavFile) { using (MemoryStream source = new MemoryStream(wavFile)) using (WaveFileReader rdr = new 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()); } } }
private void compress(string inputFile, string outputFile) { try { WaveLib.WaveFormat format = new WaveLib.WaveFormat(16000, 16, 1); ///////////////////////////////////////////////////// Yeti.Lame.BE_CONFIG cfg = new Yeti.Lame.BE_CONFIG(format, 80); cfg.format.lhv1.bCopyright = 1; cfg.format.lhv1.bCRC = 0; cfg.format.lhv1.bOriginal = 1; cfg.format.lhv1.bPrivate = 1; cfg.format.lhv1.bEnableVBR = 0; /////////////////////////////////////////////////////// Mp3WriterConfig config = new Mp3WriterConfig(format, cfg); bool Compressing = true; try { WaveStream InStr = new WaveStream(inputFile); try { Mp3Writer writer = new Mp3Writer(new FileStream(outputFile, FileMode.Create), config); try { byte[] buff = new byte[writer.OptimalBufferSize]; int read = 0; int actual = 0; long total = InStr.Length; Cursor.Current = Cursors.WaitCursor; try { while ((read = InStr.Read(buff, 0, buff.Length)) > 0) { Application.DoEvents(); writer.Write(buff, 0, read); actual += read; Application.DoEvents(); } } finally { Cursor.Current = Cursors.Default; } } finally { writer.Close(); } } finally { InStr.Close(); } } finally { Compressing = false; } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "An exception has ocurred with the following message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }