Audacity  2.2.2
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 <math.h>
#include <stdio.h>
#include "Audacity.h"
#include "PitchName.h"
#include "Internat.h"

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)
 
wxString PitchName (const double dMIDInote, const PitchNameChoice choice)
 
wxString 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

double FreqToMIDInote ( const double  freq)

utilities for converting among frequency, MIDI note number, pitch index, pitch name

Definition at line 28 of file PitchName.cpp.

Referenced by FreqWindow::PlotPaint().

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 }
double MIDInoteToFreq ( const double  dMIDInote)

Definition at line 34 of file PitchName.cpp.

Referenced by PitchToFreq().

35 {
36  return (440.0 * pow(2.0, (dMIDInote - 69.0) / 12.0));
37 }
unsigned int PitchIndex ( const double  dMIDInote)

Definition at line 39 of file PitchName.cpp.

Referenced by PitchName().

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 }
wxString PitchName ( const double  dMIDInote,
const PitchNameChoice  choice 
)

Definition at line 59 of file PitchName.cpp.

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

Referenced by PitchName_Absolute().

60 {
61  static const wxString 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 wxString 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 wxString 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 wxString *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 }
#define XO(s)
Definition: Internat.h:33
unsigned int PitchIndex(const double dMIDInote)
Definition: PitchName.cpp:39
wxString PitchName_Absolute ( const double  dMIDInote,
const PitchNameChoice  choice 
)

Definition at line 153 of file PitchName.cpp.

References PitchName(), and PitchOctave().

Referenced by FreqWindow::PlotPaint().

154 {
155  // The format string is not localized. Should it be?
156  return wxString::Format(
157  wxT("%s%d"), PitchName(dMIDInote, choice), PitchOctave(dMIDInote) );
158 }
wxString PitchName(const double dMIDInote, const PitchNameChoice choice)
Definition: PitchName.cpp:59
int PitchOctave(const double dMIDInote)
Definition: PitchName.cpp:52
int PitchOctave ( const double  dMIDInote)

Definition at line 52 of file PitchName.cpp.

Referenced by PitchName_Absolute().

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

Definition at line 165 of file PitchName.cpp.

References MIDInoteToFreq(), and PitchToMIDInote().

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
double PitchToMIDInote ( const unsigned int  nPitchIndex,
const int  nPitchOctave 
)

Definition at line 160 of file PitchName.cpp.

Referenced by PitchToFreq().

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