..
This commit is contained in:
@@ -18,6 +18,8 @@ set(SOURCES
|
|||||||
src/spsc_queue.cpp
|
src/spsc_queue.cpp
|
||||||
src/synth.cpp
|
src/synth.cpp
|
||||||
src/midi_input.cpp
|
src/midi_input.cpp
|
||||||
|
src/voice.cpp
|
||||||
|
src/oscillator.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef OSCILLATOR
|
||||||
|
#define OSCILLATOR
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
struct Oscillator{
|
||||||
|
double frequency = 440.0;
|
||||||
|
double amplitude = 0.5;
|
||||||
|
double phase = 0.0;
|
||||||
|
|
||||||
|
uint32_t sample_rate = 48000;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+6
-8
@@ -5,18 +5,17 @@
|
|||||||
#include <numbers>
|
#include <numbers>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
#include "../include/envelope.hpp"
|
#include "../include/envelope.hpp"
|
||||||
#include "../include/spsc_queue.hpp"
|
#include "../include/spsc_queue.hpp"
|
||||||
#include "../include/command.hpp"
|
#include "../include/command.hpp"
|
||||||
|
#include "../include/oscillator.hpp"
|
||||||
|
#include "../include/voice.hpp"
|
||||||
|
|
||||||
class Synth {
|
class Synth {
|
||||||
public:
|
public:
|
||||||
Synth(){
|
Synth(){
|
||||||
frequency = 440.0;
|
|
||||||
amplitude = 0.5;
|
|
||||||
sample_rate = 48000;
|
|
||||||
phase = 0.0;
|
|
||||||
current_note = 0;
|
current_note = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -38,12 +37,11 @@ class Synth {
|
|||||||
|
|
||||||
Envelope envelope;
|
Envelope envelope;
|
||||||
SpscQueue queue;
|
SpscQueue queue;
|
||||||
|
Oscillator oscillator;
|
||||||
|
|
||||||
|
std::array<Voice, 8> voices;
|
||||||
|
|
||||||
double frequency;
|
|
||||||
double amplitude;
|
|
||||||
double phase;
|
|
||||||
|
|
||||||
uint32_t sample_rate;
|
|
||||||
uint8_t current_note;
|
uint8_t current_note;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef VOICE
|
||||||
|
#define VOICE
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <numbers>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#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
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
+10
-10
@@ -23,7 +23,7 @@ double Synth::generate(){
|
|||||||
else{
|
else{
|
||||||
current_note = command.note;
|
current_note = command.note;
|
||||||
set_midi_note(command.note);
|
set_midi_note(command.note);
|
||||||
amplitude = (static_cast<double>(command.velocity) / 127);
|
oscillator.amplitude = (static_cast<double>(command.velocity) / 127);
|
||||||
envelope.note_on();
|
envelope.note_on();
|
||||||
}
|
}
|
||||||
++processed;
|
++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);
|
double y = oscillator.amplitude*std::sin(oscillator.phase);
|
||||||
phase += (2*std::numbers::pi*frequency)/(sample_rate);
|
oscillator.phase += (2*std::numbers::pi*oscillator.frequency)/(oscillator.sample_rate);
|
||||||
|
|
||||||
return y * envelope.process();
|
return y * envelope.process();
|
||||||
}
|
}
|
||||||
@@ -53,15 +53,15 @@ void Synth::generate_buffer(double* array, int size){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Synth::set_frequency(double value){
|
void Synth::set_frequency(double value){
|
||||||
if (value < 0.0) frequency = 0.0;
|
if (value < 0.0) oscillator.frequency = 0.0;
|
||||||
else if (value >(sample_rate/2)) frequency = sample_rate/2;
|
else if (value >(oscillator.sample_rate/2)) oscillator.frequency = oscillator.sample_rate/2;
|
||||||
else frequency = value;
|
else oscillator.frequency = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Synth::set_amplitude(double value){
|
void Synth::set_amplitude(double value){
|
||||||
if (value < 0.0) amplitude = 0.0;
|
if (value < 0.0) oscillator.amplitude = 0.0;
|
||||||
else if (value > 1.0) amplitude = 1.0;
|
else if (value > 1.0) oscillator.amplitude = 1.0;
|
||||||
else amplitude = value;
|
else oscillator.amplitude = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Synth::set_midi_note(uint8_t note){
|
void Synth::set_midi_note(uint8_t note){
|
||||||
|
|||||||
@@ -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<int>(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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user