Skip to content

Commit

Permalink
added a new datatype about Musical Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandromrc committed Mar 12, 2023
1 parent 32c3f44 commit e866d05
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 218 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pico_sdk_init()
add_executable(${PROJECT_NAME}
main.cpp
core.hpp
debouncing.hpp
)

# Create map/bin/hex/uf2 files
Expand Down
97 changes: 4 additions & 93 deletions core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "hardware/rtc.h" // RTC
#include "pico/util/datetime.h" // Datetime utils

// our own imports from the core

#include "instruments/notes.hpp"

#define AIRCR_Register (*((volatile uint32_t *)(PPB_BASE + 0x0ED0C)))
#define HIGH true
#define LOW false
Expand All @@ -32,99 +36,6 @@
#define STATIC static
#define CONST const

// Notes to frequency

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#define REST 0

namespace PICO {

class String {
Expand Down
124 changes: 0 additions & 124 deletions debouncing.hpp

This file was deleted.

90 changes: 90 additions & 0 deletions instruments/notes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
namespace PICO {

typedef float Note;

class Notes {
private:

static const int octave_arr_index = 12;

const float zeroth_octave[octave_arr_index] = { 16.35, 17.32, 18.35, 19.45, 20.6, 21.83, 23.12, 24.5, 25.96, 27.5, 29.14, 30.87 };
const float first_octave[octave_arr_index] = { 32.7, 34.65, 36.71, 38.89, 41.2, 43.65, 46.25, 49, 51.91, 55, 58.27, 61.74 };
const float second_octave[octave_arr_index] = { 65.41, 69.3, 73.42, 77.78, 82.41, 87.31, 92.5, 98, 103.8, 110, 116.5, 123.5 };
const float third_octave[octave_arr_index] = { 130.8, 138.6, 146.8, 155.6, 164.8, 174.6, 185, 196, 207.7, 220, 233.1, 246.9 };
const float fourth_octave[octave_arr_index] = { 261.6, 277.2, 293.7, 311.1, 329.6, 349.2, 370, 392, 415.3, 440, 466.2, 493.9 };
const float fifth_octave[octave_arr_index] = { 523.3, 554.4, 587.3, 622.3, 659.3, 698.5, 740, 784, 830.6, 880, 932.3, 987.8 };
const float sixth_octave[octave_arr_index] = { 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976 };
const float seventh_octave[octave_arr_index] = { 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951 };
const float eighth_octave[octave_arr_index] = { 4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902 };

const float get_frequency(const int Note, const int Octave)
{
if (Octave > 8 || note > 11)
return 0;

switch(Octave)
{
case 0:
return zeroth_octave[Note];
break;
case 1:
return first_octave[Note];
break;
case 2:
return second_octave[Note];
break;
case 3:
return third_octave[Note];
break;
case 4:
return fourth_octave[Note];
break;
case 5:
return fifth_octave[Note];
break;
case 6:
return sixth_octave[Note];
break;
case 7:
return seventh_octave[Note];
break;
case 8:
return eighth_octave[Note];
break;
default:
return 0;
break;
}
return 0;
}

public:
enum List {
C = 0,
CS = 1,
D = 2,
DS = 3,
E = 4,
F = 5,
FS = 6,
G = 7,
GS = 8,
A = 9,
AS = 10,
B = 11
};

// returned freqyency from all the functions here that the user can access
float frequency
= 0;
int note = 0; // note choosen by th euser
int octave = 0; // octave choosen by the user

Notes(const int note, const int octave)
{
this->note = note;
this->octave = octave;
frequency = get_frequency(this->note, this->octave);
}
};
}
3 changes: 3 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ void loop1() {
}
}

Note note = Notes(Notes::List::C, 8).frequency;


void enc_change(void) { usb_serial.println(encoder.getValue()); }

void loop() {
Expand Down

0 comments on commit e866d05

Please sign in to comment.