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 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 GetAccidG() const;
63  data_ACCIDENTAL_WRITTEN GetAccidW() const;
64  data_PITCHNAME GetPitchName() const;
65  std::u32string GetPitchString() const;
66  std::string GetSimplePitchString() const;
67  bool IsValid(int maxAccid);
68  void SetPitch(int aPname, int anAccid, int anOct);
69 
70  //----------------//
71  // Static methods //
72  //----------------//
73 
74  static int GetChromaticAlteration(data_ACCIDENTAL_GESTURAL accidG, data_ACCIDENTAL_WRITTEN accidW);
75 };
76 
77 std::ostream &operator<<(std::ostream &out, const TransPitch &pitch);
78 
80 //
81 // The Transposer class is an interface for transposing notes represented in the
82 // TransPitch class format.
83 //
84 
85 class Transposer {
86 public:
87  Transposer();
88  ~Transposer();
89 
90  // Set the interval class for an octave (default is 40, +/- two sharps/flats).
91  void SetMaxAccid(int maxAccid);
92  int GetMaxAccid();
93  void SetBase40();
94  void SetBase600();
95  int GetBase();
96 
97  // Set the transposition amount for use with Transpose() functions. These functions
98  // need to be rerun after SetMaxAccid() or SetBase*() are called; otherwise, the
99  // transposition will be 0/P1/unison.
100  bool SetTransposition(int transVal);
101  bool SetTransposition(const std::string &transString);
102  bool SetTransposition(const TransPitch &fromPitch, const std::string &toString);
103  bool SetTransposition(int keyFifths, int semitones);
104  bool SetTransposition(int keyFifths, const std::string &semitones);
105 
106  // Accessor functions for retrieving stored transposition interval.
107  int GetTranspositionIntervalClass();
108  std::string GetTranspositionIntervalName();
109 
110  // Transpostion based on stored transposition interval.
111  void Transpose(TransPitch &pitch);
112  int Transpose(int iPitch);
113 
114  // Transpose based on second input parameter (not with stored transposition interval).
115  void Transpose(TransPitch &pitch, int transVal);
116  void Transpose(TransPitch &pitch, const std::string &transString);
117 
118  // Convert between integer intervals and interval name strings:
119  std::string GetIntervalName(const TransPitch &p1, const TransPitch &p2);
120  std::string GetIntervalName(int intervalClass);
121  int GetInterval(const std::string &intervalName);
122 
123  // Convert between TransPitch class and integer pitch and interval representations.
124  int TransPitchToIntegerPitch(const TransPitch &pitch);
125  TransPitch IntegerPitchToTransPitch(int ipitch);
126  int GetInterval(const TransPitch &p1, const TransPitch &p2);
127 
128  // Convert between Semitones and integer interval representation.
129  std::string SemitonesToIntervalName(int keyFifths, int semitones);
130  int SemitonesToIntervalClass(int keyFifths, int semitones);
131  int IntervalToSemitones(int intervalClass);
132  int IntervalToSemitones(const std::string &intervalName);
133 
134  // Circle-of-fifths related functions.
135  int IntervalToCircleOfFifths(const std::string &transString);
136  int IntervalToCircleOfFifths(int transval);
137  std::string CircleOfFifthsToIntervalName(int fifths);
138  int CircleOfFifthsToIntervalClass(int fifths);
139 
140  // Key-signature related functions.
141  bool GetKeyTonic(const std::string &keyTonic, TransPitch &tonic);
142  TransPitch CircleOfFifthsToMajorTonic(int fifths);
143  TransPitch CircleOfFifthsToMinorTonic(int fifths);
144  TransPitch CircleOfFifthsToDorianTonic(int fifths);
145  TransPitch CircleOfFifthsToPhrygianTonic(int fifths);
146  TransPitch CircleOfFifthsToLydianTonic(int fifths);
147  TransPitch CircleOfFifthsToMixolydianTonic(int fifths);
148  TransPitch CircleOfFifthsToLocrianTonic(int fifths);
149 
150  // Conversions between diatonic/chromatic system and integer system of intervals.
151  std::string DiatonicChromaticToIntervalName(int diatonic, int chromatic);
152  int DiatonicChromaticToIntervalClass(int diatonic, int chromatic);
153  void IntervalToDiatonicChromatic(int &diatonic, int &chromatic, int intervalClass);
154  void IntervalToDiatonicChromatic(int &diatonic, int &chromatic, const std::string &intervalName);
155 
156  // Convenience functions for calculating common interval classes. Augmented classes
157  // can be calculated by adding 1 to perfect/major classes, and diminished classes can be
158  // calcualted by subtracting 1 from perfect/minor classes.
159  int PerfectUnisonClass();
160  int MinorSecondClass();
161  int MajorSecondClass();
162  int MinorThirdClass();
163  int MajorThirdClass();
164  int PerfectFourthClass();
165  int PerfectFifthClass();
166  int MinorSixthClass();
167  int MajorSixthClass();
168  int MinorSeventhClass();
169  int MajorSeventhClass();
170  int PerfectOctaveClass();
171 
172  // Convenience functions for acessing m_diatonicMapping.
173  int GetCPitchClass() { return m_diatonicMapping[0]; }
174  int GetDPitchClass() { return m_diatonicMapping[1]; }
175  int GetEPitchClass() { return m_diatonicMapping[2]; }
176  int GetFPitchClass() { return m_diatonicMapping[3]; }
177  int GetGPitchClass() { return m_diatonicMapping[4]; }
178  int GetAPitchClass() { return m_diatonicMapping[5]; }
179  int GetBPitchClass() { return m_diatonicMapping[6]; }
180 
181  //----------------//
182  // Static methods //
183  //----------------//
184 
185  // Input string validity helper functions.
186  static bool IsValidIntervalName(const std::string &name);
187  static bool IsValidKeyTonic(const std::string &name);
188  static bool IsValidSemitones(const std::string &name);
189 
190 protected:
191  // integer representation for perfect octave:
192  int m_base;
193 
194  // maximum allowable sharp/flats for transposing:
195  int m_maxAccid;
196 
197  // integer interval class for transposing:
198  int m_transpose;
199 
200  // pitch integers for each natural diatonic pitch class:
201  std::vector<int> m_diatonicMapping;
202 
203  // used to calculate semitones between diatonic pitch classes:
204  const std::vector<int> m_diatonic2semitone{ 0, 2, 4, 5, 7, 9, 11 };
205 
206 private:
207  void CalculateDiatonicMapping();
208 };
209 
210 } // namespace vrv
211 
212 #endif
vrv::Transposer
Definition: transposition.h:85
vrv::TransPitch
Definition: transposition.h:35