refactoring du fichier de configuration. On peut maintenant modifier les paramètres...
[minwii.git] / src / minwii / config.py
index f45ef84..4a36201 100755 (executable)
@@ -8,6 +8,7 @@ $URL$
 
 import pygame
 import os.path
+from ConfigParser import ConfigParser
 
 _here = os.path.abspath(__file__).split(os.path.sep)[:-1]
 
@@ -17,72 +18,144 @@ def _computePath(path) :
     path = os.path.abspath(os.path.sep.join(path))
     return path
 
+def str2IntTuple(s) :
+    return tuple(map(int, s.strip('()').split(',')))
+
 
 pygame.font.init()
 
+conf = ConfigParser()
+
+conf.add_section('playingscreen')
+conf.set('playingscreen', 'FRAMERATE', '100')
+conf.set('playingscreen', 'FIRST_HUE', '0.6')
+conf.set('playingscreen', 'OFF_LUMINANCE', '0.2')
+conf.set('playingscreen', 'OFF_SATURATION', '1')
+conf.set('playingscreen', 'ON_TOP_LUMINANCE', '0.6')
+conf.set('playingscreen', 'ON_BOTTOM_LUMINANCE', '0.9')
+conf.set('playingscreen', 'ON_SATURATION', '1')
+conf.set('playingscreen', 'ON_COLUMN_OVERSIZING', '2')
+conf.set('playingscreen', 'ON_COLUMN_ALPHA', '1')
+conf.set('playingscreen', 'FONT_COLOR', '(0,0,0)')
+conf.set('playingscreen', 'MIDI_VELOCITY_RANGE', '(64, 127)')
+conf.set('playingscreen', 'MIDI_PAN_RANGE', '(32, 96)')
+conf.set('playingscreen', 'MIDI_VELOCITY_WRONG_NOTE_ATTN', '0.5')
+
+conf.add_section('locations')
+conf.set('locations', 'notes_font_file', _computePath('fonts/Arial Unicode.ttf'))
+conf.set('locations', 'SONG_FILE_PATH', _computePath('../chansons'))
+conf.set('locations', 'LOGS_DIR', os.path.join(os.path.expanduser('~'), 'minwii_logs'))
+conf.set('locations', 'SOUND_FONT', _computePath('soundfonts/FluidR3_GM.sf2'))
+
+# customisation
+conf.read(_computePath('../minwii.ini'))
+
+FRAMERATE = conf.getint('playingscreen', 'FRAMERATE')
+FIRST_HUE = conf.getfloat('playingscreen', 'FIRST_HUE')
+OFF_LUMINANCE = conf.getfloat('playingscreen', 'OFF_LUMINANCE',)
+OFF_SATURATION = conf.getfloat('playingscreen', 'OFF_SATURATION')
+ON_TOP_LUMINANCE = conf.getfloat('playingscreen', 'ON_TOP_LUMINANCE')
+ON_BOTTOM_LUMINANCE = conf.getfloat('playingscreen', 'ON_BOTTOM_LUMINANCE')
+ON_SATURATION = conf.getfloat('playingscreen', 'ON_SATURATION')
+ON_COLUMN_OVERSIZING = conf.getfloat('playingscreen', 'ON_COLUMN_OVERSIZING')
+ON_COLUMN_ALPHA = conf.getfloat('playingscreen', 'ON_COLUMN_ALPHA')
+FONT_COLOR = str2IntTuple(conf.get('playingscreen', 'FONT_COLOR'))
+MIDI_VELOCITY_RANGE = str2IntTuple(conf.get('playingscreen', 'MIDI_VELOCITY_RANGE'))
+MIDI_PAN_RANGE = str2IntTuple(conf.get('playingscreen', 'MIDI_PAN_RANGE'))
+MIDI_VELOCITY_WRONG_NOTE_ATTN = conf.getfloat('playingscreen', 'MIDI_VELOCITY_WRONG_NOTE_ATTN')
+
+SONG_FILE_PATH = conf.get('locations', 'SONG_FILE_PATH')
+LOGS_DIR = conf.get('locations', 'LOGS_DIR')
+SOUND_FONT = conf.get('locations', 'SOUND_FONT')
 
-# playingscreen
-FRAMERATE = 100
-FIRST_HUE = 0.6
-OFF_LUMINANCE = 0.2
-OFF_SATURATION = 1
-ON_TOP_LUMINANCE = 0.6
-ON_BOTTOM_LUMINANCE = 0.9
-ON_SATURATION = 1
-ON_COLUMN_OVERSIZING = 2
-ON_COLUMN_ALPHA = 1
-font_file = _computePath('fonts/Arial Unicode.ttf')
-NOTES_FONT = pygame.font.Font(font_file, 50)
+NOTES_FONT = pygame.font.Font(conf.get('locations', 'notes_font_file'), 50)
 LYRICS_FONT = pygame.font.Font(None, 80)
-FONT_COLOR = (0,0,0)
-MIDI_VELOCITY_RANGE = (64, 127)
-MIDI_PAN_RANGE = (32, 96)
-MIDI_VELOCITY_WRONG_NOTE_ATTN = 0.5
-
-SONG_FILE_PATH = _computePath('../chansons')
-
-INSTRUMENTS = (
-    {'name'   : 'accordeon',
-     'bank'   : 0,
-     'preset' : 23
-    },
-    {'name' : 'celesta',
-     'bank'   : 0,
-     'preset' : 8,
-     'octave' : 1
-    },
-    {'name' : 'flute',
-     'bank'   : 0,
-     'preset' : 73
-    },
-    {'name' : 'guitare',
-     'bank'   : 0,
-     'preset' : 24,
-     'octave' : -1
-    },
-    {'name' : 'orgue',
-     'bank'   : 0,
-     'preset' : 19
-    },
-    {'name' : 'piano',
-     'bank'   : 0,
-     'preset' : 0
-    },
-    {'name' : 'tuba',
-     'bank'   : 0,
-     'preset' : 58,
-     'octave' : -2
-    },
-    {'name' : 'violon',
-     'bank'   : 0,
-     'preset' : 40
-    },
-    {'name' : 'violoncelle',
-     'bank'   : 0,
-     'preset' : 42,
-     'octave' : -2
-    },
-)
-
-for i in INSTRUMENTS :
-    i['octave'] = i.get('octave', 0)
+
+instruments = ConfigParser()
+instruments.add_section('accordeon')
+instruments.set('accordeon', 'bank', '0')
+instruments.set('accordeon', 'preset', '23')
+instruments.set('accordeon', 'pos', '0')
+
+instruments.add_section('celesta')
+instruments.set('celesta', 'bank', '0')
+instruments.set('celesta', 'preset', '8')
+instruments.set('celesta', 'octave', '1')
+instruments.set('celesta', 'pos', '1')
+
+instruments.add_section('flute')
+instruments.set('flute', 'bank', '0')
+instruments.set('flute', 'preset', '73')
+instruments.set('flute', 'pos', '2')
+
+instruments.add_section('guitare')
+instruments.set('guitare', 'bank', '0')
+instruments.set('guitare', 'preset', '24')
+instruments.set('guitare', 'octave', '-1')
+instruments.set('guitare', 'pos', '3')
+
+instruments.add_section('orgue')
+instruments.set('orgue', 'bank', '0')
+instruments.set('orgue', 'preset', '19')
+instruments.set('orgue', 'pos', '4')
+
+instruments.add_section('piano')
+instruments.set('piano', 'bank', '0')
+instruments.set('piano', 'preset', '0')
+instruments.set('piano', 'pos', '5')
+
+instruments.add_section('tuba')
+instruments.set('tuba', 'bank', '0')
+instruments.set('tuba', 'preset', '58')
+instruments.set('tuba', 'octave', '-2')
+instruments.set('tuba', 'pos', '6')
+
+
+instruments.add_section('violon')
+instruments.set('violon', 'bank', '0')
+instruments.set('violon', 'preset', '40')
+instruments.set('violon', 'pos', '7')
+
+instruments.add_section('violoncelle')
+instruments.set('violoncelle', 'bank', '0')
+instruments.set('violoncelle', 'preset', '42')
+instruments.set('violoncelle', 'octave', '-2')
+instruments.set('violoncelle', 'pos', '8')
+
+# customisation
+instruments.read(_computePath('../instruments.ini'))
+
+
+INSTRUMENTS = []
+for section in instruments.sections() :
+    items = instruments.items(section)
+    instru = dict([(k, int(v)) for k, v in items])
+    instru['octave'] = instru.get('octave', 0)
+    instru['name'] = section
+    INSTRUMENTS.append(instru)
+
+INSTRUMENTS.sort(lambda a, b : cmp(a['pos'], b['pos']))
+map(lambda a : a.pop('pos'), INSTRUMENTS)
+
+#INSTRUMENTS = (
+#    {'name' : 'violoncelle',
+#     'bank'   : 0,
+#     'preset' : 42,
+#     'octave' : -2
+#    },
+#)
+#
+#for i in INSTRUMENTS :
+#    i['octave'] = i.get('octave', 0)
+
+
+if __name__ == '__main__' :
+    for f in (conf, instruments) :
+        for section in f.sections() :
+            print '=== ', section, ' ==='
+            for k, v in f.items(section) :
+                print k, v
+            print '*'*80
+    
+    from pprint import pprint
+    pprint(INSTRUMENTS)
\ No newline at end of file