Compare commits
18 Commits
feature/ad
...
feature/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
63a6479be5
|
|||
|
7ee245d4d1
|
|||
|
e9391c2e7b
|
|||
|
3af5c344f2
|
|||
|
d7dd36ccd1
|
|||
|
de37b814bf
|
|||
|
576cf8e3b5
|
|||
|
1cf3cccb69
|
|||
|
55242db407
|
|||
|
4057bf5d42
|
|||
|
42f9be5dba
|
|||
|
6dc3de1eda
|
|||
|
28ceaec23a
|
|||
|
307660d83a
|
|||
|
5b97136f44
|
|||
|
868a59da0e
|
|||
|
f9ad4d844b
|
|||
| c50a8335d7 |
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug",
|
||||
"program": "${workspaceFolder}/bin/main",
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
||||
18
.vscode/tasks.json
vendored
18
.vscode/tasks.json
vendored
@@ -3,24 +3,12 @@
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: clang сборка активного файла",
|
||||
"command": "/usr/bin/clang",
|
||||
"command": "sh",
|
||||
"args": [
|
||||
"-fcolor-diagnostics",
|
||||
"-fansi-escape-codes",
|
||||
"-g",
|
||||
"${file}",
|
||||
"${fileDirname}/utils.c",
|
||||
"${fileDirname}/ring_buffer.c",
|
||||
"${fileDirname}/oscillator.c",
|
||||
"${fileDirname}/parser.c",
|
||||
"${fileDirname}/export.c",
|
||||
"-lm",
|
||||
"-lraylib",
|
||||
"-o",
|
||||
"${fileDirname}/bin/${fileBasenameNoExtension}"
|
||||
"${workspaceFolder}/build.sh"
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
|
||||
2
build.sh
2
build.sh
@@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
CC="${CXX:-c++}"
|
||||
LL="-lm -lraylib"
|
||||
FLAGS="-Wall -std=c++17 -I./inc/"
|
||||
FLAGS="-Wall -std=c++17 -I./inc/ -g"
|
||||
$CC $FLAGS $(find ./src -type f -iregex ".*\.cpp") $LL -o ./bin/main
|
||||
|
||||
BIN
docs/ADSR States.png
Normal file
BIN
docs/ADSR States.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 276 KiB |
@@ -26,6 +26,7 @@ release_samples = int(release_time * sample_rate)
|
||||
|
||||
# Attack phase
|
||||
envelope[:attack_samples] = np.linspace(0, 1, num=attack_samples)
|
||||
# 1/n * count;
|
||||
|
||||
# Decay phase
|
||||
decay_slope = (1 - sustain_level) / decay_samples
|
||||
|
||||
BIN
docs/Attack Formula.png
Normal file
BIN
docs/Attack Formula.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
30
inc/ADSR.h
Normal file
30
inc/ADSR.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include "Effect.h"
|
||||
#include "Ramp.h"
|
||||
#include <cstddef>
|
||||
|
||||
class ADSR : public Effect {
|
||||
enum ADSRState { sOff, sAttack, sDecay, sSustain, sRelease };
|
||||
|
||||
private:
|
||||
float m_attack_time;
|
||||
float m_decay_time;
|
||||
float m_sustain_level;
|
||||
float m_release_time;
|
||||
ADSRState m_state;
|
||||
Ramp* m_ramp;
|
||||
|
||||
void process_sample(float* sample);
|
||||
bool is_attack_elapsed();
|
||||
bool is_decay_elapsed();
|
||||
bool is_release_elapsed();
|
||||
void recheck_state();
|
||||
|
||||
public:
|
||||
ADSR(/* args */);
|
||||
~ADSR();
|
||||
void Trigger() override;
|
||||
void Release() override;
|
||||
void Process(std::vector<float>& samples) override;
|
||||
void SetParameters(float attack, float decay, float sustain, float release);
|
||||
};
|
||||
16
inc/Adder.h
16
inc/Adder.h
@@ -6,23 +6,17 @@
|
||||
#include <vector>
|
||||
|
||||
struct Adder {
|
||||
static std::vector<float>&
|
||||
SumOscillators(const std::vector<Oscillator*>& oscillators,
|
||||
float duration) {
|
||||
size_t sample_count = (size_t)(duration * SAMPLE_RATE);
|
||||
|
||||
std::vector<float>* output = new std::vector<float>();
|
||||
output->reserve(sample_count);
|
||||
static void SumOscillators(const std::vector<Oscillator*>& oscillators,
|
||||
std::vector<float>& signal) {
|
||||
size_t sample_count = STREAM_BUFFER_SIZE;
|
||||
|
||||
for (size_t i = 0; i < sample_count; i++) {
|
||||
float sample = 0.0f;
|
||||
for (Oscillator* osc : oscillators) {
|
||||
sample += osc->GenerateSample(duration);
|
||||
sample += osc->Process();
|
||||
}
|
||||
|
||||
output->push_back(sample);
|
||||
signal[i] = sample;
|
||||
}
|
||||
|
||||
return (*output);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
#include "Note.h"
|
||||
#include "Renderer.h"
|
||||
#include "RingBuffer.h"
|
||||
#include "Synth.h"
|
||||
#include "SynthGuiState.h"
|
||||
#include "raylib.h"
|
||||
@@ -10,18 +9,15 @@ class Application {
|
||||
private:
|
||||
Synth m_synth;
|
||||
SynthGuiState m_synth_gui_state;
|
||||
RingBuffer<float>* m_ring_buffer;
|
||||
AudioStream m_synth_stream;
|
||||
int m_sound_played_count;
|
||||
float* m_temp_buffer;
|
||||
Note* m_current_note;
|
||||
Renderer m_renderer;
|
||||
std::size_t detect_note_pressed(Note* note);
|
||||
void init_synth();
|
||||
void init_audio();
|
||||
void update_on_note_input();
|
||||
void play_buffered_audio();
|
||||
void fill_audio_buffer();
|
||||
bool DetectNotePressed(Note* note);
|
||||
void InitSynth();
|
||||
void InitAudio();
|
||||
void UpdateOnNoteInput();
|
||||
void PlayBufferedAudio();
|
||||
|
||||
public:
|
||||
Application(/* args */);
|
||||
|
||||
14
inc/BandPassFilter.h
Normal file
14
inc/BandPassFilter.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "Filter.h"
|
||||
|
||||
class BandPassFilter : public Filter {
|
||||
private:
|
||||
void CalculateCoefficients() override;
|
||||
|
||||
public:
|
||||
BandPassFilter(Filter* filter);
|
||||
BandPassFilter(float freq, float res, float q);
|
||||
BandPassFilter(/* args */);
|
||||
~BandPassFilter();
|
||||
bool IsSameFilterType(FilterType type) override { return type == BandPass; };
|
||||
};
|
||||
12
inc/Effect.h
Normal file
12
inc/Effect.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
class Effect {
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
Effect(/* args */){};
|
||||
~Effect(){};
|
||||
virtual void Trigger(){};
|
||||
virtual void Release(){};
|
||||
virtual void Process(std::vector<float>& samples){};
|
||||
};
|
||||
35
inc/Filter.h
Normal file
35
inc/Filter.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
#include "Effect.h"
|
||||
|
||||
enum FilterType {
|
||||
LowPass,
|
||||
BandPass,
|
||||
HighPass
|
||||
};
|
||||
|
||||
class Filter : public Effect {
|
||||
protected:
|
||||
float m_freq; // cutoff frequency
|
||||
float m_q; // filter quantity (resonance)
|
||||
float m_order; // filter order (peakGain)
|
||||
/* todo: filter adsr */
|
||||
float m_norm, m_v, m_k;
|
||||
float m_a0, m_a1, m_a2, m_b1, m_b2;
|
||||
float m_z1, m_z2;
|
||||
|
||||
void CalculateNormals();
|
||||
virtual void CalculateCoefficients(){};
|
||||
|
||||
public:
|
||||
Filter(/* args */);
|
||||
virtual ~Filter();
|
||||
void Trigger() override;
|
||||
void Release() override;
|
||||
float Process(float in);
|
||||
void Process(std::vector<float>& samples) override;
|
||||
void SetParameters(float freq, float res, float q);
|
||||
float GetFreq() { return m_freq; }
|
||||
float GetRes() { return m_q; }
|
||||
float GetPeakGain() { return m_norm; }
|
||||
virtual bool IsSameFilterType(FilterType type){ return false; };
|
||||
};
|
||||
29
inc/FilterFactory.h
Normal file
29
inc/FilterFactory.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include "Filter.h"
|
||||
#include "LowPassFilter.h"
|
||||
#include "BandPassFilter.h"
|
||||
#include "HighPassFilter.h"
|
||||
|
||||
struct FilterFactory {
|
||||
static Filter* CreateFilter(Filter* old_filter, FilterType new_type) {
|
||||
Filter* new_filter;
|
||||
switch (new_type) {
|
||||
case LowPass:
|
||||
new_filter = new LowPassFilter(old_filter);
|
||||
break;
|
||||
case BandPass:
|
||||
new_filter = new BandPassFilter(old_filter);
|
||||
break;
|
||||
case HighPass:
|
||||
new_filter = new HighPassFilter(old_filter);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return new_filter;
|
||||
}
|
||||
|
||||
static Filter* GetDefaultFilter() {
|
||||
return new LowPassFilter();
|
||||
}
|
||||
};
|
||||
14
inc/HighPassFilter.h
Normal file
14
inc/HighPassFilter.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "Filter.h"
|
||||
|
||||
class HighPassFilter : public Filter {
|
||||
private:
|
||||
void CalculateCoefficients() override;
|
||||
|
||||
public:
|
||||
HighPassFilter();
|
||||
HighPassFilter(Filter* filter);
|
||||
HighPassFilter(float freq, float res, float q);
|
||||
~HighPassFilter();
|
||||
bool IsSameFilterType(FilterType type) override { return type == HighPass; };
|
||||
};
|
||||
@@ -7,8 +7,7 @@
|
||||
|
||||
class KeyBoard {
|
||||
private:
|
||||
/* data */
|
||||
static int get_semitone_shift_internal(const char* root_note,
|
||||
static int GetSemitoneShiftInternal(const char* root_note,
|
||||
char* target_note) {
|
||||
const char* pitch_classes[12] = {"C", "C#", "D", "D#", "E", "F",
|
||||
"F#", "G", "G#", "A", "A#", "B"};
|
||||
@@ -66,7 +65,7 @@ class KeyBoard {
|
||||
char* target_note_cstr = new char[target_note.length() + 1];
|
||||
strcpy(target_note_cstr, target_note.c_str());
|
||||
|
||||
int result = get_semitone_shift_internal("A4", target_note_cstr);
|
||||
int result = GetSemitoneShiftInternal("A4", target_note_cstr);
|
||||
|
||||
delete[] target_note_cstr;
|
||||
return result;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "cstdio"
|
||||
|
||||
#define write_log(format,args...) do { \
|
||||
printf(format, ## args); \
|
||||
} while(0)
|
||||
#define write_log(format, args...) \
|
||||
do { \
|
||||
printf(format, ##args); \
|
||||
} while (0)
|
||||
|
||||
15
inc/LowPassFilter.h
Normal file
15
inc/LowPassFilter.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "Filter.h"
|
||||
|
||||
class LowPassFilter : public Filter {
|
||||
protected:
|
||||
void CalculateCoefficients() override;
|
||||
|
||||
public:
|
||||
LowPassFilter();
|
||||
LowPassFilter(Filter* filter);
|
||||
LowPassFilter(float freq, float res, float q);
|
||||
~LowPassFilter();
|
||||
bool IsSameFilterType(FilterType type) override { return type == LowPass; };
|
||||
};
|
||||
@@ -31,5 +31,5 @@ class Oscillator {
|
||||
float GetFreq() { return m_freq; }
|
||||
void SetFreq(float freq);
|
||||
void Reset();
|
||||
float GenerateSample(float duration);
|
||||
float Process();
|
||||
};
|
||||
|
||||
16
inc/Ramp.h
Normal file
16
inc/Ramp.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
class Ramp {
|
||||
private:
|
||||
float m_level;
|
||||
float m_sample_rate;
|
||||
float m_increment;
|
||||
int m_counter;
|
||||
|
||||
public:
|
||||
Ramp(float starting_level, float sample_rate);
|
||||
~Ramp();
|
||||
void RampTo(float value, float time);
|
||||
float Process();
|
||||
bool IsCompleted();
|
||||
};
|
||||
@@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#include "ADSR.h"
|
||||
#include "Filter.h"
|
||||
#include "Synth.h"
|
||||
#include "SynthGuiState.h"
|
||||
#include "raylib.h"
|
||||
@@ -7,9 +9,7 @@
|
||||
class Renderer {
|
||||
private:
|
||||
void draw_main_panel(const Rectangle& panel_bounds);
|
||||
void draw_add_oscillator_button(Synth& synth, SynthGuiState& synth_gui,
|
||||
Rectangle panel_bounds);
|
||||
void draw_oscillators_panels(
|
||||
float draw_oscillators_panels(
|
||||
const std::vector<Oscillator*>& oscillators,
|
||||
const std::vector<OscillatorGuiState*>& gui_oscillators,
|
||||
const Rectangle& panel_bounds);
|
||||
@@ -18,6 +18,11 @@ class Renderer {
|
||||
const std::vector<OscillatorGuiState*>& guiOscillators);
|
||||
void draw_ui(Synth& synth, SynthGuiState& synth_gui);
|
||||
void draw_signal(Synth& synth, SynthGuiState& synth_gui);
|
||||
void draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr,
|
||||
const Rectangle& panel_bounds, float panel_y_offset);
|
||||
void draw_second_panel(Rectangle& bounds);
|
||||
float DrawFilterPanel(Synth& synth, FilterGuiState& gui_filter,
|
||||
const Rectangle& panel_bounds);
|
||||
|
||||
public:
|
||||
Renderer(/* args */);
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#define SAMPLE_RATE 48000.f
|
||||
#define SAMPLE_RATE 44100.f
|
||||
#define BPM 120.f
|
||||
#define BEAT_DURATION 60.f/BPM
|
||||
#define BEAT_DURATION 60.f / BPM
|
||||
#define PITCH_STANDARD 440.f
|
||||
#define VOLUME 0.5f
|
||||
#define ATTACK_MS 100.f
|
||||
#define STREAM_BUFFER_SIZE 4096
|
||||
#define STREAM_BUFFER_SIZE 1024
|
||||
#define FPS 60
|
||||
|
||||
#define SYNTH_PI 3.1415926535f
|
||||
#define SYNTH_VOLUME 0.5f
|
||||
|
||||
28
inc/Synth.h
28
inc/Synth.h
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "ADSR.h"
|
||||
#include "Filter.h"
|
||||
#include "Adder.h"
|
||||
#include "Effect.h"
|
||||
#include "Note.h"
|
||||
#include "Oscillator.h"
|
||||
#include "Settings.h"
|
||||
@@ -8,18 +11,29 @@
|
||||
|
||||
class Synth {
|
||||
private:
|
||||
bool is_note_triggered;
|
||||
std::vector<Oscillator*> m_oscillators;
|
||||
Adder m_adder;
|
||||
// OscillatorUI* ui_oscillators;
|
||||
// Note m_current_note;
|
||||
std::vector<Effect*> m_effects;
|
||||
std::vector<float> m_out_signal;
|
||||
std::vector<float>& get_note(int semitone, float beats);
|
||||
|
||||
Oscillator* m_lfo;
|
||||
void zero_signal();
|
||||
void get_note();
|
||||
void trigger_note_on_effects();
|
||||
void untrigger_note_on_effects();
|
||||
void apply_effects();
|
||||
void add_oscillator();
|
||||
void apply_filter_lfo();
|
||||
public:
|
||||
Synth(/* args */);
|
||||
~Synth();
|
||||
void ProduceNoteSound(Note input);
|
||||
void AddOscillator();
|
||||
void Trigger(Note input);
|
||||
void Process();
|
||||
void Release();
|
||||
void AddEffect(Effect* fx);
|
||||
const std::vector<float>& GetOutSignal() { return m_out_signal; }
|
||||
const std::vector<Oscillator*>& GetOscillators() { return m_oscillators; }
|
||||
const bool& GetIsNoteTriggered() { return is_note_triggered; }
|
||||
ADSR* GetADSR() { return (ADSR*)m_effects[0]; }
|
||||
Filter* GetFilter() { return (Filter*)m_effects[1]; }
|
||||
void SetFilter(FilterType type);
|
||||
};
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "OscillatorType.h"
|
||||
#include "raygui.h"
|
||||
#include <vector>
|
||||
#include "Filter.h"
|
||||
|
||||
struct OscillatorGuiState {
|
||||
float volume;
|
||||
@@ -11,6 +12,22 @@ struct OscillatorGuiState {
|
||||
Rectangle shape_dropdown_rect;
|
||||
};
|
||||
|
||||
struct ADSRGuiState {
|
||||
float attack;
|
||||
float decay;
|
||||
float sustain;
|
||||
float release;
|
||||
};
|
||||
|
||||
struct FilterGuiState {
|
||||
float freq;
|
||||
float res; //todo: res
|
||||
FilterType type;
|
||||
bool is_dropdown_open;
|
||||
};
|
||||
|
||||
struct SynthGuiState {
|
||||
std::vector<OscillatorGuiState*> oscillators;
|
||||
ADSRGuiState adsr;
|
||||
FilterGuiState filter;
|
||||
};
|
||||
94
src/ADSR.cpp
Normal file
94
src/ADSR.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "ADSR.h"
|
||||
#include "Logger.h"
|
||||
#include "Settings.h"
|
||||
|
||||
ADSR::ADSR(/* args */) {
|
||||
m_attack_time = 1.f;
|
||||
m_decay_time = 0.4f;
|
||||
m_sustain_level = 0.6f;
|
||||
m_release_time = 0.8f;
|
||||
m_ramp = new Ramp(0, SAMPLE_RATE);
|
||||
}
|
||||
|
||||
ADSR::~ADSR() { delete m_ramp; }
|
||||
|
||||
bool ADSR::is_attack_elapsed() {
|
||||
return m_state == sAttack && m_ramp->IsCompleted();
|
||||
}
|
||||
|
||||
bool ADSR::is_decay_elapsed() {
|
||||
return m_state == sDecay && m_ramp->IsCompleted();
|
||||
}
|
||||
|
||||
bool ADSR::is_release_elapsed() {
|
||||
return m_state == sRelease && m_ramp->IsCompleted();
|
||||
}
|
||||
|
||||
void ADSR::recheck_state() {
|
||||
switch (m_state) {
|
||||
case sAttack:
|
||||
if (is_attack_elapsed()) {
|
||||
m_state = sDecay;
|
||||
m_ramp->RampTo(m_sustain_level, m_decay_time);
|
||||
}
|
||||
break;
|
||||
case sDecay:
|
||||
if (is_decay_elapsed()) {
|
||||
m_state = sSustain;
|
||||
}
|
||||
break;
|
||||
case sRelease:
|
||||
if (is_release_elapsed()) {
|
||||
m_state = sOff;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ADSR::process_sample(float* sample) {
|
||||
if (m_state == sOff) {
|
||||
(*sample) = 0;
|
||||
} else if (m_state == sAttack) {
|
||||
(*sample) = (*sample) * m_ramp->Process();
|
||||
} else if (m_state == sDecay) {
|
||||
(*sample) = (*sample) * m_ramp->Process();
|
||||
} else if (m_state == sSustain) {
|
||||
(*sample) = (*sample) * m_sustain_level;
|
||||
} else if (m_state == sRelease) {
|
||||
(*sample) = (*sample) * m_ramp->Process();
|
||||
}
|
||||
}
|
||||
|
||||
void ADSR::Trigger() {
|
||||
write_log("Set ADSR\n");
|
||||
if (m_state == sOff) {
|
||||
m_state = sAttack;
|
||||
} else if (m_state == sRelease) {
|
||||
m_state = sAttack;
|
||||
};
|
||||
|
||||
m_ramp->RampTo(1, m_attack_time);
|
||||
}
|
||||
|
||||
void ADSR::Release() {
|
||||
write_log("Unset ADSR\n");
|
||||
m_state = sRelease;
|
||||
m_ramp->RampTo(0, m_release_time);
|
||||
}
|
||||
|
||||
void ADSR::Process(std::vector<float>& samples) {
|
||||
for (std::size_t i = 0; i < samples.size(); i++) {
|
||||
recheck_state();
|
||||
process_sample(&samples[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void ADSR::SetParameters(float attack, float decay, float sustain,
|
||||
float release) {
|
||||
m_attack_time = attack;
|
||||
m_decay_time = decay;
|
||||
m_sustain_level = sustain;
|
||||
m_release_time = release;
|
||||
}
|
||||
@@ -4,10 +4,8 @@
|
||||
#include <string>
|
||||
|
||||
Application::Application(/* args */) {
|
||||
m_ring_buffer = new RingBuffer<float>((std::size_t)STREAM_BUFFER_SIZE);
|
||||
m_temp_buffer = new float[STREAM_BUFFER_SIZE];
|
||||
init_synth();
|
||||
init_audio();
|
||||
InitSynth();
|
||||
InitAudio();
|
||||
}
|
||||
|
||||
Application::~Application() {
|
||||
@@ -15,15 +13,13 @@ Application::~Application() {
|
||||
UnloadAudioStream(m_synth_stream);
|
||||
CloseAudioDevice();
|
||||
CloseWindow();
|
||||
delete m_ring_buffer;
|
||||
delete[] m_temp_buffer;
|
||||
// todo: move to gui state class destructor (make it a class)
|
||||
for (int i = 0; i < m_synth_gui_state.oscillators.size(); i++) {
|
||||
delete m_synth_gui_state.oscillators[i];
|
||||
}
|
||||
}
|
||||
|
||||
void Application::init_audio() {
|
||||
void Application::InitAudio() {
|
||||
m_sound_played_count = 0;
|
||||
|
||||
InitAudioDevice();
|
||||
@@ -35,12 +31,11 @@ void Application::init_audio() {
|
||||
PlayAudioStream(m_synth_stream);
|
||||
}
|
||||
|
||||
void Application::init_synth() {
|
||||
// todo: move that variables to Synth declaration
|
||||
void Application::InitSynth() {
|
||||
std::string* nameString = new std::string(std::string(new char[3]));
|
||||
m_current_note = new Note{.length = 1, .name = (*nameString)};
|
||||
m_current_note->name.assign("G4");
|
||||
|
||||
// todo: move somewhere in initialization
|
||||
std::vector<Oscillator*> oscillators = m_synth.GetOscillators();
|
||||
m_synth_gui_state.oscillators.reserve(oscillators.size());
|
||||
for (size_t i = 0; i < oscillators.size(); i++) {
|
||||
@@ -55,103 +50,71 @@ void Application::init_synth() {
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t Application::detect_note_pressed(Note* note) {
|
||||
bool Application::DetectNotePressed(Note* note) {
|
||||
std::size_t is_pressed = 0;
|
||||
note->length = 8;
|
||||
if (IsKeyPressed(KEY_A)) {
|
||||
note->name.assign("A4");
|
||||
if (IsKeyDown(KEY_A)) {
|
||||
note->name.assign("A2");
|
||||
is_pressed = 1;
|
||||
}
|
||||
if (IsKeyPressed(KEY_B)) {
|
||||
note->name.assign("B4");
|
||||
if (IsKeyDown(KEY_B)) {
|
||||
note->name.assign("B2");
|
||||
is_pressed = 1;
|
||||
}
|
||||
if (IsKeyPressed(KEY_C)) {
|
||||
note->name.assign("C4");
|
||||
if (IsKeyDown(KEY_C)) {
|
||||
note->name.assign("C2");
|
||||
is_pressed = 1;
|
||||
}
|
||||
if (IsKeyPressed(KEY_D)) {
|
||||
note->name.assign("D4");
|
||||
if (IsKeyDown(KEY_D)) {
|
||||
note->name.assign("D2");
|
||||
is_pressed = 1;
|
||||
}
|
||||
if (IsKeyPressed(KEY_E)) {
|
||||
note->name.assign("E4");
|
||||
if (IsKeyDown(KEY_E)) {
|
||||
note->name.assign("E2");
|
||||
is_pressed = 1;
|
||||
}
|
||||
if (IsKeyPressed(KEY_F)) {
|
||||
note->name.assign("F4");
|
||||
if (IsKeyDown(KEY_F)) {
|
||||
note->name.assign("F2");
|
||||
is_pressed = 1;
|
||||
}
|
||||
if (IsKeyPressed(KEY_G)) {
|
||||
note->name.assign("G4");
|
||||
if (IsKeyDown(KEY_G)) {
|
||||
note->name.assign("G2");
|
||||
is_pressed = 1;
|
||||
}
|
||||
return is_pressed;
|
||||
return is_pressed == 1;
|
||||
}
|
||||
|
||||
// Update On Input
|
||||
void Application::update_on_note_input() {
|
||||
if (detect_note_pressed(m_current_note)) {
|
||||
m_synth.ProduceNoteSound((*m_current_note));
|
||||
m_sound_played_count = 0;
|
||||
bool is_note_up() {
|
||||
return IsKeyUp(KEY_A) || IsKeyUp(KEY_B) || IsKeyUp(KEY_C) ||
|
||||
IsKeyUp(KEY_D) || IsKeyUp(KEY_E) || IsKeyUp(KEY_F) || IsKeyUp(KEY_G);
|
||||
}
|
||||
|
||||
void Application::UpdateOnNoteInput() {
|
||||
if (DetectNotePressed(m_current_note)) {
|
||||
if (!m_synth.GetIsNoteTriggered()) {
|
||||
m_synth.Trigger((*m_current_note));
|
||||
}
|
||||
write_log("Note played: %s\n", m_current_note->name.c_str());
|
||||
} else if (is_note_up() && m_synth.GetIsNoteTriggered()) {
|
||||
m_synth.Release();
|
||||
}
|
||||
// will produce 0 signal if ADSR is in off state
|
||||
m_synth.Process();
|
||||
}
|
||||
|
||||
// Play ring-buffered audio
|
||||
void Application::play_buffered_audio() {
|
||||
if (IsAudioStreamProcessed(m_synth_stream) && !m_ring_buffer->IsEmpty()) {
|
||||
std::size_t size_to_read = m_ring_buffer->GetSize();
|
||||
|
||||
write_log("Samples to play:%zu \n", size_to_read);
|
||||
// todo: try to start reading directly from ring buffer, avoiding
|
||||
// temp_buffer
|
||||
m_ring_buffer->Read(m_temp_buffer, size_to_read);
|
||||
// can try the SetAudioStreamCallback
|
||||
UpdateAudioStream(m_synth_stream, m_temp_buffer, size_to_read);
|
||||
// can overwrite the ring buffer to avoid that
|
||||
if (m_synth.GetOutSignal().size() == m_sound_played_count) {
|
||||
m_ring_buffer->Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fill ring buffer from current sound
|
||||
void Application::fill_audio_buffer() {
|
||||
if (!m_ring_buffer->IsFull() &&
|
||||
m_synth.GetOutSignal().size() != m_sound_played_count) {
|
||||
write_log("[INFO] IsFull:%d Samples:%zu Played:%d\n",
|
||||
m_ring_buffer->IsFull(), m_synth.GetOutSignal().size(),
|
||||
m_sound_played_count);
|
||||
|
||||
// how many samples need write
|
||||
std::size_t size_to_fill = 0;
|
||||
|
||||
if ((m_synth.GetOutSignal().size() - m_sound_played_count) >
|
||||
m_ring_buffer->GetCapacity()) {
|
||||
size_to_fill = m_ring_buffer->GetCapacity();
|
||||
} else {
|
||||
size_to_fill = m_synth.GetOutSignal().size() - m_sound_played_count;
|
||||
}
|
||||
|
||||
write_log("[INFO] SizeToFill:%zu\n", size_to_fill);
|
||||
for (size_t i = 0; i < size_to_fill; i++) {
|
||||
m_temp_buffer[i] = m_synth.GetOutSignal()[i];
|
||||
}
|
||||
|
||||
m_ring_buffer->Write(m_temp_buffer, size_to_fill);
|
||||
m_sound_played_count += size_to_fill;
|
||||
void Application::PlayBufferedAudio() {
|
||||
if (IsAudioStreamProcessed(m_synth_stream)) {
|
||||
UpdateOnNoteInput();
|
||||
UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(),
|
||||
STREAM_BUFFER_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::Run() {
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
fill_audio_buffer();
|
||||
play_buffered_audio();
|
||||
update_on_note_input();
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
PlayBufferedAudio();
|
||||
m_renderer.Draw(m_synth, m_synth_gui_state);
|
||||
}
|
||||
}
|
||||
|
||||
23
src/BandPassFilter.cpp
Normal file
23
src/BandPassFilter.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "BandPassFilter.h"
|
||||
|
||||
BandPassFilter::BandPassFilter(/* args */) {}
|
||||
|
||||
BandPassFilter::BandPassFilter(Filter* filter) {
|
||||
m_freq = filter->GetFreq();
|
||||
m_q = filter->GetRes();
|
||||
m_order = filter->GetPeakGain();
|
||||
}
|
||||
|
||||
BandPassFilter::BandPassFilter(float freq, float res, float q) {}
|
||||
|
||||
BandPassFilter::~BandPassFilter() {}
|
||||
|
||||
void BandPassFilter::CalculateCoefficients() {
|
||||
CalculateNormals();
|
||||
m_norm = 1 / (1 + m_k / m_q + m_k * m_k);
|
||||
m_a0 = m_k / m_q * m_norm;
|
||||
m_a1 = 0;
|
||||
m_a2 = -m_a0;
|
||||
m_b1 = 2 * (m_k * m_k - 1) * m_norm;
|
||||
m_b2 = (1 - m_k / m_q + m_k * m_k) * m_norm;
|
||||
}
|
||||
36
src/Filter.cpp
Normal file
36
src/Filter.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "Filter.h"
|
||||
#include "Settings.h"
|
||||
|
||||
Filter::Filter(/* args */) {}
|
||||
|
||||
Filter::~Filter() {}
|
||||
|
||||
void Filter::CalculateNormals() {
|
||||
m_v = powf(10, fabs(m_order) / 20.0);
|
||||
m_k = tanf(M_PI * m_freq);
|
||||
}
|
||||
|
||||
void Filter::Trigger() {}
|
||||
|
||||
void Filter::Release() {}
|
||||
|
||||
float Filter::Process(float in) {
|
||||
// may move to a compile-time dictionary calculation, if needed
|
||||
CalculateCoefficients();
|
||||
float out = in * m_a0 + m_z1;
|
||||
m_z1 = in * m_a1 + m_z2 - m_b1 * out;
|
||||
m_z2 = in * m_a2 - m_b2 * out;
|
||||
return out;
|
||||
}
|
||||
|
||||
void Filter::Process(std::vector<float>& samples) {
|
||||
for (std::size_t i = 0; i < samples.size(); i++) {
|
||||
samples[i] = Process(samples[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Filter::SetParameters(float freq, float res, float q) {
|
||||
m_freq = freq / SAMPLE_RATE;
|
||||
m_q = res;
|
||||
m_order = q;
|
||||
}
|
||||
23
src/HighPassFilter.cpp
Normal file
23
src/HighPassFilter.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "HighPassFilter.h"
|
||||
|
||||
HighPassFilter::HighPassFilter(/* args */) {}
|
||||
|
||||
HighPassFilter::HighPassFilter(Filter* filter) {
|
||||
m_freq = filter->GetFreq();
|
||||
m_q = filter->GetRes();
|
||||
m_order = filter->GetPeakGain();
|
||||
}
|
||||
|
||||
HighPassFilter::HighPassFilter(float freq, float res, float q) {}
|
||||
|
||||
HighPassFilter::~HighPassFilter() {}
|
||||
|
||||
void HighPassFilter::CalculateCoefficients() {
|
||||
CalculateNormals();
|
||||
m_norm = 1 / (1 + m_k / m_q + m_k * m_k);
|
||||
m_a0 = 1 * m_norm;
|
||||
m_a1 = -2 * m_a0;
|
||||
m_a2 = m_a0;
|
||||
m_b1 = 2 * (m_k * m_k - 1) * m_norm;
|
||||
m_b2 = (1 - m_k / m_q + m_k * m_k) * m_norm;
|
||||
}
|
||||
33
src/LowPassFilter.cpp
Normal file
33
src/LowPassFilter.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "LowPassFilter.h"
|
||||
#include "Settings.h"
|
||||
|
||||
LowPassFilter::LowPassFilter() {
|
||||
// todo: defaults
|
||||
m_freq = 200.f / SAMPLE_RATE;
|
||||
m_q = 1.0f;//0.707f;
|
||||
m_order = 0;
|
||||
}
|
||||
|
||||
LowPassFilter::LowPassFilter(float freq, float res, float q) {
|
||||
m_freq = freq / SAMPLE_RATE;
|
||||
m_q = res;
|
||||
m_order = q;
|
||||
}
|
||||
|
||||
LowPassFilter::LowPassFilter(Filter* filter) {
|
||||
m_freq = filter->GetFreq();
|
||||
m_q = filter->GetRes();
|
||||
m_order = filter->GetPeakGain();
|
||||
}
|
||||
|
||||
LowPassFilter::~LowPassFilter() {}
|
||||
|
||||
void LowPassFilter::CalculateCoefficients() {
|
||||
CalculateNormals();
|
||||
m_norm = 1 / (1 + m_k / m_q + m_k * m_k);
|
||||
m_a0 = m_k * m_k * m_norm;
|
||||
m_a1 = 2 * m_a0;
|
||||
m_a2 = m_a0;
|
||||
m_b1 = 2 * (m_k * m_k - 1) * m_norm;
|
||||
m_b2 = (1 - m_k / m_q + m_k * m_k) * m_norm;
|
||||
}
|
||||
@@ -45,7 +45,7 @@ void Oscillator::SetFreq(float freq) {
|
||||
m_phase_dt = (this->*m_dt_function)(freq);
|
||||
}
|
||||
|
||||
float Oscillator::GenerateSample(float duration) {
|
||||
float Oscillator::Process() {
|
||||
return (this->*m_osc_function)() * m_volume;
|
||||
}
|
||||
|
||||
|
||||
27
src/Ramp.cpp
Normal file
27
src/Ramp.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "Ramp.h"
|
||||
#include "Logger.h"
|
||||
|
||||
Ramp::Ramp(float starting_level, float sample_rate) {
|
||||
m_level = starting_level;
|
||||
m_sample_rate = sample_rate;
|
||||
}
|
||||
|
||||
Ramp::~Ramp() {}
|
||||
|
||||
void Ramp::RampTo(float val, float time) {
|
||||
m_increment = (val - m_level) / (m_sample_rate * time);
|
||||
m_counter = (int)(m_sample_rate * time);
|
||||
write_log("Ramping from: %.1f to: %.1f by: %lf for: %d\n", m_level, val,
|
||||
m_increment, m_counter);
|
||||
}
|
||||
|
||||
float Ramp::Process() {
|
||||
if (m_counter > 0) {
|
||||
m_counter--;
|
||||
m_level += m_increment;
|
||||
}
|
||||
|
||||
return m_level;
|
||||
}
|
||||
|
||||
bool Ramp::IsCompleted() { return m_counter == 0; }
|
||||
186
src/Renderer.cpp
186
src/Renderer.cpp
@@ -2,24 +2,27 @@
|
||||
#define RAYGUI_IMPLEMENTATION
|
||||
#include "Logger.h"
|
||||
#include "Settings.h"
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
#include "raygui.h"
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
Renderer::Renderer(/* args */) {
|
||||
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
|
||||
SetTargetFPS(60);
|
||||
SetTargetFPS(FPS);
|
||||
}
|
||||
|
||||
Renderer::~Renderer() {}
|
||||
|
||||
void Renderer::Draw(Synth& synth, SynthGuiState& synth_gui) {
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
// todo: implement renderer
|
||||
|
||||
draw_ui(synth, synth_gui);
|
||||
draw_signal(synth, synth_gui);
|
||||
// DrawText("Congrats! You created your first window!", 190, 200, 20,
|
||||
// LIGHTGRAY); DrawFPS(0,0);
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
@@ -70,7 +73,7 @@ void Renderer::draw_oscillators_shape_inputs(
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::draw_oscillators_panels(
|
||||
float Renderer::draw_oscillators_panels(
|
||||
const std::vector<Oscillator*>& oscillators,
|
||||
const std::vector<OscillatorGuiState*>& gui_oscillators,
|
||||
const Rectangle& panel_bounds) {
|
||||
@@ -105,7 +108,7 @@ void Renderer::draw_oscillators_panels(
|
||||
// Volume slider
|
||||
float decibels = (20.f * log10f(osc->GetVolume()));
|
||||
char amp_slider_label[32];
|
||||
sprintf(amp_slider_label, "%.1f dB", decibels);
|
||||
snprintf(amp_slider_label, 7, "%.1f dB", decibels);
|
||||
decibels =
|
||||
GuiSlider(el_rect, amp_slider_label, "", decibels, -60.0f, 0.0f);
|
||||
ui_osc->volume = powf(10.f, decibels * (1.f / 20.f));
|
||||
@@ -116,51 +119,137 @@ void Renderer::draw_oscillators_panels(
|
||||
// Defer shape drop-down box.
|
||||
ui_osc->shape_dropdown_rect = el_rect;
|
||||
el_rect.y += el_rect.height + el_spacing;
|
||||
/*
|
||||
Rectangle delete_button_rect = el_rect;
|
||||
delete_button_rect.x = osc_panel_x + 5;
|
||||
delete_button_rect.y -= el_rect.height + el_spacing;
|
||||
delete_button_rect.width = 30;
|
||||
bool is_delete_button_pressed = GuiButton(delete_button_rect, "X");
|
||||
if (is_delete_button_pressed)
|
||||
{
|
||||
memmove(
|
||||
synth->ui_oscillator + ui_osc_i,
|
||||
synth->ui_oscillator + ui_osc_i + 1,
|
||||
(synth->ui_oscillator_count - ui_osc_i) *
|
||||
sizeof(UiOscillator)
|
||||
);
|
||||
synth->ui_oscillator_count -= 1;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
return panel_y_offset;
|
||||
}
|
||||
|
||||
void Renderer::draw_main_panel(const Rectangle& panel_bounds) {
|
||||
bool is_shape_dropdown_open = false;
|
||||
int shape_index = 0;
|
||||
GuiPanel(panel_bounds, "");
|
||||
}
|
||||
|
||||
void Renderer::draw_add_oscillator_button(Synth& synth, SynthGuiState& synth_gui,
|
||||
Rectangle panel_bounds) {
|
||||
//clang-format off
|
||||
bool click_add_oscillator =
|
||||
GuiButton((Rectangle){panel_bounds.x + 10, panel_bounds.y + 10,
|
||||
panel_bounds.width - 20, 25.f},
|
||||
"Add Oscillator");
|
||||
//clang-format on
|
||||
void Renderer::draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr,
|
||||
const Rectangle& panel_bounds,
|
||||
float panel_y_offset) {
|
||||
// Draw ADSR Panel
|
||||
const int osc_panel_width = panel_bounds.width - 20;
|
||||
const int osc_panel_height = 120;
|
||||
const int osc_panel_x = panel_bounds.x + 10;
|
||||
const int osc_panel_y = panel_bounds.y + 50 + panel_y_offset;
|
||||
panel_y_offset += osc_panel_height + 5;
|
||||
GuiPanel((Rectangle){(float)osc_panel_x, (float)osc_panel_y,
|
||||
(float)osc_panel_width, (float)osc_panel_height},
|
||||
"ADSR");
|
||||
|
||||
if (click_add_oscillator) {
|
||||
synth.AddOscillator();
|
||||
Oscillator* osc = synth.GetOscillators().back();
|
||||
const float slider_padding = 50.f;
|
||||
const float el_spacing = 5.f;
|
||||
Rectangle el_rect = {.x = (float)osc_panel_x + slider_padding + 30,
|
||||
.y = (float)osc_panel_y + 10,
|
||||
.width = (float)osc_panel_width - (slider_padding * 2),
|
||||
.height = 25.f};
|
||||
|
||||
OscillatorGuiState* ui =
|
||||
new OscillatorGuiState{.freq = osc->GetFreq(),
|
||||
.waveshape = osc->GetType(),
|
||||
.volume = osc->GetVolume()};
|
||||
synth_gui.oscillators.push_back(ui);
|
||||
// Attack slider
|
||||
float attack = gui_adsr.attack;
|
||||
char attack_slider_label[32];
|
||||
snprintf(attack_slider_label, 7, "%.1f s", attack);
|
||||
attack = GuiSlider(el_rect, attack_slider_label, "", attack, 0.0f, 2.0f);
|
||||
gui_adsr.attack = attack;
|
||||
el_rect.y += el_rect.height + el_spacing;
|
||||
|
||||
// Decay slider
|
||||
float decay = gui_adsr.decay;
|
||||
char decay_slider_label[32];
|
||||
snprintf(decay_slider_label, 7, "%.1f s", decay);
|
||||
decay = GuiSlider(el_rect, decay_slider_label, "", decay, 0.0f, 1.0f);
|
||||
gui_adsr.decay = decay;
|
||||
el_rect.y += el_rect.height + el_spacing;
|
||||
|
||||
// Sustain slider
|
||||
float sustain = gui_adsr.sustain;
|
||||
char sustain_slider_label[32];
|
||||
snprintf(sustain_slider_label, 7, "%.1f u", sustain);
|
||||
sustain = GuiSlider(el_rect, sustain_slider_label, "", sustain, 0.0f, 1.0f);
|
||||
gui_adsr.sustain = sustain;
|
||||
el_rect.y += el_rect.height + el_spacing;
|
||||
|
||||
// Release slider
|
||||
float release = gui_adsr.release;
|
||||
char release_slider_label[32];
|
||||
snprintf(release_slider_label, 7, "%.1f s", release);
|
||||
release = GuiSlider(el_rect, release_slider_label, "", release, 0.0f, 5.0f);
|
||||
gui_adsr.release = release;
|
||||
el_rect.y += el_rect.height + el_spacing;
|
||||
|
||||
// apply values to real one
|
||||
adsr->SetParameters(gui_adsr.attack, gui_adsr.decay, gui_adsr.sustain,
|
||||
gui_adsr.release);
|
||||
}
|
||||
|
||||
void Renderer::draw_second_panel(Rectangle& bounds) {
|
||||
bounds.x += bounds.width;
|
||||
GuiPanel(bounds, "");
|
||||
}
|
||||
|
||||
float Renderer::DrawFilterPanel(Synth& synth, FilterGuiState& gui_filter,
|
||||
const Rectangle& panel_bounds) {
|
||||
#define FILTER_TYPE_OPTIONS "LP;BP;HP"
|
||||
Filter* filter = synth.GetFilter();
|
||||
float panel_y_offset = 0;
|
||||
|
||||
// Draw Filter Panel
|
||||
const int osc_panel_width = panel_bounds.width - 20;
|
||||
const int osc_panel_height = 100;
|
||||
const int osc_panel_x = panel_bounds.x + 10;
|
||||
const int osc_panel_y = panel_bounds.y + 50;
|
||||
panel_y_offset += osc_panel_height + 5;
|
||||
GuiPanel((Rectangle){(float)osc_panel_x, (float)osc_panel_y,
|
||||
(float)osc_panel_width, (float)osc_panel_height},
|
||||
"Filter");
|
||||
|
||||
const float slider_padding = 50.f;
|
||||
const float el_spacing = 5.f;
|
||||
Rectangle el_rect = {.x = (float)osc_panel_x + slider_padding + 30,
|
||||
.y = (float)osc_panel_y + 10,
|
||||
.width = (float)osc_panel_width - (slider_padding * 2),
|
||||
.height = 25.f};
|
||||
|
||||
// Frequency slider
|
||||
float freq = log10f(gui_filter.freq);
|
||||
char freq_slider_label[32];
|
||||
snprintf(freq_slider_label, 10, "%.1f hz", powf(10.f, freq));
|
||||
freq = GuiSlider(el_rect, freq_slider_label, "", freq, 0.f, 4.f);
|
||||
gui_filter.freq = powf(10.f, freq);
|
||||
el_rect.y += el_rect.height + el_spacing;
|
||||
|
||||
//todo: implement that when Res will be fixed
|
||||
// Resonance slider
|
||||
// float res = gui_filter.res;
|
||||
// char res_slider_label[32];
|
||||
// snprintf(res_slider_label, 7, "%.1f u", res);
|
||||
// res = GuiSlider(el_rect, res_slider_label, "", res, 0.0f, 1.0f);
|
||||
// gui_filter.res = res;
|
||||
// el_rect.y += el_rect.height + el_spacing;
|
||||
|
||||
// Shape select
|
||||
int shape_index = (int)(gui_filter.type);
|
||||
bool is_dropdown_click =
|
||||
GuiDropdownBox(el_rect, FILTER_TYPE_OPTIONS,
|
||||
&shape_index, gui_filter.is_dropdown_open);
|
||||
|
||||
if (is_dropdown_click) {
|
||||
write_log("Dropdown clicked!\n");
|
||||
gui_filter.is_dropdown_open = !gui_filter.is_dropdown_open;
|
||||
gui_filter.type = (FilterType)(shape_index);
|
||||
// APPLY STATE TO REAL SYNTH
|
||||
synth.SetFilter(gui_filter.type);
|
||||
}
|
||||
|
||||
// apply values to real one
|
||||
// todo: thrid (order) parameter
|
||||
// todo: why resonance changing does not work?
|
||||
filter->SetParameters(gui_filter.freq, filter->GetRes(), filter->GetPeakGain());
|
||||
|
||||
return panel_y_offset;
|
||||
}
|
||||
|
||||
void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) {
|
||||
@@ -169,11 +258,16 @@ void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) {
|
||||
.width = OSCILLATOR_PANEL_WIDTH,
|
||||
.height = WINDOW_HEIGHT};
|
||||
draw_main_panel(panel_bounds);
|
||||
draw_add_oscillator_button(synth, synth_gui, panel_bounds);
|
||||
// Draw Oscillators
|
||||
|
||||
std::vector<Oscillator*> oscillators = synth.GetOscillators();
|
||||
std::vector<OscillatorGuiState*> gui_oscillators = synth_gui.oscillators;
|
||||
|
||||
draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds);
|
||||
float panel_y_offset =
|
||||
draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds);
|
||||
draw_adsr_panel(synth.GetADSR(), synth_gui.adsr, panel_bounds,
|
||||
panel_y_offset);
|
||||
draw_oscillators_shape_inputs(oscillators, gui_oscillators);
|
||||
}
|
||||
|
||||
draw_second_panel(panel_bounds);
|
||||
DrawFilterPanel(synth, synth_gui.filter, panel_bounds);
|
||||
}
|
||||
|
||||
111
src/Synth.cpp
111
src/Synth.cpp
@@ -1,32 +1,105 @@
|
||||
#include "Synth.h"
|
||||
#include "KeyBoard.h"
|
||||
#include "Logger.h"
|
||||
#include "OscillatorType.h"
|
||||
#include "Settings.h"
|
||||
#include "FilterFactory.h"
|
||||
|
||||
Synth::Synth(/* args */) { AddOscillator(); }
|
||||
|
||||
Synth::~Synth() {}
|
||||
|
||||
std::vector<float>& Synth::get_note(int semitone, float beats) {
|
||||
float hz = KeyBoard::GetHzBySemitone(semitone);
|
||||
float duration = beats * BEAT_DURATION;
|
||||
|
||||
// will change after oscillator starts to be more autonomous
|
||||
for (Oscillator* osc : m_oscillators) {
|
||||
osc->SetFreq(hz);
|
||||
Synth::Synth(/* args */) {
|
||||
m_lfo = new Oscillator(OscillatorType::Sine, 5.f, VOLUME);
|
||||
add_oscillator();
|
||||
add_oscillator();
|
||||
AddEffect(new ADSR());
|
||||
AddEffect(FilterFactory::GetDefaultFilter());
|
||||
for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) {
|
||||
float sample = 0.0f;
|
||||
m_out_signal.push_back(sample);
|
||||
}
|
||||
|
||||
// todo: add other pipeline steps (e.g ADSR, Filters, FX);
|
||||
return m_adder.SumOscillators(m_oscillators, duration);
|
||||
zero_signal();
|
||||
}
|
||||
|
||||
void Synth::ProduceNoteSound(Note input) {
|
||||
float length = 1.f / input.length;
|
||||
int semitone_shift = KeyBoard::GetSemitoneShift(input.name);
|
||||
m_out_signal = get_note(semitone_shift, length);
|
||||
Synth::~Synth() {
|
||||
m_oscillators.clear();
|
||||
m_effects.clear();
|
||||
m_out_signal.clear();
|
||||
}
|
||||
|
||||
void Synth::AddOscillator() {
|
||||
void Synth::zero_signal() {
|
||||
float sample = 0.0f;
|
||||
for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) {
|
||||
m_out_signal[i] = sample;
|
||||
}
|
||||
}
|
||||
|
||||
void Synth::get_note() {
|
||||
zero_signal();
|
||||
Adder::SumOscillators(m_oscillators, m_out_signal);
|
||||
}
|
||||
|
||||
void Synth::apply_effects() {
|
||||
for (Effect* effect : m_effects) {
|
||||
effect->Process(m_out_signal);
|
||||
}
|
||||
}
|
||||
|
||||
void Synth::trigger_note_on_effects() {
|
||||
for (Effect* effect : m_effects) {
|
||||
effect->Trigger();
|
||||
}
|
||||
}
|
||||
|
||||
void Synth::untrigger_note_on_effects() {
|
||||
for (Effect* effect : m_effects) {
|
||||
effect->Release();
|
||||
}
|
||||
}
|
||||
|
||||
void Synth::add_oscillator() {
|
||||
m_oscillators.push_back(
|
||||
new Oscillator(OscillatorType::Sine, 440.f, VOLUME));
|
||||
}
|
||||
|
||||
void Synth::Trigger(Note input) {
|
||||
int semitone_shift = KeyBoard::GetSemitoneShift(input.name);
|
||||
float hz = KeyBoard::GetHzBySemitone(semitone_shift);
|
||||
|
||||
for (Oscillator* osc : m_oscillators) {
|
||||
osc->SetFreq(hz);
|
||||
}
|
||||
is_note_triggered = true;
|
||||
trigger_note_on_effects();
|
||||
}
|
||||
|
||||
// todo: fix this
|
||||
void Synth::apply_filter_lfo() {
|
||||
float dt = m_lfo->Process();
|
||||
Filter* filter = (Filter*)m_effects[1];
|
||||
float freq = filter->GetFreq();
|
||||
//todo: check formula
|
||||
//filter->SetParameters(freq + dt * 0.2f, filter->GetRes(), filter->GetPeakGain());
|
||||
}
|
||||
|
||||
void Synth::Process() {
|
||||
//todo: on each sample.
|
||||
//in order to do that, we need to move to per-sample processing
|
||||
apply_filter_lfo();
|
||||
get_note();
|
||||
apply_effects();
|
||||
}
|
||||
|
||||
void Synth::Release() {
|
||||
zero_signal();
|
||||
is_note_triggered = false;
|
||||
untrigger_note_on_effects();
|
||||
}
|
||||
|
||||
void Synth::AddEffect(Effect* fx) { m_effects.push_back(fx); }
|
||||
|
||||
void Synth::SetFilter(FilterType type) {
|
||||
Filter* old_filter = this->GetFilter();
|
||||
if (!old_filter->IsSameFilterType(type)) {
|
||||
Filter* new_filter = FilterFactory::CreateFilter(old_filter, type);
|
||||
delete old_filter;
|
||||
m_effects[1] = new_filter;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user