public void TestWindowing() { float[] samples = new float[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; byte[] samplesAsBytes = new byte[samples.Length * 4]; Buffer.BlockCopy(samples, 0, samplesAsBytes, 0, samplesAsBytes.Length); var source = new MemorySourceStream(new System.IO.MemoryStream(samplesAsBytes), new AudioProperties(1, 44100, 32, AudioFormat.IEEE)); var windower = new StreamWindower(source, 10, 10); float[] frame = new float[10]; // Read first 10 items that are 1 windower.ReadFrame(frame); CollectionAssert.AreEqual(frame, Enumerable.Repeat(1f, 10).ToArray()); // Read next 10 items that are 0 windower.ReadFrame(frame); CollectionAssert.AreEqual(frame, Enumerable.Repeat(0f, 10).ToArray()); // Read last 10 items that are -1 windower.ReadFrame(frame); CollectionAssert.AreEqual(frame, Enumerable.Repeat(-1f, 10).ToArray()); }
private static void DecodeAudio(string filename) { FFmpegReader reader = new FFmpegReader(filename, Type.Audio); Console.WriteLine("length {0}, frame_size {1}, sample_rate {2}, sample_size {3}, channels {4}", reader.AudioOutputConfig.length, reader.AudioOutputConfig.frame_size, reader.AudioOutputConfig.format.sample_rate, reader.AudioOutputConfig.format.sample_size, reader.AudioOutputConfig.format.channels); int sampleBlockSize = reader.AudioOutputConfig.format.channels * reader.AudioOutputConfig.format.sample_size; int output_buffer_size = reader.AudioOutputConfig.frame_size * reader.AudioOutputConfig.format.channels * reader.AudioOutputConfig.format.sample_size; byte[] output_buffer = new byte[output_buffer_size]; int samplesRead; long timestamp; Type type; MemoryStream ms = new MemoryStream(); // read full stream while ((samplesRead = reader.ReadFrame(out timestamp, output_buffer, output_buffer_size, out type)) > 0) { Console.WriteLine("read " + samplesRead + " @ " + timestamp); // read samples into memory int bytesRead = samplesRead * sampleBlockSize; ms.Write(output_buffer, 0, bytesRead); } // seek back to start reader.Seek(0, Type.Audio); // read again (output should be the same as above) while ((samplesRead = reader.ReadFrame(out timestamp, output_buffer, output_buffer_size, out type)) > 0) { Console.WriteLine("read " + samplesRead + " @ " + timestamp); } reader.Dispose(); // write memory to wav file ms.Position = 0; MemorySourceStream mss = new MemorySourceStream(ms, new AudioProperties( reader.AudioOutputConfig.format.channels, reader.AudioOutputConfig.format.sample_rate, reader.AudioOutputConfig.format.sample_size * 8, reader.AudioOutputConfig.format.sample_size == 4 ? AudioFormat.IEEE : AudioFormat.LPCM)); IeeeStream ieee = new IeeeStream(mss); NAudioSinkStream nAudioSink = new NAudioSinkStream(ieee); WaveFileWriter.CreateWaveFile(filename + ".ffmpeg.wav", nAudioSink); }