Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var files = Directory.GetFiles("../testfiles/", "*.wav", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                _lameEnc = new Mp3Encoder();

                var audioFile = new WavReader();
                audioFile.OpenFile(file);

                var srcFormat = audioFile.GetFormat();

                _lameEnc.SetFormat(srcFormat, srcFormat);

                var inBuffer = audioFile.readWav();

                var outBuffer = new byte[inBuffer.Length];

                var timer = new System.Diagnostics.Stopwatch();
                timer.Start();
                var len = _lameEnc.EncodeBuffer(inBuffer, 0, inBuffer.Length, outBuffer);
                timer.Stop();

                _lameEnc.Close();

                // _lameDec = new LameDecoder();

                var outFile = File.Create(file + ".mp3");
                outFile.Write(outBuffer, 0, len);
                outFile.Close();

                Console.WriteLine($"Converted {file} to MP3 in {timer.ElapsedMilliseconds / 1000}s");
            }
        }
Exemplo n.º 2
0
        private static void ComvertWav2Mp3(string dirName)
        {
            var bitrate = 64;
            var encoder = new Mp3Encoder(new AudioFormat(),
                                         bitrate, Mp3Encoder.CHANNEL_MODE_MONO, Mp3Encoder.QUALITY_MIDDLE, true);
            var audioFile = new WavReader();

            audioFile.OpenFile(dirName + TMP_EXTENTION);
            var srcFormat = audioFile.GetFormat();

            encoder.SetFormat(srcFormat, srcFormat);
            var inBuffer  = audioFile.readWav();
            var outBuffer = new byte[inBuffer.Length];

            var len = encoder.EncodeBuffer(inBuffer, 0, inBuffer.Length, outBuffer);

            encoder.Close();

            using (var outfile = System.IO.File.Create(dirName + MP3_EXTENTION))
                outfile.Write(outBuffer, 0, len);
        }