b6fdee8bfc
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
60 lines
1.9 KiB
Swift
60 lines
1.9 KiB
Swift
// WavEncoding.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
/// PCM sample encodings supported by Lyrebird's portable WAV reader/writer.
|
|
///
|
|
/// The portable WAV codec exists so offline render (`NRTDriver`), disk streaming
|
|
/// generators, and buffer file I/O keep working on platforms without Apple's
|
|
/// `AudioToolbox`/`ExtAudioFile`. It handles canonical RIFF/WAVE with a single
|
|
/// `fmt ` chunk and a single `data` chunk — the format every tool writes and
|
|
/// reads. CAF and AIFF containers remain Apple-only (they route through
|
|
/// `ExtAudioFile` under `#if canImport(AudioToolbox)`).
|
|
public enum WavEncoding: Sendable, Equatable {
|
|
/// 32-bit IEEE float (WAVE format tag 3). The default — lossless for the
|
|
/// engine's internal `Double`/`Float` sample representation.
|
|
case float32
|
|
/// 16-bit signed integer PCM (WAVE format tag 1).
|
|
case int16
|
|
/// 24-bit signed integer PCM, packed 3 bytes/sample (WAVE format tag 1).
|
|
case int24
|
|
|
|
/// WAVE `wFormatTag`: 1 = integer PCM, 3 = IEEE float.
|
|
var formatTag: UInt16 {
|
|
switch self {
|
|
case .float32: return 3
|
|
case .int16, .int24: return 1
|
|
}
|
|
}
|
|
|
|
/// Bytes per single-channel sample.
|
|
var bytesPerSample: Int {
|
|
switch self {
|
|
case .float32: return 4
|
|
case .int16: return 2
|
|
case .int24: return 3
|
|
}
|
|
}
|
|
|
|
/// Bits per single-channel sample (WAVE `wBitsPerSample`).
|
|
var bitsPerSample: UInt16 {
|
|
UInt16(bytesPerSample * 8)
|
|
}
|
|
|
|
/// Whether this is an integer PCM tag.
|
|
///
|
|
/// RIFF holds non-PCM tags to a stricter header: the `fmt ` body must
|
|
/// declare `cbSize` (18 bytes rather than 16) and the file must carry a
|
|
/// `fact` chunk. `WavFileWriter` branches on this.
|
|
var isPCM: Bool {
|
|
switch self {
|
|
case .int16, .int24: return true
|
|
case .float32: return false
|
|
}
|
|
}
|
|
}
|