envoi des événements NOTEON et NOTEOFF du synthé.
[minwii.git] / src / app / synth.py
index fa65638..71fa126 100755 (executable)
@@ -7,23 +7,39 @@ $URL$
 """
 from os.path import realpath, sep, exists
 from  fluidsynth import Synth as FSynth
+from log import console
+import pygame
+import events
 
 class Synth(FSynth) :
+    """
+    Interface fluidsynth avec les adaptations suivantes :
+    - la soundfont FluidR3_GM.sf2 est chargée par défaut
+    - le constructeur démarre le synthé
+    - octaviation
+    """
     
-    def __init__(self, gain=0.2, samplerate=44100) :
+    def __init__(self, gain=0.2, samplerate=44100, sfPath='') :
         FSynth.__init__(self, gain=gain, samplerate=samplerate)
         
-        sfPath = realpath(__file__).split(sep)
-        sfPath = sfPath[:-1]
-        sfPath.append('soundfonts')
+        if not sfPath :
+            sfPath = realpath(__file__).split(sep)
+            sfPath = sfPath[:-1]
+            sfPath.append('soundfonts')
+
+            sfPath.append('FluidR3_GM.sf2')
+            sfPath = sep.join(sfPath)
 
-        sfPath.append('FluidR3_GM.sf2')
-        sfPath = sep.join(sfPath)
         assert exists(sfPath)
 
         self.start()
         self.fsid = self.sfload(sfPath)
         self._octaveAjusts = {}
+        console.info('démarrage du synthétiseur\nsoundfont : %s', sfPath)
+    
+    def __del__(self) :
+        console.info('arrêt du synthétiseur.')
+        self.delete()
     
     def adjust_octave(self, chan, octave) :
         '''
@@ -43,11 +59,11 @@ class Synth(FSynth) :
     def noteon(self, chan, key, vel):
         key = key + self._octaveAjusts.get(chan, 0) * 12
         FSynth.noteon(self, chan, key, vel)
+        evt = pygame.event.Event(events.NOTEON, chan=chan, key=key, vel=vel)
+        pygame.event.post(evt)
     
     def noteoff(self, chan, key) :
         key = key + self._octaveAjusts.get(chan, 0) * 12
         FSynth.noteoff(self, chan, key)
-
-
-if __name__ == '__main__' :
-    initsynth()
\ No newline at end of file
+        evt = pygame.event.Event(events.NOTEOFF, chan=chan, key=key)
+        pygame.event.post(evt)