Files
chirping/note.cpp

107 lines
2.3 KiB
C++

#include "note.h"
const uint8_t get_octave_from_wholenote(const uint8_t wholenote) {
return wholenote == 0 ? 0 : 2+int((wholenote-1)/12);
}
const uint8_t get_note_from_wholenote(const uint8_t wholenote) {
return wholenote == 0 ? 0 : (wholenote-1)%12;
}
Note::Note() {
note = octave = instrument = volume = effect = 0;
}
Note::Note(const uint16_t note) {
this->Set(note);
}
Note::Note(const uint8_t note, const uint8_t instrument, const uint8_t volume, const uint8_t effect) {
this->note = note;
this->instrument = instrument;
this->volume = volume;
this->effect = effect;
this->octave = get_octave_from_wholenote(this->note);
this->note = get_note_from_wholenote(this->note);
}
Note::Note(const uint8_t note, const uint8_t octave, const uint8_t instrument, const uint8_t volume, const uint8_t effect) {
this->note = note;
this->octave = octave;
this->instrument = instrument;
this->volume = volume;
this->effect = effect;
}
void Note::Set(const uint16_t note) {
this->note = note >> 10;
this->instrument = (note >> 8) & 0x3;
this->volume = (note >> 4) & 0xf;
this->effect = note & 0xf;
this->octave = get_octave_from_wholenote(this->note);
this->note = get_note_from_wholenote(this->note);
}
void Note::SetAbsoluteNote(const uint8_t note) {
this->octave = get_octave_from_wholenote(note);
this->note = get_note_from_wholenote(note);
}
void Note::SetNote(const uint8_t note) {
this->note = note;
}
void Note::SetOctave(const uint8_t octave) {
this->octave = octave;
}
void Note::SetInstrument(const uint8_t instrument) {
this->instrument = instrument;
}
void Note::SetVolume(const uint8_t volume) {
this->volume = volume;
}
void Note::SetEffect(const uint8_t effect) {
this->effect = effect;
}
const uint16_t Note::Get() {
uint16_t result = (this->GetAbsoluteNote() << 10) + (this->instrument << 8) + (this->volume << 4) + this->effect;
return result;
}
const uint8_t Note::GetAbsoluteNote() {
if ((this->note == 0) && (this->octave == 0)) {
return 0;
} else {
uint8_t result = ((this->octave-2)*12)+this->note+1;
return result;
}
}
const uint8_t Note::GetNote() {
return this->note;
}
const uint8_t Note::GetOctave() {
return this->octave;
}
const uint8_t Note::GetInstrument() {
return this->instrument;
}
const uint8_t Note::GetVolume() {
return this->volume;
}
const uint8_t Note::GetEffect() {
return this->effect;
}