Audacity 3.2.0
PitchName.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4 Audacity(R) is copyright (c) 1999-2012 Audacity Team.
5 License: GPL v2 or later. See License.txt.
6
7 PitchName.cpp
8 Vaughan Johnson and Dominic Mazzoni.
9
10******************************************************************//*******************************************************************/
18
19#include "PitchName.h"
20#include "Internat.h"
21#include <math.h>
22
23double FreqToMIDInote(const double freq)
24{
25 // Make the calculation relative to A440 (A4), note number 69.
26 return (69.0 + (12.0 * (log(freq / 440.0) / log(2.0))));
27}
28
29double MIDInoteToFreq(const double dMIDInote)
30{
31 return (440.0 * pow(2.0, (dMIDInote - 69.0) / 12.0));
32}
33
34unsigned int PitchIndex(const double dMIDInote)
35{
36 // MIDI numbers can be negative. Round in the right direction.
37 double dRound = (dMIDInote < 0.0) ? -0.5 : 0.5;
38 int nPitchIndex = ((int)(dMIDInote + dRound) % 12);
39
40 // Because of the modulo, we know we're within 12 of positive, if dMIDInote is negative.
41 if (nPitchIndex < 0)
42 nPitchIndex += 12;
43
44 return nPitchIndex;
45}
46
47int PitchOctave(const double dMIDInote)
48{
49 double dRound = (dMIDInote < 0.0) ? -0.5 : 0.5;
50 return ((int)((dMIDInote + dRound) / 12.0) - 1);
51}
52
53
54TranslatableString PitchName(const double dMIDInote, const PitchNameChoice choice)
55{
56 static const TranslatableString sharpnames[12] = {
57 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
58 XO("C"),
59 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
60 XO("C\u266f"),
61 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
62 XO("D"),
63 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
64 XO("D\u266f"),
65 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
66 XO("E"),
67 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
68 XO("F"),
69 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
70 XO("F\u266f"),
71 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
72 XO("G"),
73 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
74 XO("G\u266f"),
75 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
76 XO("A"),
77 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
78 XO("A\u266f"),
79 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
80 XO("B"),
81 };
82
83 static const TranslatableString flatnames[12] = {
84 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
85 XO("C"),
86 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
87 XO("D\u266d"),
88 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
89 XO("D"),
90 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
91 XO("E\u266d"),
92 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
93 XO("E"),
94 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
95 XO("F"),
96 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
97 XO("G\u266d"),
98 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
99 XO("G"),
100 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
101 XO("A\u266d"),
102 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
103 XO("A"),
104 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
105 XO("B\u266d"),
106 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
107 XO("B"),
108 };
109
110 static const TranslatableString bothnames[12] = {
111 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
112 XO("C"),
113 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
114 XO("C\u266f/D\u266d"),
115 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
116 XO("D"),
117 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
118 XO("D\u266f/E\u266d"),
119 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
120 XO("E"),
121 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
122 XO("F"),
123 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
124 XO("F\u266f/G\u266d"),
125 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
126 XO("G"),
127 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
128 XO("G\u266f/A\u266d"),
129 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
130 XO("A"),
131 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
132 XO("A\u266f/B\u266d"),
133 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
134 XO("B"),
135 };
136
137 const TranslatableString *table = nullptr;
138 switch ( choice ) {
139 case PitchNameChoice::Sharps: table = sharpnames; break;
140 case PitchNameChoice::Flats: table = flatnames; break;
141 case PitchNameChoice::Both: table = bothnames; break;
142 default: wxASSERT(false); break;
143 }
144
145 return table[PitchIndex(dMIDInote)];
146}
147
148TranslatableString PitchName_Absolute(const double dMIDInote, const PitchNameChoice choice)
149{
150 // The format string is not localized. Should it be?
151 return Verbatim( wxT("%s%d") )
152 .Format( PitchName(dMIDInote, choice), PitchOctave(dMIDInote) );
153}
154
155double PitchToMIDInote(const unsigned int nPitchIndex, const int nPitchOctave)
156{
157 return ((double)nPitchIndex + (((double)nPitchOctave + 1.0) * 12.0));
158}
159
160double PitchToFreq(const unsigned int nPitchIndex, const int nPitchOctave)
161{
162 return MIDInoteToFreq(PitchToMIDInote(nPitchIndex, nPitchOctave));
163}
wxT("CloseDown"))
XO("Cut/Copy/Paste")
unsigned int PitchIndex(const double dMIDInote)
Definition: PitchName.cpp:34
double MIDInoteToFreq(const double dMIDInote)
Definition: PitchName.cpp:29
TranslatableString PitchName_Absolute(const double dMIDInote, const PitchNameChoice choice)
Definition: PitchName.cpp:148
double PitchToFreq(const unsigned int nPitchIndex, const int nPitchOctave)
Definition: PitchName.cpp:160
double PitchToMIDInote(const unsigned int nPitchIndex, const int nPitchOctave)
Definition: PitchName.cpp:155
int PitchOctave(const double dMIDInote)
Definition: PitchName.cpp:47
double FreqToMIDInote(const double freq)
Definition: PitchName.cpp:23
TranslatableString PitchName(const double dMIDInote, const PitchNameChoice choice)
Definition: PitchName.cpp:54
PitchNameChoice
Definition: PitchName.h:43
TranslatableString Verbatim(wxString str)
Require calls to the one-argument constructor to go through this distinct global function name.
Holds a msgid for the translation catalog; may also bind format arguments.
TranslatableString & Format(Args &&...args) &
Capture variadic format arguments (by copy) when there is no plural.