From 614fa6cdc800a75d3471a9b5d029d8d4fc1757a4 Mon Sep 17 00:00:00 2001 From: Damian Date: Sun, 1 Feb 2026 13:55:45 +0100 Subject: [PATCH] init --- .gitignore | 1 + LICENSE | 22 +++++--- README.md | 18 ++++++- control.c | 30 +++++++++++ control.h | 8 +++ dsp.c | 55 ++++++++++++++++++++ dsp.h | 19 +++++++ licenses/portaudio.txt | 31 +++++++++++ main.c | 114 +++++++++++++++++++++++++++++++++++++++++ osc.c | 33 ++++++++++++ osc.h | 17 ++++++ run.sh | 5 ++ 12 files changed, 345 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 control.c create mode 100644 control.h create mode 100644 dsp.c create mode 100644 dsp.h create mode 100644 licenses/portaudio.txt create mode 100644 main.c create mode 100644 osc.c create mode 100644 osc.h create mode 100755 run.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6569f67 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +synth diff --git a/LICENSE b/LICENSE index 51c2c2e..09ba549 100644 --- a/LICENSE +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md index bcdb1d9..069c1f4 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file +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. diff --git a/control.c b/control.c new file mode 100644 index 0000000..b3e91c8 --- /dev/null +++ b/control.c @@ -0,0 +1,30 @@ +#include "control.h" +#include +#include + +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; + } + } +} diff --git a/control.h b/control.h new file mode 100644 index 0000000..0bb4812 --- /dev/null +++ b/control.h @@ -0,0 +1,8 @@ +#ifndef CONTROL_H +#define CONTROL_H + +#include "dsp.h" + +int control_run(synthesizer *synth); + +#endif diff --git a/dsp.c b/dsp.c new file mode 100644 index 0000000..1838a89 --- /dev/null +++ b/dsp.c @@ -0,0 +1,55 @@ +#include "dsp.h" +#include "osc.h" +#include +#include + +#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); +} diff --git a/dsp.h b/dsp.h new file mode 100644 index 0000000..fdb929a --- /dev/null +++ b/dsp.h @@ -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 diff --git a/licenses/portaudio.txt b/licenses/portaudio.txt new file mode 100644 index 0000000..474c832 --- /dev/null +++ b/licenses/portaudio.txt @@ -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. diff --git a/main.c b/main.c new file mode 100644 index 0000000..1dd48c5 --- /dev/null +++ b/main.c @@ -0,0 +1,114 @@ +/* + * This program uses the PortAudio Portable Audio Library and contains code adapted from PortAudio examples. + * Copyright (c) 1999–2000 Ross Bencina and Phil Burk + * Licensed under the MIT License – see licenses/portaudio.txt + */ + +#include +#include +#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 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; +} diff --git a/osc.c b/osc.c new file mode 100644 index 0000000..d2bdc9e --- /dev/null +++ b/osc.c @@ -0,0 +1,33 @@ +#include +#include "osc.h" +#include "dsp.h" +#include + +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); +} diff --git a/osc.h b/osc.h new file mode 100644 index 0000000..3d51a30 --- /dev/null +++ b/osc.h @@ -0,0 +1,17 @@ +#ifndef OSC_H +#define OSC_H + +#include + +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 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..4238b39 --- /dev/null +++ b/run.sh @@ -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