📄 pitch.cpp
字号:
// -*- c-basic-offset: 4 -*-#include "NotationRules.h"#include "NotationTypes.h"using namespace Rosegarden;using std::cout;using std::endl;using std::string;static const int verbose = 0;// This is the old NotationDisplayPitch -- this file was written for// regression testing when implementing the new Pitch class. It won't// compile any more as NotationDisplayPitch needs to be a friend of// Pitch for this implementation to work. Add "friend class// NotationDisplayPitch;" to end of Pitch in ../NotationTypes.h to// build it/** * NotationDisplayPitch stores a note's pitch in terms of the position * of the note on the staff and its associated accidental, and * converts these values to and from performance (MIDI) pitches. * * Rationale: When we insert a note, we need to query the height of the * staff line next to which it's being inserted, then translate this * back to raw pitch according to the clef in force at the x-coordinate * at which the note is inserted. For display, we translate from raw * pitch using both the clef and the key in force. * * Whether an accidental should be displayed or not depends on the * current key, on whether we've already shown the same accidental for * that pitch in the same bar, on whether the note event explicitly * requests an accidental... All we calculate here is whether the * pitch "should" have an accidental, not whether it really will * (e.g. if the accidental has already appeared). * * (See also docs/discussion/units.txt for explanation of pitch units.) */class NotationDisplayPitch{public: /** * Construct a NotationDisplayPitch containing the given staff * height and accidental */ NotationDisplayPitch(int heightOnStaff, const Accidental &accidental); /** * Construct a NotationDisplayPitch containing the height and * accidental to which the given performance pitch corresponds * in the given clef and key */ NotationDisplayPitch(int pitch, const Clef &clef, const Key &key, const Accidental &explicitAccidental = Accidentals::NoAccidental); int getHeightOnStaff() const { return m_heightOnStaff; } Accidental getAccidental() const { return m_accidental; } /** * Calculate and return the performance (MIDI) pitch * corresponding to the stored height and accidental, in the * given clef and key */ int getPerformancePitch(const Clef &clef, const Key &key) const; /** * Calculate and return the performance (MIDI) pitch * corresponding to the stored height and accidental, * interpreting them as Rosegarden-2.1-style values (for * backward compatibility use), in the given clef and key */ int getPerformancePitchFromRG21Pitch(const Clef &clef, const Key &key) const; /** * Return the stored pitch as a string (C4, Bb2, etc...) * according to http://www.harmony-central.com/MIDI/Doc/table2.html * * If inclOctave is false, this will return C, Bb, etc. */ std::string getAsString(const Clef &clef, const Key &key, bool inclOctave = true, int octaveBase = -2) const; /** * Return the stored pitch as a description of a note in a * scale. Return values are: * * -- placeInScale: a number from 0-6 where 0 is C and 6 is B * * -- accidentals: a number from -2 to 2 where -2 is double flat, * -1 is flat, 0 is nothing, 1 is sharp, 2 is double sharp * * -- octave: MIDI octave in range -2 to 8, where pitch 0 is in * octave -2 and thus middle-C is in octave 3 * * This function is guaranteed never to return values out of * the above ranges. */ void getInScale(const Clef &clef, const Key &key, int &placeInScale, int &accidentals, int &octave) const; private: int m_heightOnStaff; Accidental m_accidental; static void rawPitchToDisplayPitch(int, const Clef &, const Key &, int &, Accidental &); static void displayPitchToRawPitch(int, Accidental, const Clef &, const Key &, int &, bool ignoreOffset = false);};//////////////////////////////////////////////////////////////////////// NotationDisplayPitch//////////////////////////////////////////////////////////////////////NotationDisplayPitch::NotationDisplayPitch(int heightOnStaff, const Accidental &accidental) : m_heightOnStaff(heightOnStaff), m_accidental(accidental){}NotationDisplayPitch::NotationDisplayPitch(int pitch, const Clef &clef, const Key &key, const Accidental &explicitAccidental) : m_accidental(explicitAccidental){ rawPitchToDisplayPitch(pitch, clef, key, m_heightOnStaff, m_accidental);}intNotationDisplayPitch::getPerformancePitch(const Clef &clef, const Key &key) const{ int p = 0; displayPitchToRawPitch(m_heightOnStaff, m_accidental, clef, key, p); return p;}intNotationDisplayPitch::getPerformancePitchFromRG21Pitch(const Clef &clef, const Key &) const{ // Rosegarden 2.1 pitches are a bit weird; see // docs/data_struct/units.txt // We pass the accidental and clef, a faked key of C major, and a // flag telling displayPitchToRawPitch to ignore the clef offset // and take only its octave into account int p = 0; displayPitchToRawPitch(m_heightOnStaff, m_accidental, clef, Key(), p, true); return p;}voidNotationDisplayPitch::rawPitchToDisplayPitch(int rawpitch, const Clef &clef, const Key &key, int &height, Accidental &accidental){ Pitch::rawPitchToDisplayPitch(rawpitch, clef, key, height, accidental);}voidNotationDisplayPitch::displayPitchToRawPitch(int height, Accidental accidental, const Clef &clef, const Key &key, int &pitch, bool ignoreOffset){ Pitch::displayPitchToRawPitch(height, accidental, clef, key, pitch, ignoreOffset);}stringNotationDisplayPitch::getAsString(const Clef &clef, const Key &key, bool inclOctave, int octaveBase) const{ static const string noteNamesSharps[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }; static const string noteNamesFlats[] = { "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" }; int performancePitch = getPerformancePitch(clef, key); // highly unlikely, but fatal if it happened: if (performancePitch < 0) performancePitch = 0; int pitch = performancePitch % 12; int octave = performancePitch / 12; if (!inclOctave) return key.isSharp() ? noteNamesSharps[pitch] : noteNamesFlats[pitch]; char tmp[1024]; if (key.isSharp()) sprintf(tmp, "%s%d", noteNamesSharps[pitch].c_str(), octave + octaveBase); else sprintf(tmp, "%s%d", noteNamesFlats[pitch].c_str(), octave + octaveBase); return string(tmp);}voidNotationDisplayPitch::getInScale(const Clef &clef, const Key &key, int &placeInScale, int &accidentals, int &octave) const{ //!!! Maybe we should bring the logic from rawPitchToDisplayPitch down // into this method, and make rawPitchToDisplayPitch wrap this static int pitches[2][12] = { { 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6 }, { 0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6 }, }; static int accidentalsForPitches[2][12] = { { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 }, { 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, 0 }, }; int performancePitch = getPerformancePitch(clef, key); // highly unlikely, but fatal if it happened: if (performancePitch < 0) performancePitch = 0; if (performancePitch > 127) performancePitch = 127; int pitch = performancePitch % 12; octave = performancePitch / 12 - 2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -