+# -*- coding: utf-8 -*-
+"""
+module pour jouer les chansons sur le fluidsynth
+
+$Id$
+$URL$
+"""
+import fluidsynth
+from musicxmltosong import musicXml2Song
+from time import sleep
+import sys
+from optparse import OptionParser
+
+class SongPlayer(object) :
+ """ Lecteur audio d'une chanson """
+
+ def __init__(self, part, soundFont, quarterNoteDuration=400, bank=0, preset=0) :
+ self.part = part
+ self.quarterNoteDuration = quarterNoteDuration
+ self.fs = fs = fluidsynth.Synth()
+ fs.start()
+ self.fsid = fsid = fs.sfload(soundFont)
+ fs.program_select(0, fsid, bank, preset)
+
+ def __del__(self) :
+ self.fs.delete()
+
+ def play(self) :
+ fs = self.fs
+ for note, verseIndex in self.part.iterNotes(indefinitely=False) :
+ print note, note.lyrics[verseIndex]
+ fs.noteon(0, note.midi, 64)
+ duration = note.duration * self.quarterNoteDuration / 1000
+ sleep(duration)
+ fs.noteoff(0, note.midi)
+
+
+
+
+
+def main() :
+ usage = "%prog musicXmlFile.xml [options]"
+ op = OptionParser(usage)
+ op.add_option("-i", "--part-index", dest="partIndex"
+ , default = 0
+ , help = "Index de la partie qui contient le champ.")
+ op.add_option("-q", "--quarter-note-duration", dest="quarterNoteDuration"
+ , type="int", default=400
+ , help="Durée de la noire [default] (en millisecondes)"
+ )
+ op.add_option("-b", "--bank", dest="bank"
+ , type="int", default="0"
+ , help="midi bank [default]")
+ op.add_option("-p", "--preset", dest="preset"
+ , type="int", default="0"
+ , help="midi preset [default]")
+
+ options, args = op.parse_args()
+
+ if len(args) != 1 :
+ raise SystemExit(op.format_help())
+
+ song = musicXml2Song(args[0], partIndex=options.partIndex)
+ sp = SongPlayer(song,
+ '/Users/pinbe/dev/minwii/fluid-soundfont-3.1/FluidR3_GM.sf2',
+ quarterNoteDuration=options.quarterNoteDuration,
+ bank = options.bank,
+ preset = options.preset)
+ sp.play()
+
+
+if __name__ == '__main__' :
+ sys.exit(main())