Initial
This commit is contained in:
473
music/note.cpp
Normal file
473
music/note.cpp
Normal file
@@ -0,0 +1,473 @@
|
||||
#include <music/note.hpp>
|
||||
#include <midiseq/midiout.hpp>
|
||||
#include <midiseq/note.hpp>
|
||||
#include <midiseq/pureevnt.hpp>
|
||||
|
||||
// Pitch array for translation to standard guitar tuning
|
||||
unsigned char Note::pitchArray[Note::Octaves][Note::NotesPerOctave]=
|
||||
{
|
||||
{0,1,2,3,4,5,6,7,8,9,10,11},
|
||||
{12,13,14,15,16,17,18,19,20,21,22,23},
|
||||
{24,25,26,27,28,29,30,31,32,33,34,35},
|
||||
{36,37,38,39,40,41,42,43,44,45,46,47},
|
||||
{48,49,50,51,52,53,54,55,56,57,58,59},
|
||||
{60,61,62,63,64,65,66,67,68,69,70,71},
|
||||
{72,73,74,75,76,77,78,79,80,81,82,83},
|
||||
{84,85,86,87,88,89,90,91,92,93,94,95},
|
||||
{96,97,98,99,100,101,102,103,104,105,106,107},
|
||||
{108,109,110,111,112,113,114,115,116,117,118,119},
|
||||
{120,121,122,123,124,125,126,127,0,1,2,3}
|
||||
};
|
||||
|
||||
/* Increments the current note 1/2 step
|
||||
* @param None
|
||||
* @return None
|
||||
*/
|
||||
const Note &Note::operator++()
|
||||
{
|
||||
mNote++;
|
||||
if(mNote>B)
|
||||
{
|
||||
mNote=C;
|
||||
mOctave++;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* Decrements the current note 1/2 step
|
||||
* @param None
|
||||
* @return None
|
||||
*/
|
||||
const Note &Note::operator--()
|
||||
{
|
||||
mNote--;
|
||||
if(mNote<C)
|
||||
{
|
||||
mNote=B;
|
||||
mOctave--;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* Increments the current note 1/2 step (returns note prior to increment)
|
||||
* @param None
|
||||
* @return None
|
||||
*/
|
||||
Note Note::operator++(int postFixDummy)
|
||||
{
|
||||
Note note(*this);
|
||||
++(*this);
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Decrements the current note 1/2 step (returns note prior to decrement)
|
||||
* @param None
|
||||
* @return None
|
||||
*/
|
||||
Note Note::operator--(int postFixDummy)
|
||||
{
|
||||
Note note(*this);
|
||||
--(*this);
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Adds a Step to the current note
|
||||
* @param step - step increment
|
||||
*/
|
||||
const Note &Note::operator+=(Step step)
|
||||
{
|
||||
int remainder;
|
||||
if(HalfStep==step)return ++(*this);
|
||||
mNote+=step;
|
||||
if(mNote>B)
|
||||
{
|
||||
mNote=C+(mNote-B)-1;
|
||||
mOctave++;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* Decrements a Step from the current note
|
||||
* @param step - step decrement
|
||||
*/
|
||||
const Note &Note::operator-=(Step step)
|
||||
{
|
||||
if(HalfStep==step)return --(*this);
|
||||
mNote-=step;
|
||||
if(mNote<C)
|
||||
{
|
||||
mNote=(B+mNote)+1;
|
||||
mOctave--;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* Turn on the current note
|
||||
* @param device - reference to the MIDI output device
|
||||
* @return true/false indicating success
|
||||
*/
|
||||
bool Note::noteOn(MIDIOutputDevice &device)
|
||||
{
|
||||
if(!device.hasDevice())return false;
|
||||
PureEvent midiEvent=((NoteOn&)toMIDINote()).getEvent();
|
||||
device.midiEvent(midiEvent);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Turn off the current note
|
||||
* @param device - reference to the MIDI output device
|
||||
* @return true/false indicating success
|
||||
*/
|
||||
bool Note::noteOff(MIDIOutputDevice &device)
|
||||
{
|
||||
if(!device.hasDevice())return false;
|
||||
PureEvent midiEvent=((NoteOff&)toMIDINote()).getEvent();
|
||||
device.midiEvent(midiEvent);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Get the note type of the current note
|
||||
* @param None
|
||||
* @return NoteType of note
|
||||
*/
|
||||
Note::NoteType Note::getNote(void)const
|
||||
{
|
||||
return (NoteType)mNote;
|
||||
}
|
||||
|
||||
/* Set the current note to NoteType
|
||||
* @param note - The NoteType to set
|
||||
* @return None
|
||||
*/
|
||||
void Note::setNote(NoteType note)
|
||||
{
|
||||
mNote=note;
|
||||
}
|
||||
|
||||
/* Get the octave associated with the current note
|
||||
* @param None
|
||||
* @return integer representation of the octave
|
||||
*/
|
||||
int Note::getOctave(void)const
|
||||
{
|
||||
return mOctave;
|
||||
}
|
||||
|
||||
/* Set the octave of the current note
|
||||
* @param octave - The octave to be associated with the current note
|
||||
* @return None
|
||||
*/
|
||||
void Note::setOctave(int octave)
|
||||
{
|
||||
mOctave=octave;
|
||||
}
|
||||
|
||||
/* Get the minor third interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the minor third interval of this note
|
||||
*/
|
||||
Note Note::getMinorThird(void)const
|
||||
{
|
||||
Note note(*this);
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::HalfStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the second interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the second interval of this note
|
||||
*/
|
||||
Note Note::getSecond(void)const
|
||||
{
|
||||
Note note(*this);
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the third interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the third interval of this note
|
||||
*/
|
||||
Note Note::getThird(void)const
|
||||
{
|
||||
Note note(*this);
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the fourth interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the fourth interval of this note
|
||||
*/
|
||||
Note Note::getFourth(void)const
|
||||
{
|
||||
Note note(getThird());
|
||||
note+=Note::HalfStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the fifth interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the fifth interval of this note
|
||||
*/
|
||||
Note Note::getFifth(void)const
|
||||
{
|
||||
Note note(getFourth());
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the diminished fifth interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the diminished fifth interval of this note
|
||||
*/
|
||||
Note Note::getDiminishedFifth(void)const
|
||||
{
|
||||
Note note(*this);
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the augmented fifth interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the augmented fifth interval of this note
|
||||
*/
|
||||
Note Note::getAugmentedFifth(void)const
|
||||
{
|
||||
Note note(*this);
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the sixth interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the sixth interval of this note
|
||||
*/
|
||||
Note Note::getSixth(void)const
|
||||
{
|
||||
Note note(getFifth());
|
||||
note+=WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the seventh interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the seventh interval of this note
|
||||
*/
|
||||
Note Note::getSeventh(void)const
|
||||
{
|
||||
Note note(getFifth());
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the ninth interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the ninth interval of this note
|
||||
*/
|
||||
Note Note::getNinth(void)const
|
||||
{
|
||||
Note note(getSeventh());
|
||||
note+=Note::HalfStep;
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the eleventh interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the eleventh interval of this note
|
||||
*/
|
||||
Note Note::getEleventh(void)const
|
||||
{
|
||||
Note note(getNinth());
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::HalfStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the thirteenth interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the thirteenth interval of this note
|
||||
*/
|
||||
Note Note::getThirteenth(void)const
|
||||
{
|
||||
Note note(getEleventh());
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the minor seventh interval associated with this note
|
||||
* @param None
|
||||
* @return Note representing the minor seventh interval of this note
|
||||
*/
|
||||
Note Note::getMinorSeventh(void)const
|
||||
{
|
||||
Note note(*this);
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
note+=Note::WholeStep;
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the parent note who's second is this note (ie) assuming this note to be an 'A'
|
||||
* this method will return an 'G'.
|
||||
* @param None
|
||||
* @return Note who's second is this note
|
||||
*/
|
||||
Note Note::getParentSecond(void)const
|
||||
{
|
||||
Note note(*this);
|
||||
note-=(Step::WholeStep);
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get the parent note who's fifth is this note (ie) assuming this note to be an 'E'
|
||||
* this method will return an 'A'.
|
||||
* @param None
|
||||
* @return Note who's fifth is this note
|
||||
*/
|
||||
Note Note::getParentFifth(void)const
|
||||
{
|
||||
Note note(*this);
|
||||
note-=(Step::WholeStep);
|
||||
note-=(Step::HalfStep);
|
||||
note-=(Step::WholeStep);
|
||||
note-=(Step::WholeStep);
|
||||
return note;
|
||||
}
|
||||
|
||||
/* Get a string representation of this note, including the octave.
|
||||
* @param None
|
||||
* @return String representing this note, including the octave
|
||||
*/
|
||||
String Note::toStringWithOctave(void)const
|
||||
{
|
||||
return toString()+String("(")+String().fromInt(mOctave)+String(")");
|
||||
}
|
||||
|
||||
/* Get a string representation of this note
|
||||
* @param None
|
||||
* @return String representing this note
|
||||
*/
|
||||
String Note::toString(void)const
|
||||
{
|
||||
switch(mNote)
|
||||
{
|
||||
case Note::A :
|
||||
return "A";
|
||||
case Note::ASh :
|
||||
return "A#";
|
||||
case Note::B :
|
||||
return "B";
|
||||
case Note::C :
|
||||
return "C";
|
||||
case Note::CSh :
|
||||
return "C#";
|
||||
case Note::D :
|
||||
return "D";
|
||||
case Note::DSh :
|
||||
return "D#";
|
||||
case Note::E :
|
||||
return "E";
|
||||
case Note::F :
|
||||
return "F";
|
||||
case Note::FSh :
|
||||
return "F#";
|
||||
case Note::G :
|
||||
return "G";
|
||||
case Note::GSh :
|
||||
return "G#";
|
||||
default :
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
/* Get a MIDI note representation of this note
|
||||
* @return PureNote - MIDI playable note
|
||||
*/
|
||||
PureNote Note::toMIDINote(void)const
|
||||
{
|
||||
PureNote pureNote;
|
||||
BYTE pitch;
|
||||
|
||||
pureNote.pitch(lookupPitch());
|
||||
pureNote.velocity(70);
|
||||
return pureNote;
|
||||
}
|
||||
|
||||
/* Create this note from a MIDI note
|
||||
* @param PureNote - MIDI note
|
||||
* @return true/false indicating success
|
||||
*/
|
||||
bool Note::fromMIDINote(const PureNote &pureNote)
|
||||
{
|
||||
Note note;
|
||||
if(!lookupNote(pureNote.pitch(),note))return false;
|
||||
*this=note;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Lookup the pitch to be played by this note
|
||||
* @param None
|
||||
* @return BYTE value representing pitch
|
||||
*/
|
||||
BYTE Note::lookupPitch(void)const
|
||||
{
|
||||
return pitchArray[getOctave()][mNote];
|
||||
}
|
||||
|
||||
/* Determine whether the given note and pitch fall within the playable region of the fretboard
|
||||
* @param Pitch - Pitch of note
|
||||
* @param Note - The note
|
||||
* @return true/false
|
||||
*/
|
||||
bool Note::lookupNote(int pitch,Note &refNote)const
|
||||
{
|
||||
for(int octave=0;octave<Octaves;octave++)
|
||||
{
|
||||
for(int note=0;note<NotesPerOctave;note++)
|
||||
{
|
||||
if(pitch==pitchArray[octave][note])
|
||||
{
|
||||
refNote.setNote((NoteType)note);
|
||||
refNote.setOctave(octave);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Get the NoteType representation of this note from the given string representation
|
||||
* @param String - string representation of note
|
||||
* @return NoteType
|
||||
*/
|
||||
Note::NoteType Note::getNoteType(String note)
|
||||
{
|
||||
note.upper();
|
||||
if(note=="C")return C;
|
||||
else if(note=="C#")return CSh;
|
||||
else if(note=="D")return D;
|
||||
else if(note=="D#")return DSh;
|
||||
else if(note=="E")return E;
|
||||
else if(note=="F")return F;
|
||||
else if(note=="F#")return FSh;
|
||||
else if(note=="G")return G;
|
||||
else if(note=="G#")return GSh;
|
||||
else if(note=="A")return A;
|
||||
else if(note=="A#")return ASh;
|
||||
else if(note=="B")return B;
|
||||
else return None;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user