Class: AudioPlayback::Playback::Action

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/audio-playback/playback.rb

Overview

Action of playing back an audio file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sounds, output, options = {}) ⇒ Action

Returns a new instance of Action.

Parameters:

  • sounds (Array<Sound>, Sound)
  • output (Output)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :buffer_size (Integer)
  • :channels (Array<Integer>, Integer) — default: or: :channel
  • :duration (Numeric)

    Play for given time in seconds

  • :end_position (Numeric)

    Stop at given time position in seconds (will use :duration if both are included)

  • :is_looping (Boolean)

    Whether to loop audio

  • :logger (IO)
  • :seek (Numeric)

    Start at given time position in seconds

  • :stream (Stream)


52
53
54
55
56
57
58
59
# File 'lib/audio-playback/playback.rb', line 52

def initialize(sounds, output, options = {})
  @sounds = Array(sounds)
  @buffer_size = options[:buffer_size] || DEFAULT[:buffer_size]
  @output = output
  @stream = options[:stream] || Device::Stream.new(@output, options)
  populate(options)
  report(options[:logger]) if options[:logger]
end

Instance Attribute Details

#buffer_sizeObject (readonly)

Returns the value of attribute buffer_size.



29
30
31
# File 'lib/audio-playback/playback.rb', line 29

def buffer_size
  @buffer_size
end

#channelsObject (readonly)

Returns the value of attribute channels.



29
30
31
# File 'lib/audio-playback/playback.rb', line 29

def channels
  @channels
end

#dataObject (readonly)

Returns the value of attribute data.



29
30
31
# File 'lib/audio-playback/playback.rb', line 29

def data
  @data
end

#num_channelsObject (readonly)

Returns the value of attribute num_channels.



29
30
31
# File 'lib/audio-playback/playback.rb', line 29

def num_channels
  @num_channels
end

#outputObject (readonly)

Returns the value of attribute output.



29
30
31
# File 'lib/audio-playback/playback.rb', line 29

def output
  @output
end

#soundsObject (readonly)

Returns the value of attribute sounds.



29
30
31
# File 'lib/audio-playback/playback.rb', line 29

def sounds
  @sounds
end

#streamObject (readonly)

Returns the value of attribute stream.



29
30
31
# File 'lib/audio-playback/playback.rb', line 29

def stream
  @stream
end

#truncateObject (readonly)

Returns the value of attribute truncate.



29
30
31
# File 'lib/audio-playback/playback.rb', line 29

def truncate
  @truncate
end

Instance Method Details

#blockStream

Block process until playback finishes

Returns:

  • (Stream)


77
78
79
# File 'lib/audio-playback/playback.rb', line 77

def block
  @stream.block
end

#channels_requested?Boolean

Has a different channel configuration than the default been requested?

Returns:

  • (Boolean)


110
111
112
# File 'lib/audio-playback/playback.rb', line 110

def channels_requested?
  !@channels.nil?
end

#data_sizeInteger

Total size of the playback’s sound frames in bytes

Returns:

  • (Integer)


103
104
105
106
# File 'lib/audio-playback/playback.rb', line 103

def data_size
  frames = size * @num_channels
  frames * FRAME_SIZE.size
end

#looping?Boolean

Is audio looping ?

Returns:

  • (Boolean)


116
117
118
# File 'lib/audio-playback/playback.rb', line 116

def looping?
  @is_looping
end

#report(logger) ⇒ Boolean

Log a report about playback

Parameters:

  • logger (IO)

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
# File 'lib/audio-playback/playback.rb', line 91

def report(logger)
  paths = @sounds.map(&:audio_file).map(&:path)
  logger.puts("Playback report for #{paths}")
  logger.puts("  Number of channels: #{@num_channels}")
  logger.puts("  Direct audio to channels #{@channels.to_s}") unless @channels.nil?
  logger.puts("  Buffer size: #{@buffer_size}")
  logger.puts("  Latency: #{@output.latency}")
  true
end

#sample_rateInteger

Sample rate of the playback sound

Returns:

  • (Integer)


63
64
65
# File 'lib/audio-playback/playback.rb', line 63

def sample_rate
  @sounds.last.sample_rate
end

#startPlayback Also known as: play

Start playback

Returns:



69
70
71
72
# File 'lib/audio-playback/playback.rb', line 69

def start
  @stream.play(self)
  self
end

#truncate?Boolean

Should playback be truncated?

eg :start 3 seconds, :duration 1 second

Returns:

  • (Boolean)


84
85
86
# File 'lib/audio-playback/playback.rb', line 84

def truncate?
  !@truncate.nil? && !@truncate.values.empty?
end