Пример #1
0
        private static void saveWaveStreamToMP3File(string fileNameToCreate, BackgroundWorker worker, Stream audioStream)
        {
            WaveStream waveStream = new WaveStream(audioStream);                    // read from stream

            //before convert check for and add .mp3 file extention if nessary
            string mp3FileName = fileNameToCreate;
            if (Path.GetExtension(fileNameToCreate).ToLower() != ".mp3".ToLower())
            { mp3FileName += ".mp3"; }

            // convert wav stream to mp3 stream then write
            Mp3WriterConfig mp3Config = new Mp3WriterConfig(waveStream.Format);
            try
            {
                Mp3Writer writer = new Mp3Writer(new FileStream(mp3FileName, FileMode.Create), mp3Config);
                try
                {
                    byte[] buff = new byte[writer.OptimalBufferSize];
                    int read = 0;
                    int actual = 0;
                    long total = waveStream.Length;
                    int progress;
                    try
                    {
                        while ((read = waveStream.Read(buff, 0, buff.Length)) > 0)
                        {
                            writer.Write(buff, 0, read);
                            actual += read;
                            progress = ((int)(((long)actual * 100) / total)) / 2 + 50; // divide by 2 and add 50 so only latter 50% will be used up
                            worker.ReportProgress(progress);
                        }
                    }
                    catch { }
                }
                catch { }
                finally
                {
                    writer.Close();
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowAndLogException(exception);
            }
            finally
            {
                waveStream.Close();
            }

            audioStream.Close();        // close audio stream
        }
Пример #2
0
        public static void Wav2Mp3(string InFile, string OutFile)
        {
            WaveStream InStr = new WaveStream(InFile);
            Mp3WriterConfig m_Config = new Mp3WriterConfig(InStr.Format);
            try
            {
                Mp3Writer writer = new Mp3Writer(new FileStream(OutFile, FileMode.Create), m_Config);
                try
                {
                    byte[] buff = new byte[writer.OptimalBufferSize];
                    int read = 0;
                    long total = InStr.Length;

                    while ((read = InStr.Read(buff, 0, buff.Length)) > 0)
                    {
                        writer.Write(buff, 0, read);
                    }

                }
                finally
                {
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                string path = MainForm.AppPath + "\\log";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                File.AppendAllText(path+"\\log"+DateTime.Now.ToString("yyyy-MM-dd")+".txt",ex.Message+"\n\r");
            }
            finally
            {
                InStr.Close();
            }
        }
Пример #3
0
        /// <summary>
        /// Converts Wav to MP3.
        /// </summary>
        /// <param name="inputFile">The input file.</param>
        /// <param name="outputPath">The output path.</param>
        /// <returns>
        /// Path of changed file
        /// </returns>
        public static string ConvertWavToMp3(string inputFile, string outputPath)
        {
            var reader = new WaveStream(inputFile);

            try
            {
                var config = new BE_CONFIG(reader.Format, (uint)(Math.Abs(BitRate - 0.0) < 0.1 ? DefaultBitRate : BitRate));
                var writer = new Mp3Writer(new FileStream(outputPath, FileMode.Create), reader.Format, config);

                try
                {
                    var buffer = new byte[writer.OptimalBufferSize];
                    int read;
                    while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        writer.Write(buffer, 0, read);
                    }
                }
                finally
                {
                    writer.Close();
                }
            }
            finally
            {
                reader.Close();
            }

            return outputPath;
        }
Пример #4
0
        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);
            }
        }