module Eagle::Audio

Overview

The mixer: global volume, named buses, and the list of playing voices.

Eagle mixes audio in Crystal and sends it to the device, SDL on desktop and WebAudio in the browser. Browsers only allow sound after the first click or key press.

Buses group voices so players can set music and effects volume separately:

Audio.bus("music").volume = 0.5
Audio.bus("sfx").muted = true
Audio.volume = 0.8 # master volume, applies to everything
Audio.stop_all

Defined in:

eagle/audio/audio.cr

Constant Summary

SAMPLE_RATE = 48000

Output sample rate requested from the device.

TARGET_LATENCY = 0.06

Seconds of audio kept queued ahead of the device. Lower means more responsive sound but a higher risk of crackles.

Class Method Summary

Class Method Detail

def self.add_stream(s : AudioStream) : AudioStream #

Starts mixing a stream. Returns it.


def self.bus(name : String) : Bus #

The bus called name, created on first use. The default bus is "master".


def self.enabled? : Bool #

True when an audio device is open.


def self.init(platform : Platform::Base) : Nil #

Opens the audio device. Eagle.run calls this unless Config#audio is false.


def self.master : Bus #

The master bus.


def self.max_voices=(n : Int32) #

Caps simultaneous voices (64 by default). When full, the oldest non-looping voice is cut off, so rapid-fire effects can't drown out music.


def self.play(sound : Sound, volume : Number = 1, pitch : Number = 1, pan : Number = 0, loop : Bool = false, bus : String = "master") : Voice #

Plays a sound and returns its voice. Same as Sound#play.


def self.remove_stream(s : AudioStream) : Nil #

Stops mixing a stream.


def self.render(frames : Int32) : Slice(Float32) #

Mixes frames stereo frames and returns them, without touching the device. Used by tests.


def self.sample_rate : Int32 #

The actual output sample rate.


def self.shutdown : Nil #

Closes the audio device.


def self.stop_all : Nil #

Stops every voice and stream.


def self.voice_count : Int32 #

Number of voices currently playing.


def self.voices : Array(Voice) #

Every voice currently playing.


def self.volume : Float32 #

Master volume.


def self.volume=(v : Number) #

Sets the master volume.