raudio.cr v0.0.0

raudio.cr

test Lines of Code

🔉 raudio - a simple audio library based on miniaudio - for Crystal

Installation

Add to shard.yml:

dependencies:
  raudio:
    github: kojix2/raudio.cr

Run shards install. The native library builds automatically.

Usage

Audio device

require "raudio"

Raudio::AudioDevice.open do
  Raudio::AudioDevice.master_volume = 0.8
end

Sound playback

Raudio::AudioDevice.open do
  sound = Raudio::Sound.load("effect.wav")
  sound.volume = 0.5
  sound.play

  while sound.playing?
    sleep 10.milliseconds
  end
end

Music streaming

Raudio::AudioDevice.init

music = Raudio::Music.load("background.mp3")
music.volume = 0.8
music.looping = false  # Disable looping (enabled by default)
music.play

loop do
  music.update
  break unless music.playing?
  sleep 10.milliseconds
end

Raudio::AudioDevice.close

Wave data

Raudio::AudioDevice.open do
  wave = Raudio::Wave.load("audio.wav")
  sound = Raudio::Sound.from_wave(wave)
  sound.play
  wave.export("output.wav")
end

Resource Management

Resources should be explicitly released when done, or use the block form for automatic cleanup.

Recommended patterns:

# Block form (automatic cleanup)
Raudio::Sound.load("effect.wav") do |sound|
  sound.play
end

# Manual release
sound = Raudio::Sound.load("effect.wav")
begin
  sound.play
ensure
  sound.release  # or sound.close
end

Finalizers are provided as a fallback but explicit cleanup is recommended.

Playback model (practical)

  • Non-blocking: Sound#play / Music#play return immediately. Mixing/output is done on a separate audio thread (miniaudio).
  • Streaming: Call Music#update regularly (e.g. once per frame/tick) to refill the buffer and avoid underruns/stutter.
  • Threading: The library synchronizes internally, but treat the public API as single-threaded. Prefer calling all raudio APIs from one thread (usually the main thread). If you call from multiple threads/fibers, serialize at a higher level.
  • Device lifetime: After AudioDevice.init (or inside AudioDevice.open), the device keeps running until AudioDevice.close.

Supported formats

WAV, OGG, MP3, FLAC, QOA, XM, MOD

API

  • Raudio::AudioDevice - audio device management
  • Raudio::Sound - short audio clips
  • Raudio::Music - streaming audio
  • Raudio::Wave - raw waveform data
  • Raudio::AudioStream - custom streaming

Low-level C bindings: Raudio::LibRaudio

API Documentation

Development

git clone --recursive https://github.com/kojix2/raudio.cr # submodule
make -C ext        # build native library
crystal spec       # run tests
crystal build examples/simple_sound.cr
./simple_sound     # one of the two sounds will play

License

MIT

Dependencies (bundled single-header/native libraries):

  • raudio (C library) – zlib
    • miniaudio – Public Domain or MIT-0
    • dr_wav – Public Domain or MIT-0
    • dr_mp3 – Public Domain or MIT-0
    • dr_flac – Public Domain or MIT-0
    • stb_vorbis – Public Domain or MIT
    • qoa – MIT
    • jar_xm – WTFPL v2 – (included in source)
    • jar_mod – WTFPL v2 – (included in source)

Summary: Project code is MIT, bundled raudio is zlib, and all other bundled dependencies are permissive (Public Domain, MIT-0, MIT, WTFPL v2) with no copyleft obligations.

Repository

raudio.cr

Owner
Statistic
  • 1
  • 0
  • 0
  • 0
  • 0
  • 7 days ago
  • October 4, 2025
License

MIT License

Links
Synced at

Sun, 19 Oct 2025 05:25:23 GMT

Languages