public Converter(IEnumerable <string> fileNames, LAMEPreset quality) { if (fileNames == null) { throw new ArgumentNullException(nameof(fileNames)); } _quality = quality; _fileNames = new List <string>(fileNames); #if DEBUG _threadCount = 1; #else _threadCount = Environment.ProcessorCount; #endif var threads = new List <Thread>(); for (int i = 0; i < _threadCount; i++) { threads.Add(new Thread(ThreadProc) { IsBackground = true }); } foreach (var thread in threads) { thread.Start(); } }
private Arguments(string directory, AfterConvert afterConvert, LAMEPreset quality, List <string> exclude) { Directory = directory; AfterConvert = afterConvert; Quality = quality; Exclude = new ReadOnlyCollection <string>(exclude); }
private void OkButton_Click(object sender, EventArgs e) { Mp3Quality = Mp3QualityOptions[Mp3QualityComboBox.SelectedIndex].Value; Properties.Settings.Default.Mp3Quality = Mp3Quality; Properties.Settings.Default.Save(); }
private static async Task Main(string[] args) { const long fadeTimeInMs = 2000; const string infile = @"../../../input.mp3"; const string outfileAll = @"../../../output.mp3"; const LAMEPreset lamePreset = LAMEPreset.ABR_128; var stream = new MemoryStream(); /* * TODO: Reuse the same Mp3Reader (var reader = GetMp3FileReaderFromFilePath(infile)) for all steps to increase performance? * TODO: Use Constant bitrate instead of Average bitrate. Turn off run-length encoding (RLE)??? Reason: To remove the crackling noise at the transition phases. * TODO: Clean Code! */ /* * A very trivial benchmark not very accurate. * TODO: Use more files, warm up phase for benchmarking... */ var stopWatch = new Stopwatch(); stopWatch.Start(); // Fade in and converting back and forward await using (var reader = GetMp3FileReaderFromFilePath(infile)) { var sampleProviderBegin = reader.ToSampleProvider().Take(TimeSpan.FromMilliseconds(fadeTimeInMs)); var fadeBegin = new FadeInOutSampleProvider(sampleProviderBegin); fadeBegin.BeginFadeIn(fadeTimeInMs); await ConvertWaveProviderToMp3Stream(fadeBegin.ToWaveProvider(), stream, lamePreset); } // Mid part, just coping the the raw mp3 stream await using (var reader = GetMp3FileReaderFromFilePath(infile)) { var beginMs = fadeTimeInMs; var endMs = reader.TotalTime.TotalMilliseconds - fadeTimeInMs; ConcatFromReaderToStream(reader, stream, beginMs, endMs); } // Fade out and converting back and forward await using (var reader = GetMp3FileReaderFromFilePath(infile)) { var sampleProviderEnd = reader.ToSampleProvider().Skip(reader.TotalTime - TimeSpan.FromMilliseconds(fadeTimeInMs)); var fadeEnd = new FadeInOutSampleProvider(sampleProviderEnd); fadeEnd.BeginFadeOut(fadeTimeInMs); await ConvertWaveProviderToMp3Stream(fadeEnd.ToWaveProvider(), stream, lamePreset); } // Write mp3 stream to file await using (var fileStream = File.Create(outfileAll)) { stream.Seek(0, SeekOrigin.Begin); await stream.CopyToAsync(fileStream); } Console.WriteLine($"Consumed time {stopWatch.Elapsed}"); }
// ################################################################## // ############################# Methods ############################ // ################################################################## /// <summary> /// Constructor /// </summary> /// <param name="tempWavFilePath">The filename of the temp raw WAV file</param> /// <param name="mp3Quality">The MP3 quality. Check NAudio.Lame.LAMEPreset enum for details </param> public Recorder(string tempWavFilePath, LAMEPreset mp3Quality) { // check if WAV file already exists if (File.Exists(_tempWavFilePath = tempWavFilePath)) { _state = RecorderState.WavFileExists; } _mp3Preset = mp3Quality; }
private void SettingsToolStripButton_Click(object sender, EventArgs e) { using (var dialog = new SettingsDialog()) { if (dialog.ShowDialog() == DialogResult.OK) { mp3Quality = dialog.Mp3Quality; } } }
private void WaveToMP3(string waveFileName, string mp3FileName, LAMEPreset bitRate = LAMEPreset.ABR_128) { using (var reader = new WaveFileReader(waveFileName)) using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate)) reader.CopyTo(writer); GetFileKey(() => { PutObjectWithHeader(_resultAction); }); }
public Mp3Writer(Stream stream, int sampleRate, int bitRate, int channels, LAMEPreset preset) { if (stream == null) { throw new ArgumentNullException("stream"); } if (channels != 1 && channels != 2) { throw new ArgumentException(string.Format("Unsupported number of channels {0}", channels), "channels"); } if (bitRate != 16) { throw new ArgumentException(string.Format("Unsupported PCM sample size {0}", bitRate), "bitRate"); } if (sampleRate < 8000 || sampleRate > 48000) { throw new ArgumentException(string.Format("Unsupported Sample Rate {0}", sampleRate), "sampleRate"); } _stream = stream; _sampleRate = sampleRate; _bitRate = bitRate; _channels = channels; _blockAlign = (short)(channels * (bitRate / 8)); _averageBytesPerSecond = sampleRate * _blockAlign; _preset = preset; _inBuffer = new ArrayUnion(_averageBytesPerSecond); _outBuffer = new byte[sampleRate * 5 / 4 + 7200]; this._lame = new LibMp3Lame(); this._lame.InputSampleRate = _sampleRate; this._lame.NumChannels = _channels; this._lame.SetPreset(_preset); this._lame.InitParams(); if (_channels == 1) { _encode = EncodePcm16Mono; } else { _encode = EncodePcm16stereo; } }
public Recorder(FrmEspionSpotify espionSpotifyForm, string path, LAMEPreset bitrate, Format format, Song song, int minTime, bool strucDossiers, string charSeparator, bool bCdTrack, bool bNumFile, int compteur) { SongGotDeleted = false; _form = espionSpotifyForm; _path = path; _bitrate = bitrate; _format = format; _song = song; _minTime = minTime; _strucDossiers = strucDossiers; _charSeparator = charSeparator; _compteur = compteur; _bCdTrack = bCdTrack; _bNumFile = bNumFile; }
// ################################################################## // ############################# Methods ############################ // ################################################################## /// <summary> /// Default constructor /// </summary> public Downtify(string tempFolder, string mp3FilesFolder, LAMEPreset mp3Preset) { // load Spotify LocalSpotifyAPI = new SpotifyLocalAPI(); _webSpotifyAPI = new SpotifyWebAPI(); DoAuth(); // load other controllers _soundControl = new Sound(SpotifyProcessName); _mp3Preset = mp3Preset; _mp3FilesFolder = mp3FilesFolder; _tempFolder = tempFolder; _recorder = new Recorder(_tempFolder + TempWavName, _mp3Preset); // register events _isRecording = false; LocalSpotifyAPI.OnPlayStateChange += _finishDownloadSongOnPlayStateChange; LocalSpotifyAPI.OnTrackChange += _finishDownloadSongOnTrackChange; }
public Watcher(FrmEspionSpotify espionSpotifyForm, string path, LAMEPreset bitrate, Recorder.Format format, VolumeWin sound, int minTime, bool strucDossiers, string charSeparator, bool bCdTrack, bool bNumFile, int cdNumTrack) { if (path == null) { path = ""; } _form = espionSpotifyForm; _path = path; _bitrate = bitrate; _format = format; _sound = sound; NumTrack = bCdTrack || bNumFile ? cdNumTrack : -1; _minTime = minTime; _strucDossiers = strucDossiers; _charSeparator = charSeparator; _bCdTrack = bCdTrack; _bNumFile = bNumFile; }
private void MainForm_Load(object sender, EventArgs e) { mp3Quality = Properties.Settings.Default.Mp3Quality; }
private static async Task ConvertWaveProviderToMp3File(IWaveProvider waveProvider, string outPutFile, LAMEPreset lamePreset, CancellationToken ctx = default) { await using var memoryStream = new MemoryStream(); using var reader = new StreamReader(memoryStream); WaveFileWriter.WriteWavFileToStream(memoryStream, waveProvider); await using var lameMp3FileWriter = new LameMP3FileWriter(outPutFile, waveProvider.WaveFormat, lamePreset); await reader.BaseStream.CopyToAsync(lameMp3FileWriter, ctx); }
private static async Task <Stream> ConvertWaveProviderToMp3Stream(IWaveProvider waveProvider, Stream outStream, LAMEPreset lamePreset, CancellationToken ctx = default) { var memoryStream = new MemoryStream(); using var streamReader = new StreamReader(memoryStream); WaveFileWriter.WriteWavFileToStream(memoryStream, waveProvider); await using var lameMp3StreamWriter = new LameMP3FileWriter(outStream, waveProvider.WaveFormat, lamePreset); await streamReader.BaseStream.CopyToAsync(lameMp3StreamWriter, ctx); return(outStream); }
/// <summary>Create MP3FileWriter to write to supplied stream</summary> /// <param name="outStream">Stream to write encoded data to</param> /// <param name="format">Input WaveFormat</param> /// <param name="quality">LAME quality preset</param> /// <param name="id3">Optional ID3 data block</param> public LameMP3FileWriter(Stream outStream, WaveFormat format, LAMEPreset quality, ID3TagData id3 = null) : this(outStream, format, new LameConfig { Preset = quality, ID3 = id3 }) { }
/// <summary>Create MP3FileWriter to write to supplied stream</summary> /// <param name="outStream">Stream to write encoded data to</param> /// <param name="format">Input WaveFormat</param> /// <param name="quality">LAME quality preset</param> public LameMP3FileWriter(Stream outStream, WaveFormat format, LAMEPreset quality) : base() { // sanity check if (outStream == null) { throw new ArgumentNullException("outStream"); } if (format == null) { throw new ArgumentNullException("format"); } // check for unsupported wave formats if (format.Channels != 1 && format.Channels != 2) { throw new ArgumentException(string.Format("Unsupported number of channels {0}", format.Channels), "format"); } if (format.Encoding != WaveFormatEncoding.Pcm && format.Encoding != WaveFormatEncoding.IeeeFloat) { throw new ArgumentException(string.Format("Unsupported encoding format {0}", format.Encoding.ToString()), "format"); } if (format.Encoding == WaveFormatEncoding.Pcm && format.BitsPerSample != 16) { throw new ArgumentException(string.Format("Unsupported PCM sample size {0}", format.BitsPerSample), "format"); } if (format.Encoding == WaveFormatEncoding.IeeeFloat && format.BitsPerSample != 32) { throw new ArgumentException(string.Format("Unsupported Float sample size {0}", format.BitsPerSample), "format"); } if (format.SampleRate < 8000 || format.SampleRate > 48000) { throw new ArgumentException(string.Format("Unsupported Sample Rate {0}", format.SampleRate), "format"); } // select encoder function that matches data format if (format.Encoding == WaveFormatEncoding.Pcm) { if (format.Channels == 1) { _encode = encode_pcm_16_mono; } else { _encode = encode_pcm_16_stereo; } } else { if (format.Channels == 1) { _encode = encode_float_mono; } else { _encode = encode_float_stereo; } } // Set base properties this.inputFormat = format; this.outStream = outStream; this.disposeOutput = false; // Allocate buffers based on sample rate this.inBuffer = new ArrayUnion(format.AverageBytesPerSecond); this.outBuffer = new byte[format.SampleRate * 5 / 4 + 7200]; // Initialize lame library this._lame = new LibMp3Lame(); this._lame.InputSampleRate = format.SampleRate; this._lame.NumChannels = format.Channels; this._lame.SetPreset(quality); this._lame.InitParams(); }
/// <summary>Create MP3FileWriter to write to a file on disk</summary> /// <param name="outFileName">Name of file to create</param> /// <param name="format">Input WaveFormat</param> /// <param name="quality">LAME quality preset</param> public LameMP3FileWriter(string outFileName, WaveFormat format, LAMEPreset quality) : this(File.Create(outFileName), format, quality) { this.disposeOutput = true; }
public MP3SongEncoder(LAMEPreset lamePreset) { this.LamePreset = lamePreset; }
/// <summary>Create MP3FileWriter to write to a file on disk</summary> /// <param name="outFileName">Name of file to create</param> /// <param name="format">Input WaveFormat</param> /// <param name="quality">LAME quality preset</param> /// <param name="id3">Optional ID3 data block</param> public LameMP3FileWriter(string outFileName, WaveFormat format, LAMEPreset quality, ID3TagData id3 = null) : this(File.Create(outFileName), format, quality, id3) { _disposeOutput = true; }
public static void WaveToMP3(string waveFileName, string mp3FileName, LAMEPreset bitRate = LAMEPreset.ABR_128) { using (var reader = new WaveFileReader(waveFileName)) using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate)) reader.CopyTo(writer); }
/// <summary> /// Static constructor which initialize properties /// </summary> static AudioIO() { ExportQuality = LAMEPreset.INSANE; }
/// <summary> /// Set the MP3 preset used by the recorder /// </summary> /// <param name="mp3Preset">MP3 preset</param> public void SetMP3Preset(LAMEPreset mp3Preset) { _mp3Preset = mp3Preset; _recorder.SetMP3Preset(mp3Preset); }
/// <summary> /// Setter for MP3Preset /// </summary> /// <param name="mp3Preset">the MP3 preset</param> public void SetMP3Preset(LAMEPreset mp3Preset) { _mp3Preset = mp3Preset; }
/// <summary>Create MP3FileWriter to write to supplied stream</summary> /// <param name="outStream">Stream to write encoded data to</param> /// <param name="format">Input WaveFormat</param> /// <param name="quality">LAME quality preset</param> /// <param name="id3">Optional ID3 data block</param> public LameMP3FileWriter(Stream outStream, WaveFormat format, LAMEPreset quality, ID3TagData id3 = null) : base() { if (format == null) { throw new ArgumentNullException("format"); } // check for unsupported wave formats if (format.Channels != 1 && format.Channels != 2) { throw new ArgumentException($"Unsupported number of channels {format.Channels}", "format"); } if (format.Encoding != WaveFormatEncoding.Pcm && format.Encoding != WaveFormatEncoding.IeeeFloat) { throw new ArgumentException($"Unsupported encoding format {format.Encoding}", "format"); } if (format.Encoding == WaveFormatEncoding.Pcm && format.BitsPerSample != 16) { throw new ArgumentException($"Unsupported PCM sample size {format.BitsPerSample}", "format"); } if (format.Encoding == WaveFormatEncoding.IeeeFloat && format.BitsPerSample != 32) { throw new ArgumentException($"Unsupported Float sample size {format.BitsPerSample}", "format"); } if (format.SampleRate < 8000 || format.SampleRate > 48000) { throw new ArgumentException($"Unsupported Sample Rate {format.SampleRate}", "format"); } // select encoder function that matches data format if (format.Encoding == WaveFormatEncoding.Pcm) { if (format.Channels == 1) { _encode = Encode_pcm_16_mono; } else { _encode = Encode_pcm_16_stereo; } } else { if (format.Channels == 1) { _encode = Encode_float_mono; } else { _encode = Encode_float_stereo; } } // Set base properties _inputFormat = format; _outStream = outStream ?? throw new ArgumentNullException("outStream"); _disposeOutput = false; // Allocate buffers based on sample rate _inBuffer = new ArrayUnion(format.AverageBytesPerSecond); _outBuffer = new byte[format.SampleRate * 5 / 4 + 7200]; // Initialize lame library _lame = new LibMp3Lame { InputSampleRate = format.SampleRate, NumChannels = format.Channels }; _lame.SetPreset((int)quality); if (id3 != null) { ApplyID3Tag(id3); } _lame.InitParams(); }
/// <summary>Create MP3FileWriter to write to supplied stream</summary> /// <param name="outStream">Stream to write encoded data to</param> /// <param name="format">Input WaveFormat</param> /// <param name="quality">LAME quality preset</param> public LameMP3FileWriter(Stream outStream, WaveFormat format, LAMEPreset quality) : base() { // sanity check if (outStream == null) throw new ArgumentNullException("outStream"); if (format == null) throw new ArgumentNullException("format"); // check for unsupported wave formats if (format.Channels != 1 && format.Channels != 2) throw new ArgumentException(string.Format("Unsupported number of channels {0}", format.Channels), "format"); if (format.Encoding != WaveFormatEncoding.Pcm && format.Encoding != WaveFormatEncoding.IeeeFloat && format.Encoding != WaveFormatEncoding.Extensible) throw new ArgumentException(string.Format("Unsupported encoding format {0}", format.Encoding.ToString()), "format"); if (format.Encoding == WaveFormatEncoding.Pcm && format.BitsPerSample != 16) throw new ArgumentException(string.Format("Unsupported PCM sample size {0}", format.BitsPerSample), "format"); if (format.Encoding == WaveFormatEncoding.Extensible && format.BitsPerSample != 16) throw new ArgumentException(string.Format("Unsupported Extensible sample size {0}", format.BitsPerSample), "format"); if (format.Encoding == WaveFormatEncoding.IeeeFloat && format.BitsPerSample != 32) throw new ArgumentException(string.Format("Unsupported Float sample size {0}", format.BitsPerSample), "format"); if (format.SampleRate < 8000 || format.SampleRate > 96000) throw new ArgumentException(string.Format("Unsupported Sample Rate {0}", format.SampleRate), "format"); // select encoder function that matches data format if (format.Encoding == WaveFormatEncoding.Pcm || format.Encoding == WaveFormatEncoding.Extensible) { if (format.Channels == 1) _encode = encode_pcm_16_mono; else _encode = encode_pcm_16_stereo; } else { if (format.Channels == 1) _encode = encode_float_mono; else _encode = encode_float_stereo; } // Set base properties this.inputFormat = format; this.outStream = outStream; this.disposeOutput = false; // Allocate buffers based on sample rate this.inBuffer = new ArrayUnion(format.AverageBytesPerSecond); this.outBuffer = new byte[format.SampleRate * 5 / 4 + 7200]; // Initialize lame library this._lame = new LibMp3Lame(); this._lame.InputSampleRate = format.SampleRate; this._lame.NumChannels = format.Channels; this._lame.SetPreset(quality); this._lame.InitParams(); }
private LAMEPresetWrapper(string description, LAMEPreset preset) { Description = description; LamePreset = (int)preset; }