From: pin Date: Thu, 11 Feb 2010 14:59:20 +0000 (+0000) Subject: Ajout d'un fichier d'essai de fluidsynth. X-Git-Url: https://scm.cri.ensmp.fr/git/minwii.git/commitdiff_plain/5f7bb1f72565a54cf49b2b3b39f13cc2bbabc112?ds=inline Ajout d'un fichier d'essai de fluidsynth. git-svn-id: https://svn.cri.ensmp.fr/svn/minwii/trunk@25 fe552daf-6dbe-4428-90eb-1537e0879342 --- diff --git a/src/songs/synth.py b/src/songs/synth.py new file mode 100755 index 0000000..07a8a5a --- /dev/null +++ b/src/songs/synth.py @@ -0,0 +1,73 @@ +# -*- 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())