Class: PlayAudio

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_gui_creator/play_audio.rb

Overview

only plays wav/pcm and midi

Constant Summary collapse

Type =
javax.sound.sampled.LineEvent::Type

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ PlayAudio

Returns a new instance of PlayAudio.



11
12
13
14
15
# File 'lib/simple_gui_creator/play_audio.rb', line 11

def initialize filename
  raise 'no filename?' unless filename
  @filename = filename
  @done = false
end

Instance Method Details

#join_finishObject



42
43
44
45
46
# File 'lib/simple_gui_creator/play_audio.rb', line 42

def join_finish
  while !@done
    sleep 0.01
  end
end

#loopObject



37
38
39
40
# File 'lib/simple_gui_creator/play_audio.rb', line 37

def loop
  warmup
  @clip.loop(Clip::LOOP_CONTINUOUSLY)
end

#play_till_endObject



48
49
50
51
# File 'lib/simple_gui_creator/play_audio.rb', line 48

def play_till_end
  start
	join_finish
end

#shutdownObject



53
54
55
56
# File 'lib/simple_gui_creator/play_audio.rb', line 53

def shutdown
  @clip.close
  @audioInputStream.close
end

#startObject Also known as: play_non_blocking



30
31
32
33
# File 'lib/simple_gui_creator/play_audio.rb', line 30

def start
  warmup
  @clip.start
end

#stopObject



58
59
60
61
# File 'lib/simple_gui_creator/play_audio.rb', line 58

def stop
  @done = true
  shutdown
end

#warmupObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/simple_gui_creator/play_audio.rb', line 17

def warmup
  @audioInputStream = AudioSystem.getAudioInputStream(java.io.File.new @filename)
  @clip = AudioSystem.getClip
  @done = false
  @clip.add_line_listener { |line_event|
      if (line_event.get_type == Type::STOP || line_event.get_type == Type::CLOSE)
        @done = true;
        shutdown
      end
  }
  @clip.open(@audioInputStream)
end