Class: RazyK::Audio::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/razyk/audio/port.rb

Instance Method Summary collapse

Constructor Details

#initializePort

Returns a new instance of Port.



7
8
9
10
11
# File 'lib/razyk/audio/port.rb', line 7

def initialize
  @player = nil
  @note_length = nil
  @octave = nil
end

Instance Method Details

#close_writeObject



32
33
34
# File 'lib/razyk/audio/port.rb', line 32

def close_write
  @player.stop
end

#write(s) ⇒ Object



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

def write(s)
  bytes = s.unpack("C*")
  if @player.nil?
    @player = RazyK::Audio::Player.new(tempo: bytes.shift)
  end
  bytes.each do |b|
    if @note_length and @octave
      @player.play(@note_length, @octave, b)
      @note_length = nil
      @octave = nil
    elsif @note_length
      @octave = b
    else
      @note_length = b
    end
  end
  s
end