адаптация s13bby/osc под vst и gui. (вайбкод)
вайбкод потому что мне в моменте стало понятно. что проще было сразу писать на фреймворке JUCE, чем адаптировать консольное приложение на MiniAudio. обновляться данный репозиторий не будет. СmakeLists адаптирован онли под linux.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
#ifndef AUDIO_ENGINE
|
||||
#define AUDIO_ENGINE
|
||||
|
||||
#include <stdexcept>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
|
||||
#include "../external/miniaudio.h"
|
||||
#include "../include/synth.hpp"
|
||||
#include "../include/stereo_sample.hpp"
|
||||
|
||||
class AudioEngine{
|
||||
|
||||
public:
|
||||
AudioEngine(Synth& synth, Synth& synth2) : synths{&synth, &synth2}{
|
||||
config = ma_device_config_init(ma_device_type_playback);
|
||||
config.playback.format = ma_format_f32;
|
||||
config.playback.channels = 2;
|
||||
config.sampleRate = 48000;
|
||||
config.pUserData = this;
|
||||
config.periodSizeInFrames = 512;
|
||||
config.dataCallback = data_callback;
|
||||
|
||||
result_init = ma_device_init(NULL, &config, &device);
|
||||
|
||||
if (result_init == MA_SUCCESS) device_is_init = true;
|
||||
else{
|
||||
throw std::runtime_error("ошибка: не удалось проинициализировать девайс");
|
||||
}
|
||||
}
|
||||
~AudioEngine(){
|
||||
if (device_is_init) ma_device_uninit(&device);
|
||||
}
|
||||
static void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount);
|
||||
|
||||
int start();
|
||||
int stop();
|
||||
|
||||
private:
|
||||
std::array<Synth*, 2> synths;
|
||||
|
||||
bool device_is_init = false;
|
||||
bool device_is_started = false;
|
||||
|
||||
ma_device_config config = {};
|
||||
ma_device device = {};
|
||||
|
||||
ma_result result_init;
|
||||
ma_result result_start;
|
||||
ma_result result_stop;
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef COMMAND
|
||||
#define COMMAND
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum class CommandType{
|
||||
None,
|
||||
NoteOn,
|
||||
NoteOff,
|
||||
PitchBend,
|
||||
ModWheel,
|
||||
SetUnisonVoices,
|
||||
SetStereoSpread,
|
||||
SetDetuneAmount,
|
||||
SetUnisonGain,
|
||||
SetWavetableSin,
|
||||
SetWavetableSaw,
|
||||
SetWavetableSquare,
|
||||
SetWavetableTriangle,
|
||||
SetFilterCutOff,
|
||||
SetFilterResonance,
|
||||
SetFilterOff,
|
||||
SetLowPassFilter,
|
||||
SetBandPassFilter,
|
||||
SetHighPassFilter
|
||||
};
|
||||
|
||||
struct Command{
|
||||
CommandType type = CommandType::None;
|
||||
uint8_t note = 0;
|
||||
uint8_t velocity = 0;
|
||||
uint16_t pitch_bend = 8192;
|
||||
uint8_t mod_wheel = 0;
|
||||
double value = 0.0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
#ifndef ENVELOPE
|
||||
#define ENVELOPE
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum class Condition{
|
||||
Idle,
|
||||
Attack,
|
||||
Decay,
|
||||
Sustain,
|
||||
Release
|
||||
};
|
||||
|
||||
class Envelope{
|
||||
|
||||
public:
|
||||
Envelope(){
|
||||
condition = Condition::Idle;
|
||||
attack_time = 0.8;
|
||||
decay_time = 1.0;
|
||||
sustain_level = 0.7;
|
||||
release_time = 0.2;
|
||||
currnet_amplitude = 0.0;
|
||||
rate = 0.0;
|
||||
sample_rate = 48000.0;
|
||||
note_is_pressed = false;
|
||||
}
|
||||
|
||||
Envelope(double attack, double decay, double sustain, double release){
|
||||
condition = Condition::Idle;
|
||||
attack_time = attack;
|
||||
decay_time = decay;
|
||||
sustain_level = sustain;
|
||||
release_time = release;
|
||||
currnet_amplitude = 0.0;
|
||||
rate = 0.0;
|
||||
sample_rate = 48000.0;
|
||||
note_is_pressed = false;
|
||||
}
|
||||
|
||||
void set_sample_rate(double sample_rate_);
|
||||
void set_attack(double value);
|
||||
void set_decay(double value);
|
||||
void set_sustain(double value);
|
||||
void set_release(double value);
|
||||
void note_on();
|
||||
void note_off();
|
||||
|
||||
double process();
|
||||
|
||||
bool is_idle() const;
|
||||
bool is_release() const;
|
||||
|
||||
private:
|
||||
Condition condition;
|
||||
|
||||
double attack_time;
|
||||
|
||||
double decay_time;
|
||||
|
||||
double sustain_level;
|
||||
|
||||
double release_time;
|
||||
|
||||
double currnet_amplitude;
|
||||
double rate;
|
||||
|
||||
double sample_rate;
|
||||
|
||||
bool note_is_pressed;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
#ifndef FILTER
|
||||
#define FILTER
|
||||
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
|
||||
#include "../include/command.hpp"
|
||||
|
||||
class Filter{
|
||||
public:
|
||||
Filter(){
|
||||
mode = CommandType::SetFilterOff;
|
||||
|
||||
cutoff = 1000.0;
|
||||
resonance = 0.707;
|
||||
frequency_coefficient = 0.0;
|
||||
low = 0.0;
|
||||
band = 0.0;
|
||||
high = 0.0;
|
||||
damping = 0.0;
|
||||
|
||||
sample_rate = 48000.0;
|
||||
update_coefficients();
|
||||
}
|
||||
|
||||
void set_sample_rate(double sample_rate_);
|
||||
void set_cutoff(double value);
|
||||
void set_resonance(double value);
|
||||
void set_filter_off();
|
||||
void set_low_pass_filter();
|
||||
void set_band_pass_filter();
|
||||
void set_high_pass_filter();
|
||||
void update_coefficients();
|
||||
|
||||
double process(double input);
|
||||
|
||||
private:
|
||||
double max_stable_cutoff() const;
|
||||
|
||||
CommandType mode;
|
||||
|
||||
double cutoff;
|
||||
double resonance;
|
||||
double frequency_coefficient;
|
||||
double low;
|
||||
double band;
|
||||
double high;
|
||||
double damping;
|
||||
|
||||
double sample_rate;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef LFO
|
||||
#define LFO
|
||||
|
||||
#include <cstdint>
|
||||
#include <numbers>
|
||||
#include <cmath>
|
||||
|
||||
class Lfo{
|
||||
public:
|
||||
Lfo(){
|
||||
sample = 0.0;
|
||||
frequency = 5.0;
|
||||
phase = 0.0;
|
||||
sample_rate = 48000.0;
|
||||
}
|
||||
|
||||
void set_sample_rate(double sample_rate_);
|
||||
double generate();
|
||||
|
||||
void set_frequency(double value);
|
||||
|
||||
private:
|
||||
double sample;
|
||||
double frequency;
|
||||
double phase;
|
||||
double sample_rate;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef MIDIINPUT
|
||||
#define MIDIINPUT
|
||||
|
||||
#include <rtmidi/RtMidi.h>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <array>
|
||||
|
||||
#include "../include/synth.hpp"
|
||||
|
||||
class MidiInput{
|
||||
|
||||
|
||||
public:
|
||||
MidiInput(Synth& synth, Synth& synth2) : synths{&synth, &synth2} {
|
||||
pitch = 8192;
|
||||
mod_wheel = 0;
|
||||
}
|
||||
|
||||
void list_ports();
|
||||
void open_port(unsigned int port);
|
||||
void read_message();
|
||||
|
||||
private:
|
||||
RtMidiIn midiIn;
|
||||
std::array<Synth*, 2> synths;
|
||||
|
||||
uint16_t pitch;
|
||||
uint8_t mod_wheel;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,60 @@
|
||||
#ifndef OSCILLATOR
|
||||
#define OSCILLATOR
|
||||
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <numbers>
|
||||
#include <array>
|
||||
|
||||
#include "../include/wavetable.hpp"
|
||||
|
||||
struct Oscillator{
|
||||
|
||||
double generate(){
|
||||
if (wavetable != nullptr){
|
||||
const std::size_t table_size = wavetable->wavetable.size();
|
||||
double position = (phase / (2.0 * std::numbers::pi)) * table_size;
|
||||
std::size_t index = static_cast<std::size_t>(position);
|
||||
std::size_t next_index = (index + 1) % table_size;
|
||||
double fraction = position - index;
|
||||
double y0 = wavetable->wavetable[index];
|
||||
double y1 = wavetable->wavetable[next_index];
|
||||
double value = y0 + (y1 - y0) * fraction;
|
||||
|
||||
|
||||
phase += (2 * std::numbers::pi * frequency)/(sample_rate);
|
||||
phase = std::fmod(phase, (2 * std::numbers::pi));
|
||||
return value;
|
||||
}
|
||||
else return 0.0;
|
||||
}
|
||||
|
||||
void set_wavetable(Wavetable* wavetable_){
|
||||
wavetable = wavetable_;
|
||||
}
|
||||
|
||||
void set_sample_rate(double sample_rate_){
|
||||
if (sample_rate_ > 0.0) sample_rate = sample_rate_;
|
||||
}
|
||||
|
||||
Wavetable* wavetable = nullptr;
|
||||
|
||||
double frequency = 440.0;
|
||||
double amplitude = 0.5;
|
||||
double phase = 0.0;
|
||||
|
||||
double sample_rate = 48000.0;
|
||||
};
|
||||
|
||||
struct UnisonOscillator{
|
||||
Oscillator osc1;
|
||||
Oscillator osc2;
|
||||
double pan = 0.0;
|
||||
double base_pan = 0.0;
|
||||
double base_detune = 0.0;
|
||||
double amplitude = 1.0;
|
||||
double detune = 0.0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef SPSC_QUEUE
|
||||
#define SPSC_QUEUE
|
||||
|
||||
#include <atomic>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
|
||||
#include "../include/command.hpp"
|
||||
|
||||
class SpscQueue{
|
||||
|
||||
public:
|
||||
SpscQueue(){
|
||||
head.store(0, std::memory_order_relaxed);
|
||||
tail.store(0, std::memory_order_relaxed);
|
||||
buffer.fill(Command{});
|
||||
}
|
||||
|
||||
bool push(const Command&);
|
||||
bool pop(Command&);
|
||||
|
||||
|
||||
private:
|
||||
std::array<Command, 64> buffer;
|
||||
std::atomic<std::size_t> head;
|
||||
std::atomic<std::size_t> tail;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef STEREOSAMPLE
|
||||
#define STEREOSAMPLE
|
||||
|
||||
struct StereoSample{
|
||||
double left;
|
||||
double right;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,127 @@
|
||||
#ifndef SYNTH
|
||||
#define SYNTH
|
||||
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
|
||||
#include "../include/spsc_queue.hpp"
|
||||
#include "../include/command.hpp"
|
||||
#include "../include/voice.hpp"
|
||||
#include "../include/stereo_sample.hpp"
|
||||
#include "../include/lfo.hpp"
|
||||
|
||||
class Synth {
|
||||
public:
|
||||
|
||||
Synth(){
|
||||
|
||||
sin_wavetable.generate_sin(32);
|
||||
saw_wavetable.generate_saw(32);
|
||||
square_wavetable.generate_square(28);
|
||||
triangle_wavetable.generate_triangle(40);
|
||||
|
||||
pitch_bend_range = 2.0;
|
||||
pitch_semitones = 0.0;
|
||||
pitch_multiplier = 1.0;
|
||||
vibrato_depth_ = 0.0;
|
||||
|
||||
mod_wheel = 0;
|
||||
stolen_voices = 0;
|
||||
sample_rate = 48000.0;
|
||||
|
||||
set_wavetable(&saw_wavetable);
|
||||
set_wavetable2(&square_wavetable);
|
||||
}
|
||||
|
||||
void prepare(double sample_rate_);
|
||||
void render_block(float* left, float* right, int num_samples);
|
||||
|
||||
StereoSample generate();
|
||||
|
||||
void generate_buffer(double* array, int size);
|
||||
void set_mod_wheel(uint8_t value);
|
||||
void set_unison_voices(uint8_t value);
|
||||
void set_unison_gain(double value);
|
||||
void set_stereo_spread(double value);
|
||||
void set_detune_amount(double value);
|
||||
void set_wavetable(Wavetable* wavetable);
|
||||
void set_wavetable2(Wavetable* wavetable);
|
||||
void set_wavetable_sin();
|
||||
void set_wavetable_saw();
|
||||
void set_wavetable_square();
|
||||
void set_wavetable_triangle();
|
||||
void set_filter_cutoff(double value);
|
||||
void set_filter_resonance(double value);
|
||||
void set_filter_off();
|
||||
void set_hpf();
|
||||
void set_bpf();
|
||||
void set_lpf();
|
||||
void pitch_bend(uint16_t value);
|
||||
|
||||
void set_attack(double value);
|
||||
void set_decay(double value);
|
||||
void set_sustain(double value);
|
||||
void set_release(double value);
|
||||
void set_lfo_frequency(double value);
|
||||
void set_pitch_bend_range(double value);
|
||||
|
||||
void apply_unison_voices(int value);
|
||||
void apply_unison_gain(double value);
|
||||
void apply_stereo_spread(double value);
|
||||
void apply_detune_amount(double value);
|
||||
void apply_waveform(int index);
|
||||
void apply_waveform2(int index);
|
||||
void apply_osc1_enabled(bool enabled);
|
||||
void apply_osc2_enabled(bool enabled);
|
||||
void apply_osc1_level(double value);
|
||||
void apply_osc2_level(double value);
|
||||
void apply_filter_type(int index);
|
||||
void apply_filter_cutoff(double value);
|
||||
void apply_filter_resonance(double value);
|
||||
void apply_mod_wheel(uint8_t value);
|
||||
void apply_pitch_bend(uint16_t value);
|
||||
|
||||
Wavetable* get_wavetable(int index);
|
||||
Wavetable* get_wavetable2(int index);
|
||||
|
||||
void note_on_direct(uint8_t note, uint8_t velocity);
|
||||
void note_off_direct(uint8_t note);
|
||||
void all_notes_off();
|
||||
|
||||
double pitch_to_semitones(uint16_t value);
|
||||
double semitones_to_multiplier(double semitones);
|
||||
double mod_wheel_to_depth(uint8_t value);
|
||||
|
||||
bool note_on(uint8_t note, uint8_t velocity);
|
||||
bool note_off(uint8_t note);
|
||||
|
||||
private:
|
||||
Voice* find_free_voice();
|
||||
Voice* find_quietest_voice();
|
||||
|
||||
SpscQueue midi_queue;
|
||||
SpscQueue gui_queue;
|
||||
|
||||
Lfo lfo;
|
||||
|
||||
Tables tables;
|
||||
|
||||
Wavetable sin_wavetable;
|
||||
Wavetable saw_wavetable;
|
||||
Wavetable square_wavetable;
|
||||
Wavetable triangle_wavetable;
|
||||
|
||||
double pitch_bend_range;
|
||||
double pitch_semitones;
|
||||
double pitch_multiplier;
|
||||
double vibrato_depth_;
|
||||
|
||||
uint8_t mod_wheel;
|
||||
uint64_t stolen_voices;
|
||||
double sample_rate;
|
||||
|
||||
std::array<Voice, 8> voices;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,111 @@
|
||||
#ifndef VOICE
|
||||
#define VOICE
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <numbers>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
|
||||
#include "../include/envelope.hpp"
|
||||
#include "../include/oscillator.hpp"
|
||||
#include "../include/stereo_sample.hpp"
|
||||
#include "../include/filter.hpp"
|
||||
|
||||
class Voice{
|
||||
public:
|
||||
Voice(){
|
||||
|
||||
oscillators[1].base_detune = -10.0;
|
||||
oscillators[1].base_pan = -1.0;
|
||||
|
||||
oscillators[2].base_detune = 10.0;
|
||||
oscillators[2].base_pan = 1.0;
|
||||
|
||||
oscillators[3].base_detune = -20.0;
|
||||
oscillators[3].base_pan = -0.6;
|
||||
|
||||
oscillators[4].base_detune = 20.0;
|
||||
oscillators[4].base_pan = 0.6;
|
||||
|
||||
oscillators[5].base_detune = -30.0;
|
||||
oscillators[5].base_pan = -0.3;
|
||||
|
||||
oscillators[6].base_detune = 30.0;
|
||||
oscillators[6].base_pan = 0.3;
|
||||
|
||||
note = 0;
|
||||
unison_voices = 7;
|
||||
|
||||
envelope_value = 0.0;
|
||||
base_frequency = 440.0;
|
||||
pitch_bend = 1.0;
|
||||
vibrato = 1.0;
|
||||
stereo_spread = 1.0;
|
||||
detune_amount = 1.0;
|
||||
unison_gain = 1.0;
|
||||
|
||||
active = false;
|
||||
}
|
||||
|
||||
void set_sample_rate(double sample_rate_);
|
||||
void set_attack(double value);
|
||||
void set_decay(double value);
|
||||
void set_sustain(double value);
|
||||
void set_release(double value);
|
||||
void note_on(uint8_t note, uint8_t velocity);
|
||||
void note_off();
|
||||
void set_pitch_bend(double multiplier);
|
||||
void set_vibrato(double multiplier);
|
||||
void set_unison_voices(uint8_t value);
|
||||
void set_stereo_spread(double value);
|
||||
void set_detune_amount(double value);
|
||||
void set_unison_gain(double value);
|
||||
void set_wavetable(Wavetable* Wavetable);
|
||||
void set_wavetable2(Wavetable* Wavetable);
|
||||
void set_osc1_enabled(bool enabled);
|
||||
void set_osc2_enabled(bool enabled);
|
||||
void set_osc1_level(double value);
|
||||
void set_osc2_level(double value);
|
||||
void set_filter_cutoff(double value);
|
||||
void set_filter_resonance(double value);
|
||||
void set_filter_off();
|
||||
void set_hpf();
|
||||
void set_bpf();
|
||||
void set_lpf();
|
||||
void update_frequency();
|
||||
|
||||
StereoSample generate();
|
||||
|
||||
double get_volume() const;
|
||||
|
||||
bool is_active() const;
|
||||
bool is_playing(uint8_t note_) const;
|
||||
bool is_released() const;
|
||||
|
||||
private:
|
||||
Envelope envelope;
|
||||
Filter filter_left;
|
||||
Filter filter_right;
|
||||
std::array<UnisonOscillator, 7> oscillators{};
|
||||
|
||||
uint8_t note;
|
||||
uint8_t unison_voices;
|
||||
|
||||
double envelope_value;
|
||||
double base_frequency;
|
||||
double pitch_bend;
|
||||
double vibrato;
|
||||
double stereo_spread;
|
||||
double detune_amount;
|
||||
double unison_gain;
|
||||
bool osc1_enabled = true;
|
||||
bool osc2_enabled = false;
|
||||
double osc1_level = 1.0;
|
||||
double osc2_level = 0.0;
|
||||
|
||||
bool active;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef WAVETABLE
|
||||
#define WAVETABLE
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <numbers>
|
||||
#include <cmath>
|
||||
|
||||
struct Wavetable{
|
||||
void generate_sin(uint16_t harmonics_);
|
||||
void generate_saw(uint16_t harmonics_);
|
||||
void generate_square(uint16_t harmonics_);
|
||||
void generate_triangle(uint16_t harmonics_);
|
||||
|
||||
std::array<double, 2048> wavetable{};
|
||||
uint16_t harmonics = 0;
|
||||
};
|
||||
|
||||
struct Tables{
|
||||
Wavetable* select_saw_tables(double frequency);
|
||||
void generate_saw_tables();
|
||||
|
||||
std::array<Wavetable, 6> tables;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user