Exemple #1
0
        private void output_FramePlayingStarted(object sender, PlayFrameEventArgs e)
        {
            updateTrackbar(e.FrameIndex);

            if (e.FrameIndex + e.Count < decoder.Frames)
            {
                int previous = decoder.Position;
                decoder.Seek(e.FrameIndex);
                Signal s = decoder.Decode(e.Count);
                decoder.Seek(previous);

                updateWaveform(s.ToFloat(), s.Length);
            }
        }
Exemple #2
0
        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 static Signal loadSignalFromWaveFile(string p_filePath)
        {
            WaveDecoder sourceDecoder = new WaveDecoder(p_filePath);

            // Decode the file and store into a signal
            Signal sourceSignal = sourceDecoder.Decode();

            return(sourceSignal);
        }
Exemple #4
0
        public static Signal ObterSinalMono(string arquivoAudio)
        {
            var audioDecoder = new WaveDecoder(arquivoAudio);

            using (var originalSignal = audioDecoder.Decode())
            {
                var monoFilter = new MonoFilter();
                return(monoFilter.Apply(originalSignal));
            }
        }
Exemple #5
0
        /// <summary>
        ///   Worker thread.
        /// </summary>
        ///
        private void WorkerThread()
        {
            SoundStream waveStream = null;

            try
            {
                waveStream = (stream != null)
                    ? new SoundStream(stream)
                    : new SoundStream(File.OpenRead(fileName));

                // Open the Wave stream
                decoder.Open(waveStream);

                while (stopEvent.WaitOne(0, false))
                {
                    // get next frame
                    Signal s = decoder.Decode(frameSize);
                    framesReceived += s.Length;
                    bytesReceived  += decoder.Bytes;

                    if (NewFrame != null)
                    {
                        NewFrame(this, new NewFrameEventArgs(s));
                    }

                    // check current position
                    if (waveStream.Position >= waveStream.Length)
                    {
                        break;
                    }

                    // sleeping ...
                    Thread.Sleep(100);
                }
            }
            catch (Exception exception)
            {
                // provide information to clients
                if (AudioSourceError != null)
                {
                    AudioSourceError(this, new AudioSourceErrorEventArgs(exception.Message));
                }
                else
                {
                    throw;
                }
            }

            if (waveStream != null)
            {
                waveStream.Close();
                waveStream.Dispose();
                waveStream = null;
            }
        }
Exemple #6
0
        public static double[] GetMfcc(string file)
        {
            WaveDecoder decoder = new WaveDecoder(file);
            Signal      signal  = decoder.Decode();
            MelFrequencyCepstrumCoefficient mfcc = new MelFrequencyCepstrumCoefficient();

            MelFrequencyCepstrumCoefficientDescriptor[] ra = mfcc.Transform(signal).ToArray();
            var query = from x in ra select x.Descriptor.Average();

            return(query.ToArray());
        }
        /// <summary>
        ///   Worker thread.
        /// </summary>
        ///
        private void WorkerThread()
        {
            this.shouldStop = false;

            SoundStream waveStream = null;

            try
            {
                waveStream = (stream != null) ?
                             new SoundStream(stream) : new SoundStream(File.OpenRead(fileName));

                // Open the Wave stream
                decoder.Open(waveStream);

                Signal signal = null;
                while (!this.shouldStop)
                {
                    // get next frame, overwriting the previous
                    signal          = decoder.Decode(frameSize, signal);
                    framesReceived += signal.NumberOfFrames;
                    bytesReceived  += decoder.NumberOfBytesRead;

                    if (NewFrame != null)
                    {
                        NewFrame(this, new NewFrameEventArgs(signal));
                    }

                    // check current position
                    if (waveStream.Position >= waveStream.Length)
                    {
                        this.shouldStop = true;
                    }
                }
            }
            catch (Exception exception)
            {
                // provide information to clients
                if (AudioSourceError == null)
                {
                    throw;
                }

                AudioSourceError(this, new AudioSourceErrorEventArgs(exception));
            }

            if (waveStream != null)
            {
                waveStream.Close();
                waveStream.Dispose();
                waveStream = null;
            }
        }
Exemple #8
0
        public ComplexSignal FFTComplex(string sFile)
        {
            WaveDecoder    signalwav    = new WaveDecoder("./Sounds/" + sFile + ".wav");
            Signal         sourceSignal = signalwav.Decode();
            BlackmanWindow window       = new BlackmanWindow(fs);

            Signal[]      windows  = sourceSignal.Split(window, fs);
            Signal        wSignal  = window.Apply(sourceSignal, fs);
            ComplexSignal sComplex = wSignal.ToComplex();

            sComplex.ForwardFourierTransform();
            signalwav.Close();
            return(sComplex);
        }
        /// <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 SignalLoadSave()
        {
            string input = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "Resources", "a.wav");

            Signal sourceSignal = Signal.FromFile(input);

            Assert.AreEqual(352800, sourceSignal.Samples);
            Assert.AreEqual(sourceSignal.Samples, sourceSignal.NumberOfSamples);
            Assert.AreEqual(176400, sourceSignal.Length);
            Assert.AreEqual(sourceSignal.Length, sourceSignal.NumberOfFrames);
            Assert.AreEqual(4000, sourceSignal.Duration.TotalMilliseconds);
            Assert.AreEqual(2, sourceSignal.Channels);
            Assert.AreEqual(44100, sourceSignal.SampleRate);
            Assert.AreEqual(sizeof(float) * 352800, sourceSignal.NumberOfBytes);

            string output = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "ab.wav");

            sourceSignal.Save(output);

            FileStream destinationStream = new FileStream(output, FileMode.Open, FileAccess.Read);

            // 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(1411200 * 2, destDecoder.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);

            byte[] actual   = sourceSignal.ToByte();
            byte[] expected = destSignal.ToByte();
            Assert.AreEqual(expected, actual);
        }
Exemple #11
0
        public void ComplexSignalConstructor()
        {
            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);

            // 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 ComplexSignalConstructor()
        {
            var          sourceStream      = SignalTest.GetSignal("a.wav");
            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(93, c.Duration.TotalMilliseconds);
                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);
            }
        }
Exemple #13
0
        public void SetDirectoryPaths(string folderPath)
        {
            audioFileName = Directory.GetFiles(folderPath, "*.wav").Select(Path.GetFileName).OrderBy(r => r.Length).ToArray();

            for (int i = 0; i < audioFileName.Length; i++)
            {
                string      fileName = audioFileName[i];
                WaveDecoder sourceWD = new WaveDecoder(fileName);
                Signal      sourceS  = sourceWD.Decode();

                RaisedCosineWindow window  = RaisedCosineWindow.Hamming(1024);
                Signal[]           windows = sourceS.Split(window, 512);

                ComplexSignal[] complex = windows.Apply(ComplexSignal.FromSignal);

                complex.ForwardFourierTransform();
            }
        }
Exemple #14
0
        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);
        }
Exemple #15
0
        private void AudioOutputDeviceNewFrameRequested(object sender, NewFrameRequestedEventArgs e)
        {
            if (decoder == null)
            {
                return;
            }

            e.FrameIndex = decoder.Position;

            Signal signal = decoder.Decode(e.Frames);

            if (signal == null)
            {
                e.Stop = true;
                return;
            }

            e.Frames = signal.Length;

            signal.CopyTo(e.Buffer);
        }
Exemple #16
0
        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
            // Bitrate:           1411kbps

            // 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(1411200, 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(1411200, 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);
            }
        }
Exemple #17
0
        public static Signal ObterSinal(string arquivoAudio)
        {
            var audioDecoder = new WaveDecoder(arquivoAudio);

            return(audioDecoder.Decode());
        }
        public void WaveEncoderConstructorTest()
        {
            string basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources");

            #region doc_properties
            // Let's say we would like to decode a wave file:
            string fileName = Path.Combine(basePath, "a.wav");

            // File is 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
            // Bitrate:           1411kbps

            // First, create a decoder for the file stream:
            var sourceDecoder = new WaveDecoder(fileName);

            // Now we can use it to check some of the the stream properties:
            int numberOfChannels     = sourceDecoder.NumberOfChannels; // 2
            int numberOfSamples      = sourceDecoder.NumberOfSamples;  // 352800
            int numberOfFrames       = sourceDecoder.NumberOfFrames;   // 176400
            int durationMilliseconds = sourceDecoder.Duration;         // 4000
            int sampleRate           = sourceDecoder.SampleRate;       // 44100
            int bitsPerSample        = sourceDecoder.BitsPerSample;    // 16
            int bps = sourceDecoder.AverageBitsPerSecond;              // 141200

            // Decode the signal in the source stream:
            Signal sourceSignal = sourceDecoder.Decode();

            // As we can see, all properties are kept in the signal:
            int      signalChannels   = sourceSignal.NumberOfChannels; // 2
            int      signalSamples    = sourceSignal.NumberOfSamples;  // 352800
            int      signalFrames     = sourceSignal.NumberOfFrames;   // 176400
            TimeSpan signalDuration   = sourceSignal.Duration;         // {00:00:04}
            int      signalLength     = sourceSignal.Length;           // 176400
            int      signalSampleRate = sourceSignal.SampleRate;       // 44100
            int      signalBytes      = sourceSignal.NumberOfBytes;    // 1411200

            // And this is the total number of bytes that have been read:
            int numberOfBytes = sourceDecoder.NumberOfBytesRead; // 705600
            #endregion


            Assert.AreEqual(2, numberOfChannels);
            Assert.AreEqual(352800, numberOfSamples);
            Assert.AreEqual(176400, numberOfFrames);
            Assert.AreEqual(4000, durationMilliseconds);
            Assert.AreEqual(44100, sampleRate);
            Assert.AreEqual(16, bitsPerSample);
            Assert.AreEqual(1411200, bps);
            Assert.AreEqual(sizeof(short) * 352800, numberOfBytes);

            Assert.AreEqual(352800, signalSamples);
            Assert.AreEqual(176400, signalLength);
            Assert.AreEqual(4000, signalDuration.TotalMilliseconds);
            Assert.AreEqual(2, signalChannels);
            Assert.AreEqual(44100, signalSampleRate);
            Assert.AreEqual(sizeof(float) * 352800, signalBytes);

            MemoryStream destinationStream = new MemoryStream();

            // 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
            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(1411200, 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);

            byte[] actual   = sourceSignal.ToByte();
            byte[] expected = destSignal.ToByte();
            Assert.AreEqual(expected, actual);
        }