полифония!

на данном этапе:
1. 8 полифонических голосов
2. ADSR огибающая для каждого голоса
3. MIDI Note On/Note Off
4. стереовыход
5. Equal-Power Panning
6. случайное распределение голосов по стереополю
This commit is contained in:
2026-11-14 20:37:48 +06:00
parent b4629fe2d3
commit 2cff003e4b
12 changed files with 127 additions and 75 deletions
-1
View File
@@ -19,7 +19,6 @@ set(SOURCES
src/synth.cpp src/synth.cpp
src/midi_input.cpp src/midi_input.cpp
src/voice.cpp src/voice.cpp
src/oscillator.cpp
) )
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})
+1
View File
@@ -5,6 +5,7 @@
#include <iostream> #include <iostream>
#include "../external/miniaudio.h" #include "../external/miniaudio.h"
#include "../include/synth.hpp" #include "../include/synth.hpp"
#include "../include/stereo_sample.hpp"
class AudioEngine{ class AudioEngine{
+2
View File
@@ -42,6 +42,8 @@ class Envelope{
void note_off(); void note_off();
double process(); double process();
bool is_idle() const;
private: private:
Condition condition; Condition condition;
+9
View File
@@ -0,0 +1,9 @@
#ifndef STEREOSAMPLE
#define STEREOSAMPLE
struct StereoSample{
double left;
double right;
};
#endif
+3 -19
View File
@@ -2,24 +2,16 @@
#define SYNTH #define SYNTH
#include <cstdint> #include <cstdint>
#include <numbers>
#include <cmath>
#include <atomic>
#include <array> #include <array>
#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" #include "../include/voice.hpp"
#include "../include/stereo_sample.hpp"
class Synth { class Synth {
public: public:
Synth(){ StereoSample generate();
current_note = 0;
};
double generate();
void generate_buffer(double* array, int size); void generate_buffer(double* array, int size);
void set_frequency(double value); void set_frequency(double value);
@@ -30,19 +22,11 @@ class Synth {
bool note_off(uint8_t note); bool note_off(uint8_t note);
private: private:
/* 1. частота звука в Герцах Voice* find_free_voice();
2. громкость звука, от 0 до 1
3. частота дискретизации
4. фаза. от 0 до 2 пи*/
Envelope envelope;
SpscQueue queue; SpscQueue queue;
Oscillator oscillator;
std::array<Voice, 8> voices; std::array<Voice, 8> voices;
uint8_t current_note;
}; };
#endif #endif
+13 -1
View File
@@ -4,24 +4,31 @@
#include <cstdint> #include <cstdint>
#include <numbers> #include <numbers>
#include <cmath> #include <cmath>
#include <algorithm>
#include <random>
#include "../include/envelope.hpp" #include "../include/envelope.hpp"
#include "../include/oscillator.hpp" #include "../include/oscillator.hpp"
#include "../include/stereo_sample.hpp"
class Voice{ class Voice{
public: public:
Voice(){ Voice(){
note = 0; note = 0;
pan = 0.0;
active = false; active = false;
} }
void note_on(uint8_t note, uint8_t velocity); void note_on(uint8_t note, uint8_t velocity);
void note_off(); void note_off();
void set_pan(double value);
double generate(); StereoSample generate();
bool is_active() const; bool is_active() const;
bool is_playing(uint8_t note_) const;
private: private:
Envelope envelope; Envelope envelope;
@@ -29,8 +36,13 @@ class Voice{
uint8_t note; uint8_t note;
double envelope_value;
double pan;
bool active; bool active;
}; };
#endif #endif
+3 -3
View File
@@ -37,8 +37,8 @@ void AudioEngine::data_callback(ma_device* pDevice, void* pOutput, const void* p
AudioEngine* data = static_cast<AudioEngine*>(pDevice->pUserData); AudioEngine* data = static_cast<AudioEngine*>(pDevice->pUserData);
float* output = static_cast<float*> (pOutput); float* output = static_cast<float*> (pOutput);
for (ma_uint32 i = 0; i < frameCount; ++i){ for (ma_uint32 i = 0; i < frameCount; ++i){
float buf = data->synth_.generate(); StereoSample buf = data->synth_.generate();
output[i*2] = buf; output[i*2] = buf.left;
output[i*2+1] = buf; output[i*2+1] = buf.right;
} }
} }
+4
View File
@@ -66,3 +66,7 @@ double Envelope::process(){
} }
} }
bool Envelope::is_idle() const{
return condition == Condition::Idle;
}
+2 -1
View File
@@ -8,6 +8,7 @@
#include "../include/spsc_queue.hpp" #include "../include/spsc_queue.hpp"
#include "../include/command.hpp" #include "../include/command.hpp"
#include "../include/midi_input.hpp" #include "../include/midi_input.hpp"
#include "../include/voice.hpp"
// void producer(SpscQueue& queue, std::atomic<bool>& producer_done){ // void producer(SpscQueue& queue, std::atomic<bool>& producer_done){
// for (int i = 0; i < 32; ++i){ // for (int i = 0; i < 32; ++i){
@@ -68,7 +69,7 @@ int main(){
MidiInput midi(synth); MidiInput midi(synth);
midi.list_ports(); midi.list_ports();
midi.open_port(1); midi.open_port(2);
// while (true) midi.read_message(); // while (true) midi.read_message();
-1
View File
@@ -1 +0,0 @@
+51 -38
View File
@@ -1,8 +1,11 @@
#include "../include/synth.hpp" #include "../include/synth.hpp"
double Synth::generate(){ StereoSample Synth::generate(){
int processed = 0; int processed = 0;
int active_voices = 0;
StereoSample generated = {0.0, 0.0};
StereoSample result = {0.0, 0.0};
Command command; Command command;
while (processed < 16 && queue.pop(command)){ while (processed < 16 && queue.pop(command)){
@@ -10,63 +13,64 @@ double Synth::generate(){
switch (command.type){ switch (command.type){
default: default:
break; break;
case CommandType::None: case CommandType::None:{
++processed; ++processed;
break; break;
}
case CommandType::NoteOn: case CommandType::NoteOn:{
if (command.velocity == 0){ Voice* voice = find_free_voice();
if (command.note == current_note){ if (voice){
envelope.note_off(); voice->note_on(command.note, command.velocity);
}
}
else{
current_note = command.note;
set_midi_note(command.note);
oscillator.amplitude = (static_cast<double>(command.velocity) / 127);
envelope.note_on();
} }
++processed; ++processed;
break; break;
}
case CommandType::NoteOff: case CommandType::NoteOff:{
if (command.note == current_note){ for (auto& voice : voices){
envelope.note_off(); if (voice.is_playing(command.note)){
voice.note_off();
}
} }
++processed; ++processed;
break; 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();
} }
for (auto& voice : voices){
void Synth::generate_buffer(double* array, int size){ generated = voice.generate();
for (int i = 0; i < size; ++i){ result.left += generated.left;
array[i] = generate(); result.right += generated.right;
if (voice.is_active()){
++active_voices;
} }
} }
if (active_voices > 0) return {(result.left / active_voices),(result.right / active_voices)};
void Synth::set_frequency(double value){ return result;
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){ // void Synth::generate_buffer(double* array, int size){
if (value < 0.0) oscillator.amplitude = 0.0; // for (int i = 0; i < size; ++i){
else if (value > 1.0) oscillator.amplitude = 1.0; // array[i] = generate();
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){ void Synth::set_midi_note(uint8_t note){
set_frequency(440.0*std::pow(2.0,( (static_cast<int>(note)-69)/12.0 ))); set_frequency(440.0*std::pow(2.0,( (static_cast<int>(note)-69)/12.0 )));
} }*/
bool Synth::note_on(uint8_t note, uint8_t velocity){ bool Synth::note_on(uint8_t note, uint8_t velocity){
return queue.push({CommandType::NoteOn, note, 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){ bool Synth::note_off(uint8_t note){
return queue.push({CommandType::NoteOff, note, 0}); return queue.push({CommandType::NoteOff, note, 0});
} }
Voice* Synth::find_free_voice(){
for (auto& voice : voices){
if (!voice.is_active()){
return &voice;
}
}
return nullptr;
}
+35 -7
View File
@@ -1,21 +1,49 @@
#include "../include/voice.hpp" #include "../include/voice.hpp"
void Voice::note_on(uint8_t note, uint8_t velocity){ std::random_device rd;
uint8_t current_note = note; std::mt19937 gen(rd());
void Voice::note_on(uint8_t note_, uint8_t velocity){
note = note_;
std::uniform_real_distribution<double> pan_distribution(-1.0, 1.0);
oscillator.frequency = 440.0*std::pow(2.0,( (static_cast<int>(note)-69)/12.0 )); oscillator.frequency = 440.0*std::pow(2.0,( (static_cast<int>(note)-69)/12.0 ));
// oscillator.amplitude = oscillator.amplitude = (static_cast<double>(velocity) / 127.0);
set_pan(pan_distribution(gen));
envelope.note_on(); envelope.note_on();
active = true; active = true;
} }
void Voice::note_off(){ void Voice::note_off(){
envelope.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; return active;
} }
bool Voice::is_playing(uint8_t note_) const {
return is_active() && note == note_;
}