-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpmodel.py
1311 lines (1123 loc) · 41 KB
/
cpmodel.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
FIXED_INSTRUMENT = -2
DEFAULT_INSTRUMENT = -1
FADE = 0
STOP = 1
NRAND = 20
FADE_CMD = "f"
STOP_CMD = "s"
PLAY_CMD = "p"
EMPTY_CMD = "e"
PROG0_CMD = "i"
TRANS_CMD = "r"
SPEED_CMD = "m"
PLAYING_CODE = "p"
STOPPED_CODE = "s"
ENDING_CODE = "e"
REMAINING_CODE = "r"
WFIFO = "/tmp/churchplayerfifo_wr"
FSFIFO = "/tmp/fluidsynthfifo"
JOLLY_ORGAN = 'Organ 7'
QUIET_ORGAN = 'Organ 9'
JOLLY_PIANO = 'Piano 2'
QUIET_PIANO = 'Warm Piano 1'
EXCLUDE_FROM_GENERAL = 'cavel'
import signal
import os.path
import subprocess
import sys
import time
import psutil
import os
import re
import copy
import random
instrumentNames = [
'Original',
'Piano 1',
'Warm Piano 1',
'Piano 2',
'Piano 3',
'E-Piano 1',
'Warm E-Piano 1',
'E-Piano 2',
'E-Piano 3',
'E-Piano 4',
'Organ 1',
'Organ 2',
'Organ 3',
'Organ 4',
'Organ 5',
'Organ 6',
'Organ 7',
'Organ 8',
'Organ 9',
'Organ 10',
'Organ 11',
'Harpsichord',
'Vibraphone',
'Xylophone',
'Strings'
]
# These are most GM numbers, but with some extra organs.
instruments = {
'(ensemble)': FIXED_INSTRUMENT,
'Original': DEFAULT_INSTRUMENT,
'Piano 1': 0,
'Warm Piano 1': 1,
'Piano 2': 2,
'Piano 3': 3,
'E-Piano 1': 4,
'Warm E-Piano 1': 5,
'E-Piano 2': 7,
'E-Piano 3': 8,
'E-Piano 4': 9,
'Organ 1': 127,
'Organ 2': 120,
'Organ 3': 16,
'Organ 4': 20,
'Organ 5': 121,
'Organ 6': 123,
'Organ 7': 122,
'Organ 8': 124,
'Organ 9': 125,
'Organ 10': 126,
'Organ 11': 19,
'Harpsichord': 6,
'Vibraphone': 11,
'Xylophone': 13,
'Strings': 48
}
# Instrument substitutions
instrument_subs = {
'118': 16,
'119': 20
}
instrumentByNumber = {}
for instr in instruments:
instrumentByNumber[instruments[instr]] = instr
def midiInstrument( value ):
if isinstance( value, int ):
return value
elif isinstance( value, str ):
if value.isdigit():
return int( value )
elif value in instruments:
return instruments[value]
else:
return DEFAULT_INSTRUMENT
else:
return DEFAULT_INSTRUMENT
def waitForProcess( name ):
ntry = 0
while ntry >= 0:
try:
subprocess.check_output(["pidof",name])
ntry = -1
except subprocess.CalledProcessError:
ntry += 1
if ntry < 60:
time.sleep(1)
else:
ntry = -1
raise ChurchPlayerError("\n\nWaited {0} seconds for the {1} "
"process to start, but no luck. "
"Something is wrong :-( ".format(ntry,name) )
# Test if a path refers to an existing usable midi file. Returns 0 if it
# is, 1 if the file exists but is not a usable midi file, and 2 if the file
# does not exist.
def isMidi(path):
if os.path.isfile( path ):
text = subprocess.getstatusoutput( "file '{0}'".format(path) )
if "Standard MIDI" in text[1]:
return 0
else:
return 1
else:
return 2
# ----------------------------------------------------------------------
class ChurchPlayerError(Exception):
"""
A base class for all the classes of Exception that can be raised by
this module.
"""
pass
# ----------------------------------------------------------------------
class Catalogue(dict):
def __init__(self,readcat=True):
super(Catalogue, self).__init__()
self.warnings = []
if readcat:
self._readCatalogue()
self.modified = False
self.midifiles = []
# Open the catalogue disk file and read its contents into the properties
# of this Catalogue object.
def _readCatalogue(self):
# Decide on the catalogue file to be opened.
if "CPMUSIC" in os.environ:
self.catname = os.environ[ "CPMUSIC" ]
else:
self.catname = os.path.join(os.path.dirname(sys.argv[0]), "cpmusic.txt")
if not os.path.isfile( self.catname ):
raise ChurchPlayerError("\n\nMusic catalogue {0} does not "
"exist".format(self.catname))
# Create regexps to match each sort of line in the catalogue file.
white = re.compile( "^\s*$" )
comment = re.compile( "^#" )
column = re.compile( "^c:(\S+) +([01]) +([0-9]+) +(.+)$" )
root = re.compile( "^r:(\S+)$" )
book = re.compile( "^b:(\S+) +(.+)$" )
instrumentation = re.compile( "^i:(\S+) +(.+)$" )
origin = re.compile( "^o:(\S+) +(.+)$" )
tags = re.compile( "^t:(\S) +(.+)$" )
record = re.compile( "@" )
# Initialise all the arrays to store the header info read from the
# catalogue.
self.rootdir = os.path.join(os.path.dirname(sys.argv[0]), "music")
self.colnames = []
self.coldescs = []
self.colsearchable = []
self.coluser = []
self.booknames = []
self.bookdescs = []
self.instrnames = []
self.instrdescs = []
self.orignames = []
self.origdescs = []
self.tagnames = []
self.tagdescs = []
self.ncol = 0
self.nrow = 0
self.usercols = [None]*20
self.metres = []
# Open the catalogue file and read each line in turn, removing any trailing
# or leading white space (e.g. the trailing newline character).
cat = open( self.catname, "r" )
for line in cat:
line = line.strip()
# Classify the line by matching it against each regexp created above.
# Extract the required info from each sort of header line and store in
# the arrays created above.
match = comment.search( line )
if match:
continue
match = white.search( line )
if match:
continue
match = root.search( line )
if match:
self.rootdir = match.group(1)
continue
match = column.search( line )
if match:
colname = match.group(1)
self.colnames.append( colname )
if match.group(2) == "1":
self.colsearchable.append( True )
else:
self.colsearchable.append( False )
iuser = int( match.group(3) )
self.coluser.append(iuser)
if iuser > 0:
self.usercols[iuser-1] = colname
self.coldescs.append( match.group(4) )
# For each column name, create a new empty array and store it as a new
# entry in the parent dict using the column name as the key name.
self[match.group(1)] = []
self.ncol += 1
continue
match = book.search( line )
if match:
self.booknames.append( match.group(1) )
self.bookdescs.append( match.group(2) )
continue
match = instrumentation.search( line )
if match:
self.instrnames.append( match.group(1) )
self.instrdescs.append( match.group(2) )
continue
match = origin.search( line )
if match:
self.orignames.append( match.group(1) )
self.origdescs.append( match.group(2) )
continue
match = tags.search( line )
if match:
self.tagnames.append( match.group(1) )
self.tagdescs.append( match.group(2) )
continue
# If this line describes a midi file (rather than being a header line),
# split it into fields and check that the number of fields matches the
# number of columns. If so, append each field value to the end of the
# arrays in the parent dict.
match = record.search( line )
if match:
values = line.split("@")
nv = len(values)
if nv > self.ncol:
self.warnings.append("Too many columns ({1}) in line: '{0}'".
format( line, nv ) )
nv = self.ncol
elif nv < self.ncol:
self.warnings.append("Too few columns ({1}) in line: '{0}'".
format( line, nv ) )
for i in range(nv):
if values[i] == '':
values[i] = None
self[ self.colnames[i] ].append( values[i] )
if nv < self.ncol:
for i in range(nv,self.ncol):
self[ self.colnames[i] ].append( None )
# Substitute new instruments
for key, value in instrument_subs.items():
if self[ 'PROG0' ][ -1 ] == key:
self[ 'PROG0' ][ -1 ] = value
# Traditional hymns should use an organ by default, not a piano.
if self[ 'TAGS' ][ -1 ] and self[ 'INSTR' ][ -1 ] == "KEYBD":
if "H" in self[ 'TAGS' ][ -1 ]:
if int(self[ 'PROG0' ][ -1 ]) == DEFAULT_INSTRUMENT:
if "Q" in self[ 'TAGS' ][ -1 ]:
prog0 = instruments[ QUIET_ORGAN ]
else:
prog0 = instruments[ JOLLY_ORGAN ]
self[ 'PROG0' ][ -1 ] = prog0
# Quiet modern should default to warm piano
elif "M" in self[ 'TAGS' ][ -1 ]:
if int(self[ 'PROG0' ][ -1 ]) == DEFAULT_INSTRUMENT:
if "Q" in self[ 'TAGS' ][ -1 ]:
prog0 = instruments[ QUIET_PIANO ]
else:
prog0 = instruments[ JOLLY_PIANO ]
self[ 'PROG0' ][ -1 ] = prog0
self.nrow += 1
continue
# Arrive here if the line does not match any of the expected line
# formats (i.e. regexps).
self.warnings.append("Cannot interpret line: '{0}'".format( line ) )
# Close the catalogue file.
cat.close()
# Get a map giving the column index for each column name.
self.colindices = {}
for i in range( len(self.colnames) ):
self.colindices[ self.colnames[i] ] = i
# Remove unused elements from the usercols list.
self.usercols = [x for x in self.usercols if x]
# Get a list of the different metres found in the catalogue.
for metre in self['METRE']:
if metre:
if metre not in self.metres:
self.metres.append( metre )
self.metres.sort()
# Cannot search on metre if none were found in the catalogue.
if len(self.metres) == 0:
icol = self.colnames.index('METRE')
if icol >= 0:
self.colsearchable[icol] = False
# Verify the values read from the catalogue.
def verify(self):
irow = -1
for tags in self['TAGS']:
irow += 1
if tags:
for tag in tags:
if tag not in self.tagnames:
self.warnings.append("Unknown tag character ('{0}') "
"associated with '{1}'.".format(tag,self['TITLE'][irow] ) )
irow = -1
for origin in self['ORIGIN']:
irow += 1
if origin:
if origin not in self.orignames:
self.warnings.append("Unknown origin ('{0}') associated"
"with '{1}'.".format(origin, self['TITLE'][irow] ) )
irow = -1
for book in self['BOOK']:
irow += 1
if book:
if book not in self.booknames:
self.warnings.append("Unknown book ('{0}') associated"
" with '{1}'.".format(book,self['TITLE'][irow] ) )
irow = -1
for instr in self['INSTR']:
irow += 1
if instr:
if instr not in self.instrnames:
self.warnings.append("Unknown instrumentation ('{0}') "
"associated with '{1}'.".format(instr, self['TITLE'][irow] ) )
if instr != "KEYBD":
self['PROG0'][irow] = FIXED_INSTRUMENT
for colname in ('NUMBER','TRANS','SPEED','VOLUME','PROG0'):
irow = -1
for value in self[colname]:
irow += 1
if value:
try:
ival = int( value )
except Exception:
self.warnings.append("Illegal value ('{0}') for column "
"{2} associated with '{1}'.".
format(value, self['TITLE'][irow],colname ) )
else:
self[colname][ irow ] = "0"
# Check all the midi files exist.
for path in self['PATH']:
if path:
midifile = os.path.join(self.rootdir,path)
if os.path.isfile( midifile ):
self.midifiles.append( midifile )
else:
self.midifiles.append( None )
self.warnings.append("Midi file {0} cannot be found within "
"directory {1}.".format( path, self.rootdir ) )
else:
self.midifiles.append( None )
self.warnings.append("No midi file given for '{1}'.".
format(instr, self['TITLE'][irow] ) )
# Search for any uncatalogued midi files in the root directory. Return a
# new catalogue that contains their paths, and guesses at everything else.
# The structure of the new catalogue is inherited from self.
def searchForNew(self):
newMidis = []
badMidis = []
rlen = len( self.rootdir ) + 1
for root, subFolders, files in os.walk(self.rootdir):
for file in files:
if os.path.splitext(file)[1] == ".mid":
midifile = os.path.join( root, file )
newpath = midifile[rlen:]
if newpath not in self["PATH"]:
if isMidi( midifile ) == 0:
newMidis.append( newpath )
else:
badMidis.append( newpath )
ngood = len(newMidis)
nbad = len(badMidis)
if ngood + nbad > 0:
newcat = copy.copy( self )
newcat.nrow = ngood + nbad
num = re.compile( "^(\d+)\W*(.*)" )
for col in newcat:
newcat[col] = [''] * ( ngood + nbad )
newcat.midifiles = []
newcat['PATH'] = []
newcat['BOOK'] = []
newcat['NUMBER'] = []
newcat['INSTR'] = []
newcat['TITLE'] = []
newcat['ORIGIN'] = []
newcat['TRANS'] =['0'] * ( ngood + nbad )
newcat['SPEED'] =['0'] * ( ngood + nbad )
newcat['VOLUME'] =['0'] * ( ngood + nbad )
newcat['PROG0'] =[]
for file in newMidis:
newcat.midifiles.append( os.path.join( self.rootdir, file ) )
newcat['PATH'].append( file )
dir = os.path.dirname( file )
if dir.upper() in self.booknames:
newcat['BOOK'].append( dir.upper() )
if dir.upper() == 'STF':
newcat['INSTR'].append( 'KEYBD' )
newcat['PROG0'].append( '0' )
else:
newcat['INSTR'].append( '' )
newcat['PROG0'].append( '-1' )
else:
newcat['BOOK'].append( '' )
newcat['INSTR'].append( '' )
newcat['PROG0'].append( '-1' )
if dir.upper() in self.orignames:
newcat['ORIGIN'].append( dir.upper() )
else:
newcat['ORIGIN'].append( '' )
basename = os.path.splitext( os.path.basename( file ) )[0]
match = num.search( basename )
if match:
newcat['NUMBER'].append( match.group(1).strip() )
title = match.group(2)
else:
newcat['NUMBER'].append( '' )
title = basename
title = re.sub( '[^0-9a-zA-Z+]', ' ', title )
if not title.strip():
title = basename
newcat['TITLE'].append( title.strip() )
for file in badMidis:
newcat.midifiles.append( None )
newcat['PATH'].append( file )
newcat['BOOK'].append( '' )
newcat['NUMBER'].append( '' )
newcat['TITLE'].append( '' )
newcat['INSTR'].append( '' )
newcat['ORIGIN'].append( '' )
newcat['PROG0'].append( '' )
return newcat
else:
return None
# Add a new row to the end of the catalogue
def addrow(self,row,midifile):
icol = 0
for colname in self.colnames:
self[ colname ].append( row[icol] )
icol += 1
self.midifiles.append(midifile)
self.nrow += 1
self.modified = True
# Remove a row from the catalogue, specified by index.
def delrow(self,irow):
for colname in self.colnames:
del self[ colname ][ irow ]
del self.midifiles[ irow ]
self.nrow -= 1
self.modified = True
# Create a playable Record from a row of the catalogue
def getRecord(self,row,prog0=None,trans=None,volume=None,tempo=None):
path = self.midifiles[row]
if path:
if None == trans:
trans = self['TRANS'][row]
if None == prog0:
prog0 = self['PROG0'][row]
if None == volume:
volume = self['VOLUME'][row]
if None == tempo:
tempo = self['SPEED'][row]
title = self['TITLE'][row]
return Record( path, row, trans, tempo, volume, prog0, title )
else:
return None
# Return a tuple in which the first item indicates the type of the
# column ('t'=arbitrary text,'i'=positive integer, 's'=single selection
# from choice,'m'=multiple selections from choice). For 's' and 'm', the
# second item is a list of options, and the third item is a list of
# option descriptions (which may be None).
def getOptions(self,icol):
type = 't'
names = None
descs = None
if self.colnames[icol] == "TAGS":
type = 'm'
names = self.tagnames
descs = self.tagdescs
elif self.colnames[icol] == "BOOK":
type = 's'
names = self.booknames
descs = self.bookdescs
elif self.colnames[icol] == "NUMBER":
type = 'i'
descs = "Hymn/song number within book"
elif self.colnames[icol] == "INSTR":
type = 's'
names = self.instrnames
descs = self.instrdescs
elif self.colnames[icol] == "METRE":
type = 's'
names = self.metres
descs = "The metrical pattern of the hymn"
elif self.colnames[icol] == "TRANS":
type = 's'
names = []
descs = []
for i in range(-6,7):
names.append(str(i))
if i < -1:
descs.append("Transpose down by {0} semitones".format(-i))
elif i == -1:
descs.append("Transpose down by 1 semitone" )
elif i == 0:
descs.append("Play in original key" )
elif i == 1:
descs.append("Transpose up by 1 semitone" )
else:
descs.append("Transpose up by {0} semitones".format(i))
elif self.colnames[icol] == "ORIGIN":
type = 's'
names = self.orignames
descs = self.origdescs
elif self.colnames[icol] == "TITLE":
type = 't'
descs = "Title of hymn/song"
elif self.colnames[icol] == "PATH":
type = 'l'
descs = "Path to midi file within the music directory"
return (type,names,descs)
def save(self):
self.modified = False
for version in range(10000000):
backup = "{0}_v{1}".format(self.catname,version+1)
if not os.path.isfile( backup ):
break
os.rename( self.catname, backup )
print("Saving changes to music catalogue" )
cat = open( self.catname, "w" )
cat.write("# Path to root directory for MIDI files\n# -------------------------------------\n")
cat.write("r:{0}\n".format(self.rootdir))
cat.write("\n# Column names, 'searchable' flags, 'user interest' flag, and titles:\n# --------------------------------------------------------\n")
for (name,sea,user,desc) in zip(self.colnames,self.colsearchable,self.coluser,self.coldescs):
if sea:
sea = 1
else:
sea = 0
cat.write("c:{0} {1} {2} {3}\n".format(name,sea,user,desc))
cat.write("\n# Known books:\n# ------------\n")
for (name,desc) in zip( self.booknames,self.bookdescs):
cat.write("b:{0} {1}\n".format(name,desc))
cat.write("\n# Known instrumentations:\n# -----------------------\n")
for (name,desc) in zip( self.instrnames,self.instrdescs):
cat.write("i:{0} {1}\n".format(name,desc))
cat.write("\n# Known origins:\n# --------------\n")
for (name,desc) in zip( self.orignames,self.origdescs):
cat.write("o:{0} {1}\n".format(name,desc))
cat.write("\n# Classification tags (all single characters) and descriptions:\n# -------------------------------------------------------------\n")
for (name,desc) in zip( self.tagnames,self.tagdescs):
cat.write("t:{0} {1}\n".format(name,desc))
cat.write("\n\n\n# Data:\n# ----\n")
for irow in range(self.nrow):
icol = 0
text = ""
for col in self.colnames:
if icol > 0:
text += "@"
if self[col][irow]:
text += str(self[col][irow])
icol += 1
text += "\n"
cat.write(text)
cat.close()
def getUserValues(self,irow):
vals = []
tips = []
widths = []
for col in self.usercols:
val = self[col][irow]
if not val:
val = ""
tip = None
ok = False
else:
ok = True
if col == "TITLE":
width = 500;
if ok:
val = '"{0}"'.format(val)
tip = "The title or first line"
elif col == "METRE":
width = 150;
if ok:
val = '"{0}"'.format(val)
tip = "The rhythmic metre of the hymn/song"
elif col == "TUNE":
width = 250;
if ok:
val = '({0})'.format(val)
tip = "The name of the tune"
elif col == "BOOK":
width = 50;
if ok:
tip = self.bookdescs[self.booknames.index(val)]
elif col == "TAGS":
width = 60;
if ok:
tip = None
if val != "":
for i in range(len(self.tagnames)):
if self.tagnames[i] in val:
if tip == None:
tip = "Tags: {0}".format(self.tagdescs[i])
else:
tip = "{0}, {1}".format(tip,self.tagdescs[i])
elif col == "INSTR":
width = 100;
if ok:
for i in range(len(self.instrnames)):
if self.instrnames[i] == val:
tip = "Instrumentation: {0}".format(self.instrdescs[i])
elif col == "ORIGIN":
width = 50;
if ok:
if val == "STF":
tip = "The midi file was made by Methodist Publishing House"
elif val == "DSB":
tip = "The midi file was made by David Berry"
else:
tip = "The origin of the midi file..."
else:
width = 50;
tip = None
vals.append( val )
tips.append( tip )
widths.append( width )
return zip(vals,tips,widths)
def countTagMatches( self, tags ):
icol = self.colindices["TAGS"]
return len( self.search( [tags], [icol], sort=False ) )
def search( self, searchVals, searchCols, sort=True ):
matchingRows = range( self.nrow )
nmatch = self.nrow
book = None
number = None
for (icol,val) in zip(searchCols,searchVals):
if val:
if self.colnames[icol] == 'BOOK':
book = val.upper()
elif self.colnames[icol] == 'NUMBER':
number = int(val)
if book and number:
isalso = "{0}:{1}".format(book,number)
else:
isalso = None
for (icol,val) in zip(searchCols,searchVals):
if val:
newmatches = []
col = self.colnames[icol]
if col == "TAGS":
tags = list( val.lower() )
for irow in matchingRows:
lctext = self[col][irow]
if lctext:
lctext = lctext.lower()
ok = True
for tag in tags:
if tag == "g":
general = True
for xtag in EXCLUDE_FROM_GENERAL:
if xtag in lctext:
general = False
break;
if not general:
ok = False
break
elif not tag in lctext:
ok = False
break
if ok:
newmatches.append( irow )
elif col == "TITLE":
words = val.lower().split()
for irow in matchingRows:
title_words = re.compile('\w+').findall(self[col][irow].lower())
ok = True
for word in words:
if not word in title_words:
ok = False
break
if ok:
newmatches.append( irow )
elif ( col == "BOOK" or col == "NUMBER" ) and isalso:
for irow in matchingRows:
if self[col][irow] == val:
newmatches.append( irow )
elif self['ISALSO'][irow]:
if isalso+" " in self['ISALSO'][irow]:
newmatches.append( irow )
elif self['ISALSO'][irow].endswith(isalso):
newmatches.append( irow )
else:
for irow in matchingRows:
if self[col][irow] == val:
newmatches.append( irow )
matchingRows = newmatches
if len( matchingRows ) == 0:
break;
if sort and len( matchingRows ) > 1:
keys = {}
for irow in matchingRows:
thisbook = self['BOOK'][irow]
if book and thisbook == book:
thisbook = "AAAA"+thisbook
thisorigin = self['ORIGIN'][irow]
thistitle = self['TITLE'][irow]
keys[irow] = "{0}_{1}_{2}".format(thisorigin,thisbook,thistitle)
matchingRows.sort( key=lambda irow: keys[irow] )
return matchingRows
def makePlaylist( self, irows ):
result = Playlist()
for irow in irows:
title = "{0} {1}: {2}".format(self['BOOK'][irow],self['NUMBER'][irow],self['TITLE'][irow] )
result.add( self.rootdir+"/"+self['PATH'][irow], irow,
self['TRANS'][irow],
self['SPEED'][irow],
self['VOLUME'][irow],
self['PROG0'][irow],
title )
return result
# Find the row index of an entry with a given path.
def findPath(self,path):
tpath = str(path)
if tpath.startswith( self.rootdir+"/" ):
npath = tpath[ len(self.rootdir+"/"): ]
else:
npath = tpath
try:
result = self['PATH'].index( npath )
except ValueError:
result = -1
return result
# ----------------------------------------------------------------------
class Record(object):
def __init__(self, path, irow, trans, speed, volume, prog0, title ):
self._setPath( path )
self._setIrow( irow )
self._setTranspose( trans )
self._setInstrument( prog0 )
self._setTitle( title )
self._setVolume( volume )
self._setTempo( speed )
def _getPath(self):
return self._path
def _setPath(self,path):
ismidi = isMidi(path)
if ismidi == 1:
raise ChurchPlayerError("\n\nFailed to create a new Record: Not "
"a usable MIDI file '{0}'.".format(path))
elif ismidi == 2:
raise ChurchPlayerError("\n\nFailed to create a new Record: No "
"such file '{0}'.".format(path))
else:
self._path = path
path = property(_getPath, None, None, "The path to the music file")
def _getIrow(self):
return self._irow
def _setIrow(self,irow):
if isinstance(irow,str):
irow = int( irow )
if irow < 0:
raise ChurchPlayerError("\n\nBad irow value ({0}) when "
"creating a Record.".format(irow))
else:
self._irow = irow
irow = property( _getIrow, _setIrow, None, "The row number within the music catalogue" )
def _getTranspose(self):
return self._transpose
def _setTranspose(self,transpose):
if isinstance(transpose,str):
transpose = int( transpose )
if transpose < -8 or transpose > 8:
raise ChurchPlayerError("\n\nFailed to transpose a Record: "
"requested key shift ({0} semitones) "
"is too great.".format(transpose))
else:
self._transpose = transpose
def _delTranspose(self):
self._transpose = 0
transpose = property( _getTranspose, _setTranspose, _delTranspose,
"The number of semitones to transpose" )
def _getInstrument(self):
inst = self._instrument
if inst == FIXED_INSTRUMENT:
inst = DEFAULT_INSTRUMENT
return inst
def _setInstrument(self,instrument):
instrument = midiInstrument( instrument )
if instrument == None or instrument == DEFAULT_INSTRUMENT:
self._instrument = DEFAULT_INSTRUMENT
elif instrument == FIXED_INSTRUMENT:
self._instrument = FIXED_INSTRUMENT
elif instrument < 0 or instrument > 127:
raise ChurchPlayerError("\n\nFailed to change instrument for a "
"Record: requested instrument number "
"({0}) is illegal (must be between 0 "
"and 127).".format(instrument))
else:
self._instrument = instrument
def _delInstrument(self):
self._instrument = DEFAULT_INSTRUMENT
instrument = property( _getInstrument, _setInstrument, _delInstrument,
"The GM program number for the instrument to use")
def _getTitle(self):
return self._title
def _setTitle(self,title):
self._title = title
title = property(_getTitle, None, None, "The music title")
def _getVolume(self):
return self._volume
def _setVolume(self,volume):
if isinstance(volume,str):
self._volume = int( volume )
else:
self._volume = volume
def _delVolume(self):
self._volume = 0
volume = property( _getVolume, _setVolume, _delVolume,
"The change in volume" )
def _getTempo(self):
return self._tempo
def _setTempo(self,tempo):
if isinstance(tempo,str):
self._tempo = int( tempo )
else:
self._tempo = tempo
def _delTempo(self):
self._tempo = 0
tempo = property( _getTempo, _setTempo, _delTempo,
"The change in tempo" )
def __str__(self):
return "Path:{0} Tran:{1} Instr:{2} Title:{3}".format(self._getPath(),self._getTranspose(),self._getInstrument(),self._getTitle())
def desc(self):
return self._title
# ----------------------------------------------------------------------
class Playlist(object):
def __init__(self):
self._records = []
def add( self, path, row, trans, speed, volume, prog0, title ):
self._records.append( Record( path, row, trans, speed, volume, prog0, title ) )
def __len__(self):
return len(self._records)