[go: up one dir, main page]

File: winsynth.py

package info (click to toggle)
solfege 3.10.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,408 kB
  • ctags: 4,270
  • sloc: python: 22,161; xml: 7,536; ansic: 4,442; makefile: 685; sh: 308
file content (73 lines) | stat: -rw-r--r-- 2,961 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# GNU Solfege - free ear training software
# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2007, 2008  Tom Cato Amundsen
# Copyright (C) 2001 Joe Lee
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from synth_common import SynthCommon
from mpd.track import MidiEventStream
import winmidi

class WinSynth(SynthCommon):
    NUM_CHANNELS = 16
    def __init__(self, devnum, verbose_init):
        SynthCommon.__init__(self)
        try:
            self.__driver = winmidi.Winmidi(devnum)
        except RuntimeError, e:
            if not winmidi.output_devices():
                e.args = e.args + (_("No MIDI output devices available."),)
                raise
            devnum = 0
            self.__driver = winmidi.Winmidi(devnum)
        self.m_type_major = "win32" #FIXME
        self.m_devnum = devnum
        if verbose_init:
            print "Solfege will use Windows multimedia output."
    def close(self):
        self.__driver = None
    def stop(self):
        # dummy function
        pass
    def play_track(self, *tracks):
        self.play_midieventstream(MidiEventStream(*tracks))
    def play_midieventstream(self, midieventstream):
        if self.__driver is None:
            raise RuntimeError, "Attempted to use synth after closing."
        self.__driver.reset()
        # bigger magic plays slower
        magic = 1440000
        self.__driver.set_tempo(int(magic * 4 / 60))
        v = []
        notelen = 0
        for e in midieventstream:
            if e[0] == midieventstream.TEMPO:
                self.__driver.set_tempo(int(magic * e[2] / e[1]))
            elif e[0] == midieventstream.NOTELEN_TIME:
                notelen = e[1]
                #print "notelen: ", notelen
            elif e[0] == midieventstream.NOTE_ON:
                self.__driver.note_on(int(1000 * notelen), e[1], e[2], e[3])
                notelen = 0
            elif e[0] == midieventstream.NOTE_OFF:
                self.__driver.note_off(int(1000 * notelen), e[1], e[2], e[3])
                notelen = 0
            elif e[0] == midieventstream.SET_PATCH:
                self.__driver.program_change(e[1], e[2])
            elif e[0] == midieventstream.BENDER:
                print "ugh todo: seq_bender for play_with_drvmidi"
                #m.seq_bender(DEV, e[1], e[2])
            else:
                raise error
        self.__driver.play()