-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicalFunctions.cpp
58 lines (51 loc) · 1.71 KB
/
MusicalFunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* MusicalFunctions.cpp
*
* Created on: Feb 19, 2014
* Author: edwinrietmeijer
*/
#include "MusicalFunctions.h"
namespace musical {
double centToRel( const double & centsVal ) {
return ( ( centsVal / 1200.0 ) + 1.0 );
}
std::vector<double> convertTempScaleToRelatives( const std::vector<double> cents_vector ) {
std::vector<double> noteRelatives;
std::vector<double>::const_iterator pos;
for( pos = cents_vector.begin(); pos != cents_vector.end(); ++pos ) {
std::cout << typeid( *pos ).name();
}
return noteRelatives;
}
double convertNoteNumToHertz( const int midiNote, const std::vector<double> tempRelatives, const double CENTER_FREQ, const int CENTER_MIDI ) {
double noteHertz;
double centerHertz = CENTER_FREQ;
int relNote;
double relOct;
double octMultiplier = 1.0;
int absNote;
int relMidiNote = midiNote - CENTER_MIDI;
if ( relMidiNote < 0 ) {
absNote = abs ( ( double ) relMidiNote );
relNote = 12 - ( absNote % 12 );
relOct = ( relMidiNote / 12 ) - 1;
if (relNote == 12) {
relNote = 0;
relOct += 1;
}
for ( int i = 0; i < std::abs( double( relOct ) ); i++) {
octMultiplier = octMultiplier / 2.0;
}
relOct = octMultiplier;
} else {
relNote = relMidiNote % 12;
relOct = ( relMidiNote / 12 ) + 1;
}
centerHertz = centerHertz * relOct;
// std::cout << "centerHertz: " << centerHertz << " relOct: " << relOct << " relNote: " << relNote << " tempRelatives[ relNote ]: " << tempRelatives[ relNote ] << std::endl;
noteHertz = centerHertz * tempRelatives[ relNote ];
// std::cout << noteHertz << std::endl;
// " = ( " << CENTER_FREQ << " * " << relOct << " ) * " << tempRelatives[ relNote ] << " [ " << relNote << " ] " << endl;
return noteHertz;
}
} // namespace musical