Class: Dare::Sound

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

Overview

wrapper for JS Audio takes a path/uri as an argument e.g. song = Dare::Sound.new('my_song.mp3') song.play will then play the mp3 in the browser

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ Sound

loads an audio resource from a path Sound.new('www.google.com/song.mp3') Sound.new('local_song_in_same_directory_of_app_js_file.mp3') a predefined volume may be passed as an option Sound.new('file.mp3', volume: 0.5)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dare/sound.rb', line 16

def initialize(path, opts = {})
  opts[:overlap] ||= 1
  opts[:volume] ||= 1
  opts[:overlap] = 1 if opts[:overlap].to_i < 1
  opts[:overlap] = 10 if opts[:overlap].to_i > 10
  @overlap = opts[:overlap]
  @sounds = []
  @overlap.times do
    `var snd = new Audio(#{path})`
    `snd.volume = #{opts[:volume]}`
    @sounds << `snd`
  end
  @sound = 0
end

Instance Method Details

#pauseObject

pause the audio resource, halting playback


54
55
56
# File 'lib/dare/sound.rb', line 54

def pause
  `#{@sound}.pause()`
end

#playObject

play the audio resource in the window/tab it was created in if resource was paused, it will start playing from where it left off


46
47
48
49
50
# File 'lib/dare/sound.rb', line 46

def play
  @sound += 1
  @sound %= @overlap
  `#{@sounds[@sound]}.play()`
end

#time=(time) ⇒ Object

seek to particular time in playback. Time passed is in seconds.


60
61
62
# File 'lib/dare/sound.rb', line 60

def time=(time)
  `#{@sound}.currentTime = #{time}`
end

#volumeObject

retrieve the current volume of the audio resource


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

def volume
  `#{@sound}.volume`
end

#volume=(vol) ⇒ Object

set the volume of the audio resource to value between 0 and 1


33
34
35
# File 'lib/dare/sound.rb', line 33

def volume=(vol)
  `#{@sound}.volume = #{vol}`
end