Created
March 17, 2013 02:23
-
-
Save jashank/5179273 to your computer and use it in GitHub Desktop.
Ruby SDL playback (as used in CMG)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I initialise on L19-22 | |
# let $SAMPLERATE be 48000; I'd like to do this more dynamically, eventually | |
SDL.init(SDL::INIT_AUDIO) | |
SDL::Mixer.open($SAMPLERATE, SDL::Mixer::DEFAULT_FORMAT, 2, 8192) | |
SDL::Mixer.allocate_channels(128) | |
# Inside the class Channel, I play audio on L68 (cherrypicking salient lines). | |
# Typically, there exist 64 Channels. | |
class Channel | |
attr_accessor :sdl, :num | |
# L58-92 | |
def fire_audio() | |
if @sdl | |
SDL::Mixer.play_channel(@num, @sdl, 0) | |
end | |
end | |
# L95-107 | |
def load_file(file) | |
@sdl = SDL::Mixer::Wave.load(file) | |
end | |
# L110-119 | |
def initialize(number) | |
@num = number | |
end | |
end | |
# I use Gdk/Gtk bindings to run 'fire_audio'; (cherrypicked from L146) | |
# let win be an instance of Gtk::Window | |
# let $key_table be an array of Gdk::Keyval names | |
# let $channel be an array of 64 Channel instances | |
win.signal_connect("key-press-event") do |w, e| | |
name = Gdk::Keyval.to_name(e.keyval) | |
if i = $key_table.index(name) | |
$channel[i].fire_audio | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment