Class: Alexandria::UI::SoundEffectsPlayer

Inherits:
Object
  • Object
show all
Defined in:
lib/alexandria/ui/sound.rb

Overview

Uses Ruby/GStreamer to play Ogg/Vorbis sound effects

Instance Method Summary collapse

Constructor Details

#initializeSoundEffectsPlayer

Returns a new instance of SoundEffectsPlayer.



31
32
33
34
35
36
37
# File 'lib/alexandria/ui/sound.rb', line 31

def initialize
  @sounds_dir = Alexandria::Config::SOUNDS_DIR
  @ogg_vorbis_pipeline = Gst::Pipeline.new
  set_up_pipeline
  @playing = false
  set_up_bus_watch
end

Instance Method Details

#play(effect) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/alexandria/ui/sound.rb', line 39

def play(effect)
  file = File.join(@sounds_dir, "#{effect}.ogg")
  if @playing
    puts "Already playing #{effect}." if $DEBUG
  else
    puts "Not playing. Starting #{effect}." if $DEBUG
    @filesrc.location = file
    start_playback
  end
end

#set_up_bus_watchObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/alexandria/ui/sound.rb', line 72

def set_up_bus_watch
  @bus = @ogg_vorbis_pipeline.bus
  @bus.add_watch do |_bus, message|
    case message.type
    when Gst::MessageType::EOS
      stop_playback
    when Gst::MessageType::ERROR
      if $DEBUG
        puts 'ERROR loop.quit'
        p message.parse
      end
      stop_playback
    end
    true
  end
end

#set_up_pipelineObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/alexandria/ui/sound.rb', line 50

def set_up_pipeline
  @filesrc = Gst::ElementFactory.make('filesrc')
  demuxer = Gst::ElementFactory.make('oggdemux')
  decoder = Gst::ElementFactory.make('vorbisdec')
  converter = Gst::ElementFactory.make('audioconvert') # #??
  audiosink = Gst::ElementFactory.make('autoaudiosink')

  @ogg_vorbis_pipeline.add(@filesrc, demuxer, decoder,
                           converter, audiosink)
  @filesrc >> demuxer

  # this next must be a dynamic link, as demuxers potentially
  # have multiple src pads (for audio/video muxed streams)

  demuxer.signal_connect('pad-added') do |_parser, ogg_src_pad|
    vorbis_sink_pad = decoder.sinkpads.first
    ogg_src_pad.link(vorbis_sink_pad)
  end

  decoder >> converter >> audiosink
end

#start_playbackObject



89
90
91
92
# File 'lib/alexandria/ui/sound.rb', line 89

def start_playback
  @playing = true
  @ogg_vorbis_pipeline.play
end

#stop_playbackObject



94
95
96
97
# File 'lib/alexandria/ui/sound.rb', line 94

def stop_playback
  @ogg_vorbis_pipeline.stop
  @playing = false
end