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 "Internat.h"
#include <math.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 23 of file PitchName.cpp.

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}

Referenced by FrequencyPlotDialog::PlotPaint().

Here is the caller graph for this function:

◆ MIDInoteToFreq()

double MIDInoteToFreq ( const double  dMIDInote)

Definition at line 29 of file PitchName.cpp.

30{
31 return (440.0 * pow(2.0, (dMIDInote - 69.0) / 12.0));
32}

Referenced by PitchToFreq().

Here is the caller graph for this function:

◆ PitchIndex()

unsigned int PitchIndex ( const double  dMIDInote)

Definition at line 34 of file PitchName.cpp.

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}

Referenced by PitchName().

Here is the caller graph for this function:

◆ PitchName()

TranslatableString PitchName ( const double  dMIDInote,
const PitchNameChoice  choice 
)

Definition at line 54 of file PitchName.cpp.

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}
XO("Cut/Copy/Paste")
unsigned int PitchIndex(const double dMIDInote)
Definition: PitchName.cpp:34
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 148 of file PitchName.cpp.

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}
wxT("CloseDown"))
int PitchOctave(const double dMIDInote)
Definition: PitchName.cpp:47
TranslatableString PitchName(const double dMIDInote, const PitchNameChoice choice)
Definition: PitchName.cpp:54
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 47 of file PitchName.cpp.

48{
49 double dRound = (dMIDInote < 0.0) ? -0.5 : 0.5;
50 return ((int)((dMIDInote + dRound) / 12.0) - 1);
51}

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 160 of file PitchName.cpp.

161{
162 return MIDInoteToFreq(PitchToMIDInote(nPitchIndex, nPitchOctave));
163}
double MIDInoteToFreq(const double dMIDInote)
Definition: PitchName.cpp:29
double PitchToMIDInote(const unsigned int nPitchIndex, const int nPitchOctave)
Definition: PitchName.cpp:155

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 155 of file PitchName.cpp.

156{
157 return ((double)nPitchIndex + (((double)nPitchOctave + 1.0) * 12.0));
158}

Referenced by PitchToFreq().

Here is the caller graph for this function: