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