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
20
21#include "PitchName.h"
22
23#include <math.h>
24
25#include "Internat.h"
26
27
28double FreqToMIDInote(const double freq)
29{
30 // Make the calculation relative to A440 (A4), note number 69.
31 return (69.0 + (12.0 * (log(freq / 440.0) / log(2.0))));
32}
33
34double MIDInoteToFreq(const double dMIDInote)
35{
36 return (440.0 * pow(2.0, (dMIDInote - 69.0) / 12.0));
37}
38
39unsigned int PitchIndex(const double dMIDInote)
40{
41 // MIDI numbers can be negative. Round in the right direction.
42 double dRound = (dMIDInote < 0.0) ? -0.5 : 0.5;
43 int nPitchIndex = ((int)(dMIDInote + dRound) % 12);
44
45 // Because of the modulo, we know we're within 12 of positive, if dMIDInote is negative.
46 if (nPitchIndex < 0)
47 nPitchIndex += 12;
48
49 return nPitchIndex;
50}
51
52int PitchOctave(const double dMIDInote)
53{
54 double dRound = (dMIDInote < 0.0) ? -0.5 : 0.5;
55 return ((int)((dMIDInote + dRound) / 12.0) - 1);
56}
57
58
59TranslatableString PitchName(const double dMIDInote, const PitchNameChoice choice)
60{
61 static const TranslatableString sharpnames[12] = {
62 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
63 XO("C"),
64 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
65 XO("C\u266f"),
66 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
67 XO("D"),
68 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
69 XO("D\u266f"),
70 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
71 XO("E"),
72 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
73 XO("F"),
74 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
75 XO("F\u266f"),
76 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
77 XO("G"),
78 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
79 XO("G\u266f"),
80 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
81 XO("A"),
82 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
83 XO("A\u266f"),
84 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
85 XO("B"),
86 };
87
88 static const TranslatableString flatnames[12] = {
89 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
90 XO("C"),
91 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
92 XO("D\u266d"),
93 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
94 XO("D"),
95 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
96 XO("E\u266d"),
97 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
98 XO("E"),
99 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
100 XO("F"),
101 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
102 XO("G\u266d"),
103 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
104 XO("G"),
105 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
106 XO("A\u266d"),
107 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
108 XO("A"),
109 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
110 XO("B\u266d"),
111 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
112 XO("B"),
113 };
114
115 static const TranslatableString bothnames[12] = {
116 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
117 XO("C"),
118 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
119 XO("C\u266f/D\u266d"),
120 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
121 XO("D"),
122 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
123 XO("D\u266f/E\u266d"),
124 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
125 XO("E"),
126 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
127 XO("F"),
128 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
129 XO("F\u266f/G\u266d"),
130 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
131 XO("G"),
132 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
133 XO("G\u266f/A\u266d"),
134 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
135 XO("A"),
136 /* i18n-hint: Two, alternate names of a musical note in the 12-tone chromatic scale */
137 XO("A\u266f/B\u266d"),
138 /* i18n-hint: Name of a musical note in the 12-tone chromatic scale */
139 XO("B"),
140 };
141
142 const TranslatableString *table = nullptr;
143 switch ( choice ) {
144 case PitchNameChoice::Sharps: table = sharpnames; break;
145 case PitchNameChoice::Flats: table = flatnames; break;
146 case PitchNameChoice::Both: table = bothnames; break;
147 default: wxASSERT(false); break;
148 }
149
150 return table[PitchIndex(dMIDInote)];
151}
152
153TranslatableString PitchName_Absolute(const double dMIDInote, const PitchNameChoice choice)
154{
155 // The format string is not localized. Should it be?
156 return Verbatim( wxT("%s%d") )
157 .Format( PitchName(dMIDInote, choice), PitchOctave(dMIDInote) );
158}
159
160double PitchToMIDInote(const unsigned int nPitchIndex, const int nPitchOctave)
161{
162 return ((double)nPitchIndex + (((double)nPitchOctave + 1.0) * 12.0));
163}
164
165double PitchToFreq(const unsigned int nPitchIndex, const int nPitchOctave)
166{
167 return MIDInoteToFreq(PitchToMIDInote(nPitchIndex, nPitchOctave));
168}
wxT("CloseDown"))
XO("Cut/Copy/Paste")
unsigned int PitchIndex(const double dMIDInote)
Definition: PitchName.cpp:39
double MIDInoteToFreq(const double dMIDInote)
Definition: PitchName.cpp:34
TranslatableString PitchName_Absolute(const double dMIDInote, const PitchNameChoice choice)
Definition: PitchName.cpp:153
double PitchToFreq(const unsigned int nPitchIndex, const int nPitchOctave)
Definition: PitchName.cpp:165
double PitchToMIDInote(const unsigned int nPitchIndex, const int nPitchOctave)
Definition: PitchName.cpp:160
int PitchOctave(const double dMIDInote)
Definition: PitchName.cpp:52
double FreqToMIDInote(const double freq)
Definition: PitchName.cpp:28
TranslatableString PitchName(const double dMIDInote, const PitchNameChoice choice)
Definition: PitchName.cpp:59
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.