Thanks to visit codestin.com
Credit goes to github.com

Skip to content

koppi/sdl2-synth

Repository files navigation

Modular Synth

A modular synthesizer application written in C, using SDL2 for graphics and audio, and supporting MIDI input via libremidi. The GUI is resizable and features oscilloscope and spectrum displays, keyboard input, and real-time control of oscillators, effects, mixer, and arpeggiator.

Screenshot

Modular Synth Screenshot

Features

  • 4-oscillator synth: Each with independent waveform, pitch, detune, gain, phase, pulse width, and unison controls.
  • Comprehensive Parameter Control: All oscillator parameters accessible via intuitive combo boxes:
    • Waveform Selection: SINE, SAW, SQUARE, TRI, NOISE
    • Pitch Presets: Common semitone offsets (-24 to +24)
    • Detune Presets: Standard cent values (-100¢ to +100¢)
    • Gain Presets: Common levels (0%, 12%, 25%, 50%, 75%, 87%, 100%)
    • Phase Presets: Musical phase relationships (0°, 45°, 90°, 135°, 180°, etc.)
    • Pulse Width Presets: Standard duty cycles (10%, 25%, 50%, 75%, 90%)
    • Unison Voices: 1 to 8 voice layering
    • Unison Spread: Preset detuning amounts (Off, Tight, Medium, Wide, Extra Wide)
    • Modern Dear ImGui Interface: Clean, responsive GUI using the Dear ImGui library- Mixer: Control the mix and master volume of each oscillator with bus compression.
  • Effects: Flanger, delay, and reverb with real-time controls.
    • Arpeggiator: Multiple modes, adjustable tempo, octave control, and multi-octave chord arpeggiation.
    • Arpeggiator Modes: UP, DOWN, ORDER, RANDOM
    • Octave Control: Select base octave (0-4 = octaves 1-5) for all notes
    • Multi-Octave Spread: Spread chords across 1-6 octaves for rich, layered arpeggios
    • Hold Function: Sustain arpeggios after key release
    • Polyphonic Mode: Play all notes simultaneously or cycle through them in order
    • MIDI CC Control:
      • CC #90: Enable/disable arpeggiator
      • CC #91: Select arpeggiator mode (UP/DOWN/ORDER/RANDOM)
      • CC #92: Hold function on/off
      • CC #93: Base octave selection (0-4)
      • CC #94: Octaves spread control (1-6)
  • Multi-Octave Arpeggiation:
    • Rich Chords: Set octaves=3 with a C major chord
    • Rising Sequences: Set octaves=2 and use UP mode with moderate tempo
    • Descending Patterns: Set octaves=4 with DOWN mode for descending bass lines
    • Random Exploration: Set octaves=6 with RANDOM mode for experimental textures
  • Oscilloscope & Spectrum: Visualize output waveform and frequency spectrum.
  • MIDI input: Map MIDI CC to synth parameters for external control.
  • Interactive Keyboard: On-screen piano keyboard with visual feedback.

Dependencies

  • SDL2 (graphics, audio, events)
  • SDL2_ttf (font rendering)
  • SDL2_image (image loading)
  • Dear ImGui (GUI library)
  • libremidi (MIDI input)
  • [math.h], [string.h], [stdlib.h], [stdio.h]
  • A TrueType font (e.g., DejaVuSans.ttf; see below)

Build Instructions

Linux

Install dependencies (example for Debian/Ubuntu):

sudo apt-get install git cmake ninja libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev

Clone, build and run:

git clone https://github.com/koppi/sdl2-synth && cd sdl2-synth
cmake --preset release
cmake --build --preset synth-release && build/release/synth

macOS

Install dependencies (with Homebrew):

brew install git ninja sdl2 sdl2_ttf sdl2_image

Then build as above.

Windows

  • Install SDL2, SDL2_ttf, SDL2_image and libremidi development libraries.
  • Use MinGW or Visual Studio to build the sources (adjust includes/libs as needed).

Web (Emscripten)

To build the synthesizer as a WebAssembly (WASM) application that runs in the browser:

Setup Emscripten

First, install the Emscripten SDK:

cd /tmp
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh

Add to your shell profile for permanent access:

echo 'source "/tmp/emsdk/emsdk_env.sh"' >> $HOME/.bashrc

Build with CMake and Emscripten

cd sdl2-synth
cmake --preset emscripten
cmake --build --preset synth-emscripten

This generates:

  • synth.js - JavaScript runtime
  • synth.wasm - WebAssembly binary

Serve the Web Build

Start a local web server to test:

python3 -m http.server 8000

Then open http://localhost:8000 in your browser and navigate to the build output directory.

Running

gdb build/debug/synth # debug the build
build/release/synth   # run the release build

The application will open a window with the synthesizer GUI.

Notes

  • The application expects to find a TrueType font (such as DejaVuSans.ttf) in /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf or the current directory.
  • MIDI input is mapped to numerous parameters (see src/midi.c for mapping).
  • All GUI controls respond to mouse drag and mouse wheel.
  • The oscilloscope is fed directly from the audio callback thread for real-time visualization.

File Overview

  • src/: Source code for synth, GUI, effects, MIDI, etc.
  • src/gui.c, src/gui.h: GUI rendering and event handling.
  • src/oscilloscope.c, src/oscilloscope.h: Oscilloscope/spectrum visualization.
  • src/synth.c, src/synth.h: Synth core and audio callback.
  • src/osc.c, src/osc.h: Oscillator code.
  • src/fx.c, src/fx.h: Effects (flanger, delay, reverb).
  • src/midi.c, src/midi.h: MIDI input and mapping.
  • src/voice.c, src/voice.h: Voice management.
  • src/mixer.c, src/mixer.h: Mixer logic.
  • src/arpeggiator.c, src/arpeggiator.h: Arpeggiator logic.
  • src/app.c, src/app.h, src/main.c: Application entry point, SDL setup, main loop.

License

MIT (see LICENSE)

Credits

About

A polyphonic modular synthesizer written in C using SDL2

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Languages