private void convert(string outputFile) { #if UNITY_STANDALONE_WIN string tmpFile = outputFile.Substring(0, outputFile.Length - 4) + "_" + SampleRate + Speaker.AudioFileExtension; bool converted = false; try { using (var reader = new NAudio.Wave.WaveFileReader(outputFile)) { if (reader.WaveFormat.SampleRate != SampleRate) { var newFormat = new NAudio.Wave.WaveFormat(SampleRate, BitsPerSample, Channels); using (var conversionStream = new NAudio.Wave.WaveFormatConversionStream(newFormat, reader)) { NAudio.Wave.WaveFileWriter.CreateWaveFile(tmpFile, conversionStream); } converted = true; } //else //{ // Debug.Log("File ignored: " + outputFile); //} } } catch (System.Exception ex) { Debug.LogError("Could not convert audio file: " + ex); } if (converted) { try { if (!CreateCopy) { System.IO.File.Delete(outputFile); System.IO.File.Move(tmpFile, outputFile); } } catch (System.Exception ex) { Debug.LogError("Could not delete and move audio files: " + ex); } } #endif }
private void convert(string outputFile) { #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN string tmpFile = outputFile.Substring(0, outputFile.Length - 4) + "_" + SampleRate + Speaker.AudioFileExtension; bool converted = false; try { using (NAudio.Wave.WaveFileReader reader = new NAudio.Wave.WaveFileReader(outputFile)) { if (reader.WaveFormat.SampleRate != (int)SampleRate) { NAudio.Wave.WaveFormat newFormat = new NAudio.Wave.WaveFormat((int)SampleRate, BitsPerSample, Channels); using (NAudio.Wave.WaveFormatConversionStream conversionStream = new NAudio.Wave.WaveFormatConversionStream(newFormat, reader)) { NAudio.Wave.WaveFileWriter.CreateWaveFile(tmpFile, conversionStream); } converted = true; } } } catch (System.Exception ex) { Debug.LogError("Could not convert audio file: " + ex); } if (converted) { try { if (!CreateCopy) { System.IO.File.Delete(outputFile); System.IO.File.Move(tmpFile, outputFile); } } catch (System.Exception ex) { Debug.LogError("Could not delete and move audio files: " + ex); } } #else Debug.LogError("Can only convert WAV audio files under Windows standalone!"); #endif }
private void Import(string inputFilePath, string outputFilePath, float tapeImportPosition, IProgress <float> progress = null) { byte[] buffer = new byte[1024 * 128 * 2]; Utils.WAVFile outputWavFile = Utils.WAVFile.Load(outputFilePath); try { long byteImportPosition = outputWavFile.dataOffsetBytes + (long)(Common.WaveFormat.SampleRate * tapeImportPosition) * (Common.WaveFormat.BitsPerSample / 8) * Common.WaveFormat.Channels; outputWavFile.stream.Seek(byteImportPosition, SeekOrigin.Begin); using (var reader = new NAudio.Wave.AudioFileReader(inputFilePath)) using (var tempConverter = new NAudio.Wave.Wave32To16Stream(reader)) using (var converter = new NAudio.Wave.WaveFormatConversionStream(Common.WaveFormat, tempConverter)) { int readCount = 0; int stepCount = 100; int step = 0; int stepSize = (int)(reader.Length / stepCount); int nextStep = 0; do { readCount = converter.Read(buffer, 0, buffer.Length); readCount = (int)Math.Min(readCount, outputWavFile.stream.Length - outputWavFile.stream.Position); outputWavFile.stream.Write(buffer, 0, readCount); if (progress != null && reader.Position >= nextStep) { nextStep += stepSize; progress.Report(step / (float)stepCount); step++; } }while (readCount > 0); } } finally { outputWavFile.Close(); } }
public static void ProcessFile(string fileName) { try { string fileExt = System.IO.Path.GetExtension(fileName.ToLower()); if (fileExt.Contains("mp3")) { using (NAudio.Wave.Mp3FileReader rdr = new NAudio.Wave.Mp3FileReader(fileName)) { //var newFormat = new NAudio.Wave.WaveFormat(48000, 16, 1); var newFormat = new NAudio.Wave.WaveFormat(16000, 16, 1); using (var conversionStream = new NAudio.Wave.WaveFormatConversionStream(newFormat, rdr)) { if (System.IO.File.Exists("mdc1200tmp.wav")) { System.IO.File.Delete("mdc1200tmp.wav"); } NAudio.Wave.WaveFileWriter.CreateWaveFile("mdc1200tmp.wav", conversionStream); } } } else { using (NAudio.Wave.WaveFileReader rdr = new NAudio.Wave.WaveFileReader(fileName)) { var newFormat = new NAudio.Wave.WaveFormat(16000, 16, 1); using (var conversionStream = new NAudio.Wave.WaveFormatConversionStream(newFormat, rdr)) { if (System.IO.File.Exists("mdc1200tmp.wav")) { System.IO.File.Delete("mdc1200tmp.wav"); } NAudio.Wave.WaveFileWriter.CreateWaveFile("mdc1200tmp.wav", conversionStream); } } } using (NAudio.Wave.AudioFileReader rdr = new NAudio.Wave.AudioFileReader("mdc1200tmp.wav")) { ProcessProvider(rdr, fileName); } } catch (Exception ex) { Console.WriteLine("Process File Exception: {0}", ex.Message); } }
public static void ProcessFile(string fileName) { try { string fileExt = System.IO.Path.GetExtension(fileName.ToLower()); if (fileExt.Contains("mp3")) { using (NAudio.Wave.Mp3FileReader rdr = new NAudio.Wave.Mp3FileReader(fileName)) { //var newFormat = new NAudio.Wave.WaveFormat(48000, 16, 1); var newFormat = new NAudio.Wave.WaveFormat(16000, 16, 1); using (var conversionStream = new NAudio.Wave.WaveFormatConversionStream(newFormat, rdr)) { if (System.IO.File.Exists("mdc1200tmp.wav")) System.IO.File.Delete("mdc1200tmp.wav"); NAudio.Wave.WaveFileWriter.CreateWaveFile("mdc1200tmp.wav", conversionStream); } } } else { using (NAudio.Wave.WaveFileReader rdr = new NAudio.Wave.WaveFileReader(fileName)) { var newFormat = new NAudio.Wave.WaveFormat(16000, 16, 1); using (var conversionStream = new NAudio.Wave.WaveFormatConversionStream(newFormat, rdr)) { if (System.IO.File.Exists("mdc1200tmp.wav")) System.IO.File.Delete("mdc1200tmp.wav"); NAudio.Wave.WaveFileWriter.CreateWaveFile("mdc1200tmp.wav", conversionStream); } } } using (NAudio.Wave.AudioFileReader rdr = new NAudio.Wave.AudioFileReader("mdc1200tmp.wav")) { ProcessProvider(rdr, fileName); } } catch (Exception ex) { Console.WriteLine("Process File Exception: {0}", ex.Message); } }
private void PlayAudio(object sender, GenericEventArgs <Stream> args) { if (Convert.ToBoolean(_cf.STORAGE[ConfigurationDictionary.PLAY_THROUGH_EZB])) { using (var memoryStream = new MemoryStream()) { args.EventData.CopyTo(memoryStream); memoryStream.Position = 0; try { using (NAudio.Wave.WaveStream wav = new NAudio.Wave.WaveFileReader(memoryStream)) { using (NAudio.Wave.WaveFormatConversionStream pcm = new NAudio.Wave.WaveFormatConversionStream(new NAudio.Wave.WaveFormat(EZ_B.EZBv4Sound.AUDIO_SAMPLE_BITRATE, 8, 1), wav)) { if (EZBManager.EZBs[0].SoundV4.IsPlaying) { stopPlaying(); } else { EZBManager.EZBs[0].SoundV4.PlayData(pcm); } } } } catch (Exception ex) { EZBManager.Log("Error: " + ex.Message); EZBManager.Log("StackTrace: " + ex.StackTrace); } } } else { SoundPlayer player = new SoundPlayer(args.EventData); player.PlaySync(); } args.EventData.Dispose(); }
public void StartRending(System.IO.DirectoryInfo baseTempDir, List <VocalUtau.Calculators.BarkerCalculator.BgmPreRender> BList, string RendToWav = "") { _IsRending = true; _ExitRending = false; if (RendingStateChange != null) { RendingStateChange(this); } string ProcessIDStr = Process.GetCurrentProcess().Id.ToString(); DirectoryInfo tempDir = baseTempDir.CreateSubdirectory("temp"); DirectoryInfo cacheDir = baseTempDir.CreateSubdirectory("cache"); string TrackFileName = tempDir.FullName + "\\Bgm_" + CacheSignal + ".wav"; FileStream Fs; headSize = InitFile(out Fs, TrackFileName); Semaphore semaphore = new Semaphore(1, 1, "VocalUtau.WavTool." + ProcessIDStr + ".Bgm_" + CacheSignal); for (int i = 0; i < BList.Count; i++) { if (BList[i].DelayTime > 0) { int ByteTime = (int)(IOHelper.NormalPcmMono16_Format.AverageBytesPerSecond * BList[i].DelayTime); ByteTime -= ByteTime % 2; byte[] byteL = new byte[ByteTime]; Array.Clear(byteL, 0, ByteTime); Fs.Write(byteL, 0, ByteTime); } semaphore.WaitOne(); try { int ByteTime = (int)(IOHelper.NormalPcmMono16_Format.AverageBytesPerSecond * BList[i].PassTime); ByteTime -= ByteTime % 2; using (NAudio.Wave.AudioFileReader reader = new NAudio.Wave.AudioFileReader(BList[i].FilePath)) { int JumpLoops = ByteTime / 2; using (NAudio.Wave.Wave32To16Stream w16 = new NAudio.Wave.Wave32To16Stream(reader)) { using (NAudio.Wave.WaveStream wfmt = new NAudio.Wave.BlockAlignReductionStream(w16)) { using (NAudio.Wave.WaveStream wout = new NAudio.Wave.WaveFormatConversionStream(IOHelper.NormalPcmMono16_Format, wfmt)) { while (wout.Position < wout.Length) { if (_ExitRending) { break; } byte[] by = new byte[2]; int rd = wout.Read(by, 0, 2); if (JumpLoops > 0) { JumpLoops--; } else { Fs.Write(by, 0, 2); } /* for (int w = 1; w < w16.WaveFormat.Channels; w++) * { * int rdr = w16.Read(by, 0, 2); * }*/ } } } } } } catch {; } Fs.Flush(); semaphore.Release(); if (_ExitRending) { break; } } _IsRending = false; long total = Fs.Length; byte[] head = IOHelper.GenerateHead((int)(total - headSize)); Fs.Seek(0, SeekOrigin.Begin); Fs.Write(head, 0, head.Length); Fs.Flush(); Fs.Close(); _ExitRending = false; if (RendingStateChange != null) { RendingStateChange(this); } if (RendToWav != "") { File.Copy(TrackFileName, RendToWav, true); try { File.Delete(TrackFileName); } catch {; } } }