DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   ruby (http://www.daniweb.com/code/ruby.html)
-   -   Play Sound and Music the SDL Way (Ruby) (http://www.daniweb.com/code/snippet483.html)

vegaseat ruby syntax
Mar 27th, 2006
Using an SDL (Simple DirectMedia Layer) wrapper is one way to play sound and music with Ruby. I have tested this snippet with .MID .MOD and .WAV files and it performs well. There are several SDL wrappers for Ruby available, I picked RUDL because of its simplicity. RUDL is great for writing games.

  1. # play a midi file or mod file using the SDL wrapper for Ruby
  2. # download RUDL from: http://sourceforge.net/projects/rudl/
  3. # file: rudl-0.8-for-ruby-1.8.2-setup-releasebuild.zip
  4. # includes the needed SDL .dll files
  5. # run install-on-windows.rb and follow instructions
  6. # tested with Ruby182 and Windows XP vegaseat 17mar2006 (yeah Irish!)
  7.  
  8. require "RUDL"
  9. # this gets rid of the namespace
  10. include RUDL
  11.  
  12. # pick a midi file you have in the workfolder
  13. # or add the full path to the filename
  14. filename = "TeaFor2.mid"
  15. music = Music.new(filename)
  16. music.play
  17.  
  18. # needed to play music to the end
  19. gets