ian@0: // Copyright 2011, Ian Hobson. ian@0: // ian@0: // This file is part of gpsynth. ian@0: // ian@0: // gpsynth is free software: you can redistribute it and/or modify ian@0: // it under the terms of the GNU General Public License as published by ian@0: // the Free Software Foundation, either version 3 of the License, or ian@0: // (at your option) any later version. ian@0: // ian@0: // gpsynth is distributed in the hope that it will be useful, ian@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of ian@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ian@0: // GNU General Public License for more details. ian@0: // ian@0: // You should have received a copy of the GNU General Public License ian@0: // along with gpsynth in the file COPYING. ian@0: // If not, see http://www.gnu.org/licenses/. ian@0: ian@0: // FeatureExtractor - responsible for managing the retrieval of features from ian@0: // audio data. Caches feature data to avoid recalculations when accessing ian@0: // different features that have the same dependencies. ian@0: ian@0: #pragma once ian@0: ian@0: #include "mfcc_analyzer.hpp" ian@0: #include "spectrum_analyzer.hpp" ian@0: ian@0: #include ian@0: #include ian@0: ian@0: namespace dsp { ian@0: ian@0: typedef double Value; ian@0: typedef std::vector ValueList; ian@0: typedef std::complex ComplexValue; ian@0: typedef std::vector ComplexValueList; ian@0: ian@0: class FeatureExtractor { ian@0: std::string file_path_; ian@0: ValueList window_; ian@0: ValueList frame_; ian@0: ValueList file_data_; ian@0: ValueList bin_frequencies_; ian@0: Value bin_size_; ian@0: std::vector spectrum_; ian@0: std::vector magnitude_spectrum_; ian@0: std::vector log_magnitude_spectrum_; ian@0: std::vector mfccs_; ian@0: std::vector delta_mfccs_; ian@0: std::vector double_delta_mfccs_; ian@0: std::vector spectral_flux_; ian@0: ValueList pitch_; ian@0: ValueList spectral_centroid_; ian@0: ValueList spectral_spread_; ian@0: ValueList energy_; ian@0: ian@0: int frames_; ian@0: int window_size_; ian@0: int hop_size_; ian@0: Value sample_rate_; ian@0: ian@0: dsp::SpectrumAnalyzer spectrum_analyzer_; ian@0: dsp::MFCCAnalyzer mfcc_analyzer_; ian@0: ian@0: public: ian@0: FeatureExtractor(const std::string& file_path = "", ian@0: int frame_size = 2048, ian@0: int hop_size = 512); ian@0: ian@0: void LoadFile(const std::string& file_path); ian@0: void ClearCaches(); ian@0: ian@0: int Frames() const { return frames_; } ian@0: ian@0: int WindowSize() const { return window_size_; } ian@0: int HopSize() const { return hop_size_; } ian@0: ian@0: void SetWindowSize(int window_size); ian@0: void SetHopSize(int hop_size); ian@0: ian@0: Value Duration() const; ian@0: ian@0: const std::vector& Spectrum(); ian@0: const std::vector& MagnitudeSpectrum(); ian@0: const std::vector& LogMagnitudeSpectrum(); ian@0: const ValueList& Pitch(); ian@0: const ValueList& SpectralCentroid(); ian@0: const ValueList& SpectralSpread(); ian@0: const std::vector& SpectralFlux(); ian@0: const ValueList& Energy(); ian@0: const std::vector& MFCCs(); ian@0: const std::vector& DeltaMFCCs(); ian@0: const std::vector& DoubleDeltaMFCCs(); ian@0: ian@0: private: ian@0: void GenerateWindow(); ian@0: }; ian@0: ian@0: } // dsp namespace