Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-audio

Source of release 0.4.1

0.4.1 / src / audio.lucb

## Short PCM playback. macOS plays 8-bit mono through NSSound; other hosts fail.
import c
import memory
import platform
import macos
import result

pub let unsupported: ErrorCode = result.unsupported
pub let invalid: ErrorCode = result.invalid
pub let failed: ErrorCode = result.failed

extern func luce_imf_pcm_bytes(imf: const u8*, imf_len: i32, rate: i32) -> i32
extern func luce_imf_render(imf: const u8*, imf_len: i32, rate: i32, dest: const u8*, dest_len: i32) -> i32

## A process-side mixer that can overlap a few one-shot clips plus one looping song.
pub struct Player:
    var host: macos.Host

    pub func close():
        self.host.close()

    ## Play unsigned 8-bit mono PCM at `rate` Hz.
    pub func play_pcm8(samples: const u8[], rate: i64) -> !:
        if samples.length == 0 or rate < 1 or rate > 192000:
            error(invalid, "PCM must be nonempty 8-bit mono at 1..192000 Hz")
        if not platform.macos:
            error(unsupported, "luce-audio plays PCM on macOS")
        try macos.play(&self.host, samples, rate, 8)

    ## Play little-endian signed 16-bit mono PCM at `rate` Hz.
    pub func play_pcm16(samples: const u8[], rate: i64) -> !:
        if samples.length < 2 or samples.length % 2 != 0 or rate < 1 or rate > 192000:
            error(invalid, "PCM must be nonempty 16-bit mono at 1..192000 Hz")
        if not platform.macos:
            error(unsupported, "luce-audio plays PCM on macOS")
        try macos.play(&self.host, samples, rate, 16)

    ## Loop little-endian signed 16-bit mono PCM (AdLib music).
    pub func play_pcm16_loop(samples: const u8[], rate: i64) -> !:
        if samples.length < 2 or samples.length % 2 != 0 or rate < 1 or rate > 192000:
            error(invalid, "PCM must be nonempty 16-bit mono at 1..192000 Hz")
        if not platform.macos:
            error(unsupported, "luce-audio plays PCM on macOS")
        try macos.play_loop(&self.host, samples, rate)

    pub func stop_loop():
        macos.stop_loop(&self.host)

## Render one loop of Wolf3D IMF (AdLib) to signed 16-bit mono PCM.
pub func render_imf(imf: const u8[], rate: i64) -> u8[]!:
    if imf.length < 4 or rate < 1000 or rate > 48000:
        error(invalid, "IMF is truncated or the rate is out of range")
    let bytes = (i64)luce_imf_pcm_bytes(imf.data, (i32)imf.length, (i32)rate)
    if bytes < 2:
        error(failed, "IMF produced no PCM")
    var pcm = try new u8[(usize)bytes] --- in memory.heap
    errdefer free(pcm) in memory.heap
    let wrote = luce_imf_render(imf.data, (i32)imf.length, (i32)rate, pcm.data, (i32)bytes)
    if wrote < 2:
        error(failed, "IMF render failed")
    if wrote < bytes:
        return pcm[0..<(usize)wrote]
    return pcm

test "an IMF song renders to one loop of 16-bit PCM":
    # a type-1 song of 8 bytes: two register writes, 350 ticks each, one second at 700 Hz
    let song: u8[10] = [0x08, 0x00, 0x20, 0x01, 0x5e, 0x01, 0x40, 0x10, 0x5e, 0x01]
    let pcm = try render_imf(song, 22050)
    defer free(pcm) in memory.heap
    # whole samples per tick: 31 at 22050 Hz, so 700 ticks are 21700 samples
    assert(pcm.length == 43400)

## Whether `render_imf` refuses `imf` at `rate` as invalid.
func refuses(imf: const u8[], rate: i64) -> bool:
    let pcm = render_imf(imf, rate) catch failure:
        return failure.code == invalid
    free(pcm) in memory.heap
    return false

test "a truncated IMF song or a rate out of range is refused":
    let short: u8[3] = [0, 0, 0]
    let song: u8[4] = [0x20, 0x01, 0x10, 0x00]
    assert(refuses(short, 22050) and refuses(song, 100) and refuses(song, 96000))

test "PCM that is empty, odd or at a bad rate is refused before anything plays":
    var player: Player
    let odd: u8[3] = [1, 2, 3]
    var refused = 0
    player.play_pcm8(odd[0..<0], 8000) catch failure:
        if failure.code == invalid: refused += 1
    player.play_pcm16(odd, 8000) catch failure:
        if failure.code == invalid: refused += 1
    player.play_pcm16_loop(odd, 0) catch failure:
        if failure.code == invalid: refused += 1
    assert(refused == 3)