Skip to content

Instantly share code, notes, and snippets.

@jashank
Created March 17, 2013 02:23
Show Gist options
  • Select an option

  • Save jashank/5179273 to your computer and use it in GitHub Desktop.

Select an option

Save jashank/5179273 to your computer and use it in GitHub Desktop.

Revisions

  1. jashank created this gist Mar 17, 2013.
    39 changes: 39 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    # 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