Compare commits
7 Commits
feature/os
...
feature/gu
| Author | SHA1 | Date | |
|---|---|---|---|
|
2c3df4baed
|
|||
|
aeeab6977f
|
|||
|
1ac275906f
|
|||
|
0eb203f9f4
|
|||
|
d7f4538418
|
|||
|
0cdac55d27
|
|||
|
10bf0c6a06
|
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,5 +2,4 @@
|
||||
.DS_Store
|
||||
/Debug/
|
||||
*.wav
|
||||
*.dSYM
|
||||
/lib
|
||||
*.dSYM
|
||||
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@@ -3,11 +3,7 @@
|
||||
"readability/casting"
|
||||
],
|
||||
"files.associations": {
|
||||
"algorithm": "c",
|
||||
"__bit_reference": "c",
|
||||
"bitset": "c",
|
||||
"chrono": "c",
|
||||
"unordered_map": "c"
|
||||
"algorithm": "c"
|
||||
},
|
||||
"FSharp.suggestGitignore": false,
|
||||
}
|
||||
4
.vscode/tasks.json
vendored
4
.vscode/tasks.json
vendored
@@ -9,11 +9,7 @@
|
||||
"-fansi-escape-codes",
|
||||
"-g",
|
||||
"${file}",
|
||||
"${fileDirname}/utils.c",
|
||||
"${fileDirname}/ring_buffer.c",
|
||||
"${fileDirname}/oscillator.c",
|
||||
"${fileDirname}/parser.c",
|
||||
"${fileDirname}/export.c",
|
||||
"-lm",
|
||||
"-lraylib",
|
||||
"-o",
|
||||
|
||||
2
build.sh
2
build.sh
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
CC="${CXX:-cc}"
|
||||
$CC -Wall -std=c11 ./main.c ./utils.c ./ring_buffer.c ./oscillator.c ./parser.c ./export.c -lm -lraylib -o ./bin/main
|
||||
$CC -Wall -std=c11 ./main.c ./parser.c -lm -lraylib -o ./bin/main
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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 2π/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.
|
||||
74
export.c
74
export.c
@@ -1,74 +0,0 @@
|
||||
#include "export.h"
|
||||
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
#include "settings.h"
|
||||
|
||||
uint16_t toInt16Sample(float sample) {
|
||||
return (uint16_t)(sample * 32767.f);
|
||||
}
|
||||
|
||||
static void write_file(char* filename, void* data, int size) {
|
||||
FILE* fp = fopen(filename, "wb"); // open file for writing in binary mode
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "Cannot open file: %s\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fwrite(data, size, 1, fp); // write data to file
|
||||
|
||||
fclose(fp); // close file
|
||||
}
|
||||
|
||||
void pack(uint16_t* d, size_t length) {
|
||||
size_t dataLength = length * 2;
|
||||
|
||||
int bytesPerSample = 2;
|
||||
int byteRate = SAMPLE_RATE * bytesPerSample;
|
||||
|
||||
size_t fileSize = 36 + dataLength;
|
||||
|
||||
uint8_t* buffer = (uint8_t*)malloc(fileSize);
|
||||
|
||||
int i = 0;
|
||||
|
||||
// RIFF header
|
||||
memcpy(buffer + i, "RIFF", 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, &fileSize, 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, "WAVE", 4);
|
||||
i += 4;
|
||||
|
||||
// fmt subchunk
|
||||
memcpy(buffer + i, "fmt ", 4);
|
||||
i += 4;
|
||||
int fmtSize = 16;
|
||||
memcpy(buffer + i, &fmtSize, 4);
|
||||
i += 4;
|
||||
uint16_t audioFormat = 1;
|
||||
memcpy(buffer + i, &audioFormat, 2);
|
||||
i += 2;
|
||||
uint16_t numChannels = 1;
|
||||
memcpy(buffer + i, &numChannels, 2);
|
||||
i += 2;
|
||||
int sampleRate = (int)SAMPLE_RATE;
|
||||
memcpy(buffer + i, &sampleRate, 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, &byteRate, 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, &bytesPerSample, 2);
|
||||
i += 2;
|
||||
int bitsPerSample = bytesPerSample * 8;
|
||||
memcpy(buffer + i, &bitsPerSample, 2);
|
||||
i += 2;
|
||||
|
||||
// data subchunk
|
||||
memcpy(buffer + i, "data", 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, &dataLength, 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, d, dataLength);
|
||||
|
||||
write_file("output.wav", buffer, fileSize);
|
||||
}
|
||||
9
export.h
9
export.h
@@ -1,9 +0,0 @@
|
||||
#ifndef EXPORT_H
|
||||
#define EXPORT_H
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
uint16_t toInt16Sample(float sample);
|
||||
void pack(uint16_t* d, size_t length);
|
||||
|
||||
#endif
|
||||
719
main.c
719
main.c
@@ -2,37 +2,354 @@
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
#include "math.h"
|
||||
|
||||
#include "parser.h"
|
||||
#include "utils.h"
|
||||
#include "ring_buffer.h"
|
||||
#include "settings.h"
|
||||
#include "oscillator.h"
|
||||
#include "export.h"
|
||||
|
||||
#include "raylib.h"
|
||||
#define RAYGUI_IMPLEMENTATION
|
||||
#include "raygui.h"
|
||||
|
||||
#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 write_log(format,args...) do { \
|
||||
printf(format, ## args); \
|
||||
} while(0)
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Ring Buffer
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
typedef struct RingBuffer {
|
||||
float* items;
|
||||
size_t head;
|
||||
size_t tail;
|
||||
int is_full;
|
||||
int is_empty;
|
||||
size_t size;
|
||||
} RingBuffer;
|
||||
|
||||
RingBuffer ring_buffer_init(size_t buffer_size) {
|
||||
RingBuffer buffer = {
|
||||
.items = calloc(buffer_size, sizeof(float)),
|
||||
.head = 0,
|
||||
.tail = 0,
|
||||
.is_full = 0,
|
||||
.is_empty = 1,
|
||||
.size = buffer_size
|
||||
};
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void ring_buffer_reset(RingBuffer* me) {
|
||||
me->head = 0;
|
||||
me->tail = 0;
|
||||
me->is_full = 0;
|
||||
}
|
||||
|
||||
// +
|
||||
static void advance_pointer(RingBuffer* me) {
|
||||
if(me->is_full) {
|
||||
me->tail++;
|
||||
if (me->tail == me->size) {
|
||||
me->tail = 0;
|
||||
}
|
||||
}
|
||||
me->head++;
|
||||
if (me->head == me->size) {
|
||||
me->head = 0;
|
||||
}
|
||||
size_t is_full = me->head == me->tail ? 1 : 0;
|
||||
me->is_full = is_full;
|
||||
}
|
||||
|
||||
// -
|
||||
static void retreat_pointer(RingBuffer* me) {
|
||||
me->is_full = 0;
|
||||
me->tail++;
|
||||
if (me->tail == me->size) {
|
||||
me->tail = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void ring_buffer_write(RingBuffer* buffer, float* data, size_t count) {
|
||||
if (buffer->is_full || buffer->head + count > buffer->size) {
|
||||
write_log("[WARN] Trying to overfill the ring buffer: \n\tIsFull:%d\n\tHead:%zu\n\tCount:%zu\n\t",
|
||||
buffer->is_full,
|
||||
buffer->head,
|
||||
count);
|
||||
return;
|
||||
}
|
||||
buffer->is_empty = 0;
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
buffer->items[buffer->head] = data[i];
|
||||
advance_pointer(buffer);
|
||||
}
|
||||
//me->is_empty = is_full && (me->head == me->tail);
|
||||
}
|
||||
|
||||
int ring_buffer_read(RingBuffer* buffer, float* output, size_t count) {
|
||||
if (buffer->is_empty) {
|
||||
write_log("[WARN] Trying to read empty buffer");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
output[i] = buffer->items[buffer->tail];
|
||||
retreat_pointer(buffer);
|
||||
}
|
||||
buffer->is_empty = !buffer->is_full && (buffer->head == buffer->tail);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t ring_buffer_size(RingBuffer* buffer) {
|
||||
size_t size = buffer->size;
|
||||
if(!buffer->is_full) {
|
||||
if(buffer->head >= buffer->tail) {
|
||||
size = (buffer->head - buffer->tail);
|
||||
}
|
||||
else {
|
||||
size = (buffer->size + buffer->head - buffer->tail);
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
void ring_buffer_print(RingBuffer* me) {
|
||||
write_log("[INFO] The ring buffer: \n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t",
|
||||
me->is_full,
|
||||
me->is_empty,
|
||||
me->head,
|
||||
me->tail);
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// General SynthSound
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
typedef struct SynthSound {
|
||||
float* samples;
|
||||
size_t sample_count;
|
||||
} SynthSound;
|
||||
|
||||
// frees the original sounds
|
||||
SynthSound concat_sounds(SynthSound* sounds, size_t count) {
|
||||
size_t total_count = 0;
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
total_count += sounds[i].sample_count;
|
||||
}
|
||||
// array to hold the result
|
||||
float* total = malloc(total_count * sizeof(float));
|
||||
|
||||
size_t current_count = 0;
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
memcpy(total + current_count,
|
||||
sounds[i].samples,
|
||||
sounds[i].sample_count * sizeof(float));
|
||||
current_count += sounds[i].sample_count;
|
||||
|
||||
free(sounds[i].samples);
|
||||
}
|
||||
|
||||
SynthSound result = {
|
||||
.samples = total,
|
||||
.sample_count = total_count
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Oscillator
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
typedef enum {
|
||||
Sine,
|
||||
Triangle,
|
||||
Saw,
|
||||
Square
|
||||
} OscillatorType;
|
||||
|
||||
typedef struct OscillatorParameter {
|
||||
OscillatorType osc;
|
||||
float freq;
|
||||
} OscillatorParameter;
|
||||
|
||||
typedef struct OscillatorParameterList {
|
||||
OscillatorParameter* array;
|
||||
size_t count;
|
||||
} OscillatorParameterList;
|
||||
|
||||
typedef struct OscillatorGenerationParameter {
|
||||
OscillatorParameterList oscillators;
|
||||
float sample;
|
||||
} OscillatorGenerationParameter;
|
||||
|
||||
static SynthSound get_init_samples(float duration) {
|
||||
size_t sample_count = (size_t)(duration * SAMPLE_RATE);
|
||||
float* samples = malloc(sizeof(float) * sample_count);
|
||||
|
||||
for (double i = 0.0; i < duration * SAMPLE_RATE; i++) {
|
||||
samples[(int)i] = i;
|
||||
}
|
||||
|
||||
SynthSound res = {
|
||||
.samples = samples,
|
||||
.sample_count = sample_count
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static float pos(float hz, float x) {
|
||||
return fmodf(hz * x / SAMPLE_RATE, 1);
|
||||
}
|
||||
|
||||
float sineosc(float hz, float x) {
|
||||
return sinf(x * (2.f * SYNTH_PI * hz / SAMPLE_RATE));
|
||||
}
|
||||
|
||||
static float sign(float v) {
|
||||
return (v > 0.0) ? 1.f : -1.f;
|
||||
}
|
||||
|
||||
float squareosc(float hz, float x) {
|
||||
return sign(sineosc(hz, x));
|
||||
}
|
||||
|
||||
float triangleosc(float hz, float x) {
|
||||
return 1.f - fabsf(pos(hz, x) - 0.5f) * 4.f;
|
||||
}
|
||||
|
||||
float sawosc(float hz, float x) {
|
||||
return pos(hz, x) * 2.f - 1.f;
|
||||
}
|
||||
|
||||
float multiosc(OscillatorGenerationParameter param) {
|
||||
float osc_sample = 0.f;
|
||||
for (size_t i = 0; i < param.oscillators.count; i++) {
|
||||
OscillatorParameter osc = param.oscillators.array[i];
|
||||
switch (osc.osc) {
|
||||
case Sine:
|
||||
osc_sample += sineosc(osc.freq, param.sample);
|
||||
break;
|
||||
case Triangle:
|
||||
osc_sample += triangleosc(osc.freq, param.sample);
|
||||
break;
|
||||
case Square:
|
||||
osc_sample += squareosc(osc.freq, param.sample);
|
||||
break;
|
||||
case Saw:
|
||||
osc_sample += sawosc(osc.freq, param.sample);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return osc_sample;
|
||||
}
|
||||
|
||||
static SynthSound freq(float duration, OscillatorParameterList 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];
|
||||
OscillatorGenerationParameter param = {
|
||||
.oscillators = osc,
|
||||
.sample = sample
|
||||
};
|
||||
output[i] = multiosc(param) * VOLUME;
|
||||
}
|
||||
|
||||
// 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
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
static SynthSound get_attack_samples() {
|
||||
float attack_time = 0.001 * ATTACK_MS;
|
||||
size_t sample_count = (size_t)(attack_time * SAMPLE_RATE);
|
||||
float* attack = malloc(sizeof(float) * sample_count);
|
||||
float samples_to_rise = SAMPLE_RATE * attack_time;
|
||||
float rising_delta = 1.0 / samples_to_rise;
|
||||
float i = 0.0;
|
||||
|
||||
for (int j = 0; j < sample_count; j++) {
|
||||
i += rising_delta;
|
||||
attack[j] = fmin(i, 1.0);
|
||||
}
|
||||
|
||||
SynthSound res = {
|
||||
.samples = attack,
|
||||
.sample_count = sample_count
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
*/
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// 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 {
|
||||
OscillatorArray oscillators;
|
||||
OscillatorUI* ui_oscillators;
|
||||
Note current_note;
|
||||
SynthSound* out_signal;
|
||||
} Synth;
|
||||
|
||||
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" };
|
||||
@@ -85,44 +402,113 @@ int get_semitone_shift(char* target_note) {
|
||||
return get_semitone_shift_internal("A4", target_note);
|
||||
}
|
||||
|
||||
static OscillatorArray init_osc_array() {
|
||||
Oscillator first = {
|
||||
SynthSound note(int semitone, float beats) {
|
||||
float hz = get_hz_by_semitone(semitone);
|
||||
float duration = beats * BEAT_DURATION;
|
||||
|
||||
OscillatorParameter first = {
|
||||
.osc = Square,
|
||||
.freq = 440.f,
|
||||
.volume = VOLUME
|
||||
.freq = hz
|
||||
};
|
||||
|
||||
Oscillator* oscArray = malloc(sizeof(Oscillator*) * 1);
|
||||
assert(oscArray);
|
||||
|
||||
oscArray[0] = first;
|
||||
OscillatorParameter second = {
|
||||
.osc = Saw,
|
||||
.freq = hz + 0.5
|
||||
};
|
||||
|
||||
OscillatorArray oscillators = {
|
||||
OscillatorParameter third = {
|
||||
.osc = Saw,
|
||||
.freq = hz - 1.f
|
||||
};
|
||||
|
||||
OscillatorParameter oscArray[] = { first/*, second, third */};
|
||||
OscillatorParameterList parameters = {
|
||||
.array = oscArray,
|
||||
.count = 1
|
||||
};
|
||||
|
||||
return oscillators;
|
||||
return freq(duration, parameters);
|
||||
}
|
||||
|
||||
SynthSound note(Synth* synth, int semitone, float beats) {
|
||||
float hz = get_hz_by_semitone(semitone);
|
||||
float duration = beats * BEAT_DURATION;
|
||||
|
||||
// will change after oscillator starts to be more autonomous
|
||||
for (size_t i = 0; i < synth->oscillators.count; i++) {
|
||||
osc_set_freq(&synth->oscillators.array[i], hz);
|
||||
}
|
||||
|
||||
return freq(duration, synth->oscillators);
|
||||
}
|
||||
|
||||
SynthSound get_note_sound(Synth* synth, Note input) {
|
||||
SynthSound get_note_sound(Note input) {
|
||||
float length = 1.f / input.length;
|
||||
int semitone_shift = get_semitone_shift(input.name);
|
||||
return note(synth, semitone_shift, length);
|
||||
return note(semitone_shift, length);
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Wav File
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
static uint16_t toInt16Sample(float sample) {
|
||||
return (uint16_t)(sample * 32767.f);
|
||||
}
|
||||
|
||||
static void write_file(char* filename, void* data, int size) {
|
||||
FILE* fp = fopen(filename, "wb"); // open file for writing in binary mode
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "Cannot open file: %s\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fwrite(data, size, 1, fp); // write data to file
|
||||
|
||||
fclose(fp); // close file
|
||||
}
|
||||
|
||||
void pack(uint16_t* d, size_t length) {
|
||||
size_t dataLength = length * 2;
|
||||
|
||||
int bytesPerSample = 2;
|
||||
int byteRate = SAMPLE_RATE * bytesPerSample;
|
||||
|
||||
size_t fileSize = 36 + dataLength;
|
||||
|
||||
uint8_t* buffer = (uint8_t*)malloc(fileSize);
|
||||
|
||||
int i = 0;
|
||||
|
||||
// RIFF header
|
||||
memcpy(buffer + i, "RIFF", 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, &fileSize, 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, "WAVE", 4);
|
||||
i += 4;
|
||||
|
||||
// fmt subchunk
|
||||
memcpy(buffer + i, "fmt ", 4);
|
||||
i += 4;
|
||||
int fmtSize = 16;
|
||||
memcpy(buffer + i, &fmtSize, 4);
|
||||
i += 4;
|
||||
uint16_t audioFormat = 1;
|
||||
memcpy(buffer + i, &audioFormat, 2);
|
||||
i += 2;
|
||||
uint16_t numChannels = 1;
|
||||
memcpy(buffer + i, &numChannels, 2);
|
||||
i += 2;
|
||||
int sampleRate = (int)SAMPLE_RATE;
|
||||
memcpy(buffer + i, &sampleRate, 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, &byteRate, 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, &bytesPerSample, 2);
|
||||
i += 2;
|
||||
int bitsPerSample = bytesPerSample * 8;
|
||||
memcpy(buffer + i, &bitsPerSample, 2);
|
||||
i += 2;
|
||||
|
||||
// data subchunk
|
||||
memcpy(buffer + i, "data", 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, &dataLength, 4);
|
||||
i += 4;
|
||||
memcpy(buffer + i, d, dataLength);
|
||||
|
||||
write_file("output.wav", buffer, fileSize);
|
||||
}
|
||||
//-------------------------------------------------------
|
||||
|
||||
size_t detect_note_pressed(Note* note) {
|
||||
size_t is_pressed = 0;
|
||||
@@ -159,205 +545,66 @@ size_t detect_note_pressed(Note* note) {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// GUI
|
||||
// UI
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
void note_on(Synth *synth, Note *note) {
|
||||
|
||||
/*
|
||||
int get_zero_crossing(SynthSound* sound) {
|
||||
int zero_crossing_index = 0;
|
||||
for (size_t i = 1; i < sound->sample_count; i++)
|
||||
{
|
||||
if (sound->samples[i] >= 0.0f && sound->samples[i-1] < 0.0f) // zero-crossing
|
||||
{
|
||||
zero_crossing_index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return zero_crossing_index;
|
||||
}
|
||||
|
||||
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;
|
||||
Vector2* GetVisualSignal(SynthSound* sound, int zero_crossing_index)
|
||||
{
|
||||
const int signal_amp = 300;
|
||||
|
||||
bool is_shape_dropdown_open = false;
|
||||
int shape_index = 0;
|
||||
Vector2* signal_points = malloc(sizeof(Vector2) * STREAM_BUFFER_SIZE);
|
||||
|
||||
|
||||
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];
|
||||
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
|
||||
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];
|
||||
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,
|
||||
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++)
|
||||
for (size_t point_idx = 0; point_idx < sound->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);
|
||||
const int signal_idx = (point_idx + zero_crossing_index) % STREAM_BUFFER_SIZE;
|
||||
signal_points[point_idx].x = (float)point_idx + WINDOW_WIDTH;
|
||||
signal_points[point_idx].y = screen_vertical_midpoint + (int)(sound->samples[signal_idx] * 300);
|
||||
}
|
||||
DrawLineStrip(signal_points, synth->out_signal->sample_count, RED);
|
||||
|
||||
return signal_points;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Main
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
|
||||
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.1");
|
||||
SetTargetFPS(60);
|
||||
|
||||
//todo: move that variables to Synth declaration
|
||||
Note g_current_note = {
|
||||
Note current_note = {
|
||||
.length = 1,
|
||||
.name = malloc(sizeof(char) * 3)
|
||||
};
|
||||
|
||||
SynthSound g_sound = {
|
||||
SynthSound sound = {
|
||||
.sample_count = 0
|
||||
};
|
||||
|
||||
Synth synth = {
|
||||
.current_note = g_current_note,
|
||||
.out_signal = &g_sound,
|
||||
.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];
|
||||
assert(ui);
|
||||
|
||||
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;
|
||||
float temp_buffer[STREAM_BUFFER_SIZE];
|
||||
RingBuffer ring_buffer = ring_buffer_init(STREAM_BUFFER_SIZE);
|
||||
|
||||
|
||||
InitAudioDevice();
|
||||
SetMasterVolume(SYNTH_VOLUME);
|
||||
SetAudioStreamBufferSizeDefault(STREAM_BUFFER_SIZE);
|
||||
AudioStream synth_stream = LoadAudioStream(SAMPLE_RATE, sizeof(float) * 8, 1);
|
||||
AudioStream synth_stream = LoadAudioStream(SAMPLE_RATE, sizeof(float) * 8, 1);//float*8
|
||||
SetAudioStreamVolume(synth_stream, 0.5f);
|
||||
|
||||
PlayAudioStream(synth_stream);
|
||||
@@ -368,28 +615,25 @@ int main(int argc, char **argv) {
|
||||
// Update Audio states
|
||||
//----------------------------------------------------------------------------------
|
||||
// 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:%d\n",
|
||||
if (!ring_buffer.is_full && sound.sample_count != sound_played_count) {
|
||||
write_log("[INFO] IsFull:%d Samples:%zu Played:%zu\n",
|
||||
ring_buffer.is_full,
|
||||
sound->sample_count,
|
||||
sound.sample_count,
|
||||
sound_played_count);
|
||||
|
||||
// how many samples need write
|
||||
size_t size_to_fill = 0;
|
||||
|
||||
if ((sound->sample_count - sound_played_count) > ring_buffer.size) {
|
||||
if ((sound.sample_count - sound_played_count) > ring_buffer.size) {
|
||||
size_to_fill = ring_buffer.size;
|
||||
} else {
|
||||
size_to_fill = sound->sample_count - sound_played_count;
|
||||
size_to_fill = sound.sample_count - sound_played_count;
|
||||
}
|
||||
|
||||
write_log("[INFO] SizeToFill:%zu\n", size_to_fill);
|
||||
for (size_t i = 0; i < size_to_fill; i++) {
|
||||
temp_buffer[i] = sound->samples[i];
|
||||
temp_buffer[i] = sound.samples[i];
|
||||
}
|
||||
|
||||
ring_buffer_write(&ring_buffer, temp_buffer, size_to_fill);
|
||||
@@ -406,7 +650,7 @@ int main(int argc, char **argv) {
|
||||
// can try the SetAudioStreamCallback
|
||||
UpdateAudioStream(synth_stream, temp_buffer, size_to_read);
|
||||
// can overwrite the ring buffer to avoid that
|
||||
if (sound->sample_count == sound_played_count) {
|
||||
if (sound.sample_count == sound_played_count) {
|
||||
ring_buffer_reset(&ring_buffer);
|
||||
}
|
||||
}
|
||||
@@ -414,11 +658,10 @@ 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);
|
||||
if (detect_note_pressed(¤t_note)) {
|
||||
sound = get_note_sound(current_note);
|
||||
sound_played_count = 0;
|
||||
write_log("Note played: %s\n", current_note->name);
|
||||
write_log("Note played: %s\n", current_note.name);
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
@@ -427,10 +670,12 @@ int main(int argc, char **argv) {
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
DrawUi(&synth);
|
||||
DrawSignal(&synth);
|
||||
//DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
||||
//DrawFPS(0,0);
|
||||
// int zero_crossing = get_zero_crossing(&sound);
|
||||
// Vector2* visual_signal = GetVisualSignal(&sound, zero_crossing);
|
||||
// DrawLineStrip(visual_signal, STREAM_BUFFER_SIZE - zero_crossing, RED);
|
||||
|
||||
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
||||
DrawFPS(0,0);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
@@ -442,17 +687,13 @@ 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);
|
||||
sounds[i] = get_note_sound(note);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
167
oscillator.c
167
oscillator.c
@@ -1,167 +0,0 @@
|
||||
#include "oscillator.h"
|
||||
#include "settings.h"
|
||||
#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);
|
||||
|
||||
for (double i = 0.0; i < duration * SAMPLE_RATE; i++) {
|
||||
samples[(int)i] = i;
|
||||
}
|
||||
|
||||
SynthSound res = {
|
||||
.samples = samples,
|
||||
.sample_count = sample_count
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static float pos(float hz, float x) {
|
||||
return fmodf(hz * x / SAMPLE_RATE, 1);
|
||||
}
|
||||
|
||||
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(Oscillator* osc) {
|
||||
return sign(sineosc(osc));
|
||||
}
|
||||
|
||||
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(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 = ¶m.oscillators.array[i];
|
||||
assert(osc);
|
||||
|
||||
switch (osc->osc) {
|
||||
case Sine:
|
||||
osc_sample += sineosc(osc) * osc->volume;
|
||||
break;
|
||||
case Triangle:
|
||||
osc_sample += triangleosc(osc) * osc->volume;
|
||||
break;
|
||||
case Square:
|
||||
osc_sample += squareosc(osc) * osc->volume;
|
||||
break;
|
||||
case Saw:
|
||||
osc_sample += sawosc(osc) * osc->volume;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return osc_sample;
|
||||
}
|
||||
|
||||
SynthSound freq(float duration, OscillatorArray osc) {
|
||||
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
|
||||
};
|
||||
output[i] = multiosc(param);
|
||||
}
|
||||
|
||||
SynthSound res = {
|
||||
.samples = output,
|
||||
.sample_count = sample_count
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
static SynthSound get_attack_samples() {
|
||||
float attack_time = 0.001 * ATTACK_MS;
|
||||
size_t sample_count = (size_t)(attack_time * SAMPLE_RATE);
|
||||
float* attack = malloc(sizeof(float) * sample_count);
|
||||
float samples_to_rise = SAMPLE_RATE * attack_time;
|
||||
float rising_delta = 1.0 / samples_to_rise;
|
||||
float i = 0.0;
|
||||
|
||||
for (int j = 0; j < sample_count; j++) {
|
||||
i += rising_delta;
|
||||
attack[j] = fmin(i, 1.0);
|
||||
}
|
||||
|
||||
SynthSound res = {
|
||||
.samples = attack,
|
||||
.sample_count = sample_count
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
*/
|
||||
37
oscillator.h
37
oscillator.h
@@ -1,37 +0,0 @@
|
||||
#ifndef OSCILLATOR_H
|
||||
#define OSCILLATOR_H
|
||||
|
||||
#include "utils.h"
|
||||
#define WAVE_SHAPE_OPTIONS "Sine;Triangle;Sawtooth;Square"
|
||||
typedef enum {
|
||||
Sine,
|
||||
Triangle,
|
||||
Saw,
|
||||
Square
|
||||
} OscillatorType;
|
||||
|
||||
typedef struct Oscillator {
|
||||
OscillatorType osc;
|
||||
float freq;
|
||||
float volume;
|
||||
float phase;
|
||||
float phase_dt;
|
||||
} Oscillator;
|
||||
|
||||
typedef struct OscillatorArray {
|
||||
Oscillator* array;
|
||||
size_t count;
|
||||
} OscillatorArray;
|
||||
|
||||
typedef struct OscillatorGenerationParameter {
|
||||
OscillatorArray oscillators;
|
||||
} OscillatorGenerationParameter;
|
||||
|
||||
void osc_set_freq(Oscillator* osc, float freq);
|
||||
void osc_reset(Oscillator* osc);
|
||||
float multiosc(OscillatorGenerationParameter param);
|
||||
SynthSound freq(float duration, OscillatorArray osc);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,99 +0,0 @@
|
||||
#include "ring_buffer.h"
|
||||
#include "utils.h"
|
||||
|
||||
RingBuffer ring_buffer_init(size_t buffer_size) {
|
||||
RingBuffer buffer = {
|
||||
.items = calloc(buffer_size, sizeof(float)),
|
||||
.head = 0,
|
||||
.tail = 0,
|
||||
.is_full = 0,
|
||||
.is_empty = 1,
|
||||
.size = buffer_size
|
||||
};
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void ring_buffer_reset(RingBuffer* me) {
|
||||
me->head = 0;
|
||||
me->tail = 0;
|
||||
me->is_full = 0;
|
||||
}
|
||||
|
||||
// +
|
||||
static void advance_pointer(RingBuffer* me) {
|
||||
if(me->is_full) {
|
||||
me->tail++;
|
||||
if (me->tail == me->size) {
|
||||
me->tail = 0;
|
||||
}
|
||||
}
|
||||
me->head++;
|
||||
if (me->head == me->size) {
|
||||
me->head = 0;
|
||||
}
|
||||
size_t is_full = me->head == me->tail ? 1 : 0;
|
||||
me->is_full = is_full;
|
||||
}
|
||||
|
||||
// -
|
||||
static void retreat_pointer(RingBuffer* me) {
|
||||
me->is_full = 0;
|
||||
me->tail++;
|
||||
if (me->tail == me->size) {
|
||||
me->tail = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void ring_buffer_write(RingBuffer* buffer, float* data, size_t count) {
|
||||
if (buffer->is_full || buffer->head + count > buffer->size) {
|
||||
write_log("[WARN] Trying to overfill the ring buffer: \n\tIsFull:%d\n\tHead:%zu\n\tCount:%zu\n\t",
|
||||
buffer->is_full,
|
||||
buffer->head,
|
||||
count);
|
||||
return;
|
||||
}
|
||||
buffer->is_empty = 0;
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
buffer->items[buffer->head] = data[i];
|
||||
advance_pointer(buffer);
|
||||
}
|
||||
//me->is_empty = is_full && (me->head == me->tail);
|
||||
}
|
||||
|
||||
int ring_buffer_read(RingBuffer* buffer, float* output, size_t count) {
|
||||
if (buffer->is_empty) {
|
||||
write_log("[WARN] Trying to read empty buffer");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
output[i] = buffer->items[buffer->tail];
|
||||
retreat_pointer(buffer);
|
||||
}
|
||||
buffer->is_empty = !buffer->is_full && (buffer->head == buffer->tail);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t ring_buffer_size(RingBuffer* buffer) {
|
||||
size_t size = buffer->size;
|
||||
if(!buffer->is_full) {
|
||||
if(buffer->head >= buffer->tail) {
|
||||
size = (buffer->head - buffer->tail);
|
||||
}
|
||||
else {
|
||||
size = (buffer->size + buffer->head - buffer->tail);
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
void ring_buffer_print(RingBuffer* me) {
|
||||
write_log("[INFO] The ring buffer: \n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t",
|
||||
me->is_full,
|
||||
me->is_empty,
|
||||
me->head,
|
||||
me->tail);
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef RING_BUFFER_H
|
||||
#define RING_BUFFER_H
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
typedef struct RingBuffer {
|
||||
float* items;
|
||||
size_t head;
|
||||
size_t tail;
|
||||
int is_full;
|
||||
int is_empty;
|
||||
size_t size;
|
||||
} RingBuffer;
|
||||
|
||||
RingBuffer ring_buffer_init(size_t buffer_size);
|
||||
void ring_buffer_reset(RingBuffer* me);
|
||||
void ring_buffer_write(RingBuffer* buffer, float* data, size_t count);
|
||||
int ring_buffer_read(RingBuffer* buffer, float* output, size_t count);
|
||||
size_t ring_buffer_size(RingBuffer* buffer);
|
||||
void ring_buffer_print(RingBuffer* me);
|
||||
|
||||
#endif
|
||||
19
settings.h
19
settings.h
@@ -1,19 +0,0 @@
|
||||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
#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
|
||||
|
||||
#endif
|
||||
30
utils.c
30
utils.c
@@ -1,30 +0,0 @@
|
||||
#include "utils.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
// frees the original sounds
|
||||
SynthSound concat_sounds(SynthSound* sounds, size_t count) {
|
||||
size_t total_count = 0;
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
total_count += sounds[i].sample_count;
|
||||
}
|
||||
// array to hold the result
|
||||
float* total = malloc(total_count * sizeof(float));
|
||||
|
||||
size_t current_count = 0;
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
memcpy(total + current_count,
|
||||
sounds[i].samples,
|
||||
sounds[i].sample_count * sizeof(float));
|
||||
current_count += sounds[i].sample_count;
|
||||
|
||||
free(sounds[i].samples);
|
||||
}
|
||||
|
||||
SynthSound result = {
|
||||
.samples = total,
|
||||
.sample_count = total_count
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
23
utils.h
23
utils.h
@@ -1,23 +0,0 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include "stdio.h"
|
||||
#include "assert.h"
|
||||
|
||||
#define write_log(format,args...) do { \
|
||||
printf(format, ## args); \
|
||||
} while(0)
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// General SynthSound
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
typedef struct SynthSound {
|
||||
float* samples;
|
||||
size_t sample_count;
|
||||
} SynthSound;
|
||||
|
||||
// frees the original sounds
|
||||
SynthSound concat_sounds(SynthSound* sounds, size_t count);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user