-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbali.py
1324 lines (1138 loc) · 45.5 KB
/
bali.py
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
'''
bali -- collection of modules for analyzing Balinese music
Copyright 2016, cuthbertLab at MIT
Released under the BSD 3-Clause license.
Authors: Katherine Young <kayoung@mit.edu>
Michael Scott Cuthbert
'''
from __future__ import print_function, absolute_import, division
#import bali
import io
import os
import re
#import weakref
import unittest
import copy
import random
import enum
import music21 # @UnresolvedImport
class BaliException(Exception):
pass
class IncorrectBeatNumberException(BaliException):
pass
class BeatLevel(enum.IntEnum):
pulse = 1
double = 2
guntang = 4
twoBeat = 8
fourBeat = 16
class Pattern(object):
'''
Represents one drum pattern.
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern
<bali.Taught Pak Tama Lanang 0 (intro):(_)_ _ e e _ e _ e _ e _ e _ e T _>
>>> pattern.title
'Pak Tama Lanang 0 (intro)'
>>> pattern.gongPattern
'(4)- ● - 1 - ● - 2 - ● - 3 - ● – 4'
>>> pattern.drumPattern
'(_)_ _ e e _ e _ e _ e _ e _ e T _'
>>> pattern.comments is None
True
>>> pattern.comments = 'intro pattern'
>>> pattern.comments
'intro pattern'
>>> pattern.indexInFile
1
'''
def __init__(self):
self.title = ""
self.gongPattern = ""
self.drumPattern = ""
self.comments = ""
self.indexInFile = -1 # -1 means undefined. otherwise 0 to ...
self.fileParser = None # the FileParser object.
def copy(self):
'''
Returns a copy of the Pattern
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern
<bali.Taught Pak Tama Lanang 0 (intro):(_)_ _ e e _ e _ e _ e _ e _ e T _>
>>> pCopy = pattern.copy()
>>> pCopy.title = 'wild lanang'
>>> pCopy
<bali.Taught wild lanang:(_)_ _ e e _ e _ e _ e _ e _ e T _>
>>> pattern
<bali.Taught Pak Tama Lanang 0 (intro):(_)_ _ e e _ e _ e _ e _ e _ e T _>
'''
return copy.deepcopy(self)
def shuffleStrokes(self):
'''
returns a new Pattern object based on this one where the
strokes have been scrambled.
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[9]
>>> pattern
<bali.Taught Pak Dewa Lanang 8:(l)_ e T l _ e T l _ e T l _ e T l>
>>> ''.join(pattern.strokes)
'l_eTl_eTl_eTl_eTl'
>>> p2 = pattern.shuffleStrokes()
>>> p2strokes = ''.join(p2.strokes)
>>> 'l_eTl_eTl_eTl_eTl' in p2strokes
False
>>> p2strokes.count('e')
4
'''
p2 = self.copy()
oldStrokes = p2.strokes
random.shuffle(oldStrokes)
p2.strokes = oldStrokes
return p2
def _getStrokes(self):
'''
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.strokes
['_', '_', '_', 'e', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', 'T', '_']
'''
dp = self.drumPattern
dpPreface = dp[0:3]
beatZero = dpPreface[1]
dpReal = dp[3:]
postBeatZeroStrokes = dpReal.split()
allStrokes = [beatZero] + postBeatZeroStrokes
return allStrokes
def _setStrokes(self, newStrokes):
'''
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.drumPattern
'(_)_ _ e e _ e _ e _ e _ e _ e T _'
>>> st = pattern.strokes
>>> st
['_', '_', '_', 'e', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', 'T', '_']
>>> st[1] = 'T'
>>> import copy
>>> patternCopy = copy.deepcopy(pattern)
>>> patternCopy.strokes = st
>>> patternCopy.drumPattern
'(_)T _ e e _ e _ e _ e _ e _ e T _'
Double check we did not change the original:
>>> pattern.drumPattern
'(_)_ _ e e _ e _ e _ e _ e _ e T _'
>>> st = pattern.strokes
>>> st
['_', '_', '_', 'e', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', 'T', '_']
'''
beatZeroStroke = newStrokes[0]
normalStrokes = newStrokes[1:]
newDrumPatternNormalPart = ' '.join(normalStrokes)
newDrumPatternAll = '(' + beatZeroStroke + ')' + newDrumPatternNormalPart
self.drumPattern = newDrumPatternAll
strokes = property(_getStrokes, _setStrokes, doc='''
Gets or sets the list of Strokes.
''')
def beatLength(self):
'''
The beatLength of a Pattern is defined as the last
thing (hopefully a number) in the Gong Pattern.
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.gongPattern
'(4)- ● - 1 - ● - 2 - ● - 3 - ● – 4'
>>> pattern.beatLength()
4
>>> pattern.gongPattern = '(6)- ● - 1 - ● - 2 - ● - 3 - ● – 4 - ● – 5 - ● – 6'
>>> pattern.beatLength()
6
'''
gp = self.gongPattern
lastLetter = gp[-1]
lastNumber = int(lastLetter)
return lastNumber
def iterateStrokes(self, maxBeat=4.0):
'''
Use only in a for loop: goes through
each stroke and tells you what the beat is
and then what the stroke letter is.
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> for beat, stroke in pattern.iterateStrokes(maxBeat=1.0):
... print(beat, stroke)
0.25 _
0.5 _
0.75 e
1.0 e
'''
beat = 0.25
strokeNumber = 1
stroke = self.strokes
while beat <= maxBeat:
yield beat, stroke[strokeNumber]
beat += 0.25
strokeNumber += 1
def typeOfStrokeByBeat(self, beat):
'''
Returns type of stroke on a given beat.
>>> from music21 import *
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.typeOfStrokeByBeat(1)
'e'
>>> pattern.typeOfStrokeByBeat(3.75)
'T'
>>> pattern.typeOfStrokeByBeat(0.25)
'_'
'''
return self.strokes[int(beat * 4)]
def descriptionOfStroke(self, stroke):
'''
Returns description and type of the given stroke symbol.
>>> from music21 import *
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.descriptionOfStroke('e')
'Lanang high stroke'
>>> pattern.descriptionOfStroke('T')
'Lanang low stroke'
>>> pattern.descriptionOfStroke('_')
'ghost stroke'
>>> pattern.descriptionOfStroke('aaa')
Traceback (most recent call last):
bali.BaliException: I do not know how to deal with this stroke
'''
strokeDict = {'e': 'Lanang high stroke',
'T': 'Lanang low stroke',
'd': 'Wadon stroke, quieter',
'D': 'Wadon bass stroke on big drum, louder',
'D.': 'dampened Wadon bass stroke on big drum',
'o': 'Wadon high stroke',
'L': 'left hand Wadon stroke', #not supposed to be there, lowercase l
'_': 'ghost stroke',
'r': 'right hand ghost stroke',
'l': 'left hand ghost stroke',
'G': 'gong stroke',
'pu': 'pung stroke',
'?': 'unclear stroke',
'U': 'taking and giving cue for dancer/singer/end of line in Lanang',
'C': 'right hand pitched stroke',
'-': 'beat in gong, metronome',
'P': 'left hand slap stroke',
'n': 'kempyang',
't': 'guntang',
'K': 'left hand slap stroke on Wadon',
'`': 'nothing'}
if stroke in strokeDict:
return strokeDict[stroke]
else:
raise BaliException('I do not know how to deal with this stroke')
unused_strokeNames = {'e': 'peng',
'T': 'tut', # doot
'd': 'dit',
'D': 'dag', #or deg
'D.': '', #
'o': 'kom',
'L': 'left hand Wadon stroke', #not supposed to be there, lowercase l
'_': 'ghost stroke',
'r': 'right hand ghost stroke',
'l': 'left hand ghost stroke',
'G': 'gong stroke',
'pu': 'pung stroke',
'?': 'unclear stroke',
'U': 'taking and giving cue for dancer/singer/end of line in Lanang',
'C': 'right hand pitched stroke',
'-': 'beat in gong, metronome',
'P': 'left hand slap stroke',
'n': 'kempyang',
't': 'guntang',
'K': 'left hand slap stroke on Wadon',
'`': 'nothing'}
def consecutiveStrokes(self):
'''
Finds all consecutive patterns (like oo) and returns copy of pattern
with repeated strokes removed and only final stroke shown, with
repeated strokes replaced by '.'
Example: Pak Tama Lanang 0 (intro) should return
['_', '_', '_', '.', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', 'T', '_']
>>> from music21 import *
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.title
'Pak Tama Lanang 0 (intro)'
>>> pattern.drumPattern
'(_)_ _ e e _ e _ e _ e _ e _ e T _'
>>> pattern.consecutiveStrokes()
['_', '_', '_', '.', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', '_', 'e', 'T', '_']
'''
consecutiveStrokesRemoved = self.strokes
repeatedStrokes = []
for stroke in range(1, len(self.strokes) - 1):
if self.strokes[stroke] != '_' and stroke < len(self.strokes) - 1:
if self.strokes[stroke] == self.strokes[stroke + 1]:
repeatedStrokes.append(self.strokes[stroke])
consecutiveStrokesRemoved[stroke] = '.'
return consecutiveStrokesRemoved
def isValidBeat(self, beat):
'''
Checks if beat entered is a valid beat in Taught Patterns.
If not, prompts user to enter a valid beat.
>>> from music21 import *
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.isValidBeat(3.2)
Please enter an integer from 1-4
False
>>> pattern.isValidBeat("hii")
Please enter an integer from 1-4
False
>>> pattern.isValidBeat(6)
Please enter an integer from 1-4
False
>>> pattern.gongPattern = '(6)- ● - 1 - ● - 2 - ● - 3 - ● – 4 - ● – 5 - ● – 6'
>>> pattern.beatLength()
6
>>> pattern.isValidBeat(5)
True
>>> pattern.isValidBeat(7)
Please enter an integer from 1-6
False
'''
if beat not in range(1, self.beatLength()) or beat != int(beat):
print("Please enter an integer from 1-" + str(self.beatLength()))
return False
else:
return True
def allLeadingUpToBeat(self, beatLedUpTo):
'''
Finds everything leading up to certain beat and places them in
allLeadingUpToBeat.
Example: Pak Tama Lanang 0 (intro) should return
['_', '_', 'e', 'e'] for leading up to beat 1.
>>> from music21 import *
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.allLeadingUpToBeat(1)
['_', '_', 'e', 'e']
'''
if self.isValidBeat(beatLedUpTo):
allLeadingUpToBeat = []
for i in range(4 * beatLedUpTo - 3,
4 * beatLedUpTo + 1):
allLeadingUpToBeat.append(self.strokes[i])
return allLeadingUpToBeat
else:
raise IncorrectBeatNumberException("Wrong beat")
def strokesLeadingUpToBeat(self, beatLedUpTo):
'''
Finds all strokes that aren't '_' leading up to certain beat and places them in
strokesLeadingUpToBeat.
Example: Pak Tama Lanang 0 (intro) should return
['e', 'e'] for leading up to beat 1.
>>> from music21 import *
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.strokesLeadingUpToBeat(1)
['e', 'e']
'''
if self.isValidBeat(beatLedUpTo):
strokesLeadingUpToBeat = []
for i in range((4 * beatLedUpTo) - 3,
(4 * beatLedUpTo) + 1):
if self.strokes[i] != '_':
strokesLeadingUpToBeat.append(self.strokes[i])
return strokesLeadingUpToBeat
else:
raise IncorrectBeatNumberException("Wrong beat")
def contiguousStrokesLeadingUpToBeat(self, beatLedUpTo):
'''
Finds all strokes that aren't '_' and are contiguous leading up to certain beat
and places them in contiguousLeadingUpToBeat.
Example: Pak Dewa Lanang 10 should return
['e', 'e', 'T'] for leading up to beat 1.
>>> from music21 import *
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[4]
>>> pattern
<bali.Taught Pak Dewa Lanang 10:(_)e e T e _ _ _ _ e e _ e _ e _ _>
>>> pattern.contiguousStrokesLeadingUpToBeat(1)
['e', 'e', 'T']
>>> pattern.contiguousStrokesLeadingUpToBeat(2)
[]
'''
if self.isValidBeat(beatLedUpTo):
contiguousStrokesLeadingUpToBeat = []
for i in range(4 * beatLedUpTo - 1,
4 * beatLedUpTo - 4,
-1):
if self.strokes[i] == '_':
break
else:
contiguousStrokesLeadingUpToBeat.insert(0, self.strokes[i])
return contiguousStrokesLeadingUpToBeat
else:
raise IncorrectBeatNumberException("Wrong beat")
def sameStrokesLeadingUpToBeat(self, beatLedUpTo):
'''
Finds all strokes leading up to certain beat that are the same
as that stroke and places them in sameStrokesLeadingUpToBeat.
Example: Pak Tama Lanang 0 (intro) should return
['e'] for leading up to beat 1.
Example 2: Pak Dewa Lanang 10 should return
[] for leading up to beat 1.
>>> from music21 import *
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.sameStrokesLeadingUpToBeat(1)
['e']
>>> pattern = fp.taught[4]
>>> pattern.sameStrokesLeadingUpToBeat(1)
[]
'''
if self.isValidBeat(beatLedUpTo):
sameStrokesLeadingUpToBeat = []
for i in range(4 * beatLedUpTo,
4 * beatLedUpTo - 4,
-1):
if self.strokes[i] == '_' or self.strokes[i-1] != self.strokes[i]:
break
elif self.strokes[i-1] == self.strokes[i] and self.strokes[i-1] != '_':
sameStrokesLeadingUpToBeat.insert(0, self.strokes[i-1])
return sameStrokesLeadingUpToBeat
else:
raise IncorrectBeatNumberException("Wrong beat")
def percentOnBeat(self, typeOfStroke='e', beatLevel=BeatLevel.double):
'''
Returns percent of a certain type of stroke that occurs on the beat
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.percentOnBeat('e')
85.7...
>>> pattern.percentOnBeat('T')
0.0
>>> pattern2 = fp.taught[-1]
>>> pattern2.percentOnBeat('o')
40.0
>>> pattern2.percentOnBeat('o', bali.BeatLevel.guntang)
20.0
>>> pattern2.percentOnBeat('o', bali.BeatLevel.twoBeat)
20.0
>>> pattern2.percentOnBeat('o', bali.BeatLevel.fourBeat)
0.0
'''
numberOnBeat = 0
numberOfStroke = 0
for beat, stroke in self.iterateStrokes():
if stroke != typeOfStroke:
continue
numberOfStroke += 1
if (beat * 4/beatLevel) % 1 == 0:
numberOnBeat += 1
if numberOfStroke == 0:
return 0.0
return (numberOnBeat * 100) / numberOfStroke
def beatsInPattern(self, typeOfStroke='e'):
'''
Returns number of a certain type of stroke in a single pattern
>>> import bali
>>> fp = bali.FileParser()
>>> pattern = fp.taught[1]
>>> pattern.beatsInPattern('e')
7
>>> pattern.beatsInPattern('T')
1
>>> pattern2 = fp.taught[-1]
>>> pattern2.beatsInPattern('o')
5
'''
numberOfStroke = 0
for stroke in self.iterateStrokes():
if stroke[1] != typeOfStroke:
continue
numberOfStroke += 1
if numberOfStroke == 0:
return 0.0
return numberOfStroke
def firstOrThirdBeat(self, typeOfStroke='e'):
'''
Returns how many strokes of a certain type land on first or third beat
in a guntang
>>> import bali
>>> fp = bali.FileParser()
>>> lanangPatterns = fp.separatePatternsByDrum()[0]
>>> wadonPatterns = fp.separatePatternsByDrum()[1]
>>> pattern = lanangPatterns[1]
>>> pattern.firstOrThirdBeat()['first']
0
>>> pattern.firstOrThirdBeat()['third']
1
>>> pattern2 = lanangPatterns[2]
>>> pattern2.firstOrThirdBeat()['first']
0
>>> pattern2.firstOrThirdBeat()['third']
1
>>> pattern3 = wadonPatterns[-1]
>>> pattern3.firstOrThirdBeat('Dd')['first']
1
>>> pattern3.firstOrThirdBeat('Dd')['first']
1
'''
firstBeat = 0
thirdBeat = 0
for beat, stroke in self.iterateStrokes():
if stroke not in typeOfStroke:
continue
if (beat - .25) % 1 == 0:
firstBeat += 1
elif (beat - .75) % 1 == 0:
thirdBeat += 1
return {'first': firstBeat, 'third': thirdBeat}
def secondOrFourthBeat(self, typeOfStroke='Dd'):
'''
Returns how many strokes of a certain type land on second or fourth beat
in a guntang
>>> import bali
>>> fp = bali.FileParser()
>>> wadonPatterns = fp.separatePatternsByDrum()[1]
>>> pattern = wadonPatterns[4]
>>> pattern.secondOrFourthBeat()['second']
2
>>> pattern.secondOrFourthBeat()['fourth']
1
>>> pattern2 = wadonPatterns[5]
>>> pattern2.secondOrFourthBeat()['second']
0
>>> pattern2.secondOrFourthBeat()['fourth']
1
'''
secondBeat = 0
fourthBeat = 0
for beat, stroke in self.iterateStrokes():
if stroke not in typeOfStroke:
continue
if (beat - .5) % 1 == 0:
secondBeat += 1
elif beat % 1 == 0:
fourthBeat += 1
return {'second': secondBeat, 'fourth': fourthBeat}
def whenLanangOffT(self, beatDivision='first'):
'''
Returns in which half of the gong lanang tut strokes land off the beat
when they're on the first or third subdivision of beat
First of all double strokes removed
>>> import bali
>>> fp = bali.FileParser()
>>> lanangPatterns = fp.separatePatternsByDrum()[0]
Testing when beatDivision is first
>>> pattern = lanangPatterns[7]
>>> pattern.whenLanangOffT()['first half']
0
>>> pattern.whenLanangOffT()['second half']
1
>>> pattern2 = lanangPatterns[-9]
>>> pattern2.whenLanangOffT()['first half']
1
>>> pattern2.whenLanangOffT()['second half']
1
Testing when beatDivision is third
>>> pattern3 = lanangPatterns[7]
>>> pattern3.whenLanangOffT('third')['first half']
0
>>> pattern3.whenLanangOffT('third')['second half']
1
>>> pattern4 = lanangPatterns[-9]
>>> pattern4.whenLanangOffT('third')['first half']
1
>>> pattern4.whenLanangOffT('third')['second half']
2
'''
firstHalf = 0
secondHalf = 0
pattern = self.removeConsecutiveStrokes('T')
for beat, stroke in zip(self.iterateStrokes(), pattern.strokes[1:]):
if stroke != 'T':
continue
if beatDivision == 'first':
if (beat[0] - .25) % 1 == 0:
if (beat[0] / 4) < 0.5:
firstHalf += 1
if (beat[0] / 4) >= 0.5:
secondHalf += 1
elif beatDivision == 'third':
if (beat[0] - .75) % 1 == 0:
if (beat[0] / 4) < 0.5:
firstHalf += 1
if (beat[0] / 4) >= 0.5:
secondHalf += 1
return {'first half': firstHalf, 'second half': secondHalf}
def whenWadonOffD(self, beatDivision='first'):
'''
Returns in which half of the gong wadon dag strokes land off the beat
when they're on the first or third division of the beat
First of all double strokes removed
>>> import bali
>>> fp = bali.FileParser()
>>> wadonPatterns = fp.separatePatternsByDrum()[1]
Testing when beatDivision is first
>>> pattern = wadonPatterns[-6]
>>> pattern = pattern.removeConsecutiveStrokes('Dd')
>>> pattern.whenWadonOffD()['first half']
1
>>> pattern.whenWadonOffD()['second half']
1
>>> pattern2 = wadonPatterns[5]
>>> pattern2.whenWadonOffD()['first half']
1
>>> pattern2.whenWadonOffD()['second half']
0
Testing when beatDivision is third
>>> pattern2 = wadonPatterns[-1]
>>> pattern2.whenWadonOffD('third')['first half']
1
>>> pattern2.whenWadonOffD('third')['second half']
0
>>> pattern2 = wadonPatterns[-7]
>>> pattern2.whenWadonOffD('third')['first half']
1
>>> pattern2.whenWadonOffD('third')['second half']
0
'''
firstHalf = 0
secondHalf = 0
pattern = self.removeConsecutiveStrokes('Dd')
for beat, stroke in zip(self.iterateStrokes(), pattern.strokes[1:]):
if stroke != 'D' and stroke != 'd':
continue
if beatDivision == 'first':
if (beat[0] - .25) % 1 == 0 or (beat[0] - .75) % 1 == 0:
if (beat[0] / 4) < 0.5:
firstHalf += 1
if (beat[0] / 4) >= 0.5:
secondHalf += 1
elif beatDivision == 'third':
if (beat[0] - .75) % 1 == 0 or (beat[0] - .75) % 1 == 0:
if (beat[0] / 4) < 0.5:
firstHalf += 1
if (beat[0] / 4) >= 0.5:
secondHalf += 1
return {'first half': firstHalf, 'second half': secondHalf}
def removeSingleStrokes(self, typeOfStroke='e'):
'''
Returns drum pattern with all single strokes of a given type removed.
Type of stroke is a string with all strokes to be removed.
The single strokes are replaced with ','
>>> import bali, taught_questions
>>> fp = bali.FileParser()
>>> pattern = fp.taught[0]
>>> pattern
<bali.Taught Lanang Dasar:(e)_ e _ e _ e _ e _ e _ e _ e _ e>
>>> removed = pattern.removeSingleStrokes('e')
>>> removed
<bali.Taught Lanang Dasar:(,)_ , _ , _ , _ , _ , _ , _ , _ ,>
>>> removed.percentOnBeat('e')
0.0
>>> pattern = fp.taught[4]
>>> pattern
<bali.Taught Pak Dewa Lanang 10:(_)e e T e _ _ _ _ e e _ e _ e _ _>
>>> removed = pattern.removeSingleStrokes('e')
>>> removed
<bali.Taught Pak Dewa Lanang 10:(_)e e T , _ _ _ _ e e _ , _ , _ _>
>>> removed.percentOnBeat('e')
50.0
>>> pattern2 = fp.taught[-1]
>>> pattern2
<bali.Taught Pak Tama Wadon Variant 3:(_)o o D _ _ _ o o D _ d D _ _ o _>
>>> removed2 = pattern2.removeSingleStrokes('D')
>>> removed2
<bali.Taught Pak Tama Wadon Variant 3:(_)o o , _ _ _ o o , _ d , _ _ o _>
>>> removed3 = pattern2.removeSingleStrokes('o')
>>> removed3
<bali.Taught Pak Tama Wadon Variant 3:(_)o o D _ _ _ o o D _ d D _ _ , _>
>>> removed3.percentOnBeat('o')
50.0
'''
newDrumPatternList = copy.deepcopy(self.strokes)
for i in range(len(self.strokes)):
if i == 0:
if self.strokes[i + 1] not in typeOfStroke and self.strokes[i] in typeOfStroke:
newDrumPatternList[i] = ','
if 0 < i < len(self.strokes) - 1:
if self.strokes[i] != self.strokes[i + 1] and self.strokes[i] in typeOfStroke:
if self.strokes[i - 1] != typeOfStroke:
newDrumPatternList[i] = ','
else:
if self.strokes[i - 1] not in typeOfStroke and self.strokes[i] in typeOfStroke:
newDrumPatternList[i] = ','
newDrumPattern = copy.deepcopy(self)
newDrumPattern.strokes = newDrumPatternList
return newDrumPattern
def removeConsecutiveStrokes(self, typeOfStroke='e', removeFirst=True, removeSecond=False):
'''
Returns drum pattern with first stroke of a double stroke of a given type removed.
Type of stroke is a string with all strokes to be removed.
The first stroke of a double stroke is replaced with '.'
>>> import bali, taught_questions
>>> fp = bali.FileParser()
>>> lanang10 = fp.taught[4]
>>> lanang10
<bali.Taught Pak Dewa Lanang 10:(_)e e T e _ _ _ _ e e _ e _ e _ _>
>>> removed = lanang10.removeConsecutiveStrokes('e')
>>> removed
<bali.Taught Pak Dewa Lanang 10:(_). e T e _ _ _ _ . e _ e _ e _ _>
>>> removed.percentOnBeat('e')
100.0
>>> lanang10
<bali.Taught Pak Dewa Lanang 10:(_)e e T e _ _ _ _ e e _ e _ e _ _>
>>> removed2 = lanang10.removeConsecutiveStrokes('e', removeSecond=True)
>>> removed2
<bali.Taught Pak Dewa Lanang 10:(_). . T e _ _ _ _ . . _ e _ e _ _>
>>> lanang10
<bali.Taught Pak Dewa Lanang 10:(_)e e T e _ _ _ _ e e _ e _ e _ _>
>>> removedDiffStrokes = fp.taught[-6]
>>> removedDiffStrokes
<bali.Taught Pak Dewa Wadon 5:(o)D o d D o D o o D o d D o D o o>
>>> removedDiffStrokes.removeConsecutiveStrokes('Dd')
<bali.Taught Pak Dewa Wadon 5:(o)D o . D o D o o D o . D o D o o>
Not appearing in the repertoire except by mistake, but for completeness sake
the name of this method is correct. It removes all but the last even
in cases where there are three or more in a row, unless removeSecond is True
>>> screwedUpLanang = fp.taught[4].copy()
>>> screwedUpLanang.drumPattern = '(_)e e T e _ _ _ _ e e e _ _ e _ _'
>>> removed = screwedUpLanang.removeConsecutiveStrokes('e')
>>> removed
<bali.Taught Pak Dewa Lanang 10:(_). e T e _ _ _ _ . . e _ _ e _ _>
Testing removing single strokes first, then removing first double strokes
>>> pattern2 = fp.taught[7]
>>> pattern2
<bali.Taught Pak Tut Lanang Dasar 2:(_)e e _ _ e e _ e _ _ e e T _ T _>
>>> removedSingle = pattern2.removeSingleStrokes('e')
>>> removedSingle
<bali.Taught Pak Tut Lanang Dasar 2:(_)e e _ _ e e _ , _ _ e e T _ T _>
>>> removedSingle.percentOnBeat('e')
50.0
>>> removedFirstDouble = removedSingle.removeConsecutiveStrokes('e')
>>> removedFirstDouble
<bali.Taught Pak Tut Lanang Dasar 2:(_). e _ _ . e _ , _ _ . e T _ T _>
>>> removedFirstDouble.percentOnBeat('e')
100.0
Testing removing both double strokes (removeFirst is True by default)
>>> removedSingle
<bali.Taught Pak Tut Lanang Dasar 2:(_)e e _ _ e e _ , _ _ e e T _ T _>
>>> removedBothDoubles = removedSingle.removeConsecutiveStrokes('e', removeSecond=True)
>>> removedBothDoubles
<bali.Taught Pak Tut Lanang Dasar 2:(_). . _ _ . . _ , _ _ . . T _ T _>
TODO: Leslie -- how to deal with across a repetition boundary
'''
newDrumPatternList = copy.deepcopy(self.strokes)
for i in range(1, len(self.strokes) - 1):
if removeFirst is True:
if self.strokes[i] in typeOfStroke and self.strokes[i + 1] in typeOfStroke:
newDrumPatternList[i] = '.'
if removeSecond is True:
if (self.strokes[i] in typeOfStroke and self.strokes[i + 1] in typeOfStroke) or self.strokes[i] == '.':
newDrumPatternList[i+1] = '.'
newDrumPattern = copy.deepcopy(self)
newDrumPattern.strokes = newDrumPatternList
return newDrumPattern
def __repr__(self):
return '<{0}.{1} {2}:{3}>'.format(self.__module__, self.__class__.__name__,
self.title, self.drumPattern)
class FileParser(object):
'''
Reads both files on disk (FileReader) and separates them into taught and transcribed patterns.
'''
def __init__(self):
self.fileReader = FileReader()
#time.sleep(1)
self.taughtPatterns = []
self.transcribedPatterns = []
@property
def taught(self):
if len(self.taughtPatterns) > 0:
return self.taughtPatterns
else:
self.parseTaught(self.fileReader.taught)
return self.taughtPatterns
def separatePatternsByDrum(self):
lanangPatterns = []
wadonPatterns = []
for p in self.taught:
if p.drumType == 'Lanang':
lanangPatterns.append(p)
elif p.drumType == 'Wadon':
wadonPatterns.append(p)
return lanangPatterns, wadonPatterns
@property
def transcribed(self):
if self.transcribedPatterns:
return self.transcribedPatterns
else:
self.parseTranscribed(self.fileReader.transcribed)
return self.transcribedPatterns
def parseTaught(self, lineList):
'''
Takes a list of lines from a file and an empty list [] and fills that
list with Taught objects.
>>> import bali
>>> fp = bali.FileParser()
>>> lineList = fp.taught
>>> lineList[4]
<bali.Taught Pak Dewa Lanang 10:(_)e e T e _ _ _ _ e e _ e _ e _ _>
'''
currentTitle = None
currentGongPattern = None
currentDrumPattern = None
currentComments = None
currentPatternIndex = 0
for line in lineList:
if line == '' and currentTitle is None:
continue
elif line == '':
patt = Taught()
patt.indexInFile = currentPatternIndex
currentPatternIndex += 1
patt.fileParser = self
if currentTitle.endswith(':'):
currentTitle = currentTitle[0:len(currentTitle) - 1]
patt.title = currentTitle
patt.gongPattern = currentGongPattern
patt.drumPattern = currentDrumPattern
patt.comments = currentComments
self.taughtPatterns.append(patt)
currentTitle = None
currentGongPattern = None
currentDrumPattern = None
currentComments = None
elif currentTitle is None:
currentTitle = line
elif currentGongPattern is None:
currentGongPattern = line
elif currentDrumPattern is None:
currentDrumPattern = line
else:
currentComments = line
def parseTranscribed(self, lineList):
'''
Takes a list of lines from a file and an empty list [] and fills that
list with Pattern objects.
This is not a good way to do this for later.
'''
currentTitle = ""
currentGongPattern = None
currentDrumPattern = None
currentComments = None
currentPatternIndex = 0
for line in lineList:
if line == '' and currentTitle is None:
continue
elif line == '':
patt = Transcribed()
patt.indexInFile = currentPatternIndex
currentPatternIndex += 1
patt.fileParser = self
if currentTitle.endswith(':'):
currentTitle = currentTitle[0:len(currentTitle) - 1]
patt.title = currentTitle
patt.gongPattern = currentGongPattern
patt.drumPattern = currentDrumPattern
patt.comments = currentComments
self.transcribedPatterns.append(patt)
currentTitle = None
currentGongPattern = None
currentDrumPattern = None
currentComments = None
elif currentTitle is None:
currentTitle = line