moon_cpal

    A MoonBit port (subset) of RustAudio/cpal.

    audio
    cpal
    port
    Download zip
    Version
    0.11.8
    License
    Apache-2.0
    Last updated
    13 days ago
    Downloads
    342

    #moon_cpal

    CPAL-like audio I/O for MoonBit (native-only).

    #Install

    Add dependency in moon.mod:

    { "deps": { "moonbit-community/moon_cpal": "0.11.6" } }

    moon_cpal supports native target only.

    #Quick Start (Typed Output Stream)

    let host = @moon_cpal.default_host()
    let device = match host.default_output_device() {
    Some(d) => d
    None => fail!("no output device")
    }
    let cfg = try! device.default_output_config()
    let stream_cfg = cfg.config()
    let stream = try! device.build_output_stream_f32(
    stream_cfg,
    fn(samples, _info) {
    for i = 0; i < samples.length(); i = i + 1 {
    samples[i] = 0.0
    }
    },
    fn(err) { println("stream error: \{err}") },
    None,
    )
    try! stream.play()

    #Quick Start (Raw Output Stream)

    Use build_output_stream_raw when you need format-specific writes:

    ///|
    let stream = try! device.build_output_stream_raw(
    stream_cfg,
    cfg.sample_format(),
    fn(data, _info) {
    match data.sample_format() {
    @moon_cpal.SampleFormat::F32 =>
    ignore(data.write_f32(Array::make(data.len(), 0.0)))
    @moon_cpal.SampleFormat::I16 =>
    ignore(data.write_i16(Array::make(data.len(), 0)))
    @moon_cpal.SampleFormat::I24 =>
    ignore(data.write_i24(Array::make(data.len(), @moon_cpal.I24::new(0))))
    _ => data.clear()
    }
    },
    fn(_err) { },
    None,
    )

    Data exposes full numeric write APIs: write_i8, write_u8, write_u16, write_i16, write_u24, write_i24, write_u32, write_i32, write_f32, write_u64, write_i64, write_f64.

    #Notes

    • Use stream.pause() / stream.close() to control lifecycle.
    • Use default_input_device() + build_input_stream_* for capture.

    BufferSize

    The buffer size requests the callback size for audio streams.

    Mirrors upstream cpal::BufferSize:
    • Default uses the host's default callback size.
    • Fixed(n) requests approximately n frames per callback.

    The actual callback size depends on the backend/host implementation and may differ from or vary around the requested size.

    BuildStreamError

    Error that can happen when creating a stream.

    ChannelCount

    type ChannelCount = Int

    DefaultStreamConfigError

    May occur when attempting to request the default input or output stream config from a device.

    DeviceDescriptionBuilder

    Builder for constructing a DeviceDescription.

    This is primarily intended for backend implementations to gradually build up descriptions with whatever metadata is available.

    DeviceNameError

    An error that may occur while attempting to retrieve a device name.

    DeviceTrait

    CPAL-like trait surface (from traits.rs in upstream).

    Devices

    Iterator types (upstream exports named iterator structs; here we expose Iter aliases).

    DevicesError

    An error that might occur while attempting to enumerate devices.

    DevicesFiltered

    using @moonbitlang/core/builtin { type Iter as DevicesFiltered }

    Iterator of devices wrapped in a filter (upstream: DevicesFiltered).

    Duration

    A minimal duration type used by the ported StreamInstant arithmetic.

    Upstream uses std::time::Duration (non-negative). Here we keep the same invariant.

    FrameCount

    type FrameCount = Int

    FromSample

    Sample and conversion traits/types (from samples_formats.rs in upstream).

    HostTrait

    CPAL-like trait surface (from traits.rs in upstream).

    InputCallbackInfo

    Information relevant to a single call to the user's input stream data callback.

    InputDevices

    using @moonbitlang/core/builtin { type Iter as InputDevices }

    A host's device iterator yielding only input devices (upstream: InputDevices).

    InputStreamTimestamp

    A timestamp associated with a call to an input stream's data callback.

    OutputCallbackInfo

    Information relevant to a single call to the user's output stream data callback.

    OutputDevices

    using @moonbitlang/core/builtin { type Iter as OutputDevices }

    A host's device iterator yielding only output devices (upstream: OutputDevices).

    OutputStreamTimestamp

    A timestamp associated with a call to an output stream's data callback.

    Sample

    Sample and conversion traits/types (from samples_formats.rs in upstream).

    SampleRate

    type SampleRate = Int

    SizedSample

    Sample and conversion traits/types (from samples_formats.rs in upstream).

    StreamInstant

    Monotonic timestamp associated with a stream.

    StreamTrait

    CPAL-like trait surface (from traits.rs in upstream).

    SupportedInputConfigs

    External iterator type. Iterator[X] is a mutable type: iterators internally maintain mutable state to advance iteration. All read operations on Iterator will advance the iterator, and would give different result when called multiple times.

    SupportedOutputConfigs

    External iterator type. Iterator[X] is a mutable type: iterators internally maintain mutable state to advance iteration. All read operations on Iterator will advance the iterator, and would give different result when called multiple times.

    SupportedStreamConfig

    A single supported stream configuration (as opposed to a range).

    Mirrors cpal's SupportedStreamConfig shape used by default_*_config.

    SupportedStreamConfigsError

    Error that can happen when enumerating the list of supported configs.

    available_hosts

    common_sample_rates

    fn common_sample_rates() -> Array[Int]

    Re-export of CPAL common sample rates table.

    default_host

    sample_cast

    Re-export of upstream-like sample conversion helper.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io