comparison src/feature_extractor.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:add35537fdbb
1 // Copyright 2011, Ian Hobson.
2 //
3 // This file is part of gpsynth.
4 //
5 // gpsynth is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // gpsynth is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with gpsynth in the file COPYING.
17 // If not, see http://www.gnu.org/licenses/.
18
19 // FeatureExtractor - responsible for managing the retrieval of features from
20 // audio data. Caches feature data to avoid recalculations when accessing
21 // different features that have the same dependencies.
22
23 #pragma once
24
25 #include "mfcc_analyzer.hpp"
26 #include "spectrum_analyzer.hpp"
27
28 #include <complex>
29 #include <vector>
30
31 namespace dsp {
32
33 typedef double Value;
34 typedef std::vector<Value> ValueList;
35 typedef std::complex<Value> ComplexValue;
36 typedef std::vector<ComplexValue> ComplexValueList;
37
38 class FeatureExtractor {
39 std::string file_path_;
40 ValueList window_;
41 ValueList frame_;
42 ValueList file_data_;
43 ValueList bin_frequencies_;
44 Value bin_size_;
45 std::vector<ComplexValueList> spectrum_;
46 std::vector<ValueList> magnitude_spectrum_;
47 std::vector<ValueList> log_magnitude_spectrum_;
48 std::vector<ValueList> mfccs_;
49 std::vector<ValueList> delta_mfccs_;
50 std::vector<ValueList> double_delta_mfccs_;
51 std::vector<ValueList> spectral_flux_;
52 ValueList pitch_;
53 ValueList spectral_centroid_;
54 ValueList spectral_spread_;
55 ValueList energy_;
56
57 int frames_;
58 int window_size_;
59 int hop_size_;
60 Value sample_rate_;
61
62 dsp::SpectrumAnalyzer spectrum_analyzer_;
63 dsp::MFCCAnalyzer mfcc_analyzer_;
64
65 public:
66 FeatureExtractor(const std::string& file_path = "",
67 int frame_size = 2048,
68 int hop_size = 512);
69
70 void LoadFile(const std::string& file_path);
71 void ClearCaches();
72
73 int Frames() const { return frames_; }
74
75 int WindowSize() const { return window_size_; }
76 int HopSize() const { return hop_size_; }
77
78 void SetWindowSize(int window_size);
79 void SetHopSize(int hop_size);
80
81 Value Duration() const;
82
83 const std::vector<ComplexValueList>& Spectrum();
84 const std::vector<ValueList>& MagnitudeSpectrum();
85 const std::vector<ValueList>& LogMagnitudeSpectrum();
86 const ValueList& Pitch();
87 const ValueList& SpectralCentroid();
88 const ValueList& SpectralSpread();
89 const std::vector<ValueList>& SpectralFlux();
90 const ValueList& Energy();
91 const std::vector<ValueList>& MFCCs();
92 const std::vector<ValueList>& DeltaMFCCs();
93 const std::vector<ValueList>& DoubleDeltaMFCCs();
94
95 private:
96 void GenerateWindow();
97 };
98
99 } // dsp namespace