diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c31c18..e903271 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,6 @@ set(SOURCES src/synth.cpp src/midi_input.cpp src/voice.cpp - src/oscillator.cpp ) add_executable(${PROJECT_NAME} ${SOURCES}) diff --git a/include/audio_engine.hpp b/include/audio_engine.hpp index edfc3f2..8f42075 100644 --- a/include/audio_engine.hpp +++ b/include/audio_engine.hpp @@ -5,6 +5,7 @@ #include #include "../external/miniaudio.h" #include "../include/synth.hpp" +#include "../include/stereo_sample.hpp" class AudioEngine{ diff --git a/include/envelope.hpp b/include/envelope.hpp index d4cf24c..79d0d23 100644 --- a/include/envelope.hpp +++ b/include/envelope.hpp @@ -42,6 +42,8 @@ class Envelope{ void note_off(); double process(); + bool is_idle() const; + private: Condition condition; diff --git a/include/stereo_sample.hpp b/include/stereo_sample.hpp new file mode 100644 index 0000000..795cc52 --- /dev/null +++ b/include/stereo_sample.hpp @@ -0,0 +1,9 @@ +#ifndef STEREOSAMPLE +#define STEREOSAMPLE + +struct StereoSample{ + double left; + double right; +}; + +#endif diff --git a/include/synth.hpp b/include/synth.hpp index 785ad4f..3055bce 100644 --- a/include/synth.hpp +++ b/include/synth.hpp @@ -2,24 +2,16 @@ #define SYNTH #include -#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" +#include "../include/stereo_sample.hpp" class Synth { public: - Synth(){ - current_note = 0; - }; - - double generate(); + StereoSample generate(); void generate_buffer(double* array, int size); void set_frequency(double value); @@ -30,19 +22,11 @@ class Synth { bool note_off(uint8_t note); private: - /* 1. частота звука в Герцах - 2. громкость звука, от 0 до 1 - 3. частота дискретизации - 4. фаза. от 0 до 2 пи*/ + Voice* find_free_voice(); - Envelope envelope; SpscQueue queue; - Oscillator oscillator; std::array voices; - - - uint8_t current_note; }; #endif diff --git a/include/voice.hpp b/include/voice.hpp index b6c1c96..935f21f 100644 --- a/include/voice.hpp +++ b/include/voice.hpp @@ -4,24 +4,31 @@ #include #include #include +#include +#include #include "../include/envelope.hpp" #include "../include/oscillator.hpp" +#include "../include/stereo_sample.hpp" + class Voice{ public: Voice(){ note = 0; + pan = 0.0; active = false; } void note_on(uint8_t note, uint8_t velocity); void note_off(); + void set_pan(double value); - double generate(); + StereoSample generate(); bool is_active() const; + bool is_playing(uint8_t note_) const; private: Envelope envelope; @@ -29,8 +36,13 @@ class Voice{ uint8_t note; + double envelope_value; + double pan; + bool active; + + }; #endif diff --git a/src/audio_engine.cpp b/src/audio_engine.cpp index 0a683df..ee7affb 100644 --- a/src/audio_engine.cpp +++ b/src/audio_engine.cpp @@ -37,8 +37,8 @@ void AudioEngine::data_callback(ma_device* pDevice, void* pOutput, const void* p AudioEngine* data = static_cast(pDevice->pUserData); float* output = static_cast (pOutput); for (ma_uint32 i = 0; i < frameCount; ++i){ - float buf = data->synth_.generate(); - output[i*2] = buf; - output[i*2+1] = buf; + StereoSample buf = data->synth_.generate(); + output[i*2] = buf.left; + output[i*2+1] = buf.right; } } diff --git a/src/envelope.cpp b/src/envelope.cpp index 5fb3dfe..d80910f 100644 --- a/src/envelope.cpp +++ b/src/envelope.cpp @@ -66,3 +66,7 @@ double Envelope::process(){ } } + +bool Envelope::is_idle() const{ + return condition == Condition::Idle; +} diff --git a/src/main.cpp b/src/main.cpp index d7d617d..e11b1c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include "../include/spsc_queue.hpp" #include "../include/command.hpp" #include "../include/midi_input.hpp" +#include "../include/voice.hpp" // void producer(SpscQueue& queue, std::atomic& producer_done){ // for (int i = 0; i < 32; ++i){ @@ -68,7 +69,7 @@ int main(){ MidiInput midi(synth); midi.list_ports(); - midi.open_port(1); + midi.open_port(2); // while (true) midi.read_message(); diff --git a/src/oscillator.cpp b/src/oscillator.cpp deleted file mode 100644 index 8b13789..0000000 --- a/src/oscillator.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/synth.cpp b/src/synth.cpp index 528cb9e..f20b145 100644 --- a/src/synth.cpp +++ b/src/synth.cpp @@ -1,8 +1,11 @@ #include "../include/synth.hpp" -double Synth::generate(){ +StereoSample Synth::generate(){ int processed = 0; + int active_voices = 0; + StereoSample generated = {0.0, 0.0}; + StereoSample result = {0.0, 0.0}; Command command; while (processed < 16 && queue.pop(command)){ @@ -10,63 +13,64 @@ double Synth::generate(){ switch (command.type){ default: break; - case CommandType::None: + case CommandType::None:{ ++processed; break; + } - case CommandType::NoteOn: - if (command.velocity == 0){ - if (command.note == current_note){ - envelope.note_off(); + case CommandType::NoteOn:{ + Voice* voice = find_free_voice(); + if (voice){ + voice->note_on(command.note, command.velocity); + } + ++processed; + break; + } + + case CommandType::NoteOff:{ + for (auto& voice : voices){ + if (voice.is_playing(command.note)){ + voice.note_off(); } } - else{ - current_note = command.note; - set_midi_note(command.note); - oscillator.amplitude = (static_cast(command.velocity) / 127); - envelope.note_on(); - } - ++processed; - break; - - case CommandType::NoteOff: - if (command.note == current_note){ - envelope.note_off(); - } ++processed; break; + } } } - - oscillator.phase = std::fmod(oscillator.phase, (2*std::numbers::pi)); - - double y = oscillator.amplitude*std::sin(oscillator.phase); - oscillator.phase += (2*std::numbers::pi*oscillator.frequency)/(oscillator.sample_rate); - - return y * envelope.process(); -} - -void Synth::generate_buffer(double* array, int size){ - for (int i = 0; i < size; ++i){ - array[i] = generate(); + for (auto& voice : voices){ + generated = voice.generate(); + result.left += generated.left; + result.right += generated.right; + if (voice.is_active()){ + ++active_voices; + } } + if (active_voices > 0) return {(result.left / active_voices),(result.right / active_voices)}; + return result; } -void Synth::set_frequency(double 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::generate_buffer(double* array, int size){ +// for (int i = 0; i < size; ++i){ +// array[i] = generate(); +// } +// } -void Synth::set_amplitude(double 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_frequency(double 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) 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){ set_frequency(440.0*std::pow(2.0,( (static_cast(note)-69)/12.0 ))); -} +}*/ bool Synth::note_on(uint8_t note, uint8_t velocity){ return queue.push({CommandType::NoteOn, note, velocity}); @@ -75,3 +79,12 @@ bool Synth::note_on(uint8_t note, uint8_t velocity){ bool Synth::note_off(uint8_t note){ return queue.push({CommandType::NoteOff, note, 0}); } + +Voice* Synth::find_free_voice(){ + for (auto& voice : voices){ + if (!voice.is_active()){ + return &voice; + } + } + return nullptr; +} diff --git a/src/voice.cpp b/src/voice.cpp index 1e81190..291c5f8 100644 --- a/src/voice.cpp +++ b/src/voice.cpp @@ -1,21 +1,49 @@ #include "../include/voice.hpp" -void Voice::note_on(uint8_t note, uint8_t velocity){ - uint8_t current_note = note; +std::random_device rd; +std::mt19937 gen(rd()); + +void Voice::note_on(uint8_t note_, uint8_t velocity){ + note = note_; + + std::uniform_real_distribution pan_distribution(-1.0, 1.0); oscillator.frequency = 440.0*std::pow(2.0,( (static_cast(note)-69)/12.0 )); - // oscillator.amplitude = + oscillator.amplitude = (static_cast(velocity) / 127.0); + set_pan(pan_distribution(gen)); envelope.note_on(); active = true; } void Voice::note_off(){ envelope.note_off(); + if (envelope.is_idle()){ + active = false; + } } -/* -double Voice::generate(){ -}*/ +void Voice::set_pan(double value){ + pan = std::clamp(value, -1.0, 1.0); +} -const bool Voice::is_active(){ +StereoSample Voice::generate(){ + envelope_value = envelope.process(); + if (envelope.is_idle()){ + active = false; + } + oscillator.phase = std::fmod(oscillator.phase, (2*std::numbers::pi)); + double center = oscillator.amplitude*std::sin(oscillator.phase); + double angle = ((pan+1.0)/2.0)*(std::numbers::pi/2.0); + double left = center*std::cos(angle); + double right = center*std::sin(angle); + oscillator.phase += (2*std::numbers::pi*oscillator.frequency)/(oscillator.sample_rate); + + return {left*envelope_value, right*envelope_value}; +} + +bool Voice::is_active() const { return active; } + +bool Voice::is_playing(uint8_t note_) const { + return is_active() && note == note_; +}