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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import pm_functions as pmf
import random

def eventorder(score):
    """ test that consecutive events order correctly """
    passtest = True
    for l in score.layers:
        cursor = 0
        for e in l.events:
            if e.start != cursor:
                print 'layer %s event %s should start at %s' % \
                        (l.layernumber, l.events.index(e), cursor)
                passtest = False
            cursor += e.length
    return 'Event order: %s' % passtest

def eventlengths(score):
    """ test that event lengths add up """
    passtest = True
    for l in score.layers:
        cursor = 0
        for e in l.events:
            if e.start != cursor:
                print 'layer %s event %s should start at %s' % \
                        (l.layernumber, l.events.index(e), cursor)
                passtest = False
            try:
                cursor += e.prerest + e.elength + e.rlength
            except AttributeError:
                print 'layer %s event %s at %s AttributeError' % \
                        (l.layernumber, l.events.index(e), cursor)
    return 'Event lengths: %s' % passtest

def layerlengths(score):
    """ test that layers are the same length as the bars """
    passtest = True
    barlength = score.bars[-1].start + score.bars[-1].length
    for l in score.layers:
        l_length = l.events[-1].start + l.events[-1].length
        if l_length != barlength:
            print 'layer %s wrong length (total length)' % l.layernumber
            passtest = False
        # include prerest even though it should be zero - tested next
        l_length = l.events[-1].start + l.events[-1].prerest + \
                l.events[-1].elength + l.events[-1].rlength
        if l_length != barlength:
            print 'layer %s wrong length (sum length)' % l.layernumber
            passtest = False
    return 'Layer lengths: %s' % passtest

def prerests(score):
    passtest = True
    for l in score.layers:
        for e in l.events[1:]:
            if e.prerest != 0:
                print 'layer %s event %s has non-zero prerest' % \
                        (l.layernumber, l.events.index(e))
                passtest = False
    return 'Prerests: %s' % passtest

def findthirteens(score):
    """ find events with thirteen flag """
    for l in score.layers:
        for e in l.events:
            if e.thirteen > 0:
                print 'layer %s event %s has thirteen flag %s' % \
                        (l.layernumber, l.events.index(e), e.thirteen)

def plotdurations(score):
    """ output duration data """
    tdfile = open('pm_durations_total.dat', 'w')
    ldfile = open('pm_durations_length.dat', 'w')
    edfile = open('pm_durations_event.dat', 'w')
    maxdur, mindur = 0, 9999
    maxlen, minlen = 0, 9999
    maxev, minev = 0, 9999
    onebeatev = 0
    shortev = 0
    totalev = 0
    for l in score.layers:
        totalev += len(l.events)
        for e in l.events:
            if e.repetition is False:
                dur = pmf.beats2seconds(e.tempo, e.length)
                tdstr = '%s\t%s\n' % (e.location, dur)
                if dur > maxdur: maxdur = dur
                if dur < mindur: mindur = dur
                tdfile.write(tdstr)
                ldstr = '%s\t%s\n' % (e.location, e.length)
                if e.length > maxlen: maxlen = e.length
                if e.length < minlen: minlen = e.length
                ldfile.write(ldstr)
                edstr = '%s\t%s\n' % (e.location, e.elength)
                if e.elength > maxev: maxev = e.elength
                if 0 < e.elength < minev: minev = e.elength
                if e.elength == 1: onebeatev += 1
                if 0 < e.elength <= 12: shortev += 1
                edfile.write(edstr)
    tdfile.close()
    ldfile.close()
    edfile.close()
    print 'Durations: %s - %s' % (mindur, maxdur)
    print 'Lengths: %s - %s' % (minlen, maxlen)
    print 'Events: %s - %s' % (minev, maxev)
    print 'Single beat events: %s (%02d%%)' % (onebeatev, \
            (onebeatev * 100) / float(totalev))
    print 'Short events (12): %s (%02d%%)' % (shortev, \
            (shortev * 100) / float(totalev))
    return 'Duration files written!'

def plotregisters(score):
    """ output register data """
    regfile = open('pm_registers.dat', 'w')
    for l in score.layers:
        for e in l.events:
            regfile.write('%s\t%s\n' % (e.location, e.register))
    regfile.close()
    return 'Register file written!'

def plotchordnumbers(score):
    """ output chord numbers """
    chfile = open('pm_chordnumbers.dat', 'w')
    for l in score.layers:
        for e in l.events:
            try:
                chfile.write('%s\t%s\n' % (e.location, len(e.cs.chords)))
            except AttributeError:
                chfile.write('%s\t%s\n' % (e.location, 0))
    chfile.close()
    return 'Chord numbers file written!'

def plotchordcons(score):
    """ output chord consonance """
    chfile = open('pm_chordconsonance.dat', 'w')
    mincon, maxcon = [1, 1], [0, 0]
    for l in score.layers:
        for e in l.events:
            try:
                c = e.cs.chords_closest[0]
                if mincon[0] > c[0]: mincon[0] = c[0]
                if mincon[1] > c[1]: mincon[1] = c[1]
                if maxcon[0] < c[0]: maxcon[0] = c[0]
                if maxcon[1] < c[1]: maxcon[1] = c[1]
                chfile.write('%s\t%s\t%s\n' % \
                        (e.location, c[1], c[0]))
            except AttributeError:
                pass
    chfile.close()
    print 'Consonance: %s - %s' % (mincon, maxcon)
    return 'Chord consonance file written!'

def plotsubscons(score):
    """ output subsidiaries consonance """
    chfile = open('pm_subsconsonance.dat', 'w')
    mincon, maxcon = [1, 1], [0, 0]
    for l in score.layers:
        for e in l.events:
            try:
                c = e.subsnotes_closest[0]
                if mincon[0] > c[0]: mincon[0] = c[0]
                if mincon[1] > c[1]: mincon[1] = c[1]
                if maxcon[0] < c[0]: maxcon[0] = c[0]
                if maxcon[1] < c[1]: maxcon[1] = c[1]
                chfile.write('%s\t%s\t%s\n' % \
                        (e.location, c[1], c[0]))
            except AttributeError:
                pass
    chfile.close()
    print 'Consonance: %s - %s' % (mincon, maxcon)
    return 'Subsidiaries consonance file written!'

def plotattacklengths(score):
    """ output calculated attack lengths """
    atfile = open('pm_attacklengths.dat', 'w')
    minlen, maxlen = 20, 0
    for l in score.layers:
        for e in l.events:
            try:
                for c, len in e.attacklengths:
                    if len < minlen: minlen = len
                    if len > maxlen: maxlen = len
                    atfile.write('%s\t%s\n' % \
                            (e.location, len))
            except AttributeError:
                pass
    atfile.close()
    print '\nAttack lengths: %s - %s' % (minlen, maxlen)
    return 'Attack lengths file written!'

def workoutallocateattacks(r):
    for x in xrange(r):
        a = random.randint(1, 100)
        snumber = None
        subsidiary = random.choice([True, False])
        if subsidiary:
            order = random.choice(['', 'b', 's', 'r'])
            order += random.choice(['', 'C', 'R', 'B', 'S', 'A', 'T'])
            order += random.choice(['', 'a', 't', 'u'])
            snumber = random.randint(1, 6)
        else:
            order = random.choice(['', 'b'])
            order += random.choice(['', 'C', 'B', 'A'])
            order += random.choice(['', 'a'])
        print a, order, snumber, pmf.allocateattacks(a, order, snumber)

def attacklengths(score):
    passtest = True
    for l in score.layers:
        for e in l.events:
            asum = sum([d for v, d in e.attacklengths])
            if e.elength != round(asum, 8):
                print 'Layer %s event %s attack lengths differ %s' % \
                        (l.layernumber, e.index, e.elength - asum)
                passtest = False
    return 'Attack lengths: %s' % passtest

def plotcomponentlengths(score):
    clfile = open('pm_componentlengths.dat', 'w')
    for st in score.staves:
        for e in st.events:
            for c in e.components:
                if c.length == 0: continue
                clfile.write('%s\t%.8f\n' % \
                        (e.location, c.length))
    clfile.close()
    return 'Component lengths file written!'

def componentlengths(score):
    passtest = True
    clengths = set()
    graces, nongraces = 0, 0
    for st in score.staves:
        for e in st.events:
            clength = 0
            for c in e.components:
                clengths.add(c.length)
                clength += c.length
                if c.length == 0:
                    graces += 1
                else:
                    nongraces += 1
            if e.length != round(clength, 8):
                print 'Staff %s event %s component sum differs %s' % \
                        (st.name, e.index, e.length - clength)
                passtest = False
                if e.index > 100: break
    clengths = list(clengths)
    clengths.sort()
    print 'Graces %s, nongraces %s' % (graces, nongraces)
    #print clengths
    return 'Component lengths: %s' % passtest

def pitchranges(score):
    for st in score.staves:
        cmax, cmin = 0, 100
        amax, amin = 0, 100
        smax, smin = 0, 100
        for e in st.events:
            if e.negative is True:
                continue
            ecmax = max(list(pmf.flatten(e.cs.chords, list)))[0]
            ecmin = min(list(pmf.flatten(e.cs.chords, list)))[0]
            if ecmax > cmax: cmax = ecmax
            if ecmin < cmin: cmin = ecmin
            try:
                eamax = max(e.accessorypitches)
                eamin = min(e.accessorypitches)
                if eamax > amax: amax = eamax
                if eamin < amin: amin = eamin
            except (TypeError, AttributeError):
                pass
            if e.subsnotes is not False:
                subs = []
                for s1, s2, _ in e.subsnotes:
                    subs.append(s1)
                    if s2 is not None: subs.append(s2)
                esmax = max(subs)
                esmin = min(subs)
                if esmax > smax: smax = esmax
                if esmin < smin: smin = esmin
        try:
            stmax = pmf.pitch2number(st.pos[0].range[1][0], st.pos[0].range[1][1])
            stmin = pmf.pitch2number(st.pos[0].range[0][0], st.pos[0].range[0][1])
        except TypeError:
            stmax, stmin = 49, 32
        print '%s: %s-%s' % (st.name, stmin, stmax)
        print '\tCentral sounds %s-%s' % (cmin, cmax),
        print '\tAccessories %s-%s' % (amin, amax),
        print '\tSubsidiaries %s-%s' % (smin, smax)

def plottuplets(score):
    tufile = open('pm_tuplets.dat', 'w')
    tuplets = 0
    nottuplets = 0
    for l in score.layers:
        for e in l.events:
            for c in e.components:
                if c.tuplet is not None:
                    tuplets += 1
                    t = c.tuplet.numerator / float(c.tuplet.denominator)
                    tufile.write('%s\t%s\n' % \
                            (e.location, t))
                else:
                    nottuplets += 1
                    tufile.write('%s\t%s\n' % \
                            (e.location, 0))
    tufile.close()
    print 'Tuplets: %s, not tuplets %s' % (tuplets, nottuplets)
    return 'Tuplets file written!'

def plotpmvalues(score):
    pmfile = open('pm_pmvalues.dat', 'w')
    for l in score.layers:
        for e in l.events:
            try:
                f, v = e.flag, e.pmvalue
                pmfile.write('%s\t%s\t%s\n' % \
                    (e.location, f, v))
            except AttributeError:
                pass
    pmfile.close()
    return 'PM values file written!'

def tupletbarlines(score):
    passtest = True
    for s in score.staves:
        for e in s.events:
            for c in e.components:
                if c.tuplet is not None:
                    tstart = c.tuplet.start
                    tend = tstart + c.tuplet.numerator
                    for b in score.bars:
                        if b.start > tstart and b.start < tend:
                            print 'Tuplet crosses barline! Staff %s, event %s, bar %s' % \
                                    (s.name, e.index, b.id)
                            passtest = False
    return 'Tuplet barlines: %s' % passtest

def testpitches(score):
    passtest = True
    for s in score.staves:
        for e in s.events:
            for c in e.components:
                if c.pitch is False:
                    print 'Note has no pitch! Staff %s, event %s' % \
                            (s.name, e.index)
                    passtest = False
    return 'Note pitches: %s' % passtest

def all(score):
    """ run all tests """
    print eventorder(score)
    print eventlengths(score)
    print layerlengths(score)
    print prerests(score)
    print attacklengths(score)
    print componentlengths(score)
    print tupletbarlines(score)
    print testpitches(score)

def allplots(score):
    """ write all plot files """
    print plotdurations(score)
    print plotregisters(score)
    print plotchordnumbers(score)
    print plotchordcons(score)
    print plotsubscons(score)
    print plotpmvalues(score)
    print plotcomponentlengths(score)