Method: Hallon::ExampleAudioDriver#stream

Defined in:
lib/hallon/audio_driver.rb

#stream {|num_frames| ... } ⇒ Object

Called once by the player, to initiate audio streaming to this driver. This method is expected to run indefinitely, and is run inside a separate thread.

It is given a block that takes an integer as an argument, which specifies how many audio frames the player may give the driver for audio playback. If the block is given no arguments, the audio driver is expected to be able to consume any number of audio frames for the given call.

When the audio driver is ready to consume audio, it should yield to the given block. If it can take only a finite number of audio frames it should be specified in the parameter.

Upon yielding to the given block, the player will:

  • if the player is currently not playing, wait until it is
  • inspect the format of the audio driver
  • if the format has changed, set the new format on the driver and return nil
  • if the format has not changed, return an array of audio frames

The number of frames returned upon yielding will be less than or equal to the number of frames requested when calling yield.

The format of the audio frames can be determined by inspecting #format once the yield has returned. It is safe to inspect this format at any point within this method.

The audio frames is a ruby array, grouped by channels. So for 2-channeled audio the returned array from a yield will look similar to this:

[[1239857, -123087], [34971, 123084], ]

Also see the implementation for this method on a more concise explanation.

Yields:

  • (num_frames)

    to retrieve audio frames for playback buffering

Yield Parameters:

  • num_frames (Integer)

    maximum number of frames that should be returned

Yield Returns:

  • (Array<[]>, nil)

    an array of audio frames, or nil if audio format has changed



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hallon/audio_driver.rb', line 121

def stream
  loop do
    # set up internal buffers for current @format
    loop do
      # calculate size of internal buffers
      audio_data = yield(4048) # can only take 4048 frames of 2-channeled int16ne data

      if audio_data.nil?
        # audio format has changed, reinitialize buffers
        break
      else
        # playback the audio data
      end
    end
  end
end