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 (Fixnum)
  • :channels (Array<Fixnum>, Fixnum) — default: or: :channel
  • :logger (IO)
  • :stream (Stream)


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

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.



23
24
25
# File 'lib/audio-playback/playback.rb', line 23

def buffer_size
  @buffer_size
end

#channelsObject (readonly)

Returns the value of attribute channels.



23
24
25
# File 'lib/audio-playback/playback.rb', line 23

def channels
  @channels
end

#dataObject (readonly)

Returns the value of attribute data.



23
24
25
# File 'lib/audio-playback/playback.rb', line 23

def data
  @data
end

#num_channelsObject (readonly)

Returns the value of attribute num_channels.



23
24
25
# File 'lib/audio-playback/playback.rb', line 23

def num_channels
  @num_channels
end

#outputObject (readonly)

Returns the value of attribute output.



23
24
25
# File 'lib/audio-playback/playback.rb', line 23

def output
  @output
end

#soundsObject (readonly)

Returns the value of attribute sounds.



23
24
25
# File 'lib/audio-playback/playback.rb', line 23

def sounds
  @sounds
end

#streamObject (readonly)

Returns the value of attribute stream.



23
24
25
# File 'lib/audio-playback/playback.rb', line 23

def stream
  @stream
end

Instance Method Details

#blockStream

Block process until playback finishes

Returns:

  • (Stream)


65
66
67
# File 'lib/audio-playback/playback.rb', line 65

def block
  @stream.block
end

#channels_requested?Boolean

Has a different channel configuration than the default been requested?

Returns:

  • (Boolean)


91
92
93
# File 'lib/audio-playback/playback.rb', line 91

def channels_requested?
  !@channels.nil?
end

#data_sizeFixnum

Total size of the playback’s sound frames in bytes

Returns:

  • (Fixnum)


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

def data_size
  frames = (size * @num_channels) + METADATA.count
  frames * FRAME_SIZE.size
end

#report(logger) ⇒ Boolean

Log a report about playback

Parameters:

  • logger (IO)

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'lib/audio-playback/playback.rb', line 72

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_rateFixnum

Sample rate of the playback sound

Returns:

  • (Fixnum)


45
46
47
# File 'lib/audio-playback/playback.rb', line 45

def sample_rate
  @sounds.last.sample_rate
end

#sizeFixnum

Size of the playback sound

Returns:

  • (Fixnum)


51
52
53
# File 'lib/audio-playback/playback.rb', line 51

def size
  @sounds.map(&:size).max
end

#startPlayback Also known as: play

Start playback

Returns:



57
58
59
60
# File 'lib/audio-playback/playback.rb', line 57

def start
  @stream.play(self)
  self
end