Class: Shoes::Swt::Sound

Inherits:
Object
  • Object
show all
Defined in:
shoes-swt/lib/shoes/swt/sound.rb

Constant Summary collapse

JFile =
java.io.File
BUFFER_SIZE =
4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsl, _app) ⇒ Sound

Returns a new instance of Sound.



24
25
26
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 24

def initialize(dsl, _app)
  @dsl = dsl
end

Instance Attribute Details

#audio_formatObject

Returns the value of attribute audio_format.



32
33
34
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 32

def audio_format
  @audio_format
end

#audio_input_streamObject

Returns the value of attribute audio_input_stream.



32
33
34
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 32

def audio_input_stream
  @audio_input_stream
end

#mixer_channelObject

Returns the value of attribute mixer_channel.



32
33
34
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 32

def mixer_channel
  @mixer_channel
end

Instance Method Details

#decode_input_stream(audio_format, audio_input_stream) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 61

def decode_input_stream(audio_format, audio_input_stream)
  case audio_format.encoding
  when Java::JavazoomSpiVorbisSampledFile::VorbisEncoding, Java::JavazoomSpiMpegSampledFile::MpegEncoding
    decoded_format = AudioFormat.new(AudioFormat::Encoding::PCM_SIGNED,
                                     audio_format.getSampleRate,
                                     16,
                                     audio_format.getChannels,
                                     audio_format.getChannels * 2,
                                     audio_format.getSampleRate,
                                     false)
    decoded_audio_input_stream = AudioSystem.getAudioInputStream(decoded_format, audio_input_stream)

    return decoded_format, decoded_audio_input_stream

  else
    [audio_format, audio_input_stream]
  end
end

#filepathObject



28
29
30
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 28

def filepath
  @dsl.filepath
end

#getLine(audio_format) ⇒ Object



104
105
106
107
108
109
110
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 104

def getLine(audio_format)
  # throws LineUnavailableException
  info = DataLine::Info.new(SourceDataLine.java_class, audio_format)
  res = AudioSystem.getLine(info)
  res.open(audio_format)
  res
end

#playObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 34

def play
  Thread.new do
    begin
      sound_file = JFile.new(filepath)

      audio_input_stream = AudioSystem.getAudioInputStream(sound_file)
      audio_format = audio_input_stream.getFormat

      decoded_audio_format, decoded_audio_input_stream = decode_input_stream(audio_format, audio_input_stream)

      # Play now.
      rawplay(decoded_audio_format, decoded_audio_input_stream)
      audio_input_stream.close
    rescue UnsupportedAudioFileException => uafex
      puts uafex.inspect, uafex.backtrace
    rescue IOException => ioex
      puts ioex.inspect, ioex.backtrace
    # rescue JIOException => jioex
    #  jioex.stacktrace
    rescue LineUnavailableException => luex
      puts luex.inspect, luex.backtrace
    rescue => e
      puts e.inspect, e.backtrace
    end
  end
end

#rawplay(decoded_audio_format, decoded_audio_input_stream) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'shoes-swt/lib/shoes/swt/sound.rb', line 80

def rawplay(decoded_audio_format, decoded_audio_input_stream)
  # throws IOException, LineUnavailableException

  sampled_data = Java::byte[BUFFER_SIZE].new

  line = getLine(decoded_audio_format)

  return if line.nil?

  # Start
  line.start
  bytes_read = 0
  while bytes_read != -1
    bytes_read = decoded_audio_input_stream.read(sampled_data, 0, sampled_data.length)
    line.write(sampled_data, 0, bytes_read) if bytes_read != -1
  end

  # Stop
  line.drain
  line.stop
  line.close
  decoded_audio_input_stream.close
end