kagura_audio

    Backend-agnostic audio output contracts for Kagura (AudioOutputHooks injection point over mizchi/audio; extracted from kagura_engine)

    Download zip
    Author
    Version
    0.6.0
    License
    Apache-2.0
    Last updated
    15 days ago
    Downloads
    18

    Dependencies

    mizchi/kagura_audio does not have a README file

    AudioContext

    pub trait AudioContext {
    fn create_player(Self, clip : AudioClip) -> PlayerId raise
    fn play(Self, id : PlayerId) -> Unit raise
    fn pause(Self, id : PlayerId) -> Unit raise
    fn stop(Self, id : PlayerId) -> Unit raise
    fn state(Self, id : PlayerId) -> PlayerState
    fn set_volume(Self, id : PlayerId, volume : Double) -> Unit
    fn current_position(Self, id : PlayerId) -> Double
    fn seek(Self, id : PlayerId, position : Double) -> Unit raise
    fn set_pan(Self, id : PlayerId, pan : Double) -> Unit
    fn dispose(Self, id : PlayerId) -> Unit
    }

    AudioStream

    pub trait AudioStream {
    fn read(Self, buf : FixedArray[Byte], length : Int) -> Int
    fn content_length(Self) -> Int
    }

    AudioClip

    pub struct AudioClip {
    format : AudioFormat
    data : Bytes
    loop_ : Bool
    } derive(
    Debug
    )

    impl Show for AudioClip

    AudioClip::output

    fn AudioClip::output(self : AudioClip, logger : &Logger) -> Unit

    AudioClip::to_string

    fn AudioClip::to_string(self : AudioClip) -> String

    AudioClipStream

    pub struct AudioClipStream {
    clip : AudioClip
    position : Int
    }

    Stream that reads sequentially from an AudioClip.

    AudioClipStream::content_length

    fn AudioClipStream::content_length(self : AudioClipStream) -> Int

    AudioClipStream::new

    AudioClipStream::read

    fn AudioClipStream::read(self : AudioClipStream, buf : FixedArray[Byte], length : Int) -> Int

    AudioClipStream::remaining

    fn AudioClipStream::remaining(self : AudioClipStream) -> Int

    AudioClipStream::reset

    fn AudioClipStream::reset(self : AudioClipStream) -> Unit

    AudioFormat

    pub struct AudioFormat {
    sample_rate : Int
    channels : Int
    bits_per_sample : Int
    } derive(
    Debug
    )

    impl Show for AudioFormat

    AudioFormat::output

    fn AudioFormat::output(self : AudioFormat, logger : &Logger) -> Unit

    AudioFormat::to_string

    fn AudioFormat::to_string(self : AudioFormat) -> String

    AudioOutputHooks

    pub struct AudioOutputHooks {
    try_initialize : (AudioFormat) -> Bool
    write_frames : (FixedArray[Float], Int) -> Int
    suspend : () -> Unit
    resume_playback : () -> Unit
    close : () -> Unit
    output_latency : () -> Double
    }

    Audio output hooks for backend integration (Web Audio API / native audio).

    AudioOutputStats

    pub struct AudioOutputStats {
    initialized : Bool
    frames_written : Int
    suspended : Bool
    } derive(
    Debug
    )

    AudioOutputStats::output

    fn AudioOutputStats::output(self : AudioOutputStats, logger : &Logger) -> Unit

    AudioOutputStats::to_string

    fn AudioOutputStats::to_string(self : AudioOutputStats) -> String

    BufferedAudioStream

    pub struct BufferedAudioStream {
    chunks : Array[Bytes]
    total_bytes : Int
    read_chunk_idx : Int
    read_byte_idx : Int
    finalized : Bool
    final_length : Int
    }

    Buffered stream that accumulates audio data chunks progressively. Useful for network streaming or progressive decoding.

    BufferedAudioStream::buffered_bytes

    fn BufferedAudioStream::buffered_bytes(self : BufferedAudioStream) -> Int

    Get the number of bytes available for reading.

    BufferedAudioStream::content_length

    fn BufferedAudioStream::content_length(self : BufferedAudioStream) -> Int

    BufferedAudioStream::finalize

    fn BufferedAudioStream::finalize(self : BufferedAudioStream) -> Unit

    Mark the stream as complete with known total length.

    BufferedAudioStream::has_data

    fn BufferedAudioStream::has_data(self : BufferedAudioStream) -> Bool

    Check if more data is available to read.

    BufferedAudioStream::is_finalized

    fn BufferedAudioStream::is_finalized(self : BufferedAudioStream) -> Bool

    BufferedAudioStream::new

    BufferedAudioStream::push_chunk

    fn BufferedAudioStream::push_chunk(self : BufferedAudioStream, data : Bytes) -> Unit

    Append a data chunk to the stream buffer.

    BufferedAudioStream::read

    fn BufferedAudioStream::read(self : BufferedAudioStream, buf : FixedArray[Byte], length : Int) -> Int

    BufferedAudioStream::reset

    fn BufferedAudioStream::reset(self : BufferedAudioStream) -> Unit

    CuePlayback

    pub(all) enum CuePlayback {
    Played
    Suppressed
    Muted
    Missing
    } derive(Eq,
    Debug
    )

    Outcome of a cue request. Playback errors remain errors, not missing cues.
    impl Show for CuePlayback

    MixerAudioContext

    pub struct MixerAudioContext {
    mixer :
    Mixer

    player_map : Array[(PlayerId,
    VoiceId
    )]
    next_id : Int
    bgm_player_id : PlayerId?
    bgm_fade_target : Double
    bgm_fade_speed : Double
    bgm_current_volume : Double
    bgm_outgoing_id : PlayerId?
    bgm_outgoing_volume : Double
    bgm_outgoing_fade_speed : Double
    }

    MixerAudioContext wraps mizchi/audio's Mixer to implement AudioContext.

    MixerAudioContext::create_player

    fn MixerAudioContext::create_player(self : MixerAudioContext, clip : AudioClip) -> PlayerId raise

    MixerAudioContext::crossfade_bgm

    fn MixerAudioContext::crossfade_bgm(self : MixerAudioContext, clip : AudioClip, duration_seconds : Double, volume? : Double) -> Unit

    Crossfade from the current BGM to a new BGM clip. The old BGM fades out and the new BGM fades in over duration_seconds.

    MixerAudioContext::current_position

    fn MixerAudioContext::current_position(self : MixerAudioContext, id : PlayerId) -> Double

    MixerAudioContext::dispose

    fn MixerAudioContext::dispose(self : MixerAudioContext, id : PlayerId) -> Unit

    MixerAudioContext::dispose_outgoing_bgm

    fn MixerAudioContext::dispose_outgoing_bgm(self : MixerAudioContext) -> Unit

    Dispose the outgoing BGM player.

    MixerAudioContext::fade_bgm

    fn MixerAudioContext::fade_bgm(self : MixerAudioContext, target_volume : Double, duration_seconds : Double) -> Unit

    Start a volume fade on the BGM.

    MixerAudioContext::new

    fn MixerAudioContext::new(sample_rate : Int) -> MixerAudioContext

    MixerAudioContext::pause

    fn MixerAudioContext::pause(self : MixerAudioContext, id : PlayerId) -> Unit raise

    MixerAudioContext::pause_bgm

    fn MixerAudioContext::pause_bgm(self : MixerAudioContext) -> Unit

    Pause the current BGM.

    MixerAudioContext::play

    fn MixerAudioContext::play(self : MixerAudioContext, id : PlayerId) -> Unit raise

    MixerAudioContext::play_bgm

    fn MixerAudioContext::play_bgm(self : MixerAudioContext, clip : AudioClip, volume? : Double) -> Unit

    Play a BGM clip with looping. Stops any currently playing BGM.

    MixerAudioContext::render

    fn MixerAudioContext::render(self : MixerAudioContext, frames : Int, output : FixedArray[Float]) -> Unit

    Render audio frames into an output buffer.

    MixerAudioContext::resume_bgm

    fn MixerAudioContext::resume_bgm(self : MixerAudioContext) -> Unit

    Resume the current BGM.

    MixerAudioContext::seek

    fn MixerAudioContext::seek(self : MixerAudioContext, id : PlayerId, position : Double) -> Unit raise

    MixerAudioContext::set_bgm_volume

    fn MixerAudioContext::set_bgm_volume(self : MixerAudioContext, volume : Double) -> Unit

    Set BGM volume immediately.

    MixerAudioContext::set_pan

    fn MixerAudioContext::set_pan(self : MixerAudioContext, id : PlayerId, pan : Double) -> Unit

    MixerAudioContext::set_volume

    fn MixerAudioContext::set_volume(self : MixerAudioContext, id : PlayerId, volume : Double) -> Unit

    MixerAudioContext::state

    MixerAudioContext::stop

    fn MixerAudioContext::stop(self : MixerAudioContext, id : PlayerId) -> Unit raise

    MixerAudioContext::stop_bgm

    fn MixerAudioContext::stop_bgm(self : MixerAudioContext) -> Unit

    Stop and dispose the current BGM and any outgoing BGM.

    MixerAudioContext::tick_bgm

    fn MixerAudioContext::tick_bgm(self : MixerAudioContext, frames : Int) -> Unit

    Advance BGM fade by the given number of audio frames. Call this once per audio tick from the engine loop.

    PlayerId

    pub(all) struct PlayerId {
    value : Int
    } derive(
    Debug
    )

    impl Show for PlayerId

    PlayerId::output

    fn PlayerId::output(self : PlayerId, logger : &Logger) -> Unit

    PlayerId::to_repr

    PlayerId::to_string

    fn PlayerId::to_string(self : PlayerId) -> String

    PlayerState

    pub enum PlayerState {
    Stopped
    Playing
    Paused
    } derive(
    Debug
    )

    impl Show for PlayerState

    PlayerState::output

    fn PlayerState::output(self : PlayerState, logger : &Logger) -> Unit

    PlayerState::to_string

    fn PlayerState::to_string(self : PlayerState) -> String

    SimpleAudioContext

    pub struct SimpleAudioContext {
    players : Array[SimpleAudioPlayer]
    next_id : Int
    format : AudioFormat
    }

    SimpleAudioContext::create_player

    fn SimpleAudioContext::create_player(self : SimpleAudioContext, clip : AudioClip) -> PlayerId raise

    SimpleAudioContext::current_position

    fn SimpleAudioContext::current_position(self : SimpleAudioContext, id : PlayerId) -> Double

    SimpleAudioContext::dispose

    fn SimpleAudioContext::dispose(self : SimpleAudioContext, id : PlayerId) -> Unit

    SimpleAudioContext::new

    SimpleAudioContext::pause

    fn SimpleAudioContext::pause(self : SimpleAudioContext, id : PlayerId) -> Unit raise

    SimpleAudioContext::play

    fn SimpleAudioContext::play(self : SimpleAudioContext, id : PlayerId) -> Unit raise

    SimpleAudioContext::player_count

    fn SimpleAudioContext::player_count(self : SimpleAudioContext) -> Int

    SimpleAudioContext::seek

    fn SimpleAudioContext::seek(self : SimpleAudioContext, id : PlayerId, position : Double) -> Unit raise

    SimpleAudioContext::set_pan

    fn SimpleAudioContext::set_pan(_self : SimpleAudioContext, _id : PlayerId, _pan : Double) -> Unit

    SimpleAudioContext::set_volume

    fn SimpleAudioContext::set_volume(self : SimpleAudioContext, id : PlayerId, volume : Double) -> Unit

    SimpleAudioContext::state

    SimpleAudioContext::stop

    fn SimpleAudioContext::stop(self : SimpleAudioContext, id : PlayerId) -> Unit raise

    SimpleAudioContext::tick

    fn SimpleAudioContext::tick(self : SimpleAudioContext, samples : Int) -> Unit

    Advance all playing players by the given number of samples.

    SimpleAudioPlayer

    pub struct SimpleAudioPlayer {
    id : PlayerId
    clip : AudioClip
    state : PlayerState
    volume : Double
    position_samples : Int
    }

    SoundBank

    pub struct SoundBank[Key] {
    // private fields
    }

    Owns one non-overlapping voice per game-defined key in an existing mixer. The game retains responsibility for event mapping, asset loading and gains.

    SoundBank::clear

    fn[Key] SoundBank::clear(self : SoundBank[Key]) -> Unit

    SoundBank::is_muted

    fn[Key] SoundBank::is_muted(self : SoundBank[Key]) -> Bool

    SoundBank::is_paused

    fn[Key] SoundBank::is_paused(self : SoundBank[Key]) -> Bool

    SoundBank::loaded_count

    fn[Key] SoundBank::loaded_count(self : SoundBank[Key]) -> Int

    SoundBank::new

    fn[Key] SoundBank::new(context : MixerAudioContext) -> SoundBank[Key]

    SoundBank::play

    fn[Key : Eq] SoundBank::play(self : SoundBank[Key], cue : Key, restart? : Bool) -> CuePlayback raise

    Finish the previous cue by default. Explicit restart is useful for UI sounds.

    SoundBank::player

    fn[Key : Eq] SoundBank::player(self : SoundBank[Key], cue : Key) -> PlayerId?

    SoundBank::playing_count

    fn[Key] SoundBank::playing_count(self : SoundBank[Key]) -> Int

    SoundBank::register

    fn[Key : Eq] SoundBank::register(self : SoundBank[Key], cue : Key, clip : AudioClip, volume? : Double) -> Unit raise

    Replacing a cue disposes its previous voice, including an actively playing one.

    SoundBank::set_muted

    fn[Key] SoundBank::set_muted(self : SoundBank[Key], muted : Bool) -> Unit

    SoundBank::set_paused

    fn[Key] SoundBank::set_paused(self : SoundBank[Key], paused : Bool) -> Unit

    Freeze cue positions without changing the user's mute preference. Only voices playing at entry resume; replaced and muted cues stay stopped.

    audio_buffer_to_clip

    fn audio_buffer_to_clip(buffer :
    AudioBuffer
    , loop_ : Bool) -> AudioClip

    Convert a mizchi/audio AudioBuffer to an AudioClip. Converts Float samples to 16-bit PCM Bytes.

    audio_close

    fn audio_close() -> Unit

    audio_output_latency

    fn audio_output_latency() -> Double

    audio_render_and_write

    fn audio_render_and_write(mixer : MixerAudioContext, frames : Int) -> Int

    Render audio from a MixerAudioContext and write to output hooks. Returns the number of frames actually written.

    audio_resume

    fn audio_resume() -> Unit

    audio_suspend

    fn audio_suspend() -> Unit

    audio_try_initialize

    fn audio_try_initialize(format : AudioFormat) -> Bool

    audio_write_frames

    fn audio_write_frames(output : FixedArray[Float], frames : Int) -> Int

    bytes_per_sample

    fn bytes_per_sample(format : AudioFormat) -> Int

    bytes_to_samples

    fn bytes_to_samples(format : AudioFormat, byte_count : Int) -> Int

    cd_quality_format

    fn cd_quality_format() -> AudioFormat

    clip_duration

    fn clip_duration(clip : AudioClip) -> Double

    clip_sample_count

    fn clip_sample_count(clip : AudioClip) -> Int

    clip_to_audio_buffer

    fn clip_to_audio_buffer(clip : AudioClip) ->
    AudioBuffer

    Convert an AudioClip back to a mizchi/audio AudioBuffer. Assumes 16-bit PCM format.

    decode_audio_clip_auto

    fn decode_audio_clip_auto(data : Bytes, loop_ : Bool) -> AudioClip? raise
    AudioError

    Detect audio format from bytes and decode accordingly. Returns None if format is not recognized.

    decode_ogg_clip

    fn decode_ogg_clip(ogg_bytes : Bytes, loop_ : Bool) -> AudioClip raise
    AudioError

    Decode OGG/Vorbis bytes into an AudioClip.

    decode_wav_clip

    fn decode_wav_clip(wav_bytes : Bytes, loop_ : Bool) -> AudioClip raise
    AudioError

    Decode WAV bytes into an AudioClip.

    default_audio_format

    fn default_audio_format() -> AudioFormat

    default_audio_output_stats

    fn default_audio_output_stats() -> AudioOutputStats

    Create default AudioOutputStats.

    detect_audio_format

    fn detect_audio_format(data : Bytes) -> String

    Detect audio format from magic bytes. Returns "wav", "ogg", or empty string if unknown.

    duration_to_samples

    fn duration_to_samples(format : AudioFormat, seconds : Double) -> Int

    mono_format

    fn mono_format(sample_rate : Int) -> AudioFormat

    new_audio_clip

    fn new_audio_clip(format : AudioFormat, data : Bytes, loop_ : Bool) -> AudioClip

    new_audio_output_hooks

    fn new_audio_output_hooks(try_initialize : (AudioFormat) -> Bool, write_frames : (FixedArray[Float], Int) -> Int, suspend : () -> Unit, resume_playback : () -> Unit, close : () -> Unit) -> AudioOutputHooks

    new_audio_output_hooks_full

    fn new_audio_output_hooks_full(try_initialize : (AudioFormat) -> Bool, write_frames : (FixedArray[Float], Int) -> Int, suspend : () -> Unit, resume_playback : () -> Unit, close : () -> Unit, output_latency : () -> Double) -> AudioOutputHooks

    new_tracked_audio_output_hooks

    fn new_tracked_audio_output_hooks(inner : AudioOutputHooks, stats :
    Ref
    [AudioOutputStats]) -> AudioOutputHooks

    Create a tracked AudioOutputHooks wrapper that updates stats.

    pcm16_format

    fn pcm16_format(sample_rate : Int, channels : Int) -> AudioFormat

    reset_audio_output_hooks

    fn reset_audio_output_hooks() -> Unit

    samples_to_bytes

    fn samples_to_bytes(format : AudioFormat, sample_count : Int) -> Int

    samples_to_duration

    fn samples_to_duration(format : AudioFormat, sample_count : Int) -> Double

    set_audio_output_hooks

    fn set_audio_output_hooks(hooks : AudioOutputHooks) -> Unit

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io