From: Giles Bowkett Date: 2006-05-18T07:50:24+09:00 Subject: Re: [QUIZ] Tab Player (#79) This is another proof of concept solution, I haven't been able to test it. In fact it's really just a quick sketch. I really haven't done much with Ruby, but one thing I've done which I'm reasonably happy with has been code-generated music, so, for what it's worth, here we go. first you steal the Jim Weirich's generators code from Hal Fulton's book "The Ruby Way." then use Jim Menard's midilib. then: class Tab < Generator def generating_loop # stuff here which reads the tab file and returns a string and a note end end class String(starting_note) :attr starting_note, track # each String starts on a particular MIDI note, each integer in tab notation # represents fret numbers, both MIDI notes and fret numbers increase in half # steps, therefore you simply add the tab note to the start note to get the total # MIDI note def play(note) @track.events << NoteOnEvent.new(1, (starting_note + note), 127, 0) @track.events << NoteOffEvent.new(1, (starting_note + note), 0, 1000) # (the magic numbers are constants which would be altered in an implementation that # cared about rhythm and dynamics. they govern volume, note length, and MIDI # channel.) end end then init stuff standard to midilib: require 'midilib/sequence' require 'midilib/consts' include MIDI seq = Sequence.new() track = Track.new(seq) seq.tracks << track track.events << Tempo.new(Tempo.bpm_to_mpq(120)) and then something like: tab = Tab.new("filename.tab") guitar [String.new(46, track), String.new(52, track), String.new(67, track)] # these aren't the right MIDI notes # for strings to start on, but you get the idea. # also in real life the guitar would have more # than three strings while (tab.next) do |string, note| string.play(note) end in addition to the oddity of a three-stringed guitar, the fact that I left out the file-reading code entirely, and any newbie syntax errors -- all of which I apologize for -- there are two additional problems with this solution: the first is that it's not properly set up to accomodate chords, since it'll play simultaneous notes in series. the other is that it outputs a MIDI file instead of making sound directly. (MIDI is the protocol common to all digital music applications since the late 70s, however, so turning MIDI files into sound is definitely an easy thing to do.) -- Giles Bowkett http://www.gilesgoatboy.org