Procedural audio engine
Music that never ends. Fits on an Arduino.
Somnia is an open source procedural audio engine in C++20: from a seed it generates endless ambient melodies on Arduino, ESP32, Raspberry Pi Pico and your terminal, with no heap and no dependencies.
- O(1) memory
- 0 heap allocations
- ~100 ns per note
Plant a seed.
The Somnia core, ported to your browser. The same notes as somnia‑cli, and the same MIDI, WAV, JSON and CSV files.
The same dream, in your terminal
somnia-cli play --scale dorian --root C --bpm 96 --seed 424242 -n 32 --rests - Keyboard shortcuts
- Space
- Pause
- S
- Scale
- R
- Seed
- M
- Mode
- + −
- Tempo
Ten constellations.
Each scale draws a different figure across the twelve semitones. Touch one and hear it.
How it dreams.
Nothing prerecorded and no AI. Deterministic math you can read from start to end.
-
Seed
A 32-bit integer. Keep it and the dream comes back identical.
-
Noise
1D Value Noise with fractal Brownian motion: smooth curves that do not wrap into a short loop.
-
Scale
The curve is quantized to 10 scales with precomputed 12-TET tables. Zero calls to pow.
-
Sound
Each event goes to speakers, buzzer, MIDI, WAV, JSON or CSV.
Where it dreams.
One core. Five places to leave it playing.
-
Arduino
ATmega328P
Q8.8 fixed point for 2 KB of SRAM. Potentiometers as controls and serial MIDI at 31250 baud.
-
ESP32 and Pico
ESP32 · RP2040
Examples for a piezo buzzer, a USB MIDI player and I2S ambient.
-
Terminal
Linux · macOS · Windows
Live piano roll with hotkeys, or raw 16-bit PCM straight through a pipe.
-
Your DAW
MIDI · WAV
Standard MIDI File format 0 at 480 PPQN and 16-bit 44.1 kHz PCM audio.
-
Your code
C · Python · TypeScript
A stable C ABI, Python bindings over ctypes and an npm package for Web Audio.
Take it. It is free.
Pick where it is going to live. Then run your first dream.
Binaries ready to run · v0.6.0
- macOS Apple Silicon · 145 KB Your system Download somnia-cli-aarch64-apple-darwin
- Linux x86_64 · 183 KB Your system Download somnia-cli-x86_64-unknown-linux-gnu
- Windows x86_64 · 134 KB Your system Download somnia-cli-x86_64-pc-windows-msvc.exe
Verify SHA256SUMS Release notes API reference
macOS
With Homebrew
brew install sxnnyside-project/tap/somnia-cli After downloading the binary
chmod +x somnia-cli-aarch64-apple-darwin
./somnia-cli-aarch64-apple-darwin play --scale dorian --seed 424242 Your first dream
somnia-cli stream --scale dorian --bpm 96 --rests Linux
With Homebrew
brew install sxnnyside-project/tap/somnia-cli After downloading the binary
chmod +x somnia-cli-x86_64-unknown-linux-gnu
./somnia-cli-x86_64-unknown-linux-gnu play --scale dorian --seed 424242 Your first dream
somnia-cli stream --scale dorian --bpm 96 --rests Windows
With Scoop
scoop bucket add sxnnyside-project https://github.com/sxnnyside-project/scoop-bucket
scoop install somnia After downloading the .exe, in PowerShell
.\somnia-cli-x86_64-pc-windows-msvc.exe play --scale dorian --seed 424242 Your first dream
somnia-cli stream --scale dorian --bpm 96 --rests Python
The PyPI package does not ship the native library: build it from source and point SOMNIA_LIB_PATH at it. The steps are in Docs.
With pip
pip install somnia-audio With uv
uv add somnia-audio Embedded
In your platformio.ini
[env:my_board]
lib_deps =
sxnnyside/somnia Arch · AUR Coming soon
In review: available once the pull request is approved.
From the AUR
yay -S somnia-bin Arduino IDE Coming soon
In review: available once the pull request is approved.
In the Library Manager, search for
Somnia Source code
Clone and build. Needs CMake 3.21+, a C++20 compiler and just.
git clone https://github.com/core-red-project/somnia.git
cd somnia
just install
just build Frequently asked.
Worth knowing before you plant the first seed.
-
What is Somnia?
An open source procedural audio engine written in C++20. From a seed it generates notes deterministically, in constant memory and with no heap allocations inside the core.
-
Is it free? What is the license?
Yes. It is distributed under the Mozilla Public License 2.0 (MPL-2.0): you can use it in your projects, and changes to Somnia files are shared under the same license.
-
Which platforms does it run on?
Arduino Uno and Nano (ATmega328P), ESP32 and Raspberry Pi Pico (RP2040). The command line tool has binaries for Apple Silicon macOS, x86_64 Linux and x86_64 Windows.
-
Does it use artificial intelligence?
No. It uses deterministic value noise with fractal Brownian motion and quantizes it to ten musical scales. There are no models and no recordings.
-
Does the same seed sound the same everywhere?
The command line tool, the lab on this site, the Python bindings and the npm package 0.6.1 give the same notes for the same parameters. The firmware uses a Q8.8 fixed-point path: it is deterministic on the board, but its notes do not match the desktop ones.
-
Can I take the music into my DAW?
Yes. Export a format 0 Standard MIDI File at 480 PPQN or 16-bit 44.1 kHz PCM WAV, plus JSON and CSV.
-
Do I need to install anything to try it?
No. The lab on the home page generates, plays and exports dreams in your browser.
-
When do AUR and the Arduino Library Manager arrive?
Both are in review as pull requests. Meanwhile you can use the binaries, PlatformIO or build from source.
If a line of code requires dynamic memory to express art, the problem is the line, not the hardware.
If Somnia kept you company for a while, give it a star. If you found a strange use for it, tell us.