Class: EasyAudio::Stream

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

Overview

Represents a single audio input/output stream. See #initialize for usage examples.

Direct Known Subclasses

EasyStream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|buffer| ... } ⇒ Stream

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

Examples:

Process audio from input (microphone) and playback on output

EasyAudio::Stream.new(in: true, out: true) do |buffer|
  buffer.samples # echos the stream to output
end

Parameters:

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

    a customizable set of options

Options Hash (opts):

  • :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:

  • (buffer)

    runs the provided block against the sample buffer data

Yield Parameters:

Yield Returns:

  • (Array<Float>)

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

See Also:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/easy_audio.rb', line 44

def initialize(opts = {}, &block)
  pa_start

  @fn = block
  @sample_rate = opts[:sample_rate] || 44100
  @frame_size = opts[:frame_size] || 256
  @input_channels = opts[:in_chans] || 1
  @output_channels = opts[:out_chans] || 1
  @latency = opts[:latency] || 0.01

  input, output = nil, nil
  if opts[:in] || opts[:in_chans]
    device = API.Pa_GetDefaultInputDevice
    input = stream_for(device, @input_channels, @latency)
  end

  if opts[:out] || opts[:out_chans] || !input
    device = API.Pa_GetDefaultOutputDevice
    output = stream_for(device, @output_channels, @latency)
  end

  open(input, output, @sample_rate, @frame_size)
end

Instance Attribute Details

#fnObject

Returns the value of attribute fn.



68
69
70
# File 'lib/easy_audio.rb', line 68

def fn
  @fn
end

#frame_sizeObject

Returns the value of attribute frame_size.



68
69
70
# File 'lib/easy_audio.rb', line 68

def frame_size
  @frame_size
end

#input_channelsObject (readonly)

Returns the value of attribute input_channels.



69
70
71
# File 'lib/easy_audio.rb', line 69

def input_channels
  @input_channels
end

#latencyObject (readonly)

Returns the value of attribute latency.



69
70
71
# File 'lib/easy_audio.rb', line 69

def latency
  @latency
end

#output_channelsObject (readonly)

Returns the value of attribute output_channels.



69
70
71
# File 'lib/easy_audio.rb', line 69

def output_channels
  @output_channels
end

#sample_rateObject

Returns the value of attribute sample_rate.



68
69
70
# File 'lib/easy_audio.rb', line 68

def sample_rate
  @sample_rate
end

Instance Method Details

#startObject

Starts processing the stream.



# File 'lib/easy_audio.rb', line 17

#stopObject

Stops processing the stream.



# File 'lib/easy_audio.rb', line 20