Skip to content

GraphiteSound: channels, queued commands and the mixer - #26

Merged
colemancda merged 11 commits into
masterfrom
feature/sound-channels
Nov 26, 2026
Merged

colemancda merged 11 commits into
masterfrom
feature/sound-channels

Conversation

@colemancda

Copy link
Copy Markdown
Member

Summary

The second part of GraphiteSound: the channels that play sounds and the mixer that renders them. Part one (#25) was the sounds and their codecs.

  • SoundCommand: a typed enum of the commands a channel takes (null, quiet, flush, reInit, wait, pause and resume, callBack, sync, buffer, amplitude, volume, frequency, rate and rateMultiplier), with SoundCommand(code:param1:param2:) for the command words that carry no sound. SoundInit and SoundSynth are the init bits and synthesizers.
  • SoundChannel (SndNewChannel, SndDoCommand, SndDoImmediate, SndChannelStatus, SndDisposeChannel) and SoundMixer:
    • Queued commands run in order. A sound holds the queue until it has played, waitCmd holds it for its time (exact to the frame), and callBackCmd calls the channel's callback with the channel and the command, after the mixer's lock is released, so a callback can send more commands.
    • syncCmd holds a channel until the named number of channels reach a sync of the same identifier; they then go on together, at the next 64-frame boundary.
    • Immediate commands run at once. The queue holds 128 commands and then throws queueFull.
    • Volume and pan (volumeCmd, ampCmd, with the gain capped at 2.5), pitch from a MIDI note or a rate, linear interpolation (off with initNoInterp), sustain loops, and a completion closure for a sound that plays to its end.
    • play(_:completion:) for SndStartFilePlay and SndPlay.
  • The mixer renders interleaved stereo floats, hard-clipped, with an output volume for each side (GetDefaultOutputVolume and SetDefaultOutputVolume as a word a side). AudioOutput is the protocol a host implements, and SoundMixer(output:) plays through it.
  • SampledSound is Hashable. The README and the package header describe the channels and mixer.

Notes for review

  • ChannelState.swift and SoundMixer.swift are adapted from rxi's cmixer through Pomme; both carry the two MIT notices. The queue, waits, callbacks and syncs are new: Pomme runs every command at once, and its SndDoCommand does nothing.
  • The mixing is in floats, with positions as doubles, where cmixer uses fixed point and a ring buffer. Loops can be any range, not only from a sustain offset to the sound's end.
  • A channel the program lets go of plays what it was given and what it had queued, and is then gone; a sound that loops plays through once then, since nothing is left to stop it. (Cutting it off was the first version, and the tests caught it: try mixer.newChannel().play(sound) is natural Swift.)
  • A sync can start channels up to one slice (64 frames) after the moment it completes, so they all start together and none before another reaches it.
  • Not here: MACE decoding, double-buffered sound (SndPlayDoubleBuffer), waiting for room in a full queue (doCommand throws queueFull), initChanLeft and initChanRight, and an SDL3 device (that is phase 5's GraphiteCacao).
  • One commit per file. Two commits don't build alone: ChannelState, SoundMixer and SoundChannel refer to one another, so 7d002b1 and fb278ac need the one after them (08eafe8).

Testing

  • 63 new tests: mixing (mono and stereo, clipping and adding channels), resampling both ways with and without interpolation, pitch from notes and rates, pan, volume and the gain cap, header loops and forced ones, completion; queue order, settings applied when reached, waits that are exact across renders, callbacks (also from an immediate command, and using the channel), flush, quiet, both kinds of pause, syncs (the exact frame channels go on at, separate identifiers, a count nobody meets, a channel that leaves), status, disposal, queueFull, sounds that can't be played, the command-word mapping, a device, and commands from other threads during rendering.
  • The sound tests also pass under swift test --sanitize=thread.
  • Built and ran everything on macOS, with and without the FreeType trait, and built the project and its tests in Xcode. Linux and the 32-bit targets are left to CI.

@colemancda
colemancda merged commit 7e2a9b4 into master Nov 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant