feat: ui for oscillator
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@
|
|||||||
/Debug/
|
/Debug/
|
||||||
*.wav
|
*.wav
|
||||||
*.dSYM
|
*.dSYM
|
||||||
|
/lib
|
||||||
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@@ -3,7 +3,11 @@
|
|||||||
"readability/casting"
|
"readability/casting"
|
||||||
],
|
],
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"algorithm": "c"
|
"algorithm": "c",
|
||||||
|
"__bit_reference": "c",
|
||||||
|
"bitset": "c",
|
||||||
|
"chrono": "c",
|
||||||
|
"unordered_map": "c"
|
||||||
},
|
},
|
||||||
"FSharp.suggestGitignore": false,
|
"FSharp.suggestGitignore": false,
|
||||||
}
|
}
|
||||||
175
main.c
175
main.c
@@ -11,13 +11,24 @@
|
|||||||
#include "export.h"
|
#include "export.h"
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
#define RAYGUI_IMPLEMENTATION
|
||||||
|
#include "raygui.h"
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Synth
|
// Synth
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef struct OscillatorUI {
|
||||||
|
float volume;
|
||||||
|
float freq;//todo: remove or change to pitch shift
|
||||||
|
OscillatorType waveshape;
|
||||||
|
bool is_dropdown_open;
|
||||||
|
Rectangle shape_dropdown_rect;
|
||||||
|
} OscillatorUI;
|
||||||
|
|
||||||
typedef struct Synth {
|
typedef struct Synth {
|
||||||
OscillatorArray oscillators;
|
OscillatorArray oscillators;
|
||||||
|
OscillatorUI* ui_oscillators;
|
||||||
Note current_note;
|
Note current_note;
|
||||||
SynthSound* out_signal;
|
SynthSound* out_signal;
|
||||||
} Synth;
|
} Synth;
|
||||||
@@ -77,7 +88,8 @@ int get_semitone_shift(char* target_note) {
|
|||||||
static OscillatorArray init_osc_array() {
|
static OscillatorArray init_osc_array() {
|
||||||
Oscillator first = {
|
Oscillator first = {
|
||||||
.osc = Square,
|
.osc = Square,
|
||||||
.freq = 440.f
|
.freq = 440.f,
|
||||||
|
.volume = VOLUME
|
||||||
};
|
};
|
||||||
|
|
||||||
Oscillator* oscArray = malloc(sizeof(Oscillator*) * 1);
|
Oscillator* oscArray = malloc(sizeof(Oscillator*) * 1);
|
||||||
@@ -144,6 +156,151 @@ size_t detect_note_pressed(Note* note) {
|
|||||||
return is_pressed;
|
return is_pressed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// GUI
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void DrawUi(Synth *synth) {
|
||||||
|
const int panel_x_start = 0;
|
||||||
|
const int panel_y_start = 0;
|
||||||
|
const int panel_width = OSCILLATOR_PANEL_WIDTH;
|
||||||
|
const int panel_height = WINDOW_HEIGHT;
|
||||||
|
|
||||||
|
bool is_shape_dropdown_open = false;
|
||||||
|
int shape_index = 0;
|
||||||
|
|
||||||
|
|
||||||
|
GuiPanel((Rectangle){
|
||||||
|
panel_x_start,
|
||||||
|
panel_y_start,
|
||||||
|
panel_width,
|
||||||
|
panel_height
|
||||||
|
},
|
||||||
|
"");
|
||||||
|
|
||||||
|
bool click_add_oscillator = GuiButton((Rectangle){
|
||||||
|
panel_x_start + 10,
|
||||||
|
panel_y_start + 10,
|
||||||
|
panel_width - 20,
|
||||||
|
25
|
||||||
|
}, "Add Oscillator");
|
||||||
|
if (click_add_oscillator)
|
||||||
|
{
|
||||||
|
// synth->ui_oscillator_count += 1;
|
||||||
|
// // Set defaults:
|
||||||
|
// UiOscillator *ui_osc = synth->ui_oscillator + (synth->ui_oscillator_count - 1);
|
||||||
|
// ui_osc->shape = WaveShape_SINE;
|
||||||
|
// ui_osc->freq = BASE_NOTE_FREQ;
|
||||||
|
// ui_osc->amplitude_ratio = 0.1f;
|
||||||
|
// ui_osc->shape_parameter_0 = 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw Oscillators
|
||||||
|
float panel_y_offset = 0;
|
||||||
|
//synth->ui_oscillator_count = 1
|
||||||
|
for (int ui_osc_i = 0; ui_osc_i < synth->oscillators.count; ui_osc_i++)
|
||||||
|
{
|
||||||
|
OscillatorUI* ui_osc = &synth->ui_oscillators[ui_osc_i];
|
||||||
|
Oscillator* osc = &synth->oscillators.array[ui_osc_i];
|
||||||
|
const bool has_shape_param = (ui_osc->waveshape == Square);
|
||||||
|
|
||||||
|
// Draw Oscillator Panel
|
||||||
|
const int osc_panel_width = panel_width - 20;
|
||||||
|
const int osc_panel_height = has_shape_param ? 130 : 100;
|
||||||
|
const int osc_panel_x = panel_x_start + 10;
|
||||||
|
const int osc_panel_y = panel_y_start + 50 + panel_y_offset;
|
||||||
|
panel_y_offset += osc_panel_height + 5;
|
||||||
|
GuiPanel((Rectangle){
|
||||||
|
osc_panel_x,
|
||||||
|
osc_panel_y,
|
||||||
|
osc_panel_width,
|
||||||
|
osc_panel_height
|
||||||
|
},
|
||||||
|
"");
|
||||||
|
|
||||||
|
const float slider_padding = 50.f;
|
||||||
|
const float el_spacing = 5.f;
|
||||||
|
Rectangle el_rect = {
|
||||||
|
.x = osc_panel_x + slider_padding + 30,
|
||||||
|
.y = osc_panel_y + 10,
|
||||||
|
.width = osc_panel_width - (slider_padding * 2),
|
||||||
|
.height = 25
|
||||||
|
};
|
||||||
|
|
||||||
|
// Volume slider
|
||||||
|
float decibels = (20.f * log10f(osc->volume));
|
||||||
|
char amp_slider_label[32];
|
||||||
|
sprintf(amp_slider_label, "%.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));
|
||||||
|
osc->volume = ui_osc->volume;
|
||||||
|
|
||||||
|
el_rect.y += el_rect.height + el_spacing;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
// DRAW OSCILLATOR SHAPE INPUTS
|
||||||
|
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];
|
||||||
|
Oscillator* osc = &synth->oscillators.array[ui_osc_i];
|
||||||
|
// Shape select
|
||||||
|
int shape_index = (int)(ui_osc->waveshape);
|
||||||
|
bool is_dropdown_click = GuiDropdownBox(ui_osc->shape_dropdown_rect,
|
||||||
|
WAVE_SHAPE_OPTIONS,
|
||||||
|
&shape_index,
|
||||||
|
ui_osc->is_dropdown_open
|
||||||
|
);
|
||||||
|
|
||||||
|
if (is_dropdown_click)
|
||||||
|
{
|
||||||
|
ui_osc->is_dropdown_open = !ui_osc->is_dropdown_open;
|
||||||
|
ui_osc->waveshape = (OscillatorType)(shape_index);
|
||||||
|
// APPLY STATE TO REAL OSC
|
||||||
|
osc->osc = (OscillatorType)(shape_index);
|
||||||
|
}
|
||||||
|
if (ui_osc->is_dropdown_open) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DrawSignal(Synth* synth) {
|
||||||
|
GuiGrid((Rectangle){0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}, "", WINDOW_HEIGHT / 8, 2);
|
||||||
|
Vector2* signal_points = malloc(sizeof(Vector2) * synth->out_signal->sample_count);
|
||||||
|
const float screen_vertical_midpoint = (WINDOW_HEIGHT/2);
|
||||||
|
for (int point_idx = 0; point_idx < synth->out_signal->sample_count; point_idx++)
|
||||||
|
{
|
||||||
|
signal_points[point_idx].x = (float)point_idx + OSCILLATOR_PANEL_WIDTH;
|
||||||
|
signal_points[point_idx].y = screen_vertical_midpoint + (int)(synth->out_signal->samples[point_idx] * 300);
|
||||||
|
}
|
||||||
|
DrawLineStrip(signal_points, synth->out_signal->sample_count, RED);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Main
|
// Main
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
@@ -167,12 +324,20 @@ int main(int argc, char **argv) {
|
|||||||
.out_signal = &g_sound,
|
.out_signal = &g_sound,
|
||||||
.oscillators = init_osc_array()
|
.oscillators = init_osc_array()
|
||||||
};
|
};
|
||||||
|
//todo: move somewhere in initialization
|
||||||
|
synth.ui_oscillators = malloc(sizeof(OscillatorUI) * synth.oscillators.count);
|
||||||
|
for (size_t i = 0; i < synth.oscillators.count; i++)
|
||||||
|
{
|
||||||
|
OscillatorUI* ui = &synth.ui_oscillators[i];
|
||||||
|
ui->freq = synth.oscillators.array[i].freq;
|
||||||
|
ui->waveshape = synth.oscillators.array[i].osc;
|
||||||
|
ui->volume = synth.oscillators.array[i].volume;
|
||||||
|
}
|
||||||
|
|
||||||
int sound_played_count = 0;
|
int sound_played_count = 0;
|
||||||
float temp_buffer[STREAM_BUFFER_SIZE];
|
float temp_buffer[STREAM_BUFFER_SIZE];
|
||||||
RingBuffer ring_buffer = ring_buffer_init(STREAM_BUFFER_SIZE);
|
RingBuffer ring_buffer = ring_buffer_init(STREAM_BUFFER_SIZE);
|
||||||
|
|
||||||
|
|
||||||
InitAudioDevice();
|
InitAudioDevice();
|
||||||
SetMasterVolume(SYNTH_VOLUME);
|
SetMasterVolume(SYNTH_VOLUME);
|
||||||
SetAudioStreamBufferSizeDefault(STREAM_BUFFER_SIZE);
|
SetAudioStreamBufferSizeDefault(STREAM_BUFFER_SIZE);
|
||||||
@@ -244,8 +409,10 @@ int main(int argc, char **argv) {
|
|||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
DrawUi(&synth);
|
||||||
DrawFPS(0,0);
|
DrawSignal(&synth);
|
||||||
|
//DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
||||||
|
//DrawFPS(0,0);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|||||||
10
oscillator.c
10
oscillator.c
@@ -49,16 +49,16 @@ float multiosc(OscillatorGenerationParameter param) {
|
|||||||
Oscillator osc = param.oscillators.array[i];
|
Oscillator osc = param.oscillators.array[i];
|
||||||
switch (osc.osc) {
|
switch (osc.osc) {
|
||||||
case Sine:
|
case Sine:
|
||||||
osc_sample += sineosc(osc.freq, param.sample);
|
osc_sample += sineosc(osc.freq, param.sample) * osc.volume;
|
||||||
break;
|
break;
|
||||||
case Triangle:
|
case Triangle:
|
||||||
osc_sample += triangleosc(osc.freq, param.sample);
|
osc_sample += triangleosc(osc.freq, param.sample) * osc.volume;
|
||||||
break;
|
break;
|
||||||
case Square:
|
case Square:
|
||||||
osc_sample += squareosc(osc.freq, param.sample);
|
osc_sample += squareosc(osc.freq, param.sample) * osc.volume;
|
||||||
break;
|
break;
|
||||||
case Saw:
|
case Saw:
|
||||||
osc_sample += sawosc(osc.freq, param.sample);
|
osc_sample += sawosc(osc.freq, param.sample) * osc.volume;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ SynthSound freq(float duration, OscillatorArray osc) {
|
|||||||
.oscillators = osc,
|
.oscillators = osc,
|
||||||
.sample = sample
|
.sample = sample
|
||||||
};
|
};
|
||||||
output[i] = multiosc(param) * VOLUME;
|
output[i] = multiosc(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
// create attack and release
|
// create attack and release
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#define OSCILLATOR_H
|
#define OSCILLATOR_H
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#define WAVE_SHAPE_OPTIONS "Sine;Triangle;Sawtooth;Square"
|
||||||
typedef enum {
|
typedef enum {
|
||||||
Sine,
|
Sine,
|
||||||
Triangle,
|
Triangle,
|
||||||
@@ -13,6 +13,7 @@ typedef enum {
|
|||||||
typedef struct Oscillator {
|
typedef struct Oscillator {
|
||||||
OscillatorType osc;
|
OscillatorType osc;
|
||||||
float freq;
|
float freq;
|
||||||
|
float volume;
|
||||||
} Oscillator;
|
} Oscillator;
|
||||||
|
|
||||||
typedef struct OscillatorArray {
|
typedef struct OscillatorArray {
|
||||||
|
|||||||
@@ -14,5 +14,6 @@
|
|||||||
|
|
||||||
#define WINDOW_WIDTH 640
|
#define WINDOW_WIDTH 640
|
||||||
#define WINDOW_HEIGHT 480
|
#define WINDOW_HEIGHT 480
|
||||||
|
#define OSCILLATOR_PANEL_WIDTH 200
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user