Exemplo n.º 1
0
        private async void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            using (var stream = new FileStream(ofd.FileName, FileMode.Open))
            {
                var waveFile = new WaveFile(stream, true);
                _signal = waveFile[Channels.Left];
            }

            var sr        = _signal.SamplingRate;
            var barkbands = FilterBanks.BarkBands(16, 512, sr, 100 /*Hz*/, 6500 /*Hz*/, overlap: false);
            var barkbank  = FilterBanks.Triangular(512, sr, barkbands);

            var mfccExtractor = new MfccExtractor(_signal.SamplingRate, 13,
                                                  //filterbankSize: 40,
                                                  //lowFreq: 100,
                                                  //highFreq: 4200,
                                                  //lifterSize: 22,
                                                  preEmphasis: 0.97,
                                                  //filterbank: barkbank,
                                                  window: WindowTypes.Hamming);

            _mfccVectors = mfccExtractor.ComputeFrom(_signal);

            //FeaturePostProcessing.NormalizeMean(_mfccVectors);        // optional
            //FeaturePostProcessing.AddDeltas(_mfccVectors);

            FillFeaturesList(_mfccVectors, mfccExtractor.FeatureDescriptions);
            mfccListView.Items[0].Selected = true;

            melFilterBankPanel.Groups = mfccExtractor.FilterBank;

            mfccPanel.Line = _mfccVectors[0].Features;

            using (var csvFile = new FileStream("mfccs.csv", FileMode.Create))
            {
                var header = mfccExtractor.FeatureDescriptions;
                //.Concat(mfccExtractor.DeltaFeatureDescriptions)
                //.Concat(mfccExtractor.DeltaDeltaFeatureDescriptions);

                var serializer = new CsvFeatureSerializer(_mfccVectors, header);
                await serializer.SerializeAsync(csvFile);
            }
        }
Exemplo n.º 2
0
        private async void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            using (var stream = new FileStream(ofd.FileName, FileMode.Open))
            {
                var waveFile = new WaveFile(stream, true);
                _signal = waveFile[Channels.Left];
            }

            var mfccExtractor = new MfccExtractor(13,
                                                  //frameSize: 0.03125,
                                                  //hopSize: 0.015625,
                                                  melFilterbankSize: 20,
                                                  //lowFreq: 100,
                                                  //highFreq: 4200,
                                                  //lifterSize: 22,
                                                  preEmphasis: 0.95,
                                                  window: WindowTypes.Hamming);

            _mfccVectors = mfccExtractor.ComputeFrom(_signal);

            //FeaturePostProcessing.NormalizeMean(_mfccVectors);
            //FeaturePostProcessing.AddDeltas(_mfccVectors);

            FillFeaturesList(_mfccVectors, mfccExtractor.FeatureDescriptions);
            mfccListView.Items[0].Selected = true;

            melFilterBankPanel.Groups = mfccExtractor.FilterBank;

            mfccPanel.Line = _mfccVectors[0].Features;

            using (var csvFile = new FileStream("mfccs.csv", FileMode.Create))
            {
                var header = mfccExtractor.FeatureDescriptions
                             .Concat(mfccExtractor.DeltaFeatureDescriptions)
                             .Concat(mfccExtractor.DeltaDeltaFeatureDescriptions);

                var serializer = new CsvFeatureSerializer(_mfccVectors, header);
                await serializer.SerializeAsync(csvFile);
            }
        }
Exemplo n.º 3
0
        public void TestOnlineFeatureExtractor()
        {
            var mfccOptions = new MfccOptions
            {
                SamplingRate   = 8000,
                FeatureCount   = 5,
                FrameSize      = 256,
                HopSize        = 50,
                FilterBankSize = 8
            };

            var signal = new WhiteNoiseBuilder().OfLength(1000).Build();

            var mfccExtractor = new MfccExtractor(mfccOptions);
            var mfccVectors   = mfccExtractor.ComputeFrom(signal);

            var onlineMfccExtractor = new OnlineFeatureExtractor(new MfccExtractor(mfccOptions));
            var onlineMfccVectors   = new List <float[]>();

            var i = 0;

            while (i < signal.Length)
            {
                // emulating online blocks with different sizes:
                var size  = (i + 1) * 15;
                var block = signal.Samples.Skip(i).Take(size).ToArray();

                var newVectors = onlineMfccExtractor.ComputeFrom(block);

                onlineMfccVectors.AddRange(newVectors);

                i += size;
            }

            var diff = mfccVectors.Zip(onlineMfccVectors, (e, o) => e.Zip(o, (f1, f2) => f1 - f2).Sum());

            Assert.That(diff, Is.All.EqualTo(0).Within(1e-7f));
        }
Exemplo n.º 4
0
        private void buttonCompute_Click(object sender, EventArgs e)
        {
            var filterCount  = int.Parse(textBoxSize.Text);
            var samplingRate = _signal.SamplingRate;
            var fftSize      = int.Parse(textBoxFftSize.Text);
            var lowFreq      = float.Parse(textBoxLowFreq.Text);
            var highFreq     = float.Parse(textBoxHighFreq.Text);

            Tuple <double, double, double>[] bands;
            float[][]  filterbank = null;
            VtlnWarper vtln       = null;

            if (checkBoxVtln.Checked)
            {
                var alpha    = float.Parse(textBoxVtlnAlpha.Text);
                var vtlnLow  = float.Parse(textBoxVtlnLow.Text);
                var vtlnHigh = float.Parse(textBoxVtlnHigh.Text);

                vtln = new VtlnWarper(alpha, lowFreq, highFreq, vtlnLow, vtlnHigh);
            }

            switch (comboBoxFilterbank.Text)
            {
            case "Mel":
                bands = FilterBanks.MelBands(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                break;

            case "Mel Slaney":
                bands      = FilterBanks.MelBandsSlaney(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                filterbank = FilterBanks.MelBankSlaney(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxNormalize.Checked, vtln);
                break;

            case "Bark":
                bands = FilterBanks.BarkBands(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                break;

            case "Bark Slaney":
                bands      = FilterBanks.BarkBandsSlaney(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                filterbank = FilterBanks.BarkBankSlaney(filterCount, fftSize, samplingRate, lowFreq, highFreq);
                break;

            case "Critical bands":
                bands = FilterBanks.CriticalBands(filterCount, fftSize, samplingRate, lowFreq, highFreq);
                break;

            case "Octave bands":
                bands = FilterBanks.OctaveBands(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                break;

            case "ERB":
                bands      = null;
                filterbank = FilterBanks.Erb(filterCount, fftSize, samplingRate, lowFreq, highFreq);
                break;

            default:
                bands = FilterBanks.HerzBands(filterCount, fftSize, samplingRate, lowFreq, highFreq, checkBoxOverlap.Checked);
                break;
            }

            if (bands != null && filterbank == null)
            {
                switch (comboBoxShape.Text)
                {
                case "Triangular":
                    filterbank = FilterBanks.Triangular(fftSize, samplingRate, bands, vtln, Utils.Scale.HerzToMel);
                    break;

                case "Trapezoidal":
                    filterbank = FilterBanks.Trapezoidal(fftSize, samplingRate, bands, vtln);
                    break;

                case "BiQuad":
                    filterbank = FilterBanks.BiQuad(fftSize, samplingRate, bands);
                    break;

                default:
                    filterbank = FilterBanks.Rectangular(fftSize, samplingRate, bands, vtln);
                    break;
                }

                if (checkBoxNormalize.Checked)
                {
                    FilterBanks.Normalize(filterCount, bands, filterbank);
                }
            }


            var spectrumType = (SpectrumType)comboBoxSpectrum.SelectedIndex;
            var nonLinearity = (NonLinearityType)comboBoxNonLinearity.SelectedIndex;
            var logFloor     = float.Parse(textBoxLogFloor.Text);

            var mfccExtractor = new MfccExtractor(//samplingRate, 13, 0.025, 0.01,
                samplingRate, 13, 512.0 / samplingRate, 0.01,
                filterbank: filterbank,
                //filterbankSize: 26,
                //highFreq: 8000,
                //preEmphasis: 0.97,
                //lifterSize: 22,
                //includeEnergy: true,
                spectrumType: spectrumType,
                nonLinearity: nonLinearity,
                dctType: comboBoxDct.Text,
                window: WindowTypes.Hamming,
                logFloor: logFloor);

            _mfccVectors = mfccExtractor.ComputeFrom(_signal);


            //_mfccVectors = mfccExtractor.ComputeFrom(_signal * 32768);
            //var mfccVectorsP = mfccExtractor.ParallelComputeFrom(_signal * 32768);

            //for (var i = 0; i < _mfccVectors.Count; i++)
            //{
            //    for (var j = 0; j < _mfccVectors[i].Features.Length; j++)
            //    {
            //        if (Math.Abs(_mfccVectors[i].Features[j] - mfccVectorsP[i].Features[j]) > 1e-32f)
            //        {
            //            MessageBox.Show($"Nope: {i} - {j}");
            //            return;
            //        }

            //        if (Math.Abs(_mfccVectors[i].TimePosition - mfccVectorsP[i].TimePosition) > 1e-32f)
            //        {
            //            MessageBox.Show($"Time: {i} - {j}");
            //            return;
            //        }
            //    }
            //}

            //FeaturePostProcessing.NormalizeMean(_mfccVectors);        // optional (but REQUIRED for PNCC!)
            //FeaturePostProcessing.AddDeltas(_mfccVectors);

            var header = mfccExtractor.FeatureDescriptions;

            //.Concat(mfccExtractor.DeltaFeatureDescriptions)
            //.Concat(mfccExtractor.DeltaDeltaFeatureDescriptions);

            FillFeaturesList(_mfccVectors, header);
            mfccListView.Items[0].Selected = true;

            melFilterBankPanel.Groups = mfccExtractor.FilterBank;

            mfccPanel.Line = _mfccVectors[0].Features;
        }
Exemplo n.º 5
0
        public void extractFeatures()
        {
            //NWaves
            //Initial setup
            if (_filePath != null)
            {
                DiscreteSignal signal;

                // load
                var mfcc_no      = 24;
                var samplingRate = 44100;
                var mfccOptions  = new MfccOptions
                {
                    SamplingRate  = samplingRate,
                    FeatureCount  = mfcc_no,
                    FrameDuration = 0.025 /*sec*/,
                    HopDuration   = 0.010 /*sec*/,
                    PreEmphasis   = 0.97,
                    Window        = WindowTypes.Hamming
                };

                var opts = new MultiFeatureOptions
                {
                    SamplingRate  = samplingRate,
                    FrameDuration = 0.025,
                    HopDuration   = 0.010
                };
                var tdExtractor   = new TimeDomainFeaturesExtractor(opts);
                var mfccExtractor = new MfccExtractor(mfccOptions);

                // Read from file.
                featureString = String.Empty;
                featureString = $"green,";
                //MFCC
                var avg_vec_mfcc = new List <float>(mfcc_no + 1);
                //TD Features
                var avg_vec_td = new List <float>(4);
                //Spectral features
                var avg_vec_spect = new List <float>(10);

                for (var i = 0; i < mfcc_no; i++)
                {
                    avg_vec_mfcc.Add(0f);
                }
                for (var i = 0; i < 4; i++)
                {
                    avg_vec_td.Add(0f);
                }

                for (var i = 0; i < 10; i++)
                {
                    avg_vec_spect.Add(0f);
                }

                string specFeatures = String.Empty;
                Console.WriteLine($"{tag} Reading from file");
                using (var stream = new FileStream(_filePath, FileMode.Open))
                {
                    var waveFile = new WaveFile(stream);
                    signal = waveFile[channel : Channels.Left];
                    ////Compute MFCC
                    float[] mfvfuck = new float[25];
                    var     sig_sam = signal.Samples;
                    mfccVectors = mfccExtractor.ComputeFrom(sig_sam);

                    var fftSize = 1024;
                    tdVectors = tdExtractor.ComputeFrom(signal.Samples);
                    var fft        = new Fft(fftSize);
                    var resolution = (float)samplingRate / fftSize;

                    var frequencies = Enumerable.Range(0, fftSize / 2 + 1)
                                      .Select(f => f * resolution)
                                      .ToArray();

                    var spectrum = new Fft(fftSize).MagnitudeSpectrum(signal).Samples;

                    var centroid  = Spectral.Centroid(spectrum, frequencies);
                    var spread    = Spectral.Spread(spectrum, frequencies);
                    var flatness  = Spectral.Flatness(spectrum, 0);
                    var noiseness = Spectral.Noiseness(spectrum, frequencies, 3000);
                    var rolloff   = Spectral.Rolloff(spectrum, frequencies, 0.85f);
                    var crest     = Spectral.Crest(spectrum);
                    var decrease  = Spectral.Decrease(spectrum);
                    var entropy   = Spectral.Entropy(spectrum);
                    specFeatures = $"{centroid},{spread},{flatness},{noiseness},{rolloff},{crest},{decrease},{entropy}";
                    //}
                    Console.WriteLine($"{tag} All features ready");
                    for (int calibC = 0; calibC < mfccVectors.Count; calibC += (mfccVectors.Count / duration) - 1)
                    {
                        featureString = String.Empty;
                        var tmp = new ModelInput();
                        for (var i = 0; i < mfcc_no; i++)
                        {
                            avg_vec_mfcc[i] = mfccVectors[calibC][i];
                        }
                        for (var i = 0; i < 4; i++)
                        {
                            avg_vec_td[i] = tdVectors[calibC][i];
                        }
                        featureString += String.Join(",", avg_vec_mfcc);
                        featureString += ",";
                        featureString += String.Join(",", avg_vec_td);
                        featureString += ",";
                        featureString += specFeatures;
                        Console.WriteLine($"{tag} Feature String ready {featureString}");
                        if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp")))
                        {
                            File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp"));
                            File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp"), featureString);
                        }
                        else
                        {
                            File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp"), featureString);
                        }

                        MLContext mLContext = new MLContext();

                        string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp");

                        IDataView dataView = mLContext.Data.LoadFromTextFile <ModelInput>(
                            path: fileName,
                            hasHeader: false,
                            separatorChar: ',',
                            allowQuoting: true,
                            allowSparse: false);

                        // Use first line of dataset as model input
                        // You can replace this with new test data (hardcoded or from end-user application)
                        ModelInput sampleForPrediction = mLContext.Data.CreateEnumerable <ModelInput>(dataView, false)
                                                         .First();
                        ModelOutput opm = ConsumeModel.Predict(sampleForPrediction);
                        featureTimeList.Add(opm.Score);
                        Console.WriteLine($"{tag} Feature vs time list ready");
                    }
                    //Console.WriteLine($"{tag} MFCC: {mfccVectors.Count}");
                    //Console.WriteLine($"{tag} TD: {tdVectors.Count}");
                    //Console.WriteLine($"{tag} featureTimeArray: {featureTimeList.Count} {featureString}");
                }
            }
        }
Exemplo n.º 6
0
        async public void extractFeatures(string _filepath, StorageFile sf)
        {
            op          = new float[10];
            tdVectors   = new List <float[]>();
            mfccVectors = new List <float[]>();


            featureTimeList = new List <float[]>();

            //NWaves
            FilePath       = _filepath;
            PredictedLabel = "Ready!.";
            //player.Load(GetStreamFromFile(FilePath));
            //player.Play();
            mMedia.Source = MediaSource.CreateFromStorageFile(sf);
            bool test = player.IsPlaying;

            mMedia.AutoPlay = true;
            MusicProperties properties = await sf.Properties.GetMusicPropertiesAsync();

            TimeSpan myTrackDuration = properties.Duration;

            duration = Convert.ToInt32(myTrackDuration.TotalSeconds);
            if (FilePath != null)
            {
                DiscreteSignal signal;

                // load
                var mfcc_no      = 24;
                var samplingRate = 44100;
                var mfccOptions  = new MfccOptions
                {
                    SamplingRate  = samplingRate,
                    FeatureCount  = mfcc_no,
                    FrameDuration = 0.025 /*sec*/,
                    HopDuration   = 0.010 /*sec*/,
                    PreEmphasis   = 0.97,
                    Window        = WindowTypes.Hamming
                };

                var opts = new MultiFeatureOptions
                {
                    SamplingRate  = samplingRate,
                    FrameDuration = 0.025,
                    HopDuration   = 0.010
                };
                var tdExtractor   = new TimeDomainFeaturesExtractor(opts);
                var mfccExtractor = new MfccExtractor(mfccOptions);

                // Read from file.
                featureString = String.Empty;
                featureString = $"green,";
                //MFCC
                var mfccList = new List <List <double> >();
                var tdList   = new List <List <double> >();
                //MFCC
                //TD Features
                //Spectral features
                for (var i = 0; i < mfcc_no; i++)
                {
                    mfccList.Add(new List <double>());
                }
                for (var i = 0; i < 4; i++)
                {
                    tdList.Add(new List <double>());
                }


                string specFeatures = String.Empty;
                Console.WriteLine($"{tag} Reading from file");
                using (var stream = new FileStream(FilePath, FileMode.Open))
                {
                    var waveFile = new WaveFile(stream);
                    signal = waveFile[channel : Channels.Left];
                    ////Compute MFCC
                    float[] mfvfuck = new float[25];
                    var     sig_sam = signal.Samples;
                    mfccVectors = mfccExtractor.ComputeFrom(sig_sam);

                    var fftSize = 1024;
                    tdVectors = tdExtractor.ComputeFrom(signal.Samples);
                    var fft        = new Fft(fftSize);
                    var resolution = (float)samplingRate / fftSize;

                    var frequencies = Enumerable.Range(0, fftSize / 2 + 1)
                                      .Select(f => f * resolution)
                                      .ToArray();

                    var spectrum = new Fft(fftSize).MagnitudeSpectrum(signal).Samples;

                    var centroid  = Spectral.Centroid(spectrum, frequencies);
                    var spread    = Spectral.Spread(spectrum, frequencies);
                    var flatness  = Spectral.Flatness(spectrum, 0);
                    var noiseness = Spectral.Noiseness(spectrum, frequencies, 3000);
                    var rolloff   = Spectral.Rolloff(spectrum, frequencies, 0.85f);
                    var crest     = Spectral.Crest(spectrum);
                    var decrease  = Spectral.Decrease(spectrum);
                    var entropy   = Spectral.Entropy(spectrum);
                    specFeatures = $"{centroid},{spread},{flatness},{noiseness},{rolloff},{crest},{decrease},{entropy}";
                    //}
                    Console.WriteLine($"{tag} All features ready");

                    for (int calibC = 0; calibC < mfccVectors.Count;)
                    {
                        featureString = String.Empty;
                        var tmp = new ModelInput();

                        for (var j = 0; j < (mfccVectors.Count / duration) - 1 && calibC < mfccVectors.Count; j++)
                        {
                            for (var i = 0; i < mfcc_no; i++)
                            {
                                mfccList[i].Add(mfccVectors[calibC][i]);
                            }
                            for (var i = 0; i < 4; i++)
                            {
                                tdList[i].Add(tdVectors[calibC][i]);
                            }
                            calibC += 1;
                        }

                        var mfcc_statistics = new List <double>();
                        for (var i = 0; i < mfcc_no; i++)
                        {
                            //preheader += m + "_mean";
                            //preheader += m + "_min";
                            //preheader += m + "_var";
                            //preheader += m + "_sd";
                            //preheader += m + "_med";
                            //preheader += m + "_lq";
                            //preheader += m + "_uq";
                            //preheader += m + "_skew";
                            //preheader += m + "_kurt";
                            mfcc_statistics.Add(Statistics.Mean(mfccList[i]));
                            mfcc_statistics.Add(Statistics.Minimum(mfccList[i]));
                            mfcc_statistics.Add(Statistics.Variance(mfccList[i]));
                            mfcc_statistics.Add(Statistics.StandardDeviation(mfccList[i]));
                            mfcc_statistics.Add(Statistics.Median(mfccList[i]));
                            mfcc_statistics.Add(Statistics.LowerQuartile(mfccList[i]));
                            mfcc_statistics.Add(Statistics.UpperQuartile(mfccList[i]));
                            mfcc_statistics.Add(Statistics.Skewness(mfccList[i]));
                            mfcc_statistics.Add(Statistics.Kurtosis(mfccList[i]));
                        }
                        var td_statistics = new List <double>();

                        for (var i = 0; i < 4; i++)
                        {
                            td_statistics.Add(Statistics.Mean(tdList[i]));
                            td_statistics.Add(Statistics.Minimum(tdList[i]));
                            td_statistics.Add(Statistics.Variance(tdList[i]));
                            td_statistics.Add(Statistics.StandardDeviation(tdList[i]));
                            td_statistics.Add(Statistics.Median(tdList[i]));
                            td_statistics.Add(Statistics.LowerQuartile(tdList[i]));
                            td_statistics.Add(Statistics.UpperQuartile(tdList[i]));
                            td_statistics.Add(Statistics.Skewness(tdList[i]));
                            td_statistics.Add(Statistics.Kurtosis(tdList[i]));
                        }

                        // Write MFCCs
                        featureString += String.Join(",", mfcc_statistics);
                        featureString += ",";
                        featureString += String.Join(",", td_statistics);
                        //Write Spectral features as well
                        featureString += ",";
                        featureString += specFeatures;
                        Console.WriteLine($"{tag} Feature String ready {featureString}");
                        if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp")))
                        {
                            File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp"));
                            File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp"), featureString);
                        }
                        else
                        {
                            File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp"), featureString);
                        }

                        MLContext mLContext = new MLContext();

                        string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp");

                        IDataView dataView = mLContext.Data.LoadFromTextFile <ModelInput>(
                            path: fileName,
                            hasHeader: false,
                            separatorChar: ',',
                            allowQuoting: true,
                            allowSparse: false);

                        // Use first line of dataset as model input
                        // You can replace this with new test data (hardcoded or from end-user application)
                        ModelInput sampleForPrediction = mLContext.Data.CreateEnumerable <ModelInput>(dataView, false)
                                                         .First();
                        ModelOutput opm = ConsumeModel.Predict(sampleForPrediction);
                        featureTimeList.Add(opm.Score);
                        Console.WriteLine($"{tag} Feature vs time list ready");
                    }
                    //Console.WriteLine($"{tag} MFCC: {mfccVectors.Count}");
                    //Console.WriteLine($"{tag} TD: {tdVectors.Count}");
                    //Console.WriteLine($"{tag} featureTimeArray: {featureTimeList.Count} {featureString}");
                }
            }
            playAudio();
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            DiscreteSignal signal;

            // load
            var mfcc_no      = 24;
            var samplingRate = 16000;
            var mfccOptions  = new MfccOptions
            {
                SamplingRate  = samplingRate,
                FeatureCount  = mfcc_no,
                FrameDuration = 0.025 /*sec*/,
                HopDuration   = 0.010 /*sec*/,
                PreEmphasis   = 0.97,
                Window        = WindowTypes.Hamming
            };

            var opts = new MultiFeatureOptions
            {
                SamplingRate  = samplingRate,
                FrameDuration = 0.025,
                HopDuration   = 0.010
            };



            var tdExtractor = new TimeDomainFeaturesExtractor(opts);

            var mfccExtractor = new MfccExtractor(mfccOptions);

            var folders = Directory.GetDirectories(Path.Combine(Environment.CurrentDirectory, "Dataset"));

            Console.WriteLine($"Started!");
            using (var writer = File.CreateText(Path.Combine(Environment.CurrentDirectory, "Data.csv")))
            {
                //Write header
                var main_header = "genre,";
                main_header += String.Join(",", mfccExtractor.FeatureDescriptions);
                main_header += ",";
                main_header += String.Join(",", tdExtractor.FeatureDescriptions);
                main_header += ",centroid,spread,flatness,noiseness,roloff,crest,decrease,spectral_entropy";
                writer.WriteLine(main_header);
                string feature_string = String.Empty;
                foreach (var folder in folders)
                {
                    var f_name = new DirectoryInfo(folder).Name;
                    var files  = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "Dataset", folder));
                    //Write the genre label here
                    Console.WriteLine($"{f_name}");
                    foreach (var filename in files)
                    {
                        feature_string = String.Empty;
                        feature_string = $"{f_name},";
                        //MFCC
                        var avg_vec_mfcc = new List <float>(mfcc_no + 1);
                        //TD Features
                        var avg_vec_td = new List <float>(4);
                        //Spectral features
                        var avg_vec_spect = new List <float>(10);

                        for (var i = 0; i < mfcc_no; i++)
                        {
                            avg_vec_mfcc.Add(0f);
                        }
                        for (var i = 0; i < 4; i++)
                        {
                            avg_vec_td.Add(0f);
                        }

                        for (var i = 0; i < 10; i++)
                        {
                            avg_vec_spect.Add(0f);
                        }

                        string specFeatures = String.Empty;
                        using (var stream = new FileStream(Path.Combine(Environment.CurrentDirectory, "Dataset", filename), FileMode.Open))
                        {
                            var waveFile = new WaveFile(stream);
                            signal = waveFile[Channels.Average];
                            //Compute MFCC
                            tdVectors   = tdExtractor.ComputeFrom(signal);
                            mfccVectors = mfccExtractor.ComputeFrom(signal);
                            var fftSize    = 1024;
                            var fft        = new Fft(fftSize);
                            var resolution = (float)samplingRate / fftSize;

                            var frequencies = Enumerable.Range(0, fftSize / 2 + 1)
                                              .Select(f => f * resolution)
                                              .ToArray();

                            var spectrum = new Fft(fftSize).MagnitudeSpectrum(signal).Samples;

                            var centroid  = Spectral.Centroid(spectrum, frequencies);
                            var spread    = Spectral.Spread(spectrum, frequencies);
                            var flatness  = Spectral.Flatness(spectrum, 0);
                            var noiseness = Spectral.Noiseness(spectrum, frequencies, 3000);
                            var rolloff   = Spectral.Rolloff(spectrum, frequencies, 0.85f);
                            var crest     = Spectral.Crest(spectrum);
                            var decrease  = Spectral.Decrease(spectrum);
                            var entropy   = Spectral.Entropy(spectrum);
                            specFeatures = $"{centroid},{spread},{flatness},{noiseness},{rolloff},{crest},{decrease},{entropy}";
                        }

                        //Write label here TODO

                        foreach (var inst in mfccVectors)
                        {
                            for (var i = 0; i < mfcc_no; i++)
                            {
                                avg_vec_mfcc[i] += inst[i];
                            }
                        }

                        foreach (var inst in tdVectors)
                        {
                            for (var i = 0; i < 4; i++)
                            {
                                avg_vec_td[i] += inst[i];
                            }
                        }

                        for (var i = 0; i < mfcc_no; i++)
                        {
                            avg_vec_mfcc[i] /= mfccVectors.Count;
                        }

                        for (var i = 0; i < 4; i++)
                        {
                            avg_vec_td[i] /= tdVectors.Count;
                        }


                        // Write MFCCs
                        feature_string += String.Join(",", avg_vec_mfcc);
                        feature_string += ",";
                        feature_string += String.Join(",", avg_vec_td);
                        //Write Spectral features as well
                        feature_string += ",";
                        feature_string += specFeatures;
                        writer.WriteLine(feature_string);
                        var file_name = new DirectoryInfo(filename).Name;
                        Console.WriteLine($"{file_name}");
                    }
                }
            }
            Console.WriteLine($"DONE");
            Console.ReadLine();
        }