Module: Gemwarrior::Audio

Defined in:
lib/gemwarrior/misc/audio.rb

Class Method Summary collapse

Class Method Details

.initObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gemwarrior/misc/audio.rb', line 10

def self.init
  if GameOptions.data['sound_system'].eql?('win32-sound')
    begin
      require 'win32/sound'
    rescue LoadError => e
      GameOptions.data['errors'] = "#{GameOptions.data['sound_system']} could not be loaded. You may need to run 'gem install #{GameOptions.data['sound_system']}'. Silence for now."
    end
  elsif GameOptions.data['sound_system'].eql?('feep')
    begin
      require 'feep'
    rescue LoadError => e
      GameOptions.data['errors'] = "#{GameOptions.data['sound_system']} could not be loaded. You may need to run 'gem install #{GameOptions.data['sound_system']}'. Silence for now."
    end
  elsif GameOptions.data['sound_system'].eql?('bloops')
    begin
      require 'bloops'
    rescue LoadError => e
      GameOptions.data['errors'] = "#{GameOptions.data['sound_system']} could not be loaded. You may need to run 'gem install #{GameOptions.data['sound_system']}aphone'. Silence for now."
    end
  end
end

.play_sample(audio_cue_symbol) ⇒ Object



99
100
101
# File 'lib/gemwarrior/misc/audio.rb', line 99

def self.play_sample(audio_cue_symbol)
  # future use
end

.play_synth(audio_cue_symbol) ⇒ Object



32
33
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gemwarrior/misc/audio.rb', line 32

def self.play_synth(audio_cue_symbol)
  if GameOptions.data['sound_enabled']
    case GameOptions.data['sound_system']
    when 'win32-sound'
      require_relative 'audio_cues'
      require_relative 'musical_notes'
      Thread.start do
        AudioCues.cues[audio_cue_symbol][:synth].each do |seq|
          threads = []
          seq[:frequencies].split(',').each do |note|
            threads << Thread.new do
              Win32::Sound::play_freq(Notes::NOTE_FREQ[note], seq[:duration], GameOptions.data['sound_volume'])
            end
          end
          threads.each { |th| th.join }
        end
      end
    when 'feep'
      require_relative 'audio_cues'
      feep_defaults = {
        frequencies:  '440',
        waveform:     'sine',
        volume:       GameOptions.data['sound_volume'],
        duration:     500,
        notext:       true
      }

      Thread.start do
        AudioCues.cues[audio_cue_symbol][:synth].each do |seq|
          seq = feep_defaults.merge(seq)

          Feep::Base.new({
            freq_or_note: seq[:frequencies],
            waveform:     seq[:waveform],
            volume:       seq[:volume],
            duration:     seq[:duration],
            notext:       seq[:notext]
          })
        end
      end
    when 'bloops'
      require_relative 'bloops_cues'
      Thread.start do
        BloopsCues.cues[audio_cue_symbol][:synth].each do |seq|
          threads = []

          seq.each do |note|
            threads << Thread.new do
              b = Bloops.new
              b.tempo = 240
              snd = b.sound Bloops::SQUARE
              snd.punch = GameOptions.data['sound_volume']/2
              snd.sustain = 0.7
              snd.decay = 0.2
              b.tune snd, note[1]
              b.play
              sleep 0.1 while !b.stopped?
            end
          end

          threads.each { |th| th.join }
        end
      end
    end
  end
end