Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix low notes; clean code #7

Merged
merged 18 commits into from
May 11, 2022
Prev Previous commit
Next Next commit
rm c++ fluff
  • Loading branch information
cvonk committed May 8, 2022
commit 1744a4883a867012d875c9f82d51515e2e5628d8
3 changes: 2 additions & 1 deletion main/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
// choice: input source
#define SRC_MICR (1)
#define SRC_FILE (2)
#define SRC (SRC_FILE)
#define SRC (SRC_MICR)

// choice: output destination (must be DST_PIANOROLL for USB-MIDI output)
// DST_SERIAL in combination with SRC_FILE and outputs freq and notes notes recognized, for offline analysis
#define DST_STAFF (1)
#define DST_PIANOROLL (2)
#define DST_SERIAL (3)
Expand Down
12 changes: 6 additions & 6 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace {
float freq = frequency_calculate(samples);

// find note from frequency
Pitch pitch(freq);
Pitch pitch(freq); // instantiate using the `freq`

# if DST == DST_STAFF

Expand All @@ -194,7 +194,7 @@ namespace {
# elif DST == DST_SERIAL

// show note on Serial monitor
Pitch pitchIn(noteName);
Pitch pitchIn(noteName); // instantiate using the `noteName`
pitch.serialOut(instrument, pitchIn, freq, amplitude);
# endif
}
Expand Down Expand Up @@ -286,7 +286,7 @@ loop()
}

// find corresponding note
Pitch pitch(freq);
Pitch pitch(freq); // instantiate using the `freq`

# if DST == DST_STAFF

Expand All @@ -306,15 +306,15 @@ loop()
pianoroll_clear();
}

#if 0
# if 0
if (midifile_write(mv.segmentBuf, "arduino.mid") != 0) {
Serial.println("midifile_write is mad");
}
#endif
# endif

# elif DST == DST_SERIAL

Pitch pitchIn(noteNr_t::C,0);
Pitch pitchIn(NOTENR_C, 0); // instantiate another using the `noteNr`
pitch.serialOut("microphone", pitchIn, freq, amplitude);

# endif
Expand Down
18 changes: 15 additions & 3 deletions main/pitch_t.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#pragma once

enum class noteNr_t {
C = 0, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B, COUNT
};
typedef enum noteNr_t {
NOTENR_C = 0,
NOTENR_Db,
NOTENR_D,
NOTENR_Eb,
NOTENR_E,
NOTENR_F,
NOTENR_Gb,
NOTENR_G,
NOTENR_Ab,
NOTENR_A,
NOTENR_Bb,
NOTENR_B,
NOTENR_COUNT
} noteNr_t;

typedef uint_least8_t octaveNr_t;

4 changes: 2 additions & 2 deletions main/src/display/pianoroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ _displayRoll(xCoordinate_t const xLeft,

noteNr_t nr = static_cast<noteNr_t>(ii % 12);

bool isC = (nr == noteNr_t::C);
bool isG = (nr == noteNr_t::G);
bool isC = (nr == NOTENR_C);
bool isG = (nr == NOTENR_G);

color_t const color = isC ? COLOR_ROLLC : isG ? COLOR_ROLLG : COLOR_ROLLOTHER;

Expand Down
6 changes: 3 additions & 3 deletions main/src/display/staff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ typedef struct position_t {

typedef struct staff_t {
Adafruit_ST7735 * tft;
staffnote_t const notes[static_cast<int>(noteNr_t::COUNT)];
staffnote_t const notes[static_cast<int>(NOTENR_COUNT)];
display_t display;
distance_t distance;
position_t position;
Expand Down Expand Up @@ -292,13 +292,13 @@ staff_init(uint_least8_t tftCS_pin, // GPIO# for SPI TFT Chip Select

_my.position = {
{ _freq2vStaffPos(CONFIG_MIDIMIKE_FREQ_MIN), _freq2vStaffPos(CONFIG_MIDIMIKE_FREQ_MAX)},
{ _nr2vStaffPos(noteNr_t::E, 4), _nr2vStaffPos(noteNr_t::F, 5) } // 30, 38
{ _nr2vStaffPos(NOTENR_E, 4), _nr2vStaffPos(NOTENR_F, 5) } // 30, 38
};

staffsymbol_init(_my.tft,
_my.display.width, _my.display.height,
_my.distance.noteRadius, _my.distance.bottom2loStaff, _my.distance.top2hiStaff,
_vStaffPos2y(_nr2vStaffPos(noteNr_t::G, 4)));
_vStaffPos2y(_nr2vStaffPos(NOTENR_G, 4)));

#if GKEY != GKEY_NONE
staffsymbol_draw(_hStaffPos2x(0), 0, (staffSymbolName_t)STAFFSYMBOL_NAME_GKEY, COLOR_STAFF);
Expand Down
24 changes: 12 additions & 12 deletions main/src/pitch/pitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
#include "pitch.h"

typedef struct note_t {
char const * const name;
frequency_t const freq;
frequency_t const freq_max; // up to, but not including
char const * const name;
frequency_t const freq;
frequency_t const freq_max; // up to, but not including
} note_t;

// note: calculating the values would probably take up more memory
static note_t const _notes[static_cast<int>(noteNr_t::COUNT)] = {
static note_t const _notes[NOTENR_COUNT] = {
{"C", 16.35159783, 16.83073622},
{"Db", 17.32391444, 17.83154388},
{"D", 18.35404799, 18.89186265},
Expand All @@ -55,7 +55,7 @@ static note_t const _notes[static_cast<int>(noteNr_t::COUNT)] = {

Pitch::Pitch(void)
{
cv.noteNr = noteNr_t::C;
cv.noteNr = NOTENR_C;
cv.octaveNr = 0;
}

Expand All @@ -68,7 +68,7 @@ Pitch::Pitch(char const * const fullname) // construct based on note name w/ oc
// chars before octave# are the note name itself
bool found = false;
note_t const * nn = _notes;
uint_least8_t max = static_cast<int>(noteNr_t::COUNT);
uint_least8_t max = static_cast<int>(NOTENR_COUNT);

for (uint_least8_t ii = 0; ii < max && !found; ii++, nn++) {
if ((nn->name[0] == fullname[0]) &&
Expand All @@ -80,7 +80,7 @@ Pitch::Pitch(char const * const fullname) // construct based on note name w/ oc
}

if (!found) {
cv.noteNr = noteNr_t::C;
cv.noteNr = NOTENR_C;
cv.octaveNr = 0;
}
}
Expand All @@ -107,19 +107,19 @@ Pitch::Pitch(frequency_t const freq) // construct based on frequency
}

} else {
cv.noteNr = noteNr_t::C;
cv.noteNr = NOTENR_C;
cv.octaveNr = 0;
}
}

Pitch::Pitch(noteNr_t const number, // note# with the octave
octaveNr_t const octave) // octave#
octaveNr_t const octave) // octave#
{
if (number < noteNr_t::COUNT) {
if (number < NOTENR_COUNT) {
cv.noteNr = number;
cv.octaveNr = octave;
} else {
cv.noteNr = noteNr_t::C;
cv.noteNr = NOTENR_C;
cv.octaveNr = 0;
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ Pitch::getFrequency(void) const
uint_least8_t const idx = static_cast<uint_least8_t>(cv.noteNr);

if (idx >= notesCnt ||
(cv.noteNr == noteNr_t::C && cv.octaveNr == 0)) {
(cv.noteNr == NOTENR_C && cv.octaveNr == 0)) {
return 0;
}
float freq = _notes[idx].freq;
Expand Down