abstract class Eagle::AudioStream

Overview

Audio you generate on the fly, a block of samples at a time: synthesizers, engine hum, procedural music.

Subclass it, implement #fill, and register it with Audio.add_stream.

class Hum < AudioStream
  @phase = 0.0

  def fill(buf : Slice(Float32), frames : Int32, sample_rate : Int32) : Nil
    frames.times do |i|
      s = (Math.sin(@phase) * 0.2).to_f32
      buf[i * 2] = s     # left
      buf[i * 2 + 1] = s # right
      @phase += Math::TAU * 60 / sample_rate
    end
  end
end

Audio.add_stream(Hum.new)

Defined in:

eagle/audio/audio.cr

Instance Method Summary

Instance Method Detail

abstract def fill(buf : Slice(Float32), frames : Int32, sample_rate : Int32) : Nil #

Writes frames stereo frames of interleaved samples into buf. It runs on the main thread once per frame, so keep it fast.


def playing? : Bool #

False after #stop. The mixer then drops the stream.


def stop : Nil #

Stops the stream.


def volume : Float32 #

Volume applied to what #fill writes.


def volume=(volume : Float32) #

Volume applied to what #fill writes.