Class: EasyAudio::EasyStream

Inherits:
Stream
  • Object
show all
Defined in:
lib/easy_audio.rb

Overview

A simplified Stream class whose processor block only processes a single frame at a time. See Waveforms for a set of pre-fabricated EasyStream processor blocks for examples of how to process a single stream.

Note that instead of passing state information as an argument to the block, state is instead stored in the class itself, and the block is instance evaluated. This makes it a bit slower to process, but much more convenient for creating blocks.

See #initialize for usage examples.

Instance Attribute Summary collapse

Attributes inherited from Stream

#fn, #frame_size, #input_channels, #latency, #output_channels, #sample_rate

Instance Method Summary collapse

Methods inherited from Stream

#start, #stop

Constructor Details

#initialize(opts = {}) { ... } ⇒ EasyStream

Creates a new stream for processing audio. Call Stream#start to start processing.

Examples:

Process audio from input (microphone) and playback on output

EasyAudio::EasyStream.new(in: true, out: true) { current_sample }.start

Play a sine wave.

EasyAudio::EasyStream.new(&EasyAudio::Waveforms::SINE).start

Play a square wave.

EasyAudio::EasyStream.new(&EasyAudio::Waveforms::SQUARE).start

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :freq (Float) — default: 440.0

    the frequency to generate #step values at.

  • :amp (Float) — default: 1.0

    the amplitude to scale values to.

  • :sample_rate (Fixnum) — default: 44100

    the sample rate to play at.

  • :frame_size (Fixnum) — default: 256

    the number of frames per buffer.

  • :in (Boolean)

    whether to use the default input device.

  • :out (Boolean)

    whether to use the default output device.

  • :in_chans (Fixnum) — default: 2

    the number of channels to process from the input device.

  • :out_chans (Fixnum) — default: 2

    the number of channels to process from the output device.

  • :latency (Float) — default: 0.0

    the default latency for processing.

Yields:

  • a process block that processes one frame at a time.

Yield Returns:

  • (Array<Float>)

    return an array of interlaced floating points for each channel in Stream#output_channels.



146
147
148
149
150
151
152
153
# File 'lib/easy_audio.rb', line 146

def initialize(opts = {}, &block)
  @frequency = opts[:freq] || 440.0
  @amp = opts[:amp] || 1.0
  @frame = 0
  @channel = 0

  super(opts, &block)
end

Instance Attribute Details

#ampObject

Returns the value of attribute amp.



155
156
157
# File 'lib/easy_audio.rb', line 155

def amp
  @amp
end

#channelObject (readonly)

Returns the value of attribute channel.



156
157
158
# File 'lib/easy_audio.rb', line 156

def channel
  @channel
end

#current_sampleObject (readonly)

Returns the value of attribute current_sample.



157
158
159
# File 'lib/easy_audio.rb', line 157

def current_sample
  @current_sample
end

#frameObject

Returns the value of attribute frame.



155
156
157
# File 'lib/easy_audio.rb', line 155

def frame
  @frame
end

#frequencyObject

Returns the value of attribute frequency.



155
156
157
# File 'lib/easy_audio.rb', line 155

def frequency
  @frequency
end

#iObject (readonly)

Returns the value of attribute i.



157
158
159
# File 'lib/easy_audio.rb', line 157

def i
  @i
end

#num_framesObject (readonly)

Returns the value of attribute num_frames.



156
157
158
# File 'lib/easy_audio.rb', line 156

def num_frames
  @num_frames
end

#samplesObject (readonly)

Returns the value of attribute samples.



156
157
158
# File 'lib/easy_audio.rb', line 156

def samples
  @samples
end

#status_infoObject (readonly)

Returns the value of attribute status_info.



157
158
159
# File 'lib/easy_audio.rb', line 157

def status_info
  @status_info
end

#stepObject (readonly)

Returns the value of attribute step.



156
157
158
# File 'lib/easy_audio.rb', line 156

def step
  @step
end

#time_infoObject (readonly)

Returns the value of attribute time_info.



157
158
159
# File 'lib/easy_audio.rb', line 157

def time_info
  @time_info
end

#user_dataObject (readonly)

Returns the value of attribute user_data.



157
158
159
# File 'lib/easy_audio.rb', line 157

def user_data
  @user_data
end