Mercurial > hg > easaier-soundaccess
comparison base/Pitch.cpp @ 0:fc9323a41f5a
start base : Sonic Visualiser sv1-1.0rc1
author | lbajardsilogic |
---|---|
date | Fri, 11 May 2007 09:08:14 +0000 |
parents | |
children | be6f263fa0ab |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fc9323a41f5a |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006 Chris Cannam. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "system/System.h" | |
17 #include "Pitch.h" | |
18 #include "Preferences.h" | |
19 | |
20 #include <cmath> | |
21 | |
22 float | |
23 Pitch::getFrequencyForPitch(int midiPitch, | |
24 float centsOffset, | |
25 float concertA) | |
26 { | |
27 if (concertA <= 0.0) { | |
28 concertA = Preferences::getInstance()->getTuningFrequency(); | |
29 } | |
30 float p = float(midiPitch) + (centsOffset / 100); | |
31 return concertA * powf(2.0, (p - 69.0) / 12.0); | |
32 } | |
33 | |
34 int | |
35 Pitch::getPitchForFrequency(float frequency, | |
36 float *centsOffsetReturn, | |
37 float concertA) | |
38 { | |
39 if (concertA <= 0.0) { | |
40 concertA = Preferences::getInstance()->getTuningFrequency(); | |
41 } | |
42 float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0; | |
43 | |
44 int midiPitch = int(p + 0.00001); | |
45 float centsOffset = (p - midiPitch) * 100.0; | |
46 | |
47 if (centsOffset >= 50.0) { | |
48 midiPitch = midiPitch + 1; | |
49 centsOffset = -(100.0 - centsOffset); | |
50 } | |
51 | |
52 if (centsOffsetReturn) *centsOffsetReturn = centsOffset; | |
53 return midiPitch; | |
54 } | |
55 | |
56 static QString notes[] = { | |
57 "C%1", "C#%1", "D%1", "D#%1", | |
58 "E%1", "F%1", "F#%1", "G%1", | |
59 "G#%1", "A%1", "A#%1", "B%1" | |
60 }; | |
61 | |
62 static QString flatNotes[] = { | |
63 "C%1", "Db%1", "D%1", "Eb%1", | |
64 "E%1", "F%1", "Gb%1", "G%1", | |
65 "Ab%1", "A%1", "Bb%1", "B%1" | |
66 }; | |
67 | |
68 QString | |
69 Pitch::getPitchLabel(int midiPitch, | |
70 float centsOffset, | |
71 bool useFlats) | |
72 { | |
73 int octave = -2; | |
74 | |
75 if (midiPitch < 0) { | |
76 while (midiPitch < 0) { | |
77 midiPitch += 12; | |
78 --octave; | |
79 } | |
80 } else { | |
81 octave = midiPitch / 12 - 2; | |
82 } | |
83 | |
84 QString plain = (useFlats ? flatNotes : notes)[midiPitch % 12].arg(octave); | |
85 | |
86 int ic = lrintf(centsOffset); | |
87 if (ic == 0) return plain; | |
88 else if (ic > 0) return QString("%1+%2c").arg(plain).arg(ic); | |
89 else return QString("%1%2c").arg(plain).arg(ic); | |
90 } | |
91 | |
92 QString | |
93 Pitch::getPitchLabelForFrequency(float frequency, | |
94 float concertA, | |
95 bool useFlats) | |
96 { | |
97 if (concertA <= 0.0) { | |
98 concertA = Preferences::getInstance()->getTuningFrequency(); | |
99 } | |
100 float centsOffset = 0.0; | |
101 int midiPitch = getPitchForFrequency(frequency, ¢sOffset, concertA); | |
102 return getPitchLabel(midiPitch, centsOffset, useFlats); | |
103 } | |
104 |