diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cfce10..5c31c18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,8 @@ set(SOURCES src/spsc_queue.cpp src/synth.cpp src/midi_input.cpp + src/voice.cpp + src/oscillator.cpp ) add_executable(${PROJECT_NAME} ${SOURCES}) diff --git a/include/oscillator.hpp b/include/oscillator.hpp new file mode 100644 index 0000000..6506143 --- /dev/null +++ b/include/oscillator.hpp @@ -0,0 +1,14 @@ +#ifndef OSCILLATOR +#define OSCILLATOR + +#include + +struct Oscillator{ + double frequency = 440.0; + double amplitude = 0.5; + double phase = 0.0; + + uint32_t sample_rate = 48000; +}; + +#endif diff --git a/include/synth.hpp b/include/synth.hpp index c8be0fd..785ad4f 100644 --- a/include/synth.hpp +++ b/include/synth.hpp @@ -5,18 +5,17 @@ #include #include #include +#include #include "../include/envelope.hpp" #include "../include/spsc_queue.hpp" #include "../include/command.hpp" +#include "../include/oscillator.hpp" +#include "../include/voice.hpp" class Synth { public: Synth(){ - frequency = 440.0; - amplitude = 0.5; - sample_rate = 48000; - phase = 0.0; current_note = 0; }; @@ -38,12 +37,11 @@ class Synth { Envelope envelope; SpscQueue queue; + Oscillator oscillator; + + std::array voices; - double frequency; - double amplitude; - double phase; - uint32_t sample_rate; uint8_t current_note; }; diff --git a/include/voice.hpp b/include/voice.hpp new file mode 100644 index 0000000..b6c1c96 --- /dev/null +++ b/include/voice.hpp @@ -0,0 +1,36 @@ +#ifndef VOICE +#define VOICE + +#include +#include +#include + +#include "../include/envelope.hpp" +#include "../include/oscillator.hpp" + +class Voice{ + public: + Voice(){ + note = 0; + + active = false; + } + + void note_on(uint8_t note, uint8_t velocity); + void note_off(); + + double generate(); + + bool is_active() const; + + private: + Envelope envelope; + Oscillator oscillator; + + uint8_t note; + + bool active; + +}; + +#endif diff --git a/src/oscillator.cpp b/src/oscillator.cpp new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/oscillator.cpp @@ -0,0 +1 @@ + diff --git a/src/synth.cpp b/src/synth.cpp index d80a068..528cb9e 100644 --- a/src/synth.cpp +++ b/src/synth.cpp @@ -23,7 +23,7 @@ double Synth::generate(){ else{ current_note = command.note; set_midi_note(command.note); - amplitude = (static_cast(command.velocity) / 127); + oscillator.amplitude = (static_cast(command.velocity) / 127); envelope.note_on(); } ++processed; @@ -38,10 +38,10 @@ double Synth::generate(){ } } - phase = std::fmod(phase, (2*std::numbers::pi)); + oscillator.phase = std::fmod(oscillator.phase, (2*std::numbers::pi)); - double y = amplitude*std::sin(phase); - phase += (2*std::numbers::pi*frequency)/(sample_rate); + double y = oscillator.amplitude*std::sin(oscillator.phase); + oscillator.phase += (2*std::numbers::pi*oscillator.frequency)/(oscillator.sample_rate); return y * envelope.process(); } @@ -53,15 +53,15 @@ void Synth::generate_buffer(double* array, int size){ } void Synth::set_frequency(double value){ - if (value < 0.0) frequency = 0.0; - else if (value >(sample_rate/2)) frequency = sample_rate/2; - else frequency = value; + if (value < 0.0) oscillator.frequency = 0.0; + else if (value >(oscillator.sample_rate/2)) oscillator.frequency = oscillator.sample_rate/2; + else oscillator.frequency = value; } void Synth::set_amplitude(double value){ - if (value < 0.0) amplitude = 0.0; - else if (value > 1.0) amplitude = 1.0; - else amplitude = value; + if (value < 0.0) oscillator.amplitude = 0.0; + else if (value > 1.0) oscillator.amplitude = 1.0; + else oscillator.amplitude = value; } void Synth::set_midi_note(uint8_t note){ diff --git a/src/voice.cpp b/src/voice.cpp new file mode 100644 index 0000000..1e81190 --- /dev/null +++ b/src/voice.cpp @@ -0,0 +1,21 @@ +#include "../include/voice.hpp" + +void Voice::note_on(uint8_t note, uint8_t velocity){ + uint8_t current_note = note; + oscillator.frequency = 440.0*std::pow(2.0,( (static_cast(note)-69)/12.0 )); + // oscillator.amplitude = + envelope.note_on(); + active = true; +} + +void Voice::note_off(){ + envelope.note_off(); +} +/* +double Voice::generate(){ + +}*/ + +const bool Voice::is_active(){ + return active; +}