# Created using the Midi Library Gem created by Jim Menerad
# to_midi Provided from the Rails Cookbook

# This script generates any website into a single midi Track
# Application website_song method logic Created & Designed by Gabriel G. Updated Nov 24th 2009
# http://pastie.org/712997
require 'rubygems'
require 'midilib'
require 'open-uri'

# Logic Behind the Midi
class Array

def to_midi(file, note_length='half')
midi_max = 128.0
midi_min = 0.0
low, high = min, max

song = MIDI::Sequence.new

song.tracks << (melody = MIDI::Track.new(song))

melody.events <<

MIDI::Tempo.new(MIDI::Tempo.bpm_to_mpq(120))
melody.events << MIDI::ProgramChange.new(0, 0)

each do |number|
midi_note = (midi_min + ((number-midi_min) * (midi_max-low)/high)).to_i
melody.events << MIDI::NoteOnEvent.new(0, midi_note, 127, 0)
melody.events << MIDI::NoteOffEvent.new(0, midi_note, 127,
song.note_to_delta(note_length))
end
open(file, 'w') { |f| song.write(f) }
end
end

# Method converts the website into a Musical composition
def website_song(w,tl)
t_a=[]; t=open("#{w}")
a=t.read; t_a=a.unpack("C*")
t_a.to_midi("#{tl}.mid")
end

# Method needs a website and title for the song example
website_song("http://google.com","google_song")