///|
/// PCM audio buffer with interleaved sample data.
#derive(Eq, Show)
pub(all) struct AudioBuffer {
channels : Int32
sample_rate : Int32
data : FixedArray[Float]
}
///|
/// Stable voice identifier.
#derive(Eq, Show, Compare, Hash)
pub(all) struct VoiceId(Int32)
///|
/// Ring buffer for streaming audio data.
#derive(Show)
pub(all) struct RingBuffer {
data : FixedArray[Float]
capacity : Int32
channels : Int32
mut write_pos : Int32
mut read_pos : Int32
mut frames_available : Int32
}
///|
/// Callback that pulls audio data into a buffer. Returns frames actually written.
pub(all) struct PullCallback((FixedArray[Float], Int32) -> Int32)
///|
/// Streaming audio source backed by a ring buffer.
pub(all) struct StreamingSource {
ring : RingBuffer
pull : PullCallback
channels : Int32
sample_rate : Int32
mut ended : Bool
mut total_pulled : Int32
}
///|
pub impl Show for PullCallback with fn output(self, logger) {
ignore(self)
logger.write_string("")
}
///|
pub impl Show for StreamingSource with fn output(self, logger) {
logger.write_string(
"StreamingSource { channels: \{self.channels}, sample_rate: \{self.sample_rate}, ended: \{self.ended} }",
)
}
///|
/// Audio source: either a complete buffer or a streaming source.
#derive(Show)
pub(all) enum AudioSource {
Buffer(AudioBuffer)
Stream(StreamingSource)
}
///|
/// Voice playback state.
#derive(Eq, Show)
pub(all) enum VoiceState {
Playing
Paused
Stopped
}
///|
/// A single voice playing an audio source.
#derive(Show)
pub(all) struct Voice {
id : VoiceId
source : AudioSource
mut position : Float
mut gain : Float
mut pan : Float
mut state : VoiceState
looping : Bool
sample_rate : Int32
loop_start : Int32 // frame index to loop back to (default 0)
loop_end : Int32 // frame index where loop restarts (0 = buffer end)
envelope : Envelope?
effects : Array[EffectNode]
}
///|
/// Resampling quality presets.
#derive(Eq, Show)
pub(all) enum ResampleQuality {
Nearest
Linear
Cubic
}
///|
/// Multi-voice mixer with master gain.
pub(all) struct Mixer {
mut voices : Array[Voice]
mut master_gain : Float
channels : Int32
sample_rate : Int32
resample_quality : ResampleQuality
mut next_voice_id : Int32
}
///|
/// ADSR envelope configuration.
#derive(Eq, Show)
pub(all) struct EnvelopeConfig {
attack : Float // seconds
decay : Float // seconds
sustain : Float // level [0.0, 1.0]
release_time : Float // seconds
}
///|
/// Envelope phase in ADSR state machine.
#derive(Eq, Show)
pub(all) enum EnvelopePhase {
Attack
Decay
Sustain
Release
Done
}
///|
/// ADSR envelope state machine.
#derive(Show)
pub(all) struct Envelope {
config : EnvelopeConfig
sample_rate : Int32
mut phase : EnvelopePhase
mut level : Float
mut phase_position : Int32
mut release_start_level : Float
}
///|
/// Biquad filter state (Direct Form II Transposed).
#derive(Show)
pub(all) struct BiquadState {
b0 : Float
b1 : Float
b2 : Float
a1 : Float
a2 : Float
mut z1 : Float
mut z2 : Float
}
///|
/// Delay effect state.
#derive(Show)
pub(all) struct DelayState {
buffer : FixedArray[Float]
mut write_pos : Int32
delay_samples : Int32
feedback : Float
mix : Float
}
///|
/// Effect node in the processing chain.
#derive(Show)
pub(all) enum EffectNode {
Lowpass(BiquadState)
Highpass(BiquadState)
Delay(DelayState)
}
///|
/// Cache eviction policy for audio assets.
#derive(Eq, Show)
pub(all) enum CachePolicy {
AlwaysCache
Normal
StreamPrefer
}
///|
/// Single entry in the asset cache.
#derive(Show)
pub(all) struct CacheEntry {
buffer : AudioBuffer
policy : CachePolicy
byte_size : Int32
mut last_access : Int32
}
///|
/// LRU asset cache for audio buffers.
#derive(Show)
pub(all) struct AssetCache {
entries : Map[String, CacheEntry]
mut total_bytes : Int32
max_bytes : Int32
max_entries : Int32
mut access_counter : Int32
}
///|
/// LSB-first bit reader for Vorbis bitstream parsing.
#derive(Show)
struct BitReader {
data : Bytes
length_bits : Int32
mut bit_pos : Int32
}
///|
/// OGG page header.
#derive(Show)
struct OggPage {
header_type : Int32
granule_position : Int64
serial_number : Int32
page_sequence : Int32
segments : Array[Bytes]
}
///|
/// OGG container demuxer.
#derive(Show)
struct OggDemuxer {
data : Bytes
mut offset : Int32
mut current_packet : Array[Bytes]
}
///|
/// Audio subsystem errors.
pub suberror AudioError {
InvalidFormat(String)
DecodeFailed(String)
}
///|
pub impl Show for AudioError with fn output(self, logger) {
match self {
InvalidFormat(msg) => logger.write_string("InvalidFormat(\{msg})")
DecodeFailed(msg) => logger.write_string("DecodeFailed(\{msg})")
}
}
///|
test "AudioError display" {
let err : AudioError = InvalidFormat("bad header")
inspect(err, content="InvalidFormat(bad header)")
let err2 : AudioError = DecodeFailed("truncated")
inspect(err2, content="DecodeFailed(truncated)")
}