Audacity 3.2.0
Functions
PitchName.cpp File Reference

Utilities for converting from frequency to pitch and from pitch to absolute (e.g., C4 for middle C) or nominal (A through G#) pitch name. More...

#include "PitchName.h"
#include <math.h>
#include "Internat.h"
Include dependency graph for PitchName.cpp:

Go to the source code of this file.

Functions

double FreqToMIDInote (const double freq)
 
double MIDInoteToFreq (const double dMIDInote)
 
unsigned int PitchIndex (const double dMIDInote)
 
int PitchOctave (const double dMIDInote)
 
TranslatableString PitchName (const double dMIDInote, const PitchNameChoice choice)
 
TranslatableString PitchName_Absolute (const double dMIDInote, const PitchNameChoice choice)
 
double PitchToMIDInote (const unsigned int nPitchIndex, const int nPitchOctave)
 
double PitchToFreq (const unsigned int nPitchIndex, const int nPitchOctave)
 

Detailed Description

Utilities for converting from frequency to pitch and from pitch to absolute (e.g., C4 for middle C) or nominal (A through G#) pitch name.

Definition in file PitchName.cpp.

Function Documentation

◆ FreqToMIDInote()

double FreqToMIDInote ( const double  freq)

Definition at line 28 of file PitchName.cpp.

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}

Referenced by FrequencyPlotDialog::PlotPaint().

Here is the caller graph for this function:

◆ MIDInoteToFreq()

double MIDInoteToFreq ( const double  dMIDInote)

Definition at line 34 of file PitchName.cpp.

35{
36 return (440.0 * pow(2.0, (dMIDInote - 69.0) / 12.0));
37}

Referenced by PitchToFreq().

Here is the caller graph for this function:

◆ PitchIndex()

unsigned int PitchIndex ( const double  dMIDInote)

Definition at line 39 of file PitchName.cpp.

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}

Referenced by PitchName().

Here is the caller graph for this function:

◆ PitchName()

TranslatableString PitchName ( const double  dMIDInote,
const PitchNameChoice  choice 
)

Definition at line 59 of file PitchName.cpp.

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}
XO("Cut/Copy/Paste")
unsigned int PitchIndex(const double dMIDInote)
Definition: PitchName.cpp:39
Holds a msgid for the translation catalog; may also bind format arguments.

References Both, Flats, PitchIndex(), Sharps, and XO().

Referenced by PitchName_Absolute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PitchName_Absolute()

TranslatableString PitchName_Absolute ( const double  dMIDInote,
const PitchNameChoice  choice 
)

Definition at line 153 of file PitchName.cpp.

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}
wxT("CloseDown"))
int PitchOctave(const double dMIDInote)
Definition: PitchName.cpp:52
TranslatableString PitchName(const double dMIDInote, const PitchNameChoice choice)
Definition: PitchName.cpp:59
TranslatableString Verbatim(wxString str)
Require calls to the one-argument constructor to go through this distinct global function name.
TranslatableString & Format(Args &&...args) &
Capture variadic format arguments (by copy) when there is no plural.

References TranslatableString::Format(), PitchName(), PitchOctave(), Verbatim(), and wxT().

Referenced by FrequencyPlotDialog::PlotPaint().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PitchOctave()

int PitchOctave ( const double  dMIDInote)

Definition at line 52 of file PitchName.cpp.

53{
54 double dRound = (dMIDInote < 0.0) ? -0.5 : 0.5;
55 return ((int)((dMIDInote + dRound) / 12.0) - 1);
56}

Referenced by PitchName_Absolute().

Here is the caller graph for this function:

◆ PitchToFreq()

double PitchToFreq ( const unsigned int  nPitchIndex,
const int  nPitchOctave 
)

Definition at line 165 of file PitchName.cpp.

166{
167 return MIDInoteToFreq(PitchToMIDInote(nPitchIndex, nPitchOctave));
168}
double MIDInoteToFreq(const double dMIDInote)
Definition: PitchName.cpp:34
double PitchToMIDInote(const unsigned int nPitchIndex, const int nPitchOctave)
Definition: PitchName.cpp:160

References MIDInoteToFreq(), and PitchToMIDInote().

Here is the call graph for this function:

◆ PitchToMIDInote()

double PitchToMIDInote ( const unsigned int  nPitchIndex,
const int  nPitchOctave 
)

Definition at line 160 of file PitchName.cpp.

161{
162 return ((double)nPitchIndex + (((double)nPitchOctave + 1.0) * 12.0));
163}

Referenced by PitchToFreq().

Here is the caller graph for this function: