doc++
[minwii.git] / src / app / synth.py
1 # -*- coding: utf-8 -*-
2 """
3 module de wrapping du synthétiseur
4
5 $Id$
6 $URL$
7 """
8 from os.path import realpath, sep, exists
9 from fluidsynth import Synth as FSynth
10 from log import console
11
12 class Synth(FSynth) :
13 """
14 Interface fluidsynth avec les adaptations suivantes :
15 - la soundfont FluidR3_GM.sf2 est chargée par défaut
16 - le constructeur démarre le synthé
17 - octaviation
18 """
19
20 def __init__(self, gain=0.2, samplerate=44100, sfPath='') :
21 FSynth.__init__(self, gain=gain, samplerate=samplerate)
22
23 if not sfPath :
24 sfPath = realpath(__file__).split(sep)
25 sfPath = sfPath[:-1]
26 sfPath.append('soundfonts')
27
28 sfPath.append('FluidR3_GM.sf2')
29 sfPath = sep.join(sfPath)
30
31 assert exists(sfPath)
32
33 self.start()
34 self.fsid = self.sfload(sfPath)
35 self._octaveAjusts = {}
36 console.info('démarrage du synthétiseur\nsoundfont : %s', sfPath)
37
38 def __del__(self) :
39 console.info('arrêt du synthétiseur.')
40 self.delete()
41
42 def adjust_octave(self, chan, octave) :
43 '''
44 Abaisse ou élève les notes de n octave
45 '''
46 self._octaveAjusts[chan] = octave
47
48 def sfunload(self, update_midi_preset=0):
49 FSynth.sfunload(self, self.fsid, update_midi_preset=update_midi_preset)
50
51 def program_select(self, chan, bank, preset):
52 FSynth.program_select(self, chan, self.fsid, bank, preset)
53
54 def sfont_select(self, chan):
55 FSynth.sfont_select(self, chan, self.fsid)
56
57 def noteon(self, chan, key, vel):
58 key = key + self._octaveAjusts.get(chan, 0) * 12
59 FSynth.noteon(self, chan, key, vel)
60
61 def noteoff(self, chan, key) :
62 key = key + self._octaveAjusts.get(chan, 0) * 12
63 FSynth.noteoff(self, chan, key)