4 Commits

Author SHA1 Message Date
66c839e2ae wip: synth api && adder 2023-08-07 02:28:06 +04:00
d20dbd920f wip: synth class 2023-08-07 00:34:14 +04:00
850dedb319 wip: oscillator class 2023-08-06 23:33:51 +04:00
bcb75a65f9 feat: phase-based oscillators (#12)
Co-authored-by: HiveBeats <e1lama@protonmail.com>
Reviewed-on: #12
2023-08-06 20:17:16 +03:00
15 changed files with 511 additions and 72 deletions

38
docs/Resources.md Normal file
View File

@@ -0,0 +1,38 @@
http://basicsynth.com/index.php?page=basic
Signal Generator
The most straightforward method of sound generation in software is to evaluate a periodic function for each sample time. A periodic function is any function that repeats at a constant interval, called the period. Consider the circle in the figure below. Starting at the 3:00 position and then sweeping around the circle counter-clockwise, we make a complete cycle in 2π radians and then the movement repeats. Thus the period is 2π radians. If we plot the points on the circumference over time we produce the waveform as shown below.
For audio signals, the period is the time it takes for the waveform to repeat and is thus the inverse of the frequency. In other words, a frequency of 100Hz repeats every 1/100 second. We need to generate an amplitude value for every sample time, thus the number of samples in one period is equal to the time of the period divided by the time of one sample. Since the time of one sample is the inverse of the sample rate, and the period is the inverse of the frequency, the number of samples is also the sample rate divided by the frequency: ((1/f) / (1/fs)) = (fs/f). Since our period is also equal to 2π radians, the phase increment for one sample time (φ) is 2π divided by the number of samples in one period:
where the frequency of the signal is f and the sample rate fs. The amplitude for any given sample is the y value of the phase at that point in time multiplied by the radius of the circle. In other words, the amplitude is the sine of the phase angle and we can also derive the phase increment from the sine function.
Signal Generation Equation
The value sn is the nth sample, An the peak amplitude (volume) at sample n, and θn the phase at sample n. To calculate θn for any sample n, we can multiply the phase increment for one sample time (φ) by the sample number. To calculate φ we need to determine the radians for one sample time at a given frequency. As there are 2π radians per cycle, we multiply the frequency by 2π to get the radians per second. The phase increment for one sample time is then the radians per second multiplied by the time for one sample. Substiting for θn in the original equation yields:
Signal Generation Equation
We can implement this as a program loop.
totalSamples = duration * sampleRate;
for (n = 0; n < totalSamples; n++)
sample[n] = sin((twoPI/sampleRate) * frequency * n);
Since /fs is constant through the loop, we can calculate it once. We can also replace the multiplication of the phase with a repeated addition.
phaseIncr = (twoPI/sampleRate) * frequency;
phase = 0;
totalSamples = duration * sampleRate;
for (n = 0; n < totalSamples; n++) {
sample[n] = sin(phase);
phase += phaseIncr;
if (phase >= twoPI)
phase -= twoPI;
}
We can replace the sin function with any periodic function that returns an amplitude for a given phase angle. Thus, this small piece of code can be used to produce a very wide range of sounds. Functionally, it is the software equivalent of an oscillator, the basic building block of almost all synthesizers.

21
inc/Adder.h Normal file
View 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()
{
}

80
inc/KeyBoard.h Normal file
View File

@@ -0,0 +1,80 @@
#pragma once
#include "Settings.h"
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
class KeyBoard
{
private:
/* data */
static int get_semitone_shift_internal(char* root_note, char* target_note) {
char* pitch_classes[12] =
{ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
// Extract the note number and pitch class for the root note
int root_note_num = (int)root_note[strlen(root_note) - 1] - '0';
char* root_pitch_class_str = (char*)malloc((strlen(root_note) - 1) * sizeof(char));
strncpy(root_pitch_class_str, root_note, strlen(root_note) - 1);
int root_pitch_class = -1;
for (int i = 0; i < 12; i++) {
if (strcmp(pitch_classes[i], root_pitch_class_str) == 0) {
root_pitch_class = i;
break;
}
}
free(root_pitch_class_str);
// Extract the note number and pitch class for the target note
int target_note_num = (int)target_note[strlen(target_note) - 1] - '0';
char* target_pitch_class_str =
(char*)malloc((strlen(target_note) - 1) * sizeof(char));
strncpy(target_pitch_class_str, target_note, strlen(target_note) - 1);
int target_pitch_class = -1;
for (int i = 0; i < 12; i++) {
if (strcmp(pitch_classes[i], target_pitch_class_str) == 0) {
target_pitch_class = i;
break;
}
}
free(target_pitch_class_str);
// Calculate the semitone shift using the formula
return (target_note_num - root_note_num) * 12 +
(target_pitch_class - root_pitch_class);
}
public:
KeyBoard(/* args */);
~KeyBoard();
static float GetHzBySemitone(int semitone) {
return PITCH_STANDARD * powf(powf(2.f, (1.f / 12.f)), semitone);
}
static int GetSemitoneShift(const std::string& target_note) {
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);
delete target_note_cstr;
return result;
}
};
KeyBoard::KeyBoard(/* args */)
{
}
KeyBoard::~KeyBoard()
{
}

8
inc/Note.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
#include <string>
struct Note {
std::string& name;
int length;
};

39
inc/Oscillator.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#include<vector>
#include "OscillatorType.h"
typedef float (Oscillator::*OscFunction)(void);
typedef float (Oscillator::*DtFunction)(float);
class Oscillator
{
private:
OscillatorType m_osc;
float m_freq;
float m_volume;
float m_phase;
float m_phase_dt;
OscFunction m_osc_function;
DtFunction m_dt_function;
void sine_osc_phase_incr();
void saw_osc_phase_incr();
float calc_saw_phase_delta(float freq);
float calc_sine_phase_delta(float freq);
float sawosc();
float triangleosc();
float squareosc();
float sign(float v);
float sineosc();
public:
Oscillator(OscillatorType osc, float freq, float volume);
~Oscillator();
OscillatorType GetType() { return m_osc; }
void SetType(OscillatorType osc);
float GetVolume() { return m_volume; }
void SetVolume(float volume) { m_volume = volume; }
float GetFreq() { return m_freq; }
void SetFreq(float freq);
void Reset();
float GenerateSample(float duration);
};

7
inc/OscillatorType.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
typedef enum {
Sine,
Triangle,
Saw,
Square
} OscillatorType;

16
inc/Settings.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#define SAMPLE_RATE 48000.f
#define BPM 120.f
#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 SYNTH_PI 3.1415926535f
#define SYNTH_VOLUME 0.5f
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define OSCILLATOR_PANEL_WIDTH 200

33
inc/Synth.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <vector>
#include "Oscillator.h"
#include "Note.h"
#include "Adder.h"
#include "Settings.h"
class Synth
{
private:
std::vector<Oscillator*> m_oscillators;
Adder m_adder;
//OscillatorUI* ui_oscillators;
Note m_current_note;
std::vector<float> m_out_signal;
std::vector<float> & get_note(int semitone, float beats);
public:
Synth(/* args */);
~Synth();
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));
}
Synth::~Synth()
{
}

34
main.c
View File

@@ -93,6 +93,8 @@ static OscillatorArray init_osc_array() {
};
Oscillator* oscArray = malloc(sizeof(Oscillator*) * 1);
assert(oscArray);
oscArray[0] = first;
OscillatorArray oscillators = {
@@ -109,7 +111,7 @@ SynthSound note(Synth* synth, int semitone, float beats) {
// will change after oscillator starts to be more autonomous
for (size_t i = 0; i < synth->oscillators.count; i++) {
synth->oscillators.array[i].freq = hz;
osc_set_freq(&synth->oscillators.array[i], hz);
}
return freq(duration, synth->oscillators);
@@ -160,6 +162,10 @@ size_t detect_note_pressed(Note* note) {
// GUI
//------------------------------------------------------------------------------------
void note_on(Synth *synth, Note *note) {
}
void DrawUi(Synth *synth) {
const int panel_x_start = 0;
const int panel_y_start = 0;
@@ -201,7 +207,11 @@ void DrawUi(Synth *synth) {
for (int ui_osc_i = 0; ui_osc_i < synth->oscillators.count; ui_osc_i++)
{
OscillatorUI* ui_osc = &synth->ui_oscillators[ui_osc_i];
assert(ui_osc);
Oscillator* osc = &synth->oscillators.array[ui_osc_i];
assert(osc);
const bool has_shape_param = (ui_osc->waveshape == Square);
// Draw Oscillator Panel
@@ -268,7 +278,11 @@ void DrawUi(Synth *synth) {
for (int ui_osc_i = 0; ui_osc_i < synth->oscillators.count; ui_osc_i += 1)
{
OscillatorUI* ui_osc = &synth->ui_oscillators[ui_osc_i];
assert(ui_osc);
Oscillator* osc = &synth->oscillators.array[ui_osc_i];
assert(osc);
// Shape select
int shape_index = (int)(ui_osc->waveshape);
bool is_dropdown_click = GuiDropdownBox(ui_osc->shape_dropdown_rect,
@@ -329,6 +343,8 @@ int main(int argc, char **argv) {
for (size_t i = 0; i < synth.oscillators.count; i++)
{
OscillatorUI* ui = &synth.ui_oscillators[i];
assert(ui);
ui->freq = synth.oscillators.array[i].freq;
ui->waveshape = synth.oscillators.array[i].osc;
ui->volume = synth.oscillators.array[i].volume;
@@ -353,9 +369,11 @@ int main(int argc, char **argv) {
//----------------------------------------------------------------------------------
// Fill ring buffer from current sound
SynthSound* sound = synth.out_signal;
assert(sound);
size_t size_for_buffer = 0;
if (!ring_buffer.is_full && sound->sample_count != sound_played_count) {
write_log("[INFO] IsFull:%d Samples:%zu Played:%zu\n",
write_log("[INFO] IsFull:%d Samples:%zu Played:%d\n",
ring_buffer.is_full,
sound->sample_count,
sound_played_count);
@@ -396,11 +414,11 @@ int main(int argc, char **argv) {
// Update On Input
//----------------------------------------------------------------------------------
Note current_note = synth.current_note;
if (detect_note_pressed(&current_note)) {
*sound = get_note_sound(&synth, current_note);
Note* current_note = &synth.current_note;
if (detect_note_pressed(current_note)) {
*sound = get_note_sound(&synth, *current_note);
sound_played_count = 0;
write_log("Note played: %s\n", current_note.name);
write_log("Note played: %s\n", current_note->name);
}
//----------------------------------------------------------------------------------
@@ -424,6 +442,8 @@ int main(int argc, char **argv) {
NoteArray note_array = parse_notes(buf, strlen(buf));
SynthSound* sounds = malloc(sizeof(SynthSound) * note_array.count);
assert(sounds);
for (size_t i = 0; i < note_array.count; i++) {
Note note = note_array.notes[i];
sounds[i] = get_note_sound(&synth, note);
@@ -431,6 +451,8 @@ int main(int argc, char **argv) {
SynthSound song = concat_sounds(sounds, note_array.count);
uint16_t* song_pcm = malloc(sizeof(uint16_t) * song.sample_count);
assert(song_pcm);
for (size_t i = 0; i < song.sample_count; i++) {
song_pcm[i] = toInt16Sample(song.samples[i]);
}

View File

@@ -3,6 +3,8 @@
#include "math.h"
#include "stdlib.h"
#define TWO_PI 2*SYNTH_PI
static SynthSound get_init_samples(float duration) {
size_t sample_count = (size_t)(duration * SAMPLE_RATE);
float* samples = malloc(sizeof(float) * sample_count);
@@ -23,42 +25,98 @@ static float pos(float hz, float x) {
return fmodf(hz * x / SAMPLE_RATE, 1);
}
static float sineosc(float hz, float x) {
return sinf(x * (2.f * SYNTH_PI * hz / SAMPLE_RATE));
static void sine_osc_phase_incr(Oscillator* osc) {
osc->phase += osc->phase_dt;
if (osc->phase >= TWO_PI)
osc->phase -= TWO_PI;
}
static void saw_osc_phase_incr(Oscillator* osc) {
osc->phase += osc->phase_dt;
if (osc->phase >= 1.0f)
osc->phase -= 1.0f;
}
static float calc_saw_phase_delta(float freq) {
return freq / SAMPLE_RATE;
}
static float calc_sine_phase_delta(float freq) {
return (TWO_PI * freq) / SAMPLE_RATE;
}
static float sineosc(Oscillator* osc) {
float result = sinf(osc->phase);
sine_osc_phase_incr(osc);
return result;
}
static float sign(float v) {
return (v > 0.0) ? 1.f : -1.f;
}
static float squareosc(float hz, float x) {
return sign(sineosc(hz, x));
static float squareosc(Oscillator* osc) {
return sign(sineosc(osc));
}
static float triangleosc(float hz, float x) {
return 1.f - fabsf(pos(hz, x) - 0.5f) * 4.f;
static float triangleosc(Oscillator* osc) {
float result = 1.f - fabsf(osc->phase - 0.5f) * 4.f;
saw_osc_phase_incr(osc);
return result;
}
static float sawosc(float hz, float x) {
return pos(hz, x) * 2.f - 1.f;
static float sawosc(Oscillator* osc) {
float result = osc->phase * 2.f - 1.f;
saw_osc_phase_incr(osc);
return result;
}
void osc_set_freq(Oscillator* osc, float freq) {
osc->freq = freq;
osc->phase = 0;
switch (osc->osc)
{
case Sine:
osc->phase_dt = calc_sine_phase_delta(freq);
break;
case Square:
osc->phase_dt = calc_sine_phase_delta(freq);
break;
case Triangle:
osc->phase_dt = calc_saw_phase_delta(freq);
break;
case Saw:
osc->phase_dt = calc_saw_phase_delta(freq);
break;
default:
break;
}
}
void osc_reset(Oscillator* osc) {
osc->volume = 0;
osc->phase = 0;
osc->phase_dt = 0;
}
float multiosc(OscillatorGenerationParameter param) {
float osc_sample = 0.f;
for (size_t i = 0; i < param.oscillators.count; i++) {
Oscillator osc = param.oscillators.array[i];
switch (osc.osc) {
Oscillator* osc = &param.oscillators.array[i];
assert(osc);
switch (osc->osc) {
case Sine:
osc_sample += sineosc(osc.freq, param.sample) * osc.volume;
osc_sample += sineosc(osc) * osc->volume;
break;
case Triangle:
osc_sample += triangleosc(osc.freq, param.sample) * osc.volume;
osc_sample += triangleosc(osc) * osc->volume;
break;
case Square:
osc_sample += squareosc(osc.freq, param.sample) * osc.volume;
osc_sample += squareosc(osc) * osc->volume;
break;
case Saw:
osc_sample += sawosc(osc.freq, param.sample) * osc.volume;
osc_sample += sawosc(osc) * osc->volume;
break;
}
}
@@ -67,63 +125,19 @@ float multiosc(OscillatorGenerationParameter param) {
}
SynthSound freq(float duration, OscillatorArray osc) {
SynthSound samples = get_init_samples(duration);
// SynthSound attack = get_attack_samples();
float* output = malloc(sizeof(float) * samples.sample_count);
for (int i = 0; i < samples.sample_count; i++) {
float sample = samples.samples[i];
size_t sample_count = (size_t)(duration * SAMPLE_RATE);
float* output = malloc(sizeof(float) * sample_count);
for (size_t i = 0; i < sample_count; i++) {
OscillatorGenerationParameter param = {
.oscillators = osc,
.sample = sample
.oscillators = osc
};
output[i] = multiosc(param);
}
// create attack and release
/*
let adsrLength = Seq.length output
let attackArray = attack |> Seq.take adsrLength
let release = Seq.rev attackArray
*/
/*
todo: I will change the ADSR approach to an explicit ADSR module(with it's own state)
size_t adsr_length = samples.sample_count;
float *attackArray = NULL, *releaseArray = NULL;
if (adsr_length > 0) {
//todo: calloc
attackArray = malloc(sizeof(float) * adsr_length);
size_t attack_length = attack.sample_count < adsr_length
? attack.sample_count
: adsr_length;
memcpy(attackArray, attack.samples, attack_length);
//todo: calloc
releaseArray = malloc(sizeof(float) * adsr_length);
memcpy(releaseArray, attackArray, attack_length);
reverse_array(releaseArray, 0, adsr_length);
}
*/
// if (samples.sample_count > 1024) {
// samples.sample_count = 1024;
// }
// //todo: move to somewhere
// for (size_t i = 0; i < 1024; i++) {
// synth_sound.samples[i] = 0.0f;
// }
// for (size_t i = 0; i < samples.sample_count; i++) {
// synth_sound.samples[i] = output[i];
// }
// synth_sound.sample_count = samples.sample_count;
// return zipped array
SynthSound res = {
.samples = output,
.sample_count = samples.sample_count
.sample_count = sample_count
};
return res;

View File

@@ -14,6 +14,8 @@ typedef struct Oscillator {
OscillatorType osc;
float freq;
float volume;
float phase;
float phase_dt;
} Oscillator;
typedef struct OscillatorArray {
@@ -23,9 +25,10 @@ typedef struct OscillatorArray {
typedef struct OscillatorGenerationParameter {
OscillatorArray oscillators;
float sample;
} OscillatorGenerationParameter;
void osc_set_freq(Oscillator* osc, float freq);
void osc_reset(Oscillator* osc);
float multiosc(OscillatorGenerationParameter param);
SynthSound freq(float duration, OscillatorArray osc);

22
src/Adder.cpp Normal file
View 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;
}

112
src/Oscillator.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include "Oscillator.h"
#include "Settings.h"
#define TWO_PI 2*SYNTH_PI
Oscillator::Oscillator(OscillatorType osc, float freq, float volume)
{
SetType(osc);
m_freq = freq;
m_volume = volume;
}
Oscillator::~Oscillator()
{
}
void Oscillator::Reset()
{
m_volume = 0;
m_phase = 0;
m_phase_dt = 0;
}
void Oscillator::SetType(OscillatorType osc)
{
m_osc = osc;
switch (m_osc) {
case Sine:
m_osc_function = &sineosc;
m_dt_function = &calc_sine_phase_delta;
break;
case Triangle:
m_osc_function = &triangleosc;
m_dt_function = &calc_saw_phase_delta;
break;
case Square:
m_osc_function = &squareosc;
m_dt_function = &calc_sine_phase_delta;
break;
case Saw:
m_osc_function = &sawosc;
m_dt_function = &calc_saw_phase_delta;
break;
}
}
void Oscillator::SetFreq(float freq)
{
m_freq = freq;
m_phase = 0;
m_phase_dt = m_dt_function(freq);
}
float Oscillator::GenerateSample(float duration)
{
return m_osc_function() * m_volume;
}
void Oscillator::sine_osc_phase_incr()
{
m_phase += m_phase_dt;
if (m_phase >= TWO_PI)
m_phase -= TWO_PI;
}
void Oscillator::saw_osc_phase_incr()
{
m_phase += m_phase_dt;
if (m_phase >= 1.0f)
m_phase -= 1.0f;
}
float Oscillator::calc_saw_phase_delta(float freq)
{
return freq / SAMPLE_RATE;
}
float Oscillator::calc_sine_phase_delta(float freq)
{
return (TWO_PI * freq) / SAMPLE_RATE;
}
float Oscillator::sineosc()
{
float result = sinf(m_phase);
sine_osc_phase_incr();
return result;
}
float Oscillator::sign(float v)
{
return (v > 0.0) ? 1.f : -1.f;
}
float Oscillator::squareosc()
{
return sign(sineosc());
}
float Oscillator::triangleosc()
{
float result = 1.f - fabsf(m_phase - 0.5f) * 4.f;
saw_osc_phase_incr();
return result;
}
float Oscillator::sawosc()
{
float result = m_phase * 2.f - 1.f;
saw_osc_phase_incr();
return result;
}

23
src/Synth.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "Synth.h"
#include "Settings.h"
#include "KeyBoard.h"
#include "OscillatorType.h"
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 (auto osc : m_oscillators) {
osc->SetFreq(hz);
}
return m_adder.SumOscillators(m_oscillators, duration); //todo: add other pipeline steps (e.g ADSR, Filters, FX);
}
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);
}

View File

@@ -2,6 +2,7 @@
#define UTILS_H
#include "stdio.h"
#include "assert.h"
#define write_log(format,args...) do { \
printf(format, ## args); \