Exemplo n.º 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
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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;
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 5
0
    private static void SaveTrack(TrackInfo trackInfo)
    {
      string targetFileName = trackInfo.MusicTag.Title;

      if (m_Drive.Open(trackInfo.Item.Path[0]))
      {
        char[] Drives = CDDrive.GetCDDriveLetters();
        if ((Array.IndexOf(Drives, trackInfo.Item.Path[0]) > -1) && (m_Drive.IsCDReady()) && (m_Drive.Refresh()))
        {
          try
          {
            m_Drive.LockCD();
            if (dlgProgress.IsCanceled)
            {
              m_CancelRipping = true;
            }
            if (!m_CancelRipping)
            {
              try
              {
                try
                {
                  WaveFormat Format = new WaveFormat(44100, 16, 2);
                  BE_CONFIG mp3Config = new BE_CONFIG(Format);
                  if (mp3VBR)
                  {
                    mp3Config.format.lhv1.bEnableVBR = 1;
                    if (mp3FastMode)
                    {
                      mp3Config.format.lhv1.nVbrMethod = VBRMETHOD.VBR_METHOD_NEW;
                    }
                    else
                    {
                      mp3Config.format.lhv1.nVbrMethod = VBRMETHOD.VBR_METHOD_DEFAULT;
                    }
                    mp3Config.format.lhv1.nVBRQuality = mp3Quality;
                  }
                  else if (mp3CBR)
                  {
                    mp3Config.format.lhv1.bEnableVBR = 0;
                    mp3Config.format.lhv1.nVbrMethod = VBRMETHOD.VBR_METHOD_NONE;
                    mp3Config.format.lhv1.dwBitrate = Convert.ToUInt16(Rates[mp3BitRate]);
                  }
                  else
                  {
                    mp3Config.format.lhv1.bEnableVBR = 1;
                    mp3Config.format.lhv1.nVbrMethod = VBRMETHOD.VBR_METHOD_ABR;
                    uint ConToKbwVbrAbr_bps = Convert.ToUInt16(Rates[mp3BitRate]);
                    mp3Config.format.lhv1.dwVbrAbr_bps = ConToKbwVbrAbr_bps * 1000;
                  }

                  if (mp3MONO)
                  {
                    mp3Config.format.lhv1.nMode = MpegMode.MONO;
                  }

                  mp3Config.format.lhv1.bWriteVBRHeader = 1;

                  Stream WaveFile = new FileStream(trackInfo.TempFileName, FileMode.Create, FileAccess.Write);
                  m_Writer = new Mp3Writer(WaveFile, Format, mp3Config);
                  if (!m_CancelRipping)
                  {
                    try
                    {
                      Log.Info("CDIMP: Processing track {0}", trackInfo.MusicTag.Track);

                      DateTime InitTime = DateTime.Now;
                      if (
                        m_Drive.ReadTrack(trackInfo.MusicTag.Track, new CdDataReadEventHandler(WriteWaveData),
                                          new CdReadProgressEventHandler(CdReadProgress)) > 0)
                      {
                        if (dlgProgress.IsCanceled)
                        {
                          m_CancelRipping = true;
                        }
                        if (!m_CancelRipping)
                        {
                          TimeSpan Duration = DateTime.Now - InitTime;
                          double Speed = m_Drive.TrackSize(trackInfo.MusicTag.Track) / Duration.TotalSeconds /
                                         Format.nAvgBytesPerSec;
                          Log.Info("CDIMP: Done reading track {0} at {1:0.00}x speed", trackInfo.MusicTag.Track, Speed);
                        }
                      }
                      else
                      {
                        Log.Info("CDIMP: Error reading track {0}", trackInfo.MusicTag.Track);
                        m_Writer.Close();
                        WaveFile.Close();
                        if (File.Exists(trackInfo.TempFileName))
                        {
                          try
                          {
                            File.Delete(trackInfo.TempFileName);
                          }
                          catch {}
                        }
                        //progressBar1.Value = 0;
                      }
                    }
                    finally
                    {
                      m_Writer.Close();
                      m_Writer = null;
                      WaveFile.Close();
                      Lame_encDll.beWriteVBRHeader(trackInfo.TempFileName);
                    }
                  }
                }
                finally {}
              }
              finally
              {
                m_Drive.Close();
              }
            }
          }
          finally
          {
            //progressBar1.Value = 0;
          }
        }
        if (dlgProgress.IsCanceled)
        {
          m_CancelRipping = true;
        }
        if (m_CancelRipping)
        {
          if (File.Exists(trackInfo.TempFileName))
          {
            File.Delete(trackInfo.TempFileName);
          }
          m_Drive.Close();
        }
      }
    }