wip: synth api && adder
This commit is contained in:
21
inc/Adder.h
Normal file
21
inc/Adder.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "Oscillator.h"
|
||||
|
||||
class Adder
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
Adder(/* args */);
|
||||
~Adder();
|
||||
std::vector<float> & SumOscillators(const std::vector<Oscillator*> & oscillators, float duration);
|
||||
};
|
||||
|
||||
Adder::Adder(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
Adder::~Adder()
|
||||
{
|
||||
}
|
||||
@@ -5,4 +5,4 @@
|
||||
struct Note {
|
||||
std::string& name;
|
||||
int length;
|
||||
}
|
||||
};
|
||||
15
inc/Synth.h
15
inc/Synth.h
@@ -3,26 +3,29 @@
|
||||
#include <vector>
|
||||
#include "Oscillator.h"
|
||||
#include "Note.h"
|
||||
|
||||
#include "Adder.h"
|
||||
#include "Settings.h"
|
||||
|
||||
class Synth
|
||||
{
|
||||
private:
|
||||
std::vector<Oscillator&> m_oscillators;
|
||||
std::vector<Oscillator*> m_oscillators;
|
||||
Adder m_adder;
|
||||
//OscillatorUI* ui_oscillators;
|
||||
Note m_current_note;
|
||||
std::vector<float> m_out_signal;
|
||||
std::vector<float> & sum_oscillators(float duration);
|
||||
std::vector<float> & note(int semitone, float beats);
|
||||
std::vector<float> & get_note(int semitone, float beats);
|
||||
|
||||
public:
|
||||
Synth(/* args */);
|
||||
~Synth();
|
||||
std::vector<float> & GetNoteSound(Note input)
|
||||
void ProduceNoteSound(Note input);
|
||||
const std::vector<float> & GetOutSignal() { return m_out_signal; }
|
||||
};
|
||||
|
||||
Synth::Synth(/* args */)
|
||||
{
|
||||
m_oscillators.push_back(new Oscillator(OscillatorType.Sine, 440.f, VOLUME));
|
||||
m_oscillators.push_back(new Oscillator(OscillatorType::Sine, 440.f, VOLUME));
|
||||
}
|
||||
|
||||
Synth::~Synth()
|
||||
|
||||
22
src/Adder.cpp
Normal file
22
src/Adder.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "Adder.h"
|
||||
|
||||
std::vector<float> & Adder::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);
|
||||
|
||||
for (size_t i = 0; i < sample_count; i++)
|
||||
{
|
||||
float sample = 0.0f;
|
||||
for (Oscillator* osc : oscillators)
|
||||
{
|
||||
sample += osc->GenerateSample(duration);
|
||||
}
|
||||
|
||||
output.push_back(sample);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
@@ -2,44 +2,22 @@
|
||||
#include "Settings.h"
|
||||
#include "KeyBoard.h"
|
||||
#include "OscillatorType.h"
|
||||
#include "Note.h"
|
||||
|
||||
std::vector<float> & Synth::sum_oscillators(float duration)
|
||||
{
|
||||
size_t sample_count = (size_t)(duration * SAMPLE_RATE);
|
||||
|
||||
std::vector<float> output;// = new std::vector<float>();
|
||||
output.reserve(sample_count);
|
||||
|
||||
for (size_t i = 0; i < sample_count; i++)
|
||||
{
|
||||
float sample = 0.0f;
|
||||
for (Oscillator& osc : m_oscillators)
|
||||
{
|
||||
sample += osc.GenerateSample(duration);
|
||||
}
|
||||
|
||||
output.push_back(sample);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<float> & Synth::note(int semitone, float beats)
|
||||
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 (size_t i = 0; i < m_oscillators.size(); i++) {
|
||||
m_oscillators[i].SetFreq(hz);
|
||||
for (auto osc : m_oscillators) {
|
||||
osc->SetFreq(hz);
|
||||
}
|
||||
|
||||
return sum_oscillators(duration);
|
||||
return m_adder.SumOscillators(m_oscillators, duration); //todo: add other pipeline steps (e.g ADSR, Filters, FX);
|
||||
}
|
||||
|
||||
std::vector<float> & Synth::GetNoteSound(Note input) {
|
||||
void Synth::ProduceNoteSound(Note input) {
|
||||
float length = 1.f / input.length;
|
||||
int semitone_shift = KeyBoard::GetSemitoneShift(input.name);
|
||||
return note(semitone_shift, length);
|
||||
m_out_signal = get_note(semitone_shift, length);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user