Exemple #1
0
        /// <summary>
        /// Преобразует поток с wav файлом в mp3
        /// </summary>
        /// <param name="wavFile"></param>
        public static void WavToMP3(string wavPath, string Mp3path)
        {
            using (Stream source = new FileStream(wavPath, FileMode.Open))
                using (Stream Mp3file = new FileStream(Mp3path, FileMode.Create))
                    using (NAudio.Wave.WaveFileReader rdr = new NAudio.Wave.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];

                        Yeti.MMedia.Mp3.Mp3Writer mp3 = new Yeti.MMedia.Mp3.Mp3Writer(Mp3file, fmt, conf);

                        int readCount;
                        while ((readCount = rdr.Read(buffer, 0, blen)) > 0)
                        {
                            mp3.Write(buffer, 0, readCount);
                        }

                        Mp3file.Flush();
                        mp3.Close();
                    }
        }
Exemple #2
0
 /// <summary>
 /// Create a Mp3Writer with specific MP3 format
 /// </summary>
 /// <param name="Output">Stream that will hold the MP3 resulting data</param>
 /// <param name="InputDataFormat">PCM format of input data</param>
 /// <param name="Mp3Config">Desired MP3 config</param>
 public Mp3Writer(Stream Output, WaveFormat InputDataFormat, BE_CONFIG Mp3Config)
   :base(Output, InputDataFormat)
 {
   try
   {
     m_Mp3Config = Mp3Config;
     uint LameResult = Lame_encDll.beInitStream(m_Mp3Config, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream);
     if ( LameResult != Lame_encDll.BE_ERR_SUCCESSFUL)
     {
       throw new ApplicationException(string.Format("Lame_encDll.beInitStream failed with the error code {0}", LameResult));
     }
     m_InBuffer = new byte[m_InputSamples*2]; //Input buffer is expected as short[]
     m_OutBuffer = new byte[m_OutBufferSize];
   }
   catch
   {
     base.Close();
     throw;
   }
 }
Exemple #3
0
 public static extern uint beInitStream(BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize,
                                        ref uint phbeStream);
Exemple #4
0
 public static extern uint beInitStream(BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);
        /// <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;
        }
    public EditMp3Writer()
    {
      // This call is required by the Windows.Forms Form Designer.
      InitializeComponent();

      m_Config = new BE_CONFIG(editFormat1.Format);
      DoSetInitialValues();
    }
        public void Download(Track track)
        {
            try
            {
                counter = 0;
                downloadingTrack = track;
                var stream = new FileStream("downloading", FileMode.Create);
                var waveFormat = new WaveFormat(44100, 16, 2);
                var beConfig = new BE_CONFIG(waveFormat, 320);
                wr = new Mp3Writer(stream, waveFormat, beConfig);
                session.PlayerLoad(track);
                session.PlayerPlay(true);
                if (OnDownloadProgress != null)
                    OnDownloadProgress(0);

            }
            catch (Exception e)
            {
                LogString("Error when playing/downloading!" + " Track Name:" + SpotifyDownloader.GetTrackFullName(downloadingTrack));
            }
        }
 public void Download(Track track)
 {
     counter = 0;
     downloadingTrack = track;
     var stream = new FileStream("downloading", FileMode.Create);
     var waveFormat = new WaveFormat(44100, 16, 2);
     var beConfig = new BE_CONFIG(waveFormat, 320);
     wr = new Mp3Writer(stream, waveFormat, beConfig);
     session.PlayerLoad(track);
     session.PlayerPlay(true);
     if (OnDownloadProgress != null)
         OnDownloadProgress(0);
 }
Exemple #9
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();
        }
      }
    }
 public Mp3WriterConfig(WaveFormat InFormat, BE_CONFIG beconfig)
   : base(InFormat)
 {
   m_BeConfig = beconfig;
 }
 protected Mp3WriterConfig(SerializationInfo info, StreamingContext context)
   : base(info, context)
 {
   m_BeConfig = (BE_CONFIG)info.GetValue("BE_CONFIG", typeof (BE_CONFIG));
 }
        public void Download(Track track)
        {
            if (!canPlay(track))
            {
                if (OnDownloadComplete != null)
                    OnDownloadComplete(false);
                return;
            }

            counter = 0;
            downloadingTrack = track;

            var dir = downloadPath + escape(downloadingTrack.Album().Name()) + "\\";
            var fileName = dir + escape(GetTrackFullName(downloadingTrack)) + ".mp3";
            if (GetDownloadType() == DownloadType.SKIP && File.Exists(fileName))
            {
                if (OnDownloadProgress != null)
                    OnDownloadProgress(100);

                if (OnDownloadComplete != null)
                    OnDownloadComplete(true);
                return;
            }

            var stream = new FileStream("downloading", FileMode.Create);
            var waveFormat = new WaveFormat(44100, 16, 2);
            var beConfig = new BE_CONFIG(waveFormat, 320);
            wr = new Mp3Writer(stream, waveFormat, beConfig);
            session.PlayerLoad(track);
            session.PlayerPlay(true);
            if (OnDownloadProgress != null)
                OnDownloadProgress(0);
        }