asigalov61 commited on
Commit
8c695bf
1 Parent(s): fd45152

Upload 2 files

Browse files
TMIDIX.py CHANGED
@@ -1484,6 +1484,7 @@ from abc import ABC, abstractmethod
1484
  from difflib import SequenceMatcher as SM
1485
 
1486
  import statistics
 
1487
 
1488
  import matplotlib.pyplot as plt
1489
 
@@ -6391,6 +6392,302 @@ def reverse_enhanced_score_notes(enhanced_score_notes):
6391
 
6392
  ###################################################################################
6393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6394
  # This is the end of the TMIDI X Python module
6395
 
6396
  ###################################################################################
 
1484
  from difflib import SequenceMatcher as SM
1485
 
1486
  import statistics
1487
+ import math
1488
 
1489
  import matplotlib.pyplot as plt
1490
 
 
6392
 
6393
  ###################################################################################
6394
 
6395
+ def count_patterns(lst, sublist):
6396
+ count = 0
6397
+ idx = 0
6398
+ for i in range(len(lst) - len(sublist) + 1):
6399
+ if lst[idx:idx + len(sublist)] == sublist:
6400
+ count += 1
6401
+ idx += len(sublist)
6402
+ else:
6403
+ idx += 1
6404
+ return count
6405
+
6406
+ def find_lrno_patterns(seq):
6407
+
6408
+ all_seqs = Counter()
6409
+
6410
+ max_pat_len = math.ceil(len(seq) / 2)
6411
+
6412
+ num_iter = 0
6413
+
6414
+ for i in range(len(seq)):
6415
+ for j in range(i+1, len(seq)+1):
6416
+ if j-i <= max_pat_len:
6417
+ all_seqs[tuple(seq[i:j])] += 1
6418
+ num_iter += 1
6419
+
6420
+ max_count = 0
6421
+ max_len = 0
6422
+
6423
+ for val, count in all_seqs.items():
6424
+
6425
+ if max_len < len(val):
6426
+ max_count = max(2, count)
6427
+
6428
+ if count > 1:
6429
+ max_len = max(max_len, len(val))
6430
+ pval = val
6431
+
6432
+ max_pats = []
6433
+
6434
+ for val, count in all_seqs.items():
6435
+ if count == max_count and len(val) == max_len:
6436
+ max_pats.append(val)
6437
+
6438
+ found_patterns = []
6439
+
6440
+ for pat in max_pats:
6441
+ count = count_patterns(seq, list(pat))
6442
+ if count > 1:
6443
+ found_patterns.append([count, len(pat), pat])
6444
+
6445
+ return found_patterns
6446
+
6447
+ ###################################################################################
6448
+
6449
+ def delta_pitches(escore_notes, pitches_index=4):
6450
+
6451
+ pitches = [p[pitches_index] for p in escore_notes]
6452
+
6453
+ return [a-b for a, b in zip(pitches[:-1], pitches[1:])]
6454
+
6455
+ ###################################################################################
6456
+
6457
+ def split_list(lst, val):
6458
+ return [lst[i:j] for i, j in zip([0] + [k + 1 for k, x in enumerate(lst) if x == val], [k for k, x in enumerate(lst) if x == val] + [len(lst)]) if j > i]
6459
+
6460
+ ###################################################################################
6461
+
6462
+ def even_timings(escore_notes,
6463
+ times_idx=1,
6464
+ durs_idx=2
6465
+ ):
6466
+
6467
+ esn = copy.deepcopy(escore_notes)
6468
+
6469
+ for e in esn:
6470
+
6471
+ if e[times_idx] != 0:
6472
+ if e[times_idx] % 2 != 0:
6473
+ e[times_idx] += 1
6474
+
6475
+ if e[durs_idx] % 2 != 0:
6476
+ e[durs_idx] += 1
6477
+
6478
+ return esn
6479
+
6480
+ ###################################################################################
6481
+
6482
+ def delta_score_to_abs_score(delta_score_notes,
6483
+ times_idx=1
6484
+ ):
6485
+
6486
+ abs_score = copy.deepcopy(delta_score_notes)
6487
+
6488
+ abs_time = 0
6489
+
6490
+ for i, e in enumerate(delta_score_notes):
6491
+
6492
+ dtime = e[times_idx]
6493
+
6494
+ abs_time += dtime
6495
+
6496
+ abs_score[i][times_idx] = abs_time
6497
+
6498
+ return abs_score
6499
+
6500
+ ###################################################################################
6501
+
6502
+
6503
+ def adjust_numbers_to_sum(numbers, target_sum):
6504
+
6505
+ current_sum = sum(numbers)
6506
+ difference = target_sum - current_sum
6507
+
6508
+ non_zero_elements = [(i, num) for i, num in enumerate(numbers) if num != 0]
6509
+
6510
+ total_non_zero = sum(num for _, num in non_zero_elements)
6511
+
6512
+ increments = []
6513
+ for i, num in non_zero_elements:
6514
+ proportion = num / total_non_zero
6515
+ increment = proportion * difference
6516
+ increments.append(increment)
6517
+
6518
+ for idx, (i, num) in enumerate(non_zero_elements):
6519
+ numbers[i] += int(round(increments[idx]))
6520
+
6521
+ current_sum = sum(numbers)
6522
+ difference = target_sum - current_sum
6523
+ non_zero_indices = [i for i, num in enumerate(numbers) if num != 0]
6524
+
6525
+ for i in range(abs(difference)):
6526
+ numbers[non_zero_indices[i % len(non_zero_indices)]] += 1 if difference > 0 else -1
6527
+
6528
+ return numbers
6529
+
6530
+ ###################################################################################
6531
+
6532
+ def find_next_bar(escore_notes, bar_time, start_note_idx, cur_bar):
6533
+ for e in escore_notes[start_note_idx:]:
6534
+ if e[1] // bar_time > cur_bar:
6535
+ return e, escore_notes.index(e)
6536
+
6537
+ ###################################################################################
6538
+
6539
+ def align_escore_notes_to_bars(escore_notes,
6540
+ bar_time=4000,
6541
+ trim_durations=False,
6542
+ split_durations=False
6543
+ ):
6544
+
6545
+ #=============================================================================
6546
+
6547
+ aligned_escore_notes = copy.deepcopy(escore_notes)
6548
+
6549
+ abs_time = 0
6550
+ nidx = 0
6551
+ delta = 0
6552
+ bcount = 0
6553
+ next_bar = [0]
6554
+
6555
+ #=============================================================================
6556
+
6557
+ while next_bar:
6558
+
6559
+ next_bar = find_next_bar(escore_notes, bar_time, nidx, bcount)
6560
+
6561
+ if next_bar:
6562
+
6563
+ gescore_notes = escore_notes[nidx:next_bar[1]]
6564
+ else:
6565
+ gescore_notes = escore_notes[nidx:]
6566
+
6567
+ original_timings = [delta] + [(b[1]-a[1]) for a, b in zip(gescore_notes[:-1], gescore_notes[1:])]
6568
+ adj_timings = adjust_numbers_to_sum(original_timings, bar_time)
6569
+
6570
+ for t in adj_timings:
6571
+
6572
+ abs_time += t
6573
+
6574
+ aligned_escore_notes[nidx][1] = abs_time
6575
+ aligned_escore_notes[nidx][2] -= int(bar_time // 200)
6576
+
6577
+ nidx += 1
6578
+
6579
+ if next_bar:
6580
+ delta = escore_notes[next_bar[1]][1]-escore_notes[next_bar[1]-1][1]
6581
+ bcount += 1
6582
+
6583
+ #=============================================================================
6584
+
6585
+ aligned_adjusted_escore_notes = []
6586
+ bcount = 0
6587
+
6588
+ for a in aligned_escore_notes:
6589
+ bcount = a[1] // bar_time
6590
+ nbtime = bar_time * (bcount+1)
6591
+
6592
+ if a[1]+a[2] > nbtime and a[3] != 9:
6593
+ if trim_durations or split_durations:
6594
+ ddiff = ((a[1]+a[2])-nbtime)
6595
+ aa = copy.deepcopy(a)
6596
+ aa[2] = a[2] - ddiff
6597
+ aligned_adjusted_escore_notes.append(aa)
6598
+
6599
+ if split_durations:
6600
+ aaa = copy.deepcopy(a)
6601
+ aaa[1] = a[1]+aa[2]
6602
+ aaa[2] = ddiff
6603
+
6604
+ aligned_adjusted_escore_notes.append(aaa)
6605
+
6606
+ else:
6607
+ aligned_adjusted_escore_notes.append(a)
6608
+
6609
+ else:
6610
+ aligned_adjusted_escore_notes.append(a)
6611
+
6612
+ #=============================================================================
6613
+
6614
+ return aligned_adjusted_escore_notes
6615
+
6616
+ ###################################################################################
6617
+
6618
+ def normalize_chord_durations(chord,
6619
+ dur_idx=2,
6620
+ norm_factor=100
6621
+ ):
6622
+
6623
+ nchord = copy.deepcopy(chord)
6624
+
6625
+ for c in nchord:
6626
+ c[dur_idx] = int(round(max(1 / norm_factor, c[dur_idx] // norm_factor) * norm_factor))
6627
+
6628
+ return nchord
6629
+
6630
+ ###################################################################################
6631
+
6632
+ def normalize_chordified_score_durations(chordified_score,
6633
+ dur_idx=2,
6634
+ norm_factor=100
6635
+ ):
6636
+
6637
+ ncscore = copy.deepcopy(chordified_score)
6638
+
6639
+ for cc in ncscore:
6640
+ for c in cc:
6641
+ c[dur_idx] = int(round(max(1 / norm_factor, c[dur_idx] // norm_factor) * norm_factor))
6642
+
6643
+ return ncscore
6644
+
6645
+ ###################################################################################
6646
+
6647
+ def horizontal_ordered_list_search(list_of_lists,
6648
+ query_list,
6649
+ start_idx=0,
6650
+ end_idx=-1
6651
+ ):
6652
+
6653
+ lol = list_of_lists
6654
+
6655
+ results = []
6656
+
6657
+ if start_idx > 0:
6658
+ lol = list_of_lists[start_idx:]
6659
+
6660
+ if start_idx == -1:
6661
+ idx = -1
6662
+ for i, l in enumerate(list_of_lists):
6663
+ try:
6664
+ idx = l.index(query_list[0])
6665
+ lol = list_of_lists[i:]
6666
+ break
6667
+ except:
6668
+ continue
6669
+
6670
+ if idx == -1:
6671
+ results.append(-1)
6672
+ return results
6673
+ else:
6674
+ results.append(i)
6675
+
6676
+ if end_idx != -1:
6677
+ lol = list_of_lists[start_idx:start_idx+max(end_idx, len(query_list))]
6678
+
6679
+ for i, q in enumerate(query_list):
6680
+ try:
6681
+ idx = lol[i].index(q)
6682
+ results.append(idx)
6683
+ except:
6684
+ results.append(-1)
6685
+ return results
6686
+
6687
+ return results
6688
+
6689
+ ###################################################################################
6690
+
6691
  # This is the end of the TMIDI X Python module
6692
 
6693
  ###################################################################################
pitches_chords_progressions_5_3_15.pickle ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2483e8388086f8c9e9c345e3bb9d14f6df7a8004428d671e74606b04c19a51c0
3
+ size 98809865