Skip to content

GraphiteSound: sampled sounds, 'snd ' resources, AIFF, and the µ-law, A-law and IMA4 codecs - #25

Merged
colemancda merged 16 commits into
masterfrom
feature/sound
Nov 26, 2026
Merged

colemancda merged 16 commits into
masterfrom
feature/sound

Conversation

@colemancda

Copy link
Copy Markdown
Member

Summary

The first part of GraphiteSound, the Sound Manager's sounds and the codecs that decode them. The channels and mixer are the second part.

  • SampledSound: a sound's encoding, rate, channels, sample size, count, base note and loop, with its samples kept as Data (a slice of the resource or file it came from, not a copy). pcm16() decodes any supported encoding to interleaved 16-bit samples.
  • 'snd ' resources: formats 1 and 2 and the commands that point at a sampled sound header (GetSoundHeaderOffset), the standard, compressed and extended headers, and writing a format 1 resource back out.
  • AIFF and AIFF-C files, with loops from the markers and instrument chunk.
  • Codecs: G.711 µ-law and A-law, and IMA4 (Apple's 34-byte packets of 64 samples), behind a SoundCodec protocol. They are written from the formats' descriptions.
  • MIDINote.frequency(of:).
  • The Sound Manager's result codes in MacError (badFormat, badFileFormat, badChannel, queueFull, channelBusy, …), with names in its description.
  • GraphiteSound is a library product, and the Graphite umbrella re-exports it. The README says so, and its status and dependencies paragraphs are brought up to date (they still described FreeType and layouts as coming, and no Foundation).

Notes for review

  • Pomme's SoundFormats.cpp, AIFF.cpp and Midi.cpp are ported; those three files carry its MIT notice. Pomme's IMA4.cpp and MACE.cpp come from FFmpeg under the LGPL and were not ported: IMA4 here is written anew.
  • MACE 3:1 and 6:1 are recognized and parsed, but pcm16() throws unimplemented for them. They need a decoder written from a format description.
  • MIDINote differs from Pomme on purpose: Pomme's semitone ratio is a typo (1.059630… for 1.059463…), so this uses exact equal temperament, 440 · 2^((n−69)/12).
  • Sizes read from a resource or file are bounded or saturating, so a hostile count can't overflow where Int is 32 bits (the i686 and 32-bit PowerPC builds).
  • One commit per file, in an order where each commit builds: the module's files and tests first, while the directory is not yet declared, then Package.swift.

Testing

  • 68 new tests: the codecs against G.711 values and a hand-computed IMA4 vector, plus IMA4 round trips against an encoder written in the test (mono and stereo, matched exactly); every encoding's samples; 'snd ' resources and AIFF files built byte by byte in the tests (every header and compression type, loops, padding, slices, errors); and truncation and random-damage runs that must not trap.
  • Built and ran everything on macOS with swift test, 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 b3ecff5 into master Nov 26, 2026
3 checks passed
@colemancda
colemancda deleted the feature/sound branch November 26, 2026 17:22
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