Class: Commands::Sound

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

Overview

Implements the “Sound” block in NXT-G

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nxt) ⇒ Sound

Returns a new instance of Sound.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/commands/sound.rb', line 29

def initialize(nxt)
  @nxt      = nxt
  
  # defaults the same as NXT-G
  @action   = :file
  @control  = :play
  @volume   = 75
  @repeat   = false
  @file     = "Good Job.rso"
  @note     = "C"
  @duration = 0.5
  @wait     = true
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



20
21
22
# File 'lib/commands/sound.rb', line 20

def action
  @action
end

#controlObject

Returns the value of attribute control.



21
22
23
# File 'lib/commands/sound.rb', line 21

def control
  @control
end

#durationObject

Returns the value of attribute duration.



26
27
28
# File 'lib/commands/sound.rb', line 26

def duration
  @duration
end

#fileObject

Returns the value of attribute file.



24
25
26
# File 'lib/commands/sound.rb', line 24

def file
  @file
end

#noteObject

Returns the value of attribute note.



25
26
27
# File 'lib/commands/sound.rb', line 25

def note
  @note
end

#repeatObject

Returns the value of attribute repeat.



23
24
25
# File 'lib/commands/sound.rb', line 23

def repeat
  @repeat
end

#volumeObject

TODO not sure how to set this with direct commands?



22
23
24
# File 'lib/commands/sound.rb', line 22

def volume
  @volume
end

#waitObject

Returns the value of attribute wait.



27
28
29
# File 'lib/commands/sound.rb', line 27

def wait
  @wait
end

Instance Method Details

#startObject

execute the Sound command based on the properties specified



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/commands/sound.rb', line 44

def start
  if @action == :file
    @nxt.play_sound_file(@file,@repeat)
  else
    @nxt.play_tone(@note.to_freq,(@duration * 1000).to_i)
  end
  
  # TODO figure out a logical way to repeat a tone without blocking execution
  
  if @wait
    if @action == :tone
      sleep(@duration)
    else
      # TODO don't know how to sleep until a sound file finishes
    end
  end
end

#stopObject

stop the Sound command



63
64
65
# File 'lib/commands/sound.rb', line 63

def stop
  @nxt.stop_sound_playback
end