1 // Copyright (C) 2010-2016  Lukas Lalinsky
2 // Distributed under the MIT license, see the LICENSE file for details.
3 
4 #include <limits>
5 #include <cmath>
6 #include "fft_frame.h"
7 #include "utils.h"
8 #include "chroma.h"
9 #include "debug.h"
10 
11 namespace chromaprint {
12 
13 static const int NUM_BANDS = 12;
14 
FreqToOctave(double freq,double base=440.0/16.0)15 inline double FreqToOctave(double freq, double base = 440.0 / 16.0)
16 {
17 	return log(freq / base) / log(2.0);
18 }
19 
Chroma(int min_freq,int max_freq,int frame_size,int sample_rate,FeatureVectorConsumer * consumer)20 Chroma::Chroma(int min_freq, int max_freq, int frame_size, int sample_rate, FeatureVectorConsumer *consumer)
21 	: m_interpolate(false),
22 	  m_notes(frame_size),
23 	  m_notes_frac(frame_size),
24 	  m_features(NUM_BANDS),
25 	  m_consumer(consumer)
26 {
27 	PrepareNotes(min_freq, max_freq, frame_size, sample_rate);
28 }
29 
~Chroma()30 Chroma::~Chroma()
31 {
32 }
33 
PrepareNotes(int min_freq,int max_freq,int frame_size,int sample_rate)34 void Chroma::PrepareNotes(int min_freq, int max_freq, int frame_size, int sample_rate)
35 {
36 	m_min_index = std::max(1, FreqToIndex(min_freq, frame_size, sample_rate));
37 	m_max_index = std::min(frame_size / 2, FreqToIndex(max_freq, frame_size, sample_rate));
38 	for (int i = m_min_index; i < m_max_index; i++) {
39 		double freq = IndexToFreq(i, frame_size, sample_rate);
40 		double octave = FreqToOctave(freq);
41 		double note = NUM_BANDS * (octave - floor(octave));
42 		m_notes[i] = (char)note;
43 		m_notes_frac[i] = note - m_notes[i];
44 	}
45 }
46 
Reset()47 void Chroma::Reset()
48 {
49 }
50 
Consume(const FFTFrame & frame)51 void Chroma::Consume(const FFTFrame &frame)
52 {
53 	fill(m_features.begin(), m_features.end(), 0.0);
54 	for (int i = m_min_index; i < m_max_index; i++) {
55 		int note = m_notes[i];
56 		double energy = frame[i];
57 		if (m_interpolate) {
58 			int note2 = note;
59 			double a = 1.0;
60 			if (m_notes_frac[i] < 0.5) {
61 				note2 = (note + NUM_BANDS - 1) % NUM_BANDS;
62 				a = 0.5 + m_notes_frac[i];
63 			}
64 			if (m_notes_frac[i] > 0.5) {
65 				note2 = (note + 1) % NUM_BANDS;
66 				a = 1.5 - m_notes_frac[i];
67 			}
68 			m_features[note] += energy * a;
69 			m_features[note2] += energy * (1.0 - a);
70 		}
71 		else {
72 			m_features[note] += energy;
73 		}
74 	}
75 	m_consumer->Consume(m_features);
76 }
77 
78 }; // namespace chromaprint
79