полифония!

на данном этапе:
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/midi_input.cpp
src/voice.cpp
src/oscillator.cpp
)
add_executable(${PROJECT_NAME} ${SOURCES})
+1
View File
@@ -5,6 +5,7 @@
#include <iostream>
#include "../external/miniaudio.h"
#include "../include/synth.hpp"
#include "../include/stereo_sample.hpp"
class AudioEngine{
+2
View File
@@ -42,6 +42,8 @@ class Envelope{
void note_off();
double process();
bool is_idle() const;
private:
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
#include <cstdint>
#include <numbers>
#include <cmath>
#include <atomic>
#include <array>
#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<Voice, 8> voices;
uint8_t current_note;
};
#endif
+13 -1
View File
@@ -4,24 +4,31 @@
#include <cstdint>
#include <numbers>
#include <cmath>
#include <algorithm>
#include <random>
#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
+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);
float* output = static_cast<float*> (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;
}
}
+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/command.hpp"
#include "../include/midi_input.hpp"
#include "../include/voice.hpp"
// void producer(SpscQueue& queue, std::atomic<bool>& 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();
-1
View File
@@ -1 +0,0 @@
+52 -39
View File
@@ -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();
}
}
else{
current_note = command.note;
set_midi_note(command.note);
oscillator.amplitude = (static_cast<double>(command.velocity) / 127);
envelope.note_on();
case CommandType::NoteOn:{
Voice* voice = find_free_voice();
if (voice){
voice->note_on(command.note, command.velocity);
}
++processed;
break;
}
case CommandType::NoteOff:
if (command.note == current_note){
envelope.note_off();
case CommandType::NoteOff:{
for (auto& voice : voices){
if (voice.is_playing(command.note)){
voice.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<int>(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;
}
+35 -7
View File
@@ -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<double> pan_distribution(-1.0, 1.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();
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_;
}