/// <summary> /// Open the given audio file and return an "AudioSignal" with the following info: /// 1. data[]: array of audio samples /// 2. sample rate /// 3. signal length in milli sec /// </summary> /// <param name="filePath">audio file path</param> /// <returns>AudioSignal containing its data, sample rate and length in ms</returns> public static AudioSignal OpenAudioFile(string filePath) { WaveDecoder waveDecoder = new WaveDecoder(filePath); AudioSignal signal = new AudioSignal(); signal.sampleRate = waveDecoder.SampleRate; signal.signalLengthInMilliSec = waveDecoder.Duration; Signal tempSignal = waveDecoder.Decode(waveDecoder.Frames); signal.data = new double[waveDecoder.Frames]; tempSignal.CopyTo(signal.data); return signal; }
public void waveInput() { writeHeaderForARFF(); for (int i = 0; i < audioFileName.Length; i++) { string type; string name; setTypeAndName(audioFileName[i], out type, out name); Accord.Audio.Formats.WaveDecoder currentWav = new Accord.Audio.Formats.WaveDecoder(audioFileName[i]); //Used for time Domain Signal timeDomain = currentWav.Decode(); //Creates an array of time domain samples for use in calculation of RootMeanSquare aka AverageEnergy float[] energyArray = new float[timeDomain.Samples]; timeDomain.CopyTo(energyArray); //average energy for the current wav file double averageEnergy = Accord.Audio.Tools.RootMeanSquare(energyArray); //ZCR for the current wav file double zeroCrossingRate = zeroCrossingRateMethod(timeDomain); Accord.Audio.Windows.RaisedCosineWindow window = Accord.Audio.Windows.RaisedCosineWindow.Hamming(1024); Signal[] windows = timeDomain.Split(window, 512); //Used for Frequency Domain ComplexSignal[] tempFrequency = windows.Apply(ComplexSignal.FromSignal); tempFrequency.ForwardFourierTransform(); ComplexSignal curComplex = tempFrequency[0]; double[] power = { }; double[] magnitudes = { }; double[] freq = { }; var length = curComplex.Length / (2 + 1); double[] meanPower = new double[length]; double[] meanMagnitudes = new double[length]; createFrequencyArray(tempFrequency, curComplex, out power, out magnitudes, out freq, out meanPower, out meanMagnitudes); //Spectral Centrois for the current wav file double spectralCentroid = meanMagnitudes.Zip(freq, (m, f) => m * f).Sum() / meanMagnitudes.Sum(); //double spectralCentroid = spectralCentroidMethod(tempFrequency); //Writes data to arff file writeARFF(name, averageEnergy, zeroCrossingRate, spectralCentroid, type); } }
public override int Run(string[] remainingArguments) { var decoder = new WaveDecoder(File); var sampleRate = decoder.SampleRate; Console.WriteLine("Sample rate is {0}, duration is {1}", sampleRate, TimeSpan.FromMilliseconds(decoder.Duration)); int count = 1; while (decoder.Position < decoder.Samples) { var signl = decoder.Decode(sampleRate * 60); Console.WriteLine(count++ + " frame has energy: " + signl.GetEnergy()); } return 0; }
public void ComplexSignalConstructor() { UnmanagedMemoryStream sourceStream = Properties.Resources.a; MemoryStream destinationStream = new MemoryStream(); // Create a decoder for the source stream WaveDecoder sourceDecoder = new WaveDecoder(sourceStream); // Decode the signal in the source stream Signal sourceSignal = sourceDecoder.Decode(); int length = (int)Math.Pow(2, 12); RaisedCosineWindow window = RaisedCosineWindow.Hamming(length); Assert.AreEqual(length, window.Length); Signal[] windows = sourceSignal.Split(window, 1024); Assert.AreEqual(windows.Length, 172); foreach (var w in windows) Assert.AreEqual(length, w.Length); ComplexSignal[] complex = windows.Apply(ComplexSignal.FromSignal); for (int i = 0; i < complex.Length - 1; i++) { ComplexSignal c = complex[i]; Assert.AreEqual(2, c.Channels); Assert.AreEqual(92, c.Duration); Assert.AreEqual(4096, c.Length); Assert.AreEqual(SampleFormat.Format128BitComplex, c.SampleFormat); Assert.AreEqual(44100, c.SampleRate); Assert.AreEqual(ComplexSignalStatus.Normal, c.Status); } complex.ForwardFourierTransform(); for (int i = 0; i < complex.Length - 1; i++) { ComplexSignal c = complex[i]; Assert.AreEqual(ComplexSignalStatus.FourierTransformed, c.Status); } }
public void WaveEncoderConstructorTest() { // Load a file in PCM 16bpp format // Number of samples: 352.800 // Number of frames: 176.400 // Sample rate: 44.100 Hz // Block align: 4 // Channels: 2 // Duration: 4.0 s // Bytes: 705.644 // sizeof(float) = 4 // sizeof(int) = 4 // sizeof(short) = 2 UnmanagedMemoryStream sourceStream = Properties.Resources.Grand_Piano___Fazioli___major_A_middle; MemoryStream destinationStream = new MemoryStream(); // Create a decoder for the source stream WaveDecoder sourceDecoder = new WaveDecoder(sourceStream); Assert.AreEqual(2, sourceDecoder.Channels); Assert.AreEqual(352800, sourceDecoder.Samples); Assert.AreEqual(176400, sourceDecoder.Frames); Assert.AreEqual(4000, sourceDecoder.Duration); Assert.AreEqual(44100, sourceDecoder.SampleRate); Assert.AreEqual(16, sourceDecoder.BitsPerSample); Assert.AreEqual(22050, sourceDecoder.AverageBitsPerSecond); // Decode the signal in the source stream Signal sourceSignal = sourceDecoder.Decode(); Assert.AreEqual(352800, sourceSignal.Samples); Assert.AreEqual(176400, sourceSignal.Length); Assert.AreEqual(4000, sourceSignal.Duration); Assert.AreEqual(2, sourceSignal.Channels); Assert.AreEqual(44100, sourceSignal.SampleRate); Assert.AreEqual(sizeof(float) * 352800, sourceSignal.RawData.Length); Assert.AreEqual(sizeof(short) * 352800, sourceDecoder.Bytes); // Create a encoder for the destination stream WaveEncoder encoder = new WaveEncoder(destinationStream); // Encode the signal to the destination stream encoder.Encode(sourceSignal); Assert.AreEqual(2, encoder.Channels); Assert.AreEqual(352800, encoder.Samples); Assert.AreEqual(176400, encoder.Frames); Assert.AreEqual(4000, encoder.Duration); Assert.AreEqual(44100, encoder.SampleRate); Assert.AreEqual(32, encoder.BitsPerSample); Assert.AreEqual(sizeof(float) * 352800, encoder.Bytes); // Rewind both streams, them attempt to read the destination sourceStream.Seek(0, SeekOrigin.Begin); destinationStream.Seek(0, SeekOrigin.Begin); // Create a decoder to read the destination stream WaveDecoder destDecoder = new WaveDecoder(destinationStream); Assert.AreEqual(2, destDecoder.Channels); Assert.AreEqual(176400, destDecoder.Frames); Assert.AreEqual(352800, destDecoder.Samples); Assert.AreEqual(4000, destDecoder.Duration); Assert.AreEqual(44100, destDecoder.SampleRate); Assert.AreEqual(32, destDecoder.BitsPerSample); Assert.AreEqual(22050, sourceDecoder.AverageBitsPerSecond); // Decode the destination stream Signal destSignal = destDecoder.Decode(); // Assert that the signal which has been saved to the destination // stream and the signal which has just been read from this same // stream are identical Assert.AreEqual(sourceSignal.Length, destSignal.Length); Assert.AreEqual(sourceSignal.SampleFormat, destSignal.SampleFormat); Assert.AreEqual(sourceSignal.SampleRate, destSignal.SampleRate); Assert.AreEqual(sourceSignal.Samples, destSignal.Samples); Assert.AreEqual(sourceSignal.Duration, destSignal.Duration); for (int i = 0; i < sourceSignal.RawData.Length; i++) { byte actual = sourceSignal.RawData[i]; byte expected = destSignal.RawData[i]; Assert.AreEqual(expected, actual); } }
private float[] getSignal(String path) { WaveDecoder decoder = new WaveDecoder(path); float[] samples = new float[decoder.Frames]; decoder.Decode(samples.Length).CopyTo(samples); return samples; }
private void adjustVolume(float value) { // First, we rewind the stream stream.Seek(0, SeekOrigin.Begin); // Then we create a decoder for it decoder = new WaveDecoder(stream); var signal = decoder.Decode(); // We apply the volume filter var volume = new VolumeFilter(value); volume.ApplyInPlace(signal); // Then we store it again stream.Seek(0, SeekOrigin.Begin); encoder = new WaveEncoder(stream); encoder.Encode(signal); }