Verovio
Source code documentation
transposition.h
1 // Name: transposition.h
3 // Author: Craig Stuart Sapp
4 // Created: 03/12/2019
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_TRANSPOSE_H__
9 #define __VRV_TRANSPOSE_H__
10 
11 #include <iostream>
12 #include <string>
13 #include <vector>
14 
15 #include "attdef.h"
16 #include "atttypes.h"
17 
18 #define INVALID_INTERVAL_CLASS -123456789
19 
20 namespace vrv {
21 
22 class TransPitch;
23 class Transposer;
24 
26 //
27 // The TransPitch class is an interface for storing information about notes that will
28 // be used in the Transposer class. The diatonic pitch class, chromatic alteration
29 // of the diatonic pitch and the octave are store in the class. Names given to the
30 // parameters are analogous to MEI note attributes. Note that note@accid can be also
31 // note/accid in MEI data, and other complications that need to be resolved into
32 // storing the correct pitch information in TransPitch.
33 //
34 
35 class TransPitch {
36 public:
37  // diatonic pitch class name of pitch: C = 0, D = 1, ... B = 6.
38  int m_pname;
39 
40  // chromatic alteration of pitch: 0 = natural, 1 = sharp, -2 = flat, +2 = double sharp
41  int m_accid;
42 
43  // octave number of pitch: 4 = middle-C octave
44  int m_oct;
45 
46  TransPitch() {}
47  TransPitch(int aPname, int anAccid, int anOct);
48  TransPitch(data_PITCHNAME pname, data_ACCIDENTAL_GESTURAL_basic accidG, data_ACCIDENTAL_WRITTEN accidW, int oct);
49  TransPitch(const TransPitch &pitch);
50 
51  // TransPitch operators
52  TransPitch &operator=(const TransPitch &pitch);
53  // lesser/greater operators
54  bool operator>(const TransPitch &pitch);
55  bool operator<(const TransPitch &pitch);
56  // increament/decrement operators
57  TransPitch &operator++();
58  TransPitch operator++(int);
59  TransPitch &operator--();
60  TransPitch operator--(int);
61 
62  data_ACCIDENTAL_GESTURAL_basic GetAccidGesBasic() const;
63  data_ACCIDENTAL_GESTURAL GetAccidGes() const;
64  data_ACCIDENTAL_WRITTEN GetAccidWritten() const;
65  data_PITCHNAME GetPitchName() const;
66  std::u32string GetPitchString() const;
67  std::string GetSimplePitchString() const;
68  bool IsValid(int maxAccid);
69  void SetPitch(int aPname, int anAccid, int anOct);
70 
71  //----------------//
72  // Static methods //
73  //----------------//
74 
75  static int GetChromaticAlteration(data_ACCIDENTAL_GESTURAL accidG, data_ACCIDENTAL_WRITTEN accidW);
76  static int GetChromaticAlteration(data_ACCIDENTAL_GESTURAL_basic accidG, data_ACCIDENTAL_WRITTEN accidW);
77 };
78 
79 std::ostream &operator<<(std::ostream &out, const TransPitch &pitch);
80 
82 //
83 // The Transposer class is an interface for transposing notes represented in the
84 // TransPitch class format.
85 //
86 
87 class Transposer {
88 public:
89  Transposer();
90  ~Transposer();
91 
92  // Set the interval class for an octave (default is 40, +/- two sharps/flats).
93  void SetMaxAccid(int maxAccid);
94  int GetMaxAccid();
95  void SetBase40();
96  void SetBase600();
97  int GetBase();
98 
99  // Set the transposition amount for use with Transpose() functions. These functions
100  // need to be rerun after SetMaxAccid() or SetBase*() are called; otherwise, the
101  // transposition will be 0/P1/unison.
102  bool SetTransposition(int transVal);
103  bool SetTransposition(const std::string &transString);
104  bool SetTransposition(const TransPitch &fromPitch, const std::string &toString);
105  bool SetTransposition(int keyFifths, int semitones);
106  bool SetTransposition(int keyFifths, const std::string &semitones);
107 
108  // Accessor functions for retrieving stored transposition interval.
109  int GetTranspositionIntervalClass();
110  std::string GetTranspositionIntervalName();
111 
112  // Transpostion based on stored transposition interval.
113  void Transpose(TransPitch &pitch);
114  int Transpose(int iPitch);
115 
116  // Transpose based on second input parameter (not with stored transposition interval).
117  void Transpose(TransPitch &pitch, int transVal);
118  void Transpose(TransPitch &pitch, const std::string &transString);
119 
120  // Convert between integer intervals and interval name strings:
121  std::string GetIntervalName(const TransPitch &p1, const TransPitch &p2);
122  std::string GetIntervalName(int intervalClass);
123  int GetInterval(const std::string &intervalName);
124 
125  // Convert between TransPitch class and integer pitch and interval representations.
126  int TransPitchToIntegerPitch(const TransPitch &pitch);
127  TransPitch IntegerPitchToTransPitch(int ipitch);
128  int GetInterval(const TransPitch &p1, const TransPitch &p2);
129 
130  // Convert between Semitones and integer interval representation.
131  std::string SemitonesToIntervalName(int keyFifths, int semitones);
132  int SemitonesToIntervalClass(int keyFifths, int semitones);
133  int IntervalToSemitones(int intervalClass);
134  int IntervalToSemitones(const std::string &intervalName);
135 
136  // Circle-of-fifths related functions.
137  int IntervalToCircleOfFifths(const std::string &transString);
138  int IntervalToCircleOfFifths(int transval);
139  std::string CircleOfFifthsToIntervalName(int fifths);
140  int CircleOfFifthsToIntervalClass(int fifths);
141 
142  // Key-signature related functions.
143  bool GetKeyTonic(const std::string &keyTonic, TransPitch &tonic);
144  TransPitch CircleOfFifthsToMajorTonic(int fifths);
145  TransPitch CircleOfFifthsToMinorTonic(int fifths);
146  TransPitch CircleOfFifthsToDorianTonic(int fifths);
147  TransPitch CircleOfFifthsToPhrygianTonic(int fifths);
148  TransPitch CircleOfFifthsToLydianTonic(int fifths);
149  TransPitch CircleOfFifthsToMixolydianTonic(int fifths);
150  TransPitch CircleOfFifthsToLocrianTonic(int fifths);
151 
152  // Conversions between diatonic/chromatic system and integer system of intervals.
153  std::string DiatonicChromaticToIntervalName(int diatonic, int chromatic);
154  int DiatonicChromaticToIntervalClass(int diatonic, int chromatic);
155  void IntervalToDiatonicChromatic(int &diatonic, int &chromatic, int intervalClass);
156  void IntervalToDiatonicChromatic(int &diatonic, int &chromatic, const std::string &intervalName);
157 
158  // Convenience functions for calculating common interval classes. Augmented classes
159  // can be calculated by adding 1 to perfect/major classes, and diminished classes can be
160  // calcualted by subtracting 1 from perfect/minor classes.
161  int PerfectUnisonClass();
162  int MinorSecondClass();
163  int MajorSecondClass();
164  int MinorThirdClass();
165  int MajorThirdClass();
166  int PerfectFourthClass();
167  int PerfectFifthClass();
168  int MinorSixthClass();
169  int MajorSixthClass();
170  int MinorSeventhClass();
171  int MajorSeventhClass();
172  int PerfectOctaveClass();
173 
174  // Convenience functions for acessing m_diatonicMapping.
175  int GetCPitchClass() { return m_diatonicMapping[0]; }
176  int GetDPitchClass() { return m_diatonicMapping[1]; }
177  int GetEPitchClass() { return m_diatonicMapping[2]; }
178  int GetFPitchClass() { return m_diatonicMapping[3]; }
179  int GetGPitchClass() { return m_diatonicMapping[4]; }
180  int GetAPitchClass() { return m_diatonicMapping[5]; }
181  int GetBPitchClass() { return m_diatonicMapping[6]; }
182 
183  //----------------//
184  // Static methods //
185  //----------------//
186 
187  // Input string validity helper functions.
188  static bool IsValidIntervalName(const std::string &name);
189  static bool IsValidKeyTonic(const std::string &name);
190  static bool IsValidSemitones(const std::string &name);
191 
192 protected:
193  // integer representation for perfect octave:
194  int m_base;
195 
196  // maximum allowable sharp/flats for transposing:
197  int m_maxAccid;
198 
199  // integer interval class for transposing:
200  int m_transpose;
201 
202  // pitch integers for each natural diatonic pitch class:
203  std::vector<int> m_diatonicMapping;
204 
205  // used to calculate semitones between diatonic pitch classes:
206  const std::vector<int> m_diatonic2semitone{ 0, 2, 4, 5, 7, 9, 11 };
207 
208 private:
209  void CalculateDiatonicMapping();
210 };
211 
212 } // namespace vrv
213 
214 #endif
Definition: transposition.h:35
Definition: transposition.h:87