private static IWaveProvider resampleTo44100(ISampleProvider audioFile) { if (audioFile.WaveFormat.SampleRate != 44100) { return(new MediaFoundationResampler(audioFile.ToWaveProvider(), 44100)); } return(audioFile.ToWaveProvider()); }
private static MemoryStream CreateMemoryStreamFromSample(ISampleProvider playlist) { var waveProvider = playlist.ToWaveProvider(); byte[] buffer = new byte[16 * 1024]; var outputStream = new MemoryStream(); var waveFileWriter = new WaveFileWriter(outputStream, waveProvider.WaveFormat); { int read; while ((read = waveProvider.Read(buffer, 0, buffer.Length)) > 0) { waveFileWriter.Write(buffer, 0, read); } waveFileWriter.Flush(); return(outputStream); } }
public MixingWaveProvider32 Overlay(ISampleProvider music, ISampleProvider watermark, double watermarkOffset) { if (music == null || watermark == null) { throw new ArgumentNullException("Не удалось наложить водяной знак. Трек или водяной знак были пустыми"); } // Инициализация энкодера(чтобы получать на выходе любой формат) NAudio.MediaFoundation.MediaFoundationApi.Startup(); var sampleProvider = new OffsetSampleProvider(watermark.ToWaveProvider().ToSampleProvider()); //Деление на sample sampleProvider.DelayBy = TimeSpan.FromSeconds(watermarkOffset); //Добавление задержки var watermarkWaveProvider = sampleProvider.ToWaveProvider(); //Перевод в wave водяного знака var musicWaveProvider = music.ToWaveProvider(); //Перевод в wave музыки // Слияние трека и водяного знака var result = new MixingWaveProvider32(new[] { musicWaveProvider, watermarkWaveProvider }); result.ToSampleProvider().ToWaveProvider16(); return(result); }
private static void SaveToMp3(string fileName, ISampleProvider audioToSave) { MediaFoundationEncoder.EncodeToMp3(audioToSave.ToWaveProvider(), fileName, 320000); }
private async Task ConvertFiles(Func <BackgroundTaskControl, Task> callBack, BackgroundTaskControl bgtc) { await Task.Delay(250); // give time for rendering changes WaveStream inputStream = null; for (int i = 0; i < filePaths.Count(); i++) { try { if (filePaths[i].EndsWith(".ogg")) { inputStream = new VorbisWaveReader(filePaths[i]); } else if (filePaths[i].EndsWith(".flac")) { inputStream = new FlacReader(filePaths[i]); } else { inputStream = new AudioFileReader(filePaths[i]); } string selectedFileType = ""; Dispatcher.Invoke(() => { selectedFileType = OutputFileTypeComboBox.SelectedItem as string; WaitingLabel.Content = $"Now converting {safeFileNames[i]} to {selectedFileType.Substring(selectedFileType.IndexOf("(")+1,4)}"; if (bgtc != null) { bgtc.BackgroundTaskLabel.Content = WaitingLabel.Content; } }); if (selectedFileType.EndsWith("(.mp3)")) { string monoStereoSelection = ""; Dispatcher.Invoke(() => { monoStereoSelection = MSConvertComboBox.SelectedItem as string; }); if (inputStream.WaveFormat.BitsPerSample == 16) { IWaveProvider provider = MonoStereoConvert16(inputStream, monoStereoSelection == "Mono"); using (FileStream fileStream = File.Create($"{outputPath}/{safeFileNames[i]}.mp3")) using (NAudio.Lame.LameMP3FileWriter writer = new NAudio.Lame.LameMP3FileWriter(fileStream, provider.WaveFormat, 320000)) { byte[] buffer = new byte[4096]; int bytesRead = 0; do { bytesRead = provider.Read(buffer, 0, buffer.Length); writer.Write(buffer, 0, bytesRead); } while (bytesRead > 0); } } else if (inputStream.WaveFormat.BitsPerSample == 32) { ISampleProvider provider = MonoStereoConvert32(inputStream as ISampleProvider, monoStereoSelection == "Mono"); using (FileStream fileStream = File.Create($"{outputPath}/{safeFileNames[i]}.mp3")) using (NAudio.Lame.LameMP3FileWriter writer = new NAudio.Lame.LameMP3FileWriter(fileStream, provider.WaveFormat, 320000)) { byte[] buffer = new byte[4096]; int bytesRead = 0; do { var providerAsWave = provider.ToWaveProvider(); // need to write with bytes not floats bytesRead = providerAsWave.Read(buffer, 0, buffer.Length); writer.Write(buffer, 0, bytesRead); } while (bytesRead > 0); } } TagLib.File outputTags = TagLib.File.Create($"{outputPath}//{safeFileNames[i]}.mp3"); TagLib.File inputTags = TagLib.File.Create(filePaths[i]); outputTags.Tag.Album = inputTags.Tag.Album; outputTags.Tag.AlbumArtists = inputTags.Tag.AlbumArtists; outputTags.Tag.Genres = inputTags.Tag.Genres; outputTags.Tag.Performers = inputTags.Tag.Performers; outputTags.Tag.Year = inputTags.Tag.Year; outputTags.Tag.Pictures = inputTags.Tag.Pictures; outputTags.Tag.Title = inputTags.Tag.Title; outputTags.Tag.Track = inputTags.Tag.Track; outputTags.Save(); inputTags.Dispose(); outputTags.Dispose(); } //else if (selectedFileType.EndsWith("(.wma)")) //{ // FileStream fileStream = File.Create($"c:/temp/conversiontest/{safeFileName}.wma"); // fileStream.Close(); // using (MediaFoundationEncoder writer = new MediaFoundationEncoder(new NAudio.MediaFoundation.MediaType(inputStream.WaveFormat))) // { // writer.Encode($"c:/temp/conversiontest/{safeFileName}.wma", inputStream); // } //} else if (selectedFileType.EndsWith("(.wav)")) { string monoStereoSelection = ""; Dispatcher.Invoke(() => { monoStereoSelection = MSConvertComboBox.SelectedItem as string; }); if (inputStream.WaveFormat.BitsPerSample == 16) { IWaveProvider provider = MonoStereoConvert16(inputStream, monoStereoSelection == "Mono"); using (FileStream fileStream = File.Create($"{outputPath}/{safeFileNames[i]}.wav")) using (WaveFileWriter writer = new WaveFileWriter(fileStream, provider.WaveFormat)) { byte[] buffer = new byte[4096]; int bytesRead = 0; do { bytesRead = provider.Read(buffer, 0, buffer.Length); writer.Write(buffer, 0, bytesRead); } while (bytesRead > 0); } } else if (inputStream.WaveFormat.BitsPerSample == 32) { ISampleProvider provider = MonoStereoConvert32(inputStream as ISampleProvider, monoStereoSelection == "Mono"); using (FileStream fileStream = File.Create($"{outputPath}/{safeFileNames[i]}.wav")) using (WaveFileWriter writer = new WaveFileWriter(fileStream, provider.WaveFormat)) { byte[] buffer = new byte[4096]; int bytesRead = 0; do { var providerAsWave = provider.ToWaveProvider(); // need to write with bytes not floats bytesRead = providerAsWave.Read(buffer, 0, buffer.Length); writer.Write(buffer, 0, bytesRead); } while (bytesRead > 0); } } TagLib.File outputTags = TagLib.File.Create($"{outputPath}/{safeFileNames[i]}.wav"); TagLib.File inputTags = TagLib.File.Create(filePaths[i]); outputTags.Tag.Album = inputTags.Tag.Album; outputTags.Tag.AlbumArtists = inputTags.Tag.AlbumArtists; outputTags.Tag.Genres = inputTags.Tag.Genres; outputTags.Tag.Performers = inputTags.Tag.Performers; outputTags.Tag.Year = inputTags.Tag.Year; outputTags.Tag.Pictures = inputTags.Tag.Pictures; outputTags.Tag.Title = inputTags.Tag.Title; outputTags.Tag.Track = inputTags.Tag.Track; outputTags.Save(); inputTags.Dispose(); outputTags.Dispose(); } } catch (Exception e) { MessageBox.Show("Error converting " + filePaths[i] + ", message: " + e.Message); //just move onto the next song } finally { (inputStream as WaveStream)?.Close(); } } await callBack(bgtc); MessageBox.Show("File(s) successfully converted!"); }