1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env ruby

require 'open-uri'
require 'osx/cocoa'
include OSX

def read_char
  system "stty raw -echo"
  STDIN.getc
ensure
  system "stty -raw echo"
end

puts "Fetching MP3s..."
mp3s = []
# if working with local files: Dir["/tmp/mp3/*.mp3"].each_with_index do |mp3,i|
open('http://www.freesound.org/packsViewSingle.php?id=532').read.scan(/http:\/\/[^"]+?\.mp3/).each_with_index do |mp3, i|
  break if i > 9
  puts "Loading #{mp3}"
  mp3s << NSSound.alloc.initWithContentsOfURL_byReference(NSURL.alloc.initWithString(mp3), 1)
  # if working with local files: # mp3s << NSSound.alloc.initWithContentsOfFile_byReference(mp3, 0)
end

puts "Done. Now hit number 0-9 to play sounds. (ctrl-c or ESC to end)"

while(1)
  c = read_char
  break if c == 27 || c == 3
  c = c.chr.to_i
  if c && mp3s[c]
    # this is broken, doesn't release the memory, just gets bigger and bigger
    mp3s[c].copy.play
  end
end