Class: AudioStream::AudioInputFile

Inherits:
Object
  • Object
show all
Includes:
AudioInput
Defined in:
lib/audio_stream/audio_input_file.rb

Instance Attribute Summary collapse

Attributes included from AudioInput

#connection

Instance Method Summary collapse

Methods included from AudioInput

buffer, device, file, #publish, #published?, #sync

Methods included from AudioObservable

#fx, #mono, #notify_complete, #notify_next, #send_to, #stereo, #subscribe, #subscribe_on_next

Constructor Details

#initialize(path, soundinfo:) ⇒ AudioInputFile

Returns a new instance of AudioInputFile.



7
8
9
10
# File 'lib/audio_stream/audio_input_file.rb', line 7

def initialize(path, soundinfo:)
  @path = path
  @soundinfo = soundinfo
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/audio_stream/audio_input_file.rb', line 5

def path
  @path
end

Instance Method Details

#connectObject



12
13
14
15
16
17
# File 'lib/audio_stream/audio_input_file.rb', line 12

def connect
  if !connected?
    @sound = RubyAudio::Sound.open(@path)
  end
  self
end

#connected?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/audio_stream/audio_input_file.rb', line 26

def connected?
  @sound && !@sound.closed?
end

#disconnectObject



19
20
21
22
23
24
# File 'lib/audio_stream/audio_input_file.rb', line 19

def disconnect
  if connected?
    @sound.close
  end
  super
end

#each(&block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/audio_stream/audio_input_file.rb', line 39

def each(&block)
  Enumerator.new do |y|
    if !connected?
      raise Error, "File is not opened. You need to exec #{self.class.name}.connect: #{@path}"
    end

    rabuf = RubyAudio::Buffer.float(@soundinfo.window_size, @soundinfo.channels)
    while @sound.read(rabuf)!=0
      y << Buffer.from_rabuffer(rabuf)
    end
  end.each(&block)
end

#seek(frames, whence = IO::SEEK_SET) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/audio_stream/audio_input_file.rb', line 30

def seek(frames, whence=IO::SEEK_SET)
  if !connected?
    raise Error, "File is not opened. You need to exec #{self.class.name}.connect: #{@path}"
  end

  @sound.seek(frames, whence)
  self
end