This commit is contained in:
Damian 2026-02-01 13:55:45 +01:00
commit 614fa6cdc8
12 changed files with 345 additions and 8 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
synth

22
LICENSE
View file

@ -1,9 +1,19 @@
MIT License
Copyright (c) 2025 Damian Simon
Copyright (c) 2026 damian2002
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,3 +1,17 @@
# synth
This is my attempt at a synthesizer using portaudio and raylib to learn C.
A personal project to learn C
You need to install the portaudio library on your system to build and you can use the command in run.sh to build with gcc. On Debian 13:
```
sudo apt update
sudo apt install portaudio19-dev
gcc -Wall -Wextra -Werror -pedantic -o synth main.c dsp.c osc.c control.c -lportaudio -lm
```
Currently the synthesizer consists of only just one oscillator that is turned on by default when starting the program. You can type "j", "k" or "l" (and then Enter) to change to different frequencies and "x" to leave.
---
This project is licensed under the MIT License see the LICENSE file for details.
See /licenses for third-party licenses.

30
control.c Normal file
View file

@ -0,0 +1,30 @@
#include "control.h"
#include <stdio.h>
#include <stdatomic.h>
int control_run(synthesizer *synth)
{
while (1) {
int c = getchar();
(void) synth;
float base = 200.0;
if (c == 'j') {
float inc = base / synth->sr;
atomic_store(&synth->vco1.inc_target, inc);
}
if (c == 'k') {
float inc = base *2 / synth->sr;
atomic_store(&synth->vco1.inc_target, inc);
}
if (c == 'l') {
float inc = base * 4/ synth->sr;
atomic_store(&synth->vco1.inc_target, inc);
}
if (c == 'x') {
return 0;
}
}
}

8
control.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef CONTROL_H
#define CONTROL_H
#include "dsp.h"
int control_run(synthesizer *synth);
#endif

55
dsp.c Normal file
View file

@ -0,0 +1,55 @@
#include "dsp.h"
#include "osc.h"
#include <stdlib.h>
#include <math.h>
#ifndef TAU
#define TAU (6.283185307179586)
#endif
float next_sample(synthesizer *synth)
{
osc *vco1 = &synth->vco1;
osc *vco2 = &synth->vco2;
osc *vco3 = &synth->vco3;
float s = 0;
// float freq = 220 + 30 * osc_process(lfo2, SAMPLE_RATE);
// atomic_store(&data->vco1.freq_target, freq);
s += osc_process(vco1);
s += osc_process(vco2);
s += osc_process(vco3);
s *= (0.33);
s *= 0.4;
return s;
}
synthesizer *synth_create(float sample_rate)
{
synthesizer *synth = malloc(sizeof *synth);
if (!synth)
return NULL;
synth->sr = sample_rate;
for (int i = 0; i < 2048; i++) {
synth->wavetable[i] = sin((float)i * TAU/2048);
}
float base = 220;
osc_create(&synth->vco1, base, sample_rate, synth->wavetable);
osc_create(&synth->vco2, base * 5.0/4.0 * 1.003, sample_rate, synth->wavetable);
osc_create(&synth->vco3, base * 3.0/2.0 * 0.998, sample_rate, synth->wavetable);
osc_create(&synth->lfo1, 440, sample_rate, synth->wavetable);
osc_create(&synth->lfo2, 880, sample_rate, synth->wavetable);
return synth;
}
void synth_destroy(synthesizer *s) {
free(s);
}

19
dsp.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef DSP_H
#define DSP_H
#include "osc.h"
typedef struct synthesizer {
float sr;
float wavetable[2048];
osc vco1, vco2, vco3;
osc lfo1, lfo2;
} synthesizer;
synthesizer *synth_create(float sample_rate);
void synth_destroy(synthesizer *s);
float next_sample(synthesizer *synth);
#endif

31
licenses/portaudio.txt Normal file
View file

@ -0,0 +1,31 @@
This program uses the PortAudio Portable Audio Library.
For more information see: http://www.portaudio.com/
Copyright (c) 1999-2000 Ross Bencina and Phil Burk
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The text above constitutes the entire PortAudio license; however,
the PortAudio community also makes the following non-binding requests:
Any person wishing to distribute modifications to the Software is
requested to send the modifications to the original developer so that
they can be incorporated into the canonical version. It is also
requested that these non-binding requests be included along with the
license above.

114
main.c Normal file
View file

@ -0,0 +1,114 @@
/*
* This program uses the PortAudio Portable Audio Library and contains code adapted from PortAudio examples.
* Copyright (c) 19992000 Ross Bencina and Phil Burk
* Licensed under the MIT License see licenses/portaudio.txt
*/
#include <stdio.h>
#include <math.h>
#include "portaudio.h"
#include "osc.h"
#include "dsp.h"
#include "control.h"
#define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (128)
static int audio_callback(
const void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData
) {
synthesizer *synth = (synthesizer*)userData;
float *out = (float*)outputBuffer;
// Prevent unused variable warnings
(void) timeInfo; (void) statusFlags;
(void) inputBuffer;
for (unsigned long i=0; i<framesPerBuffer; i++) {
float s = next_sample(synth);
s *= 0.2;
if (s > 1.0) s = 1.0;
if (s < -1.0) s = -1.0;
*out++ = s;
*out++ = s;
}
return paContinue;
}
static int run_app(synthesizer *synth)
{
PaStreamParameters outputParameters;
PaStream *stream = NULL;
PaError paerr;
paerr = Pa_Initialize();
if( paerr != paNoError ) goto cleanup;
outputParameters.device = Pa_GetDefaultOutputDevice();
if (outputParameters.device == paNoDevice) {
fprintf(stderr,"Error: No default output device.\n");
paerr = paNoDevice;
goto cleanup;
}
/* stereo output */
outputParameters.channelCount = 2;
/* 32 bit floating point output */
outputParameters.sampleFormat = paFloat32;
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
paerr = Pa_OpenStream(
&stream,
NULL, /* no input */
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff,
audio_callback,
synth );
if( paerr != paNoError ) goto cleanup;
paerr = Pa_StartStream( stream );
if( paerr != paNoError ) goto cleanup;
// start the control interface and let the stream run until control_run() returns
int control_err = 0;
control_err = control_run(synth);
goto cleanup;
cleanup:
if (stream) {
Pa_StopStream( stream );
Pa_CloseStream( stream );
}
Pa_Terminate();
if (paerr != paNoError) {
fprintf( stderr, "An error occurred while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", paerr );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( paerr ) );
return 1;
}
return control_err;
}
int main(void)
{
synthesizer *synth = synth_create(SAMPLE_RATE);
if (!synth) return 1;
int err = run_app(synth);
synth_destroy(synth);
return err;
}

33
osc.c Normal file
View file

@ -0,0 +1,33 @@
#include <stdatomic.h>
#include "osc.h"
#include "dsp.h"
#include <stdio.h>
float osc_process(osc *o)
{
o->phase += o->inc;
if (o->phase >= 1.0) {
o->phase -= 1.0;
}
float target = atomic_load(&o->inc_target);
o->inc += 0.001f * (target - o->inc);
float index = o->phase * 2048;
int i0 = (int)(index);
int i1 = (i0 + 1) & (2048 - 1);
float s0 = o->wavetable[i0];
float s1 = o->wavetable[i1];
float frac = index - i0;
return s0 + (s1-s0) * frac;
}
void osc_create(osc *o, float freq, float sample_rate, float *wavetable)
{
o->phase = 0.0;
float inc = freq / sample_rate;
o->inc = inc;
o->wavetable = wavetable;
atomic_store(&o->inc_target, inc);
}

17
osc.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef OSC_H
#define OSC_H
#include <stdatomic.h>
typedef struct osc {
const float *wavetable;
float phase; // in [0.0, 1.0)
float inc;
_Atomic float inc_target;
} osc;
float osc_process(osc *o);
void osc_create(osc *o, float freq, float sample_rate, float *wavetable);
#endif

5
run.sh Executable file
View file

@ -0,0 +1,5 @@
#!/bin/sh
clear
gcc -Wall -Wextra -Werror -pedantic -o synth main.c dsp.c osc.c control.c -lm -lportaudio
./synth