Example #1
0
        private 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);
                _signal = waveFile[Channels.Left];
            }

            var frameSize = (double)4096 / _signal.SamplingRate;
            var hopSize   = (double)2048 / _signal.SamplingRate;

            var tdExtractor       = new TimeDomainFeaturesExtractor("all", frameSize, hopSize);
            var spectralExtractor = new SpectralFeaturesExtractor("all", frameSize, hopSize);

            var tdVectors       = tdExtractor.ParallelComputeFrom(_signal);
            var spectralVectors = spectralExtractor.ParallelComputeFrom(_signal);

            _vectors = FeaturePostProcessing.Join(tdVectors, spectralVectors);

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

            var descriptions = tdExtractor.FeatureDescriptions
                               .Concat(spectralExtractor.FeatureDescriptions);

            FillFeaturesList(_vectors, descriptions);
        }
Example #2
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();
        }