Sound and audio
Umfeld's native audio engine — oscillators, samples, envelopes, FFT, drum machines and effects.
Umfeld has its own audio system built in — no external audio library to add. You request
audio in settings(), then fill the output buffer inside audioEvent(),
which runs on the audio thread once per buffer. Generators and effects each expose a
process() that returns one sample at a time.
The callback is a real-time thread. Pre-allocate its buffers in setup(); never allocate, perform
file or console I/O, touch drawing state, or wait for a mutex in audioEvent(). Ordinary shared
variables are not safe: simultaneous access from draw() and audioEvent() is a C++ data race.
Keep DSP objects owned by the audio thread and pass simple controls or meter values with atomics
that are lock-free on your target, as in the examples below.
The audio skeleton
#include "Umfeld.h"
#include "audio/AudioUtilities.h"
#include "audio/Wavetable.h"
#include <atomic>
#include <vector>
using namespace umfeld;
Wavetable* osc;
std::vector<float> buffer;
std::atomic<float> requested_frequency{220.0f};
static_assert(std::atomic<float>::is_always_lock_free);
void settings() {
size(640, 480);
audio(); // request default in / out
}
void setup() {
buffer.resize(get_audio_buffer_size()); // allocate before the callback starts
osc = new Wavetable(1024, get_audio_sample_rate());
osc->set_waveform(WAVEFORM_SINE); // SINE / TRIANGLE / SAWTOOTH / SQUARE / NOISE
osc->set_frequency(220.0f);
osc->set_amplitude(0.7f);
}
void draw() {
background(216);
requested_frequency.store(
map(mouseX, 0, width, 20.0f, 880.0f),
std::memory_order_relaxed);
}
void audioEvent(const PAudio& audio) {
// Only the audio thread mutates osc and buffer.
osc->set_frequency(requested_frequency.load(std::memory_order_relaxed));
for (uint32_t i = 0; i < audio.buffer_size; ++i) {
buffer[i] = osc->process();
}
if (audio.output_channels == 2) {
merge_interleaved_stereo(
buffer.data(), buffer.data(), audio.output_buffer, audio.buffer_size);
}
}
void shutdown() {
delete osc; // free what you `new`-ed
}
Playing audio files
loadSample() loads a clip into a Sampler you can play(), loop, and scrub:
#include "audio/Sampler.h"
Sampler* sample;
std::vector<float> sample_buffer;
std::atomic<float> playback_position{0.0f};
void setup() {
sample_buffer.resize(get_audio_buffer_size());
sample = loadSample("loop.wav");
sample->set_looping();
sample->play();
}
void audioEvent(const PAudio& audio) {
for (uint32_t i = 0; i < audio.buffer_size; ++i) {
sample_buffer[i] = sample->process();
}
playback_position.store(
sample->get_position_normalized(), std::memory_order_relaxed);
merge_interleaved_stereo(
sample_buffer.data(), sample_buffer.data(), audio.output_buffer, audio.buffer_size);
}
For a drawing-thread playhead, read playback_position.load(std::memory_order_relaxed). Do not call
sample->get_position_normalized() from draw() while the audio thread is mutating the sampler.
Using live audio
Request input channels with audio(inputs, outputs) and read audio.input_buffer in the
callback. This passes the microphone straight to the speakers and measures its energy:
std::vector<float> buffer;
std::atomic<float> displayed_energy{0.0f};
void settings() { size(640, 480); audio(1, 2); } // 1 in, 2 out
void setup() { buffer.resize(get_audio_buffer_size()); }
void audioEvent(const PAudio& audio) {
float energy = 0.0f; // local to the audio thread
for (uint32_t i = 0; i < audio.buffer_size; ++i) {
buffer[i] = audio.input_buffer[i];
energy += std::abs(buffer[i]);
}
displayed_energy.store(
energy / static_cast<float>(audio.buffer_size),
std::memory_order_relaxed);
merge_interleaved_stereo(
buffer.data(), buffer.data(), audio.output_buffer, audio.buffer_size);
}
Drawing a waveform and FFT spectrum
Run the buffer through the FFT to get frequency bins you can draw as bars:
#include "audio/FFT.h"
#include <array>
#include <atomic>
std::vector<float> audio_buffer; // audio-thread-owned
std::vector<float> fft_input; // drawing-thread-owned
std::vector<std::pair<float, float>> spectrum; // drawing-thread-owned
std::array<std::atomic<float>, MAX_AUDIO_BUFFER_SIZE> snapshot;
std::atomic<uint32_t> snapshot_size{0};
void setup() {
const uint32_t size = get_audio_buffer_size();
audio_buffer.resize(size);
fft_input.resize(size);
for (auto& sample : snapshot) sample.store(0.0f, std::memory_order_relaxed);
fft_start(size, get_audio_sample_rate());
}
void draw() {
// FFT may allocate, so perform it on the drawing thread.
const uint32_t count = std::min(
snapshot_size.load(std::memory_order_acquire),
static_cast<uint32_t>(fft_input.size()));
for (uint32_t i = 0; i < count; ++i) {
fft_input[i] = snapshot[i].load(std::memory_order_relaxed);
}
if (count == fft_input.size()) {
spectrum = fft_process(fft_input.data(), 20.0f, 800.0f);
}
background(216);
noStroke();
fill(0, 127, 255);
const float bw = spectrum.empty() ? 0.0f : width / spectrum.size();
for (const auto& [freq, db] : spectrum) {
const float x = map(freq, 20.0f, 800.0f, 0.0f, width);
const float h = map(db, 0.0f, 50.0f, height, 0.0f);
rect(x, h, bw, height - h);
}
}
void audioEvent(const PAudio& audio) {
for (uint32_t i = 0; i < audio.buffer_size; ++i) {
audio_buffer[i] = /* your signal */ 0.0f;
snapshot[i].store(audio_buffer[i], std::memory_order_relaxed);
}
snapshot_size.store(audio.buffer_size, std::memory_order_release);
merge_interleaved_stereo(
audio_buffer.data(), audio_buffer.data(), audio.output_buffer, audio.buffer_size);
}
void shutdown() { fft_stop(); }
Audio-reactive graphics
Values published from the audio thread (like displayed_energy above) can drive the drawing. Load
the atomic once per frame:
void draw() {
const float energy = displayed_energy.load(std::memory_order_relaxed);
background(216);
noFill();
stroke(255, 63, 89);
circle(width / 2, height / 2, energy * 0.5f * height + 0.25f * height);
}
Building a synthesizer (envelopes and notes)
An ADSR envelope shapes a note; Note and Scale turn musical degrees into frequencies:
#include "audio/ADSR.h"
#include "audio/Note.h"
#include "audio/Scale.h"
Wavetable* osc;
ADSR* adsr;
std::vector<float> synth_buffer;
std::atomic<int> requested_degree{-1};
void setup() {
synth_buffer.resize(get_audio_buffer_size());
osc = new Wavetable(512, get_audio_sample_rate());
adsr = new ADSR(get_audio_sample_rate());
osc->set_waveform(WAVEFORM_SINE);
adsr->set_attack(0.01f);
adsr->set_decay(0.2f);
adsr->set_sustain(0.0f);
adsr->set_release(0.0f);
}
// Safe to call from an input callback on the drawing thread.
void play_degree(int degree) {
requested_degree.store(degree, std::memory_order_release);
}
void audioEvent(const PAudio& audio) {
const int degree = requested_degree.exchange(-1, std::memory_order_acquire);
if (degree >= 0) {
const int midi = Scale::note(Scale::MAJOR, Note::C_4, degree);
const float freq = AudioUtilities::midi_note_to_frequency(
static_cast<uint8_t>(midi));
osc->set_frequency(freq);
adsr->start();
}
for (uint32_t i = 0; i < audio.buffer_size; ++i) {
synth_buffer[i] = adsr->process(osc->process());
}
merge_interleaved_stereo(
synth_buffer.data(), synth_buffer.data(), audio.output_buffer, audio.buffer_size);
}
Building a drum machine
BeatDSP fires a callback on a clock; trigger samples on the beats you want:
#include "audio/BeatDSP.h"
Sampler *kick, *snare, *hihat;
BeatDSP* beat;
void on_beat(uint32_t count) {
if (count % 8 == 0) kick->play();
if (count % 8 == 4) snare->play();
hihat->play();
}
void setup() {
kick = loadSample("kick.wav");
snare = loadSample("snare.wav");
hihat = loadSample("hat.wav");
beat = new BeatDSP(get_audio_sample_rate());
beat->set_bpm(130 * 4);
beat->set_callback(on_beat);
}
Using effects
Effects (here a Delay) also expose process() — chain them after your source in the loop:
#include "audio/Delay.h"
Delay* delay; // new Delay(sample_rate, echo_length, decay, wet)
std::vector<float> effect_buffer;
void setup() {
effect_buffer.resize(get_audio_buffer_size());
delay = new Delay(get_audio_sample_rate(), 0.3f, 0.6f, 0.5f);
}
void audioEvent(const PAudio& audio) {
for (uint32_t i = 0; i < audio.buffer_size; ++i) {
effect_buffer[i] = delay->process(osc->process());
}
merge_interleaved_stereo(
effect_buffer.data(), effect_buffer.data(), audio.output_buffer, audio.buffer_size);
}
The repository ships more building blocks the same way — LowPassFilter, OscillatorFunction,
distortion, waveshaper, resonator, and a vocoder.
The complete data and audio sketches under tests/cookbook/ are compiled as C++17 against the
current umfeld/umfeld main branch in CI. Keep those fixtures in sync when changing cookbook
APIs or threading patterns.