File: patch.py

package info (click to toggle)
mma 21.09-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 51,828 kB
  • sloc: python: 16,751; sh: 26; makefile: 18; perl: 12
file content (162 lines) | stat: -rw-r--r-- 4,295 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# patch.py

"""
This module is an integeral part of the program
MMA - Musical Midi Accompaniment.

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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Bob van der Poel <bob@mellowood.ca>

This module contains the various patch manger routines.

"""

from MMA.common import *
from MMA.miditables import *
import MMA.file
import MMA.midiC


def patch(ln):
    """ Main routine to manage midi patch names. """

    for i, a in enumerate(ln):

        if a.count('=') == 1:
            a, b = a.split('=')
        else:
            b = ''
        a = a.upper()

        if a == "LIST":
            b = b.upper()
            if b == "ALL":
                plistall()
            elif b == "EXT":
                plistext()
            elif b == "GM":
                plistgm()
            else:
                error("Expecting All, EXT or GM argument for List")

        elif a == "RENAME":
            prename(ln[i + 1:])
            break

        elif a == 'SET':
            patchset(ln[i + 1:])
            break

        else:
            error("Unknown option for Patch: %s" % a)

# Set a patch value=name


def patchset(ln):
    if not ln:
        error("Patch Set expecting list of value pairs.")

    notopt, ln = opt2pair(ln, toupper=True)
    if notopt:
        error("PATCH SET: All options must be VALUE=PAIR items.")

    for v, n in ln:
        v = v.split('.')
        if len(v) > 3 or len(v) < 1:
            error("Patch Set: Expecting a voice value Prog.MSB.LSB.")

        # Parse out value. Can be 'nn', 'nn.nn' or 'nn.nn.nn'
        # the parts are for MSB(ctrl32), LSB(ctrl0), and Patch

        voc = 0
        if len(v) > 2:    # ctrl32
            i = stoi(v[2], "Patch Set LSB expecting integer.")
            if i < 0 or i > 127:
                error("LSB must be 0..127, not '%s'." % i)
            voc = i << 16

        if len(v) > 1:    # ctrl0
            i = stoi(v[1], "Patch Set MSB expecting integer.")
            if i < 0 or i > 127:
                error("MSB must be 0..127, not '%s'." % i)
            voc += i << 8

        i = stoi(v[0], "Patch Set Voice expecting integer.")
        if i < 0 or i > 127:
            error("Program must be 0..127, not '%s'." % i)
        voc += i

        # Handle the name.

        if voc in voiceNames:
            warning("Patch Set duplicating voice name %s=%s with %s=%s" %
                    (voiceNames[voc], voc, n, MMA.midiC.extVocStr(voc)))

        if n in voiceInx:
            warning("Patch Set duplicating voice value %s=%s with %s=%s" %
                    (MMA.midiC.extVocStr(voiceInx[n]), n, MMA.midiC.extVocStr(voc), n))

        voiceNames[voc] = n
        voiceInx[n] = voc

# Rename


def prename(ln):
    if not ln:
        error("Patch Rename expecting list of value pairs.")

    notopt, ln = opt2pair(ln, 1)

    if notopt:
        error("PATCH RENAME: expecting OLDNAME=NEWNAME pairs.")

    for a, b in ln:
        if not a in voiceInx:
            error("PATCH RENAME: oldname '%s' doen't exist, can't be renamed." % a)

        if b in voiceInx:
            error("PATCH RENAME: newname '%s' already exists." % b)

        v = voiceInx[a]
        voiceNames[v] = b
        del voiceInx[a]
        voiceInx[b] = v

# list funcs


def plistgm():
    """ List GM voices. """

    for v in sorted(voiceNames.keys()):
        if v <= 127:
            print("%s=%s" % (MMA.midiC.extVocStr(v), voiceNames[v]))


def plistext():
    """ List extended voices. """

    for v in sorted(voiceNames.keys()):
        if v > 127:
            print("%s=%s" % (MMA.midiC.extVocStr(v), voiceNames[v]))


def plistall():
    """ List all voices. """
    plistgm()
    plistext()