Class: Clicker::KeyboardSoundModel

Inherits:
Object
  • Object
show all
Includes:
SDL
Defined in:
lib/clicker/sound.rb

Constant Summary collapse

ACTIVATE =
0
DEACTIVATE =
1

Instance Method Summary collapse

Constructor Details

#initializeKeyboardSoundModel

Returns a new instance of KeyboardSoundModel.



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

def initialize
  init(INIT_AUDIO)

  begin
    SDL::Mixer
  rescue NameError
    raise "SDL::Mixer class not found. Make sure the rubysdl gem is built with sdl mixer."
  end

  # バッファー長 128 くらいでも動作するが、pulseaudio のCPU負荷が高
  # くなるのであまり調子にのらないほうがいい。
  Mixer.open(44100, Mixer::DEFAULT_FORMAT, 2, 1024)

  @sounds = [nil, nil]
  @sounds[ACTIVATE] = Mixer::Wave.load(File.dirname(__FILE__) + '/ibm_on.wav')
  @sounds[DEACTIVATE] = Mixer::Wave.load(File.dirname(__FILE__) + '/ibm_off.wav')
  @counts = [0, 0]
  Mixer.allocate_channels(12)
end

Instance Method Details

#activate_key(code) ⇒ Object



31
32
33
# File 'lib/clicker/sound.rb', line 31

def activate_key(code)
  @counts[ACTIVATE] += 1
end

#deactivate_key(code) ⇒ Object



35
36
37
# File 'lib/clicker/sound.rb', line 35

def deactivate_key(code)
  @counts[DEACTIVATE] += 1
end

#repeat_key(code) ⇒ Object



39
40
# File 'lib/clicker/sound.rb', line 39

def repeat_key(code)
end

#updateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/clicker/sound.rb', line 42

def update
  # p Mixer.playing_channels
  [ACTIVATE, DEACTIVATE].each do |kind|
    @counts[kind].times do 
  begin
        Mixer.play_channel(-1, @sounds[kind], 0)
  rescue SDL::Error
  end
      sleep(0.0027)
    end
  end
  @counts[0] = @counts[1] = 0
end