public void ConstructorTest() { AudioBuffer buff = WAVReader.ReadAllSamples("test.wav", null); ALACWriter target; ALACWriter.Vendor = "CUETools"; target = new ALACWriter("alacwriter1.m4a", null, new ALACWriterSettings() { PCM = buff.PCM }); target.Settings.Padding = 1; target.CreationTime = DateTime.Parse("15 Aug 1976"); target.FinalSampleCount = buff.Length; target.Write(buff); target.Close(); CollectionAssert.AreEqual(File.ReadAllBytes("alac.m4a"), File.ReadAllBytes("alacwriter1.m4a"), "alacwriter1.m4a doesn't match."); target = new ALACWriter("alacwriter0.m4a", null, new ALACWriterSettings() { PCM = buff.PCM }); target.Settings.Padding = 1; target.CreationTime = DateTime.Parse("15 Aug 1976"); target.Write(buff); target.Close(); CollectionAssert.AreEqual(File.ReadAllBytes("alac.m4a"), File.ReadAllBytes("alacwriter0.m4a"), "alacwriter0.m4a doesn't match."); }
private Point[] Init2() { WAVReader wAVReader = new WAVReader(); wAVReader.ReadWAVFile(fileName); wAVReader = ZipWav(wAVReader); //Complex[] complices = new Complex[frame]; canvasOs.Width = this.Width; canvasOs.Height = this.Height; int i_0 = 0;//前面为0的部分 while (wAVReader.stereos[i_0].Left == 0 && wAVReader.stereos[i_0].Right == 0) { i_0++; } Point[] points = new Point[wAVReader.stereos.Count - i_0]; for (int i = 0; i < points.Length; i++) { double x = (i * 1.0 / samplingRate) * unitOfOneSecond; double y = TranY(wAVReader.stereos[i + i_0].Left / heightZipCount); points[i] = new Point(x, y); } return(points); }
private int number = 30; //默认取前100帧作为E0 /// <summary> /// 自动去掉音频中。。。。,这里的窗函数w(n)自动设为1,能量En /// 参考资料:https://wenku.baidu.com/view/5c76a2d9ce2f0066f5332270.html /// 这里为了计算省时间,把平方运算换成了取绝对值 /// </summary> /// <param name="wAVReader"></param> /// <returns></returns> public WAVReader EndpointDetectionDeal(WAVReader wAVReader) { int count = 0; while (wAVReader.stereos[count].Left == 0 && wAVReader.stereos[count].Right == 0) { count++; } double E0 = 0; for (int i = 0; i < number && (i + 1) * N < wAVReader.stereos.Count; i++) { E0 += GetEnForOneFrame(wAVReader, i * N, (i + 1) * N); } E0 /= number; for (int i = 0; i + N < wAVReader.stereos.Count; i += N) { if (GetEnForOneFrame(wAVReader, i, i + N) <= E0) { wAVReader.stereos.RemoveRange(i, N); i -= N; } } return(wAVReader); }
private byte[] Wav2FlacBuffConverter(byte[] Buffer) { Stream OutWavStream = new MemoryStream(); Stream OutFlacStream = new MemoryStream(); AudioPCMConfig pcmconf = new AudioPCMConfig(SAMPLE_ENCODING, CHANELS, RATE); WAVWriter wr = new WAVWriter(null, OutWavStream, pcmconf); wr.Write(new AudioBuffer(pcmconf, Buffer, Buffer.Length / 2)); OutWavStream.Seek(0, SeekOrigin.Begin); WAVReader audioSource = new WAVReader(null, OutWavStream); if (audioSource.PCM.SampleRate != RATE) { return(null); } AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); FlakeWriter flakeWriter = new FlakeWriter(null, OutFlacStream, audioSource.PCM); flakeWriter.CompressionLevel = 8; while (audioSource.Read(buff, -1) != 0) { flakeWriter.Write(buff); } OutFlacStream.Seek(0, SeekOrigin.Begin); byte[] barr = new byte[OutFlacStream.Length]; OutFlacStream.Read(barr, 0, (int)OutFlacStream.Length); return(barr); }
private void ConvertToFlac(Stream sourceStream) { var audioSource = new WAVReader(null, sourceStream); try { if (audioSource.PCM.SampleRate != 44100) { throw new InvalidOperationException("Incorrect frequency - WAV file must be at 44.1 KHz."); } var buff = new AudioBuffer(audioSource, 0x10000); var flakeWriter = new FlakeWriter(@"lol.flac", audioSource.PCM); flakeWriter.CompressionLevel = 8; while (audioSource.Read(buff, -1) != 0) { flakeWriter.Write(buff); } flakeWriter.Close(); } finally { audioSource.Close(); //richTextBox1.Text += "Koniec Konwersji\n"; } }
private static Hashtable Initialize(string fileName) { string _wav = _dir + "\\files\\" + fileName + ".wav"; string _flac = _dir + "\\files\\temporary.flac"; int sampleRate = 0; IAudioSource audioSource = new WAVReader(_wav, null); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); FlakeWriter flakewriter = new FlakeWriter(_flac, audioSource.PCM); sampleRate = audioSource.PCM.SampleRate; while (audioSource.Read(buff, -1) != 0) { flakewriter.Write(buff); } flakewriter.Close(); int _sample = sampleRate; Hashtable Response = new Hashtable(); Response.Add("wav_name", _wav); Response.Add("flac_name", _flac); Response.Add("sample_rate", _sample); return(Response); }
private void convert() { using (var reader = new WaveFileReader(path + rawFile)) { var newFormat = new NAudio.Wave.WaveFormat(16000, 16, 1); using (var conversionStream = new WaveFormatConversionStream(newFormat, reader)) { WaveFileWriter.CreateWaveFile(path + wavFile, conversionStream); } } if (!File.Exists(path + wavFile)) { Console.WriteLine("wav file no!"); } else { using (FileStream sourceStream = new FileStream(path + wavFile, FileMode.Open)) { WAVReader audioSource = new WAVReader(path + wavFile, sourceStream); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); FlakeWriter flakeWriter = new FlakeWriter(path + flacFile, audioSource.PCM); flakeWriter.CompressionLevel = 8; while (audioSource.Read(buff, -1) != 0) { flakeWriter.Write(buff); } flakeWriter.Close(); audioSource.Close(); } } }
public void TestWriting() { AudioBuffer buff = WAVReader.ReadAllSamples("test.wav", null); FlakeWriter target; target = new FlakeWriter("flakewriter0.flac", null, new FlakeWriterSettings { PCM = buff.PCM, EncoderMode = "7" }); target.Settings.Padding = 1; target.DoSeekTable = false; //target.Vendor = "CUETools"; //target.CreationTime = DateTime.Parse("15 Aug 1976"); target.FinalSampleCount = buff.Length; target.Write(buff); target.Close(); //CollectionAssert.AreEqual(File.ReadAllBytes("flake.flac"), File.ReadAllBytes("flakewriter0.flac"), "flakewriter0.flac doesn't match."); target = new FlakeWriter("flakewriter1.flac", null, new FlakeWriterSettings { PCM = buff.PCM, EncoderMode = "7" }); target.Settings.Padding = 1; target.DoSeekTable = false; //target.Vendor = "CUETools"; //target.CreationTime = DateTime.Parse("15 Aug 1976"); target.Write(buff); target.Close(); CollectionAssert.AreEqual(File.ReadAllBytes("flake.flac"), File.ReadAllBytes("flakewriter1.flac"), "flakewriter1.flac doesn't match."); }
public void MyTestCleanup() { pipe.Close(); pipe = null; wave.Close(); wave = null; }
/// <summary> /// start开始,不到end /// </summary> /// <param name="wAVReader"></param> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> private double GetEnForOneFrame(WAVReader wAVReader, int start, int end) { double En = 0; for (int i = start; i < end; i++) { En += Math.Abs(wAVReader.stereos[i].Left) + Math.Abs(wAVReader.stereos[i].Right); } return(En); }
public static Tuple <int, string> WavToFlac(byte[] wavBytes, string flacSavePath) { Tuple <int, string> sampleRateAndTargetFlac = null; string targetFlacPath = FormatFileName(flacSavePath, ".FLAC"); using (Stream dataStream = new MemoryStream(wavBytes)) { WAVReader audioSource = new WAVReader(null, dataStream); sampleRateAndTargetFlac = WavToFlacHelper(audioSource, targetFlacPath); } return(sampleRateAndTargetFlac); }
private WAVReader ZipWav(WAVReader wAVReader) { WAVReader newWav = new WAVReader(); newWav.stereos = new List <Stereo>(); for (int i = wAVReader.stereos.Count - 1; i >= 0; i -= waveZipTimes) { newWav.stereos.Add(wAVReader.stereos[i]); //wAVReader.stereos.RemoveAt(i); } return(newWav); }
private WaveMap GetWaveMaps(string fileName, float shiftNumber = 0, float deviation = 0, float threshold = 0) { WAVReader wAVReader = new WAVReader(); wAVReader.ReadWAVFile(fileName); WAVReader newWAVReader = ZipWav(wAVReader); EndpointDetection endpoint = new EndpointDetection(); newWAVReader = endpoint.EndpointDetectionDeal(newWAVReader); //Stereo test169 = wAVReader.stereos[169]; //Stereo test170 = wAVReader.stereos[170]; //Stereo test171 = wAVReader.stereos[171]; //List<WaveMap> waveMaps = new List<WaveMap>(); Spectrogram spectrogram = new Spectrogram(); return(spectrogram.GetSpectrogram(newWAVReader.stereos, shiftNumber, deviation, threshold)); }
public void ConstructorTest() { AudioBuffer buff = WAVReader.ReadAllSamples("test.wav", null); WAVWriter target; target = new WAVWriter("wavwriter0.wav", null, new WAVWriterSettings(buff.PCM)); //target.FinalSampleCount = buff.Length; target.Write(buff); target.Close(); CollectionAssert.AreEqual(File.ReadAllBytes("test.wav"), File.ReadAllBytes("wavwriter0.wav"), "wavwriter0.wav doesn't match."); target = new WAVWriter("wavwriter1.wav", null, new WAVWriterSettings(buff.PCM)); target.FinalSampleCount = buff.Length; target.Write(buff); target.Close(); CollectionAssert.AreEqual(File.ReadAllBytes("test.wav"), File.ReadAllBytes("wavwriter1.wav"), "wavwriter1.wav doesn't match."); }
private static Tuple <int, string> WavToFlacHelper(WAVReader audioSource, string targetFlacPath) { int sampleRate; AudioBuffer buffer = new AudioBuffer(audioSource, 0x10000); FlakeWriterSettings settings = new FlakeWriterSettings(); settings.PCM = audioSource.PCM; FlakeWriter audioDestination = new FlakeWriter(targetFlacPath, settings); while (audioSource.Read(buffer, -1) != 0) { audioDestination.Write(buffer); } sampleRate = settings.PCM.SampleRate; audioDestination.Close(); audioSource.Close(); return(new Tuple <int, string>(sampleRate, targetFlacPath)); }
public void ConstructorTest() { AudioBuffer buff = WAVReader.ReadAllSamples("test.wav", null); FLACWriter target; target = new FLACWriter("flacwriter2.flac", new FLACWriterSettings() { PCM = buff.PCM, EncoderMode = "7" }); target.Settings.Padding = 1; target.Settings.BlockSize = 32; //target.Vendor = "CUETools"; //target.CreationTime = DateTime.Parse("15 Aug 1976"); target.FinalSampleCount = buff.Length; target.Write(buff); target.Close(); CollectionAssert.AreEqual(File.ReadAllBytes("flacwriter1.flac"), File.ReadAllBytes("flacwriter2.flac"), "flacwriter2.flac doesn't match."); }
/// <summary> Конвертирование wav-файла во flac </summary> /// <returns>Частота дискретизации</returns> public static int Wav2Flac(Stream wavStream, Stream flacStream) { int sampleRate = 0; IAudioSource audioSource = new WAVReader(null, wavStream); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); FlakeWriter flakewriter = new FlakeWriter(null, flacStream, audioSource.PCM); sampleRate = audioSource.PCM.SampleRate; FlakeWriter audioDest = flakewriter; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); } return(sampleRate); }
public static Tuple <int, string> WavToFlac(string targetWavFile) { string targetFlacPath = FormatFileName(targetWavFile, ".FLAC"); WAVReader audioSource = new WAVReader(targetWavFile, null); //AudioBuffer buffer = new AudioBuffer(audioSource, 0x10000); //FlakeWriterSettings settings = new FlakeWriterSettings(); //settings.PCM = audioSource.PCM; //FlakeWriter audioDestination = new FlakeWriter(targetFlacPath, settings); //while (audioSource.Read(buffer, -1) != 0) //{ // audioDestination.Write(buffer); //} //sampleRate = settings.PCM.SampleRate; //audioDestination.Close(); //audioSource.Close(); //Tuple<int, string> output = new Tuple<int, string>(sampleRate, targetFlacPath); return(WavToFlacHelper(audioSource, targetFlacPath)); }
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { System.GC.Collect(); OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "波形文件|*.wav"; if (dlg.ShowDialog() == DialogResult.OK) { _wr = new WAVReader(); _wr.ReadWAVFile(dlg.FileName); 关闭文件ToolStripMenuItem.Enabled = true; myChart.Datas = _wr.GetData(); myChart.sample_frequent = _wr.GetSamplesPerSec(); trackBar_move.Maximum = _wr.GetData().Length; this.Text = "WaveViewer — " + dlg.SafeFileName; AllMenuInitial(true); myChart.Refresh(); } }
/// <summary>Конвертирование wav-файла во flac</summary> /// <returns>Частота дискретизации</returns> private static void _Wav2Flac(String wavName, string flacName) { Debug.Assert(wavName != null); Debug.Assert(!string.IsNullOrEmpty(flacName)); IAudioSource audioSource = new WAVReader(wavName, null); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); FlakeWriter flakewriter = new FlakeWriter(flacName, audioSource.PCM); FlakeWriter audioDest = flakewriter; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); } audioDest.Close(); audioSource.Close(); }
public static string WavStreamToGoogle(Stream stream) { FlakeWriter audioDest = null; IAudioSource audioSource = null; string answer; try { var outStream = new MemoryStream(); stream.Position = 0; audioSource = new WAVReader("", stream); var buff = new AudioBuffer(audioSource, 0x10000); audioDest = new FlakeWriter("", outStream, audioSource.PCM); var sampleRate = audioSource.PCM.SampleRate; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); } answer = GoogleRequest(outStream, sampleRate); } finally { if (audioDest != null) { audioDest.Close(); } if (audioSource != null) { audioSource.Close(); } } return(answer); }
private Point[] Init() { WAVReader wAVReader = new WAVReader(); wAVReader.ReadWAVFile(fileName); Complex[] complices = new Complex[frame]; canvasOs.Width = this.Width; canvasOs.Height = this.Height; int i_0 = 0;//前面为0的部分 while (wAVReader.stereos[i_0].Left == 0 && wAVReader.stereos[i_0].Right == 0) { i_0++; } for (int i = 0; i < complices.Length; i++) { complices[i] = new Complex(wAVReader.stereos[i + i_0 + startNumber].Right * Hamming(i, frame), 0); } FourierTransform.DFT(complices, FourierTransform.Direction.Backward); double max = 0; Point[] points = new Point[frame]; for (int i = 0; i < points.Length; i++) { double x = (i * 1.0 / 44100) * unitOfOneSecond; if (complices[i].Magnitude > max) { max = complices[i].Magnitude; } double y = TranY(complices[i].Magnitude / heightZipCount); points[i] = new Point(x, y); } return(points); }
private void ConvertToFlac(Stream sourceStream, Stream destinationStream) { var audioSource = new WAVReader(null, sourceStream); try { if (audioSource.PCM.SampleRate != 16000) { throw new InvalidOperationException("Incorrect frequency - WAV file must be at 16 KHz."); } var buff = new AudioBuffer(audioSource, 0x10000); var flakeWriter = new FlakeWriter(null, destinationStream, audioSource.PCM); //flakeWriter.CompressionLevel = 8; while (audioSource.Read(buff, -1) != 0) { flakeWriter.Write(buff); } flakeWriter.Close(); } finally { audioSource.Close(); } }
public async Task TestWriting() { var wavStream = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///test.wav"))) .OpenStreamForReadAsync(); var expectedBytes = ReadFully(await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///flake.flac"))) .OpenStreamForReadAsync()); var buff = WAVReader.ReadAllSamples(wavStream); FlakeWriter target; var outputStream = new MemoryStream(); target = new FlakeWriter(outputStream, new FlakeWriterSettings { PCM = buff.PCM, EncoderMode = "7" }); target.Settings.Padding = 1; target.DoSeekTable = false; target.Write(buff); target.Close(); outputStream.Seek(0, SeekOrigin.Begin); var resultContent = outputStream.ToArray(); var outStream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("out.flac", CreationCollisionOption.ReplaceExisting); outStream.Write(resultContent, 0, resultContent.Length); outStream.Dispose(); Debug.WriteLine(ApplicationData.Current.LocalFolder.Path); CollectionAssert.AreEqual(expectedBytes, outputStream.ToArray(), "result and expected doesn't match."); }
/// <summary> /// Converts .wav to .flac and returns sample rate via CUETools. /// </summary> /// <param name="wavFilePath">Full path to the .wav file.</param> /// <param name="flacFilePath">Full path to the .flac file.</param> /// <returns>Returns sample rate of the .flac file.</returns> public static int ConvertWavToFlac(String wavFilePath, String flacFilePath) { int sampleRate = 0; IAudioSource audioSource = new WAVReader(wavFilePath, null); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); FlakeWriter flakewriter = new FlakeWriter(flacFilePath, audioSource.PCM); sampleRate = audioSource.PCM.SampleRate; FlakeWriter audioDest = flakewriter; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); } audioDest.Close(); //TODO: Check - maybe it should be flakewriter.Close(); audioDest.Close(); return(sampleRate); }
private Point[] Init3() { WAVReader wAVReader = new WAVReader(); wAVReader.ReadWAVFile(fileName); canvasOs.Width = this.Width; canvasOs.Height = this.Height; EndpointDetection endpointDetection = new EndpointDetection(); wAVReader = ZipWav(wAVReader); wAVReader = endpointDetection.EndpointDetectionDeal(wAVReader); Point[] points = new Point[wAVReader.stereos.Count]; for (int i = 0; i < points.Length; i++) { double x = (i * 1.0 / 44100) * unitOfOneSecond; double y = TranY(wAVReader.stereos[i].Left / heightZipCount); points[i] = new Point(x, y); } return(points); }
/// <summary> Конвертирование wav-файла во flac </summary> /// <returns>Частота дискретизации</returns> public static int Wav2Flac(String wavName, string flacName) { int sampleRate = 0; IAudioSource audioSource = new WAVReader(wavName, null); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); FlakeWriter flakewriter = new FlakeWriter(flacName, audioSource.PCM); sampleRate = audioSource.PCM.SampleRate; FlakeWriter audioDest = flakewriter; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); } audioDest.Close(); audioDest.Close(); return(sampleRate); }
/// <summary> /// 从win10中导入movie播放 /// </summary> private void ImportMovie() { transform.Find(ConstTable.Instance.R_TextInfo).GetComponent <Text>().text = ""; OpenFileName pth = new OpenFileName(); pth.structSize = Marshal.SizeOf(pth); //pth.filter = "All files (*.*)|*.*"; pth.filter = "WAV files (*.wav)\0*.wav"; //pth.filter = "Excel文件(*.xlsx)\0*.xlsx"; pth.file = new string(new char[256]); pth.maxFile = pth.file.Length; pth.fileTitle = new string(new char[64]); pth.maxFileTitle = pth.fileTitle.Length; pth.initialDir = Application.dataPath.Replace("/", "\\") + "\\Resources"; //默认路径 pth.title = "打开项目"; pth.defExt = "dat"; pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; string filePath = ""; if (LocalDialog.GetOpenFileName(pth)) { filePath = pth.file; //选择的文件路径; //Debug.Log(filePath); } if (filePath != "") { _isPlay = true; _isDraw = true; WAVReader wav = new WAVReader(); wav.ReadWavFile(filePath); InvalidOtherBtn(); //Debug.Log("载入"); Invoke("DrawWavData", 0.5f); } }
private byte[] Wav2FlacBuffConverter(byte[] Buffer) { AudioPCMConfig pcmconf = new AudioPCMConfig(16, 1, 16000); if (flakeWriter == null) { OutFlacStream = new MemoryStream(); flakeWriter = new FlakeWriter(null, OutFlacStream, pcmconf); flakeWriter.Padding = 0; } Stream OutWavStream = new MemoryStream(); WAVWriter wr = new WAVWriter(null, OutWavStream, pcmconf); wr.Write(new AudioBuffer(pcmconf, Buffer, Buffer.Length / 2)); OutWavStream.Seek(0, SeekOrigin.Begin); WAVReader audioSource = new WAVReader(null, OutWavStream); if (audioSource.PCM.SampleRate != 16000) { return(null); } AudioBuffer buff = new AudioBuffer(audioSource, Buffer.Length); flakeWriter.CompressionLevel = 8; while (audioSource.Read(buff, -1) != 0) { flakeWriter.Write(buff); } OutFlacStream.Seek(0, SeekOrigin.Begin); byte[] barr = new byte[OutFlacStream.Length]; OutFlacStream.Read(barr, 0, (int)OutFlacStream.Length); return(barr); }
/// <summary> /// Convert .wav file to .flac file with the same name /// </summary> /// <param name="WavName">path to .wav file</param> /// <returns>Sample Rate of converted .flac</returns> public static int Wav2Flac(string WavName) { int sampleRate; var flacName = Path.ChangeExtension(WavName, "flac"); FlakeWriter audioDest = null; IAudioSource audioSource = null; try { audioSource = new WAVReader(WavName, null); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); audioDest = new FlakeWriter(flacName, audioSource.PCM); sampleRate = audioSource.PCM.SampleRate; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); } } finally { if (audioDest != null) { audioDest.Close(); } if (audioSource != null) { audioSource.Close(); } } return(sampleRate); }