Class: AudioPlayback::Device::Stream

Inherits:
FFI::PortAudio::Stream
  • Object
show all
Defined in:
lib/audio-playback/device/stream.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, options = {}) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • output (Output)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • logger (IO)


19
20
21
22
23
24
25
26
27
# File 'lib/audio-playback/device/stream.rb', line 19

def initialize(output, options = {})
  @is_playing = false
  @is_muted = false
  @gain = 1.0
  @input = nil
  @output = output.resource
  initialize_exit_callback(:logger => options[:logger])
  Stream.streams << self
end

Instance Attribute Details

#is_playingObject (readonly) Also known as: playing?

Returns the value of attribute is_playing.



7
8
9
# File 'lib/audio-playback/device/stream.rb', line 7

def is_playing
  @is_playing
end

Class Method Details

.streamsArray<Stream>

Keep track of all streams

Returns:



12
13
14
# File 'lib/audio-playback/device/stream.rb', line 12

def self.streams
  @streams ||= []
end

Instance Method Details

#active?Boolean

Is the stream active?

Returns:

  • (Boolean)


48
49
50
# File 'lib/audio-playback/device/stream.rb', line 48

def active?
  FFI::PortAudio::API.Pa_IsStreamActive(@stream.read_pointer) == 1
end

#blockBoolean

Block process until the current playback finishes

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/audio-playback/device/stream.rb', line 54

def block
  begin
    while active?
      sleep(0.001)
    end
    while FFI::PortAudio::API.Pa_IsStreamActive(@stream.read_pointer) != :paNoError
      sleep(0.1)
    end
  rescue SystemExit, Interrupt
    # Control-C
    @is_playing = false
    exit
  end
  @is_playing = false
  true
end

#play(playback, options = {}) ⇒ Stream

Perform the given playback

Parameters:

  • playback (Playback)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • logger (IO)

Returns:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/audio-playback/device/stream.rb', line 34

def play(playback, options = {})
  @is_playing = true
  report(playback, options[:logger]) if options[:logger]
  if @stream.nil?
    open_playback(playback)
  else
    continue(playback)
  end
  start
  self
end