Class: StepSequencer::SoundPlayer

Inherits:
Object
  • Object
show all
Defined in:
lib/step_sequencer/sound_player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources) ⇒ SoundPlayer

Returns a new instance of SoundPlayer.



7
8
9
10
# File 'lib/step_sequencer/sound_player.rb', line 7

def initialize(sources)
  @sources = sources
  reset_state
end

Instance Attribute Details

#playingObject (readonly)

Returns the value of attribute playing.



5
6
7
# File 'lib/step_sequencer/sound_player.rb', line 5

def playing
  @playing
end

Instance Method Details

#play(tempo: 120, string: nil, matrix: nil, limit: nil, hit_char: 'x', rest_char: '_') ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/step_sequencer/sound_player.rb', line 12

def play(tempo: 120, string: nil, matrix: nil, limit: nil, hit_char: 'x', rest_char: '_')
  @limit = limit
  @hit_char, @rest_char = hit_char, rest_char
  if @playing
    raise( StandardError,
      "A sound player received #play when it was not in a stopped state.
       Use multiple instances instead"
    )
  end
  if matrix
    play_from_matrix(tempo: tempo, matrix: matrix)
  else
    play_from_string(tempo: tempo, string: string)
  end
  @playing = true
end

#reset_stateObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/step_sequencer/sound_player.rb', line 34

def reset_state
  @thread = nil
  @playing = false # It can't be called multiple times at once.
                   # Use multiple instances instead.
                   # There's a check to precaution against this.
  @row_lengths = []
  @active_steps = []
  @steps_played = 0
  @limit = nil # an upper limit for steps_played, defaults to no limit
end

#stopObject



29
30
31
32
# File 'lib/step_sequencer/sound_player.rb', line 29

def stop
  reset_state
  @thread&.kill
end