projectlosangeles commited on
Commit
758037b
1 Parent(s): 0039f6c

Upload 4 files

Browse files
Los_Angeles_MIDI_Dataset_CHORDS_DATA_Decoder.ipynb ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "gradient": {
7
+ "editing": false,
8
+ "id": "ac5a4cf0-d9d2-47b5-9633-b53f8d99a4d2",
9
+ "kernelId": ""
10
+ },
11
+ "id": "SiTIpPjArIyr"
12
+ },
13
+ "source": [
14
+ "# Los Angeles MIDI Dataset CHORDS DATA Decoder (ver. 1.0)\n",
15
+ "\n",
16
+ "***\n",
17
+ "\n",
18
+ "Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools\n",
19
+ "\n",
20
+ "***\n",
21
+ "\n",
22
+ "#### Project Los Angeles\n",
23
+ "\n",
24
+ "#### Tegridy Code 2024\n",
25
+ "\n",
26
+ "***"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "markdown",
31
+ "metadata": {
32
+ "gradient": {
33
+ "editing": false,
34
+ "id": "fa0a611c-1803-42ae-bdf6-a49b5a4e781b",
35
+ "kernelId": ""
36
+ },
37
+ "id": "gOd93yV0sGd2"
38
+ },
39
+ "source": [
40
+ "# (SETUP ENVIRONMENT)"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": null,
46
+ "metadata": {
47
+ "cellView": "form",
48
+ "gradient": {
49
+ "editing": false,
50
+ "id": "a1a45a91-d909-4fd4-b67a-5e16b971d179",
51
+ "kernelId": ""
52
+ },
53
+ "id": "fX12Yquyuihc"
54
+ },
55
+ "outputs": [],
56
+ "source": [
57
+ "#@title Install all dependencies (run only once per session)\n",
58
+ "\n",
59
+ "!git clone https://github.com/asigalov61/tegridy-tools\n",
60
+ "!pip install tqdm"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": null,
66
+ "metadata": {
67
+ "cellView": "form",
68
+ "gradient": {
69
+ "editing": false,
70
+ "id": "b8207b76-9514-4c07-95db-95a4742e52c5",
71
+ "kernelId": ""
72
+ },
73
+ "id": "z7n9vnKmug1J"
74
+ },
75
+ "outputs": [],
76
+ "source": [
77
+ "#@title Import all needed modules\n",
78
+ "\n",
79
+ "print('Loading core modules... Please wait...')\n",
80
+ "import os\n",
81
+ "\n",
82
+ "import math\n",
83
+ "import statistics\n",
84
+ "import random\n",
85
+ "from collections import Counter\n",
86
+ "import shutil\n",
87
+ "import hashlib\n",
88
+ "from tqdm import tqdm\n",
89
+ "\n",
90
+ "print('Creating IO dirs...')\n",
91
+ "\n",
92
+ "if not os.path.exists('/content/CHORDS_DATA'):\n",
93
+ " os.makedirs('/content/CHORDS_DATA')\n",
94
+ "\n",
95
+ "print('Loading TMIDIX module...')\n",
96
+ "os.chdir('/content/tegridy-tools/tegridy-tools')\n",
97
+ "\n",
98
+ "import TMIDIX\n",
99
+ "\n",
100
+ "print('Done!')\n",
101
+ "\n",
102
+ "os.chdir('/content/')\n",
103
+ "print('Enjoy! :)')"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "markdown",
108
+ "source": [
109
+ "# (Load sample LAMDa CHORDS DATA file)"
110
+ ],
111
+ "metadata": {
112
+ "id": "ijCbwyQ0JFhN"
113
+ }
114
+ },
115
+ {
116
+ "cell_type": "code",
117
+ "source": [
118
+ "chords_data = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/CHORDS_DATA/LAMDa_CHORDS_DATA_2500')\n",
119
+ "print('Done!')"
120
+ ],
121
+ "metadata": {
122
+ "id": "o__b6ZQMHUiW"
123
+ },
124
+ "execution_count": null,
125
+ "outputs": []
126
+ },
127
+ {
128
+ "cell_type": "markdown",
129
+ "source": [
130
+ "# (Decode to MIDI)"
131
+ ],
132
+ "metadata": {
133
+ "id": "sd5-yZM0JNK8"
134
+ }
135
+ },
136
+ {
137
+ "cell_type": "code",
138
+ "source": [
139
+ "data = random.choice(chords_data)\n",
140
+ "\n",
141
+ "file_name = data[0]\n",
142
+ "song = data[1]\n",
143
+ "\n",
144
+ "song_f = []\n",
145
+ "\n",
146
+ "time = 0\n",
147
+ "dur = 0\n",
148
+ "vel = 90\n",
149
+ "pitch = 0\n",
150
+ "channel = 0\n",
151
+ "\n",
152
+ "patches = [-1] * 16\n",
153
+ "\n",
154
+ "channels = [0] * 16\n",
155
+ "channels[9] = 1\n",
156
+ "\n",
157
+ "for ss in song:\n",
158
+ "\n",
159
+ " time += ss[0] * 16\n",
160
+ "\n",
161
+ " for i in range(0, len(ss[1:]), 4):\n",
162
+ "\n",
163
+ " s = ss[1:][i:i+4]\n",
164
+ "\n",
165
+ " dur = s[0] * 16\n",
166
+ "\n",
167
+ " patch = s[1]\n",
168
+ "\n",
169
+ " if patch < 128:\n",
170
+ " if patch not in patches:\n",
171
+ " if 0 in channels:\n",
172
+ " cha = channels.index(0)\n",
173
+ " channels[cha] = 1\n",
174
+ " else:\n",
175
+ " cha = 15\n",
176
+ " patches[cha] = patch\n",
177
+ " channel = patches.index(patch)\n",
178
+ " else:\n",
179
+ " channel = patches.index(patch)\n",
180
+ "\n",
181
+ " if patch == 128:\n",
182
+ " channel = 9\n",
183
+ "\n",
184
+ " pitch = s[2]\n",
185
+ "\n",
186
+ " vel = s[3]\n",
187
+ "\n",
188
+ " song_f.append(['note', time, dur, channel, pitch, vel, patch ])\n",
189
+ "\n",
190
+ "patches = [0 if x==-1 else x for x in patches]\n",
191
+ "\n",
192
+ "\n",
193
+ "detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,\n",
194
+ " output_signature = file_name+'.mid',\n",
195
+ " output_file_name = '/content/Los-Angeles-MIDI-Dataset-Composition',\n",
196
+ " track_name='Project Los Angeles',\n",
197
+ " list_of_MIDI_patches=patches\n",
198
+ " )"
199
+ ],
200
+ "metadata": {
201
+ "id": "pG1-n4qhHt7d"
202
+ },
203
+ "execution_count": null,
204
+ "outputs": []
205
+ },
206
+ {
207
+ "cell_type": "markdown",
208
+ "metadata": {
209
+ "id": "YzCMd94Tu_gz"
210
+ },
211
+ "source": [
212
+ "# Congrats! You did it! :)"
213
+ ]
214
+ }
215
+ ],
216
+ "metadata": {
217
+ "colab": {
218
+ "machine_shape": "hm",
219
+ "private_outputs": true,
220
+ "provenance": []
221
+ },
222
+ "gpuClass": "standard",
223
+ "kernelspec": {
224
+ "display_name": "Python 3 (ipykernel)",
225
+ "language": "python",
226
+ "name": "python3"
227
+ },
228
+ "language_info": {
229
+ "codemirror_mode": {
230
+ "name": "ipython",
231
+ "version": 3
232
+ },
233
+ "file_extension": ".py",
234
+ "mimetype": "text/x-python",
235
+ "name": "python",
236
+ "nbconvert_exporter": "python",
237
+ "pygments_lexer": "ipython3",
238
+ "version": "3.9.7"
239
+ }
240
+ },
241
+ "nbformat": 4,
242
+ "nbformat_minor": 0
243
+ }
TMIDIX.py CHANGED
@@ -280,7 +280,7 @@ then opus2score()
280
  '''
281
  return opus2score(to_millisecs(midi2opus(midi)))
282
 
283
- def midi2single_track_ms_score(midi=b'', recalculate_channels = True, verbose = False):
284
  r'''
285
  Translates MIDI into a single track "score" with 16 instruments and one beat per second and one
286
  tick per millisecond
@@ -343,13 +343,13 @@ tick per millisecond
343
  itrack += 1
344
 
345
  opus = score2opus([score[0], events_matrix1])
346
- ms_score = opus2score(to_millisecs(opus))
347
 
348
  return ms_score
349
 
350
  #------------------------ Other Transformations ---------------------
351
 
352
- def to_millisecs(old_opus=None, desired_time_in_ms=1):
353
  r'''Recallibrates all the times in an "opus" to use one beat
354
  per second and one tick per millisecond. This makes it
355
  hard to retrieve any information about beats or barlines,
@@ -389,7 +389,11 @@ but it does make it easy to mix different scores together.
389
  ticks_so_far = 0
390
  ms_so_far = 0.0
391
  previous_ms_so_far = 0.0
392
- new_track = [['set_tempo',0,1000000 * desired_time_in_ms],] # new "crochet" is 1 sec
 
 
 
 
393
  for old_event in old_opus[itrack]:
394
  # detect if ticks2tempo has something before this event
395
  # 20160702 if ticks2tempo is at the same time, leave it
@@ -405,9 +409,19 @@ but it does make it easy to mix different scores together.
405
  new_event = copy.deepcopy(old_event) # now handle the new event
406
  ms_so_far += (ms_per_old_tick * old_event[1] * desired_time_in_ms)
407
  new_event[1] = round(ms_so_far - previous_ms_so_far)
408
- if old_event[0] != 'set_tempo':
409
- previous_ms_so_far = ms_so_far
410
- new_track.append(new_event)
 
 
 
 
 
 
 
 
 
 
411
  ticks_so_far += event_delta_ticks
412
  new_opus.append(new_track)
413
  itrack += 1
@@ -1458,6 +1472,8 @@ from difflib import SequenceMatcher as SM
1458
 
1459
  import statistics
1460
 
 
 
1461
  ###################################################################################
1462
  #
1463
  # Original TMIDI Tegridy helper functions
@@ -1692,6 +1708,90 @@ def Tegridy_ms_SONG_to_MIDI_Converter(SONG,
1692
 
1693
  ###################################################################################
1694
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1695
  def Tegridy_SONG_to_Full_MIDI_Converter(SONG,
1696
  output_signature = 'Tegridy TMIDI Module',
1697
  track_name = 'Composition Track',
@@ -3382,6 +3482,1034 @@ def Tegridy_Split_List(list_to_split, split_value=0):
3382
 
3383
  ###################################################################################
3384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3385
  # This is the end of the TMIDI X Python module
3386
 
3387
  ###################################################################################
 
280
  '''
281
  return opus2score(to_millisecs(midi2opus(midi)))
282
 
283
+ def midi2single_track_ms_score(midi=b'', recalculate_channels = True, pass_old_timings_events= False, verbose = False):
284
  r'''
285
  Translates MIDI into a single track "score" with 16 instruments and one beat per second and one
286
  tick per millisecond
 
343
  itrack += 1
344
 
345
  opus = score2opus([score[0], events_matrix1])
346
+ ms_score = opus2score(to_millisecs(opus, pass_old_timings_events=pass_old_timings_events))
347
 
348
  return ms_score
349
 
350
  #------------------------ Other Transformations ---------------------
351
 
352
+ def to_millisecs(old_opus=None, desired_time_in_ms=1, pass_old_timings_events = False):
353
  r'''Recallibrates all the times in an "opus" to use one beat
354
  per second and one tick per millisecond. This makes it
355
  hard to retrieve any information about beats or barlines,
 
389
  ticks_so_far = 0
390
  ms_so_far = 0.0
391
  previous_ms_so_far = 0.0
392
+
393
+ if pass_old_timings_events:
394
+ new_track = [['set_tempo',0,1000000 * desired_time_in_ms],['old_tpq', 0, old_tpq]] # new "crochet" is 1 sec
395
+ else:
396
+ new_track = [['set_tempo',0,1000000 * desired_time_in_ms],] # new "crochet" is 1 sec
397
  for old_event in old_opus[itrack]:
398
  # detect if ticks2tempo has something before this event
399
  # 20160702 if ticks2tempo is at the same time, leave it
 
409
  new_event = copy.deepcopy(old_event) # now handle the new event
410
  ms_so_far += (ms_per_old_tick * old_event[1] * desired_time_in_ms)
411
  new_event[1] = round(ms_so_far - previous_ms_so_far)
412
+
413
+ if pass_old_timings_events:
414
+ if old_event[0] != 'set_tempo':
415
+ previous_ms_so_far = ms_so_far
416
+ new_track.append(new_event)
417
+ else:
418
+ new_event[0] = 'old_set_tempo'
419
+ previous_ms_so_far = ms_so_far
420
+ new_track.append(new_event)
421
+ else:
422
+ if old_event[0] != 'set_tempo':
423
+ previous_ms_so_far = ms_so_far
424
+ new_track.append(new_event)
425
  ticks_so_far += event_delta_ticks
426
  new_opus.append(new_track)
427
  itrack += 1
 
1472
 
1473
  import statistics
1474
 
1475
+ import matplotlib.pyplot as plt
1476
+
1477
  ###################################################################################
1478
  #
1479
  # Original TMIDI Tegridy helper functions
 
1708
 
1709
  ###################################################################################
1710
 
1711
+ def hsv_to_rgb(h, s, v):
1712
+ if s == 0.0:
1713
+ return v, v, v
1714
+ i = int(h*6.0)
1715
+ f = (h*6.0) - i
1716
+ p = v*(1.0 - s)
1717
+ q = v*(1.0 - s*f)
1718
+ t = v*(1.0 - s*(1.0-f))
1719
+ i = i%6
1720
+ return [(v, t, p), (q, v, p), (p, v, t), (p, q, v), (t, p, v), (v, p, q)][i]
1721
+
1722
+ def generate_colors(n):
1723
+ return [hsv_to_rgb(i/n, 1, 1) for i in range(n)]
1724
+
1725
+ def add_arrays(a, b):
1726
+ return [sum(pair) for pair in zip(a, b)]
1727
+
1728
+ #-------------------------------------------------------------------------------
1729
+
1730
+ def plot_ms_SONG(ms_song,
1731
+ preview_length_in_notes=0,
1732
+ block_lines_times_list = None,
1733
+ plot_title='ms Song',
1734
+ max_num_colors=129,
1735
+ drums_color_num=128,
1736
+ plot_size=(11,4),
1737
+ note_height = 0.75,
1738
+ show_grid_lines=False):
1739
+
1740
+ '''Tegridy ms SONG plotter/vizualizer'''
1741
+
1742
+ notes = [s for s in ms_song if s[0] == 'note']
1743
+
1744
+ if (len(max(notes, key=len)) != 7) and (len(min(notes, key=len)) != 7):
1745
+ print('The song notes do not have patches information')
1746
+ print('Ploease add patches to the notes in the song')
1747
+
1748
+ else:
1749
+
1750
+ start_times = [s[1] / 1000 for s in notes]
1751
+ durations = [s[2] / 1000 for s in notes]
1752
+ pitches = [s[4] for s in notes]
1753
+ patches = [s[6] for s in notes]
1754
+
1755
+ colors = generate_colors(max_num_colors)
1756
+ colors[drums_color_num] = (1, 1, 1)
1757
+
1758
+ pbl = notes[preview_length_in_notes][1] / 1000
1759
+
1760
+ fig, ax = plt.subplots(figsize=plot_size)
1761
+ #fig, ax = plt.subplots()
1762
+
1763
+ # Create a rectangle for each note with color based on patch number
1764
+ for start, duration, pitch, patch in zip(start_times, durations, pitches, patches):
1765
+ rect = plt.Rectangle((start, pitch), duration, note_height, facecolor=colors[patch])
1766
+ ax.add_patch(rect)
1767
+
1768
+ # Set the limits of the plot
1769
+ ax.set_xlim([min(start_times), max(add_arrays(start_times, durations))])
1770
+ ax.set_ylim([min(pitches)-1, max(pitches)+1])
1771
+
1772
+ # Set the background color to black
1773
+ ax.set_facecolor('black')
1774
+ fig.patch.set_facecolor('white')
1775
+
1776
+ if preview_length_in_notes > 0:
1777
+ ax.axvline(x=pbl, c='white')
1778
+
1779
+ if block_lines_times_list:
1780
+ for bl in block_lines_times_list:
1781
+ ax.axvline(x=bl, c='white')
1782
+
1783
+ if show_grid_lines:
1784
+ ax.grid(color='white')
1785
+
1786
+ plt.xlabel('Time', c='black')
1787
+ plt.ylabel('Pitch', c='black')
1788
+
1789
+ plt.title(plot_title)
1790
+
1791
+ plt.show()
1792
+
1793
+ ###################################################################################
1794
+
1795
  def Tegridy_SONG_to_Full_MIDI_Converter(SONG,
1796
  output_signature = 'Tegridy TMIDI Module',
1797
  track_name = 'Composition Track',
 
3482
 
3483
  ###################################################################################
3484
 
3485
+ # Binary chords functions
3486
+
3487
+ def tones_chord_to_bits(chord):
3488
+ bits = [0] * 12
3489
+ for num in chord:
3490
+ bits[num] = 1
3491
+
3492
+ return bits
3493
+
3494
+ def bits_to_tones_chord(bits):
3495
+ return [i for i, bit in enumerate(bits) if bit == 1]
3496
+
3497
+ def shift_bits(bits, n):
3498
+ return bits[-n:] + bits[:-n]
3499
+
3500
+ def bits_to_int(bits, shift_bits_value=0):
3501
+ bits = shift_bits(bits, shift_bits_value)
3502
+ result = 0
3503
+ for bit in bits:
3504
+ result = (result << 1) | bit
3505
+
3506
+ return result
3507
+
3508
+ def int_to_bits(n):
3509
+ bits = [0] * 12
3510
+ for i in range(12):
3511
+ bits[11 - i] = n % 2
3512
+ n //= 2
3513
+
3514
+ return bits
3515
+
3516
+ def bad_chord(chord):
3517
+ bad = any(b - a == 1 for a, b in zip(chord, chord[1:]))
3518
+ if (0 in chord) and (11 in chord):
3519
+ bad = True
3520
+
3521
+ return bad
3522
+
3523
+ def pitches_chord_to_int(pitches_chord, tones_transpose_value=0):
3524
+
3525
+ pitches_chord = [x for x in pitches_chord if 0 < x < 128]
3526
+
3527
+ if not (-12 < tones_transpose_value < 12):
3528
+ tones_transpose_value = 0
3529
+
3530
+ tones_chord = sorted(list(set([c % 12 for c in sorted(list(set(pitches_chord)))])))
3531
+ bits = tones_chord_to_bits(tones_chord)
3532
+ integer = bits_to_int(bits, shift_bits_value=tones_transpose_value)
3533
+
3534
+ return integer
3535
+
3536
+ def int_to_pitches_chord(integer, chord_base_pitch=60):
3537
+ if 0 < integer < 4096:
3538
+ bits = int_to_bits(integer)
3539
+ tones_chord = bits_to_tones_chord(bits)
3540
+ if not bad_chord(tones_chord):
3541
+ pitches_chord = [t+chord_base_pitch for t in tones_chord]
3542
+ return [pitches_chord, tones_chord]
3543
+
3544
+ else:
3545
+ return 0 # Bad chord code
3546
+
3547
+ else:
3548
+ return -1 # Bad integer code
3549
+
3550
+ ###################################################################################
3551
+
3552
+ def bad_chord(chord):
3553
+ bad = any(b - a == 1 for a, b in zip(chord, chord[1:]))
3554
+ if (0 in chord) and (11 in chord):
3555
+ bad = True
3556
+
3557
+ return bad
3558
+
3559
+ def validate_pitches_chord(pitches_chord, return_sorted = True):
3560
+
3561
+ pitches_chord = sorted(list(set([x for x in pitches_chord if 0 < x < 128])))
3562
+
3563
+ tones_chord = sorted(list(set([c % 12 for c in sorted(list(set(pitches_chord)))])))
3564
+
3565
+ if not bad_chord(tones_chord):
3566
+ if return_sorted:
3567
+ pitches_chord.sort(reverse=True)
3568
+ return pitches_chord
3569
+
3570
+ else:
3571
+ if 0 in tones_chord and 11 in tones_chord:
3572
+ tones_chord.remove(0)
3573
+
3574
+ fixed_tones = [[a, b] for a, b in zip(tones_chord, tones_chord[1:]) if b-a != 1]
3575
+
3576
+ fixed_tones_chord = []
3577
+ for f in fixed_tones:
3578
+ fixed_tones_chord.extend(f)
3579
+ fixed_tones_chord = list(set(fixed_tones_chord))
3580
+
3581
+ fixed_pitches_chord = []
3582
+
3583
+ for p in pitches_chord:
3584
+ if (p % 12) in fixed_tones_chord:
3585
+ fixed_pitches_chord.append(p)
3586
+
3587
+ if return_sorted:
3588
+ fixed_pitches_chord.sort(reverse=True)
3589
+
3590
+ return fixed_pitches_chord
3591
+
3592
+ def validate_pitches(chord, channel_to_check = 0, return_sorted = True):
3593
+
3594
+ pitches_chord = sorted(list(set([x[4] for x in chord if 0 < x[4] < 128 and x[3] == channel_to_check])))
3595
+
3596
+ if pitches_chord:
3597
+
3598
+ tones_chord = sorted(list(set([c % 12 for c in sorted(list(set(pitches_chord)))])))
3599
+
3600
+ if not bad_chord(tones_chord):
3601
+ if return_sorted:
3602
+ chord.sort(key = lambda x: x[4], reverse=True)
3603
+ return chord
3604
+
3605
+ else:
3606
+ if 0 in tones_chord and 11 in tones_chord:
3607
+ tones_chord.remove(0)
3608
+
3609
+ fixed_tones = [[a, b] for a, b in zip(tones_chord, tones_chord[1:]) if b-a != 1]
3610
+
3611
+ fixed_tones_chord = []
3612
+ for f in fixed_tones:
3613
+ fixed_tones_chord.extend(f)
3614
+ fixed_tones_chord = list(set(fixed_tones_chord))
3615
+
3616
+ fixed_chord = []
3617
+
3618
+ for c in chord:
3619
+ if c[3] == channel_to_check:
3620
+ if (c[4] % 12) in fixed_tones_chord:
3621
+ fixed_chord.append(c)
3622
+ else:
3623
+ fixed_chord.append(c)
3624
+
3625
+ if return_sorted:
3626
+ fixed_chord.sort(key = lambda x: x[4], reverse=True)
3627
+
3628
+ return fixed_chord
3629
+
3630
+ else:
3631
+ chord.sort(key = lambda x: x[4], reverse=True)
3632
+ return chord
3633
+
3634
+ def adjust_score_velocities(score, max_velocity):
3635
+
3636
+ min_velocity = min([c[5] for c in score])
3637
+ max_velocity_all_channels = max([c[5] for c in score])
3638
+ min_velocity_ratio = min_velocity / max_velocity_all_channels
3639
+
3640
+ max_channel_velocity = max([c[5] for c in score])
3641
+ if max_channel_velocity < min_velocity:
3642
+ factor = max_velocity / min_velocity
3643
+ else:
3644
+ factor = max_velocity / max_channel_velocity
3645
+ for i in range(len(score)):
3646
+ score[i][5] = int(score[i][5] * factor)
3647
+
3648
+ def chordify_score(score,
3649
+ return_choridfied_score=True,
3650
+ return_detected_score_information=False
3651
+ ):
3652
+
3653
+ if score:
3654
+
3655
+ num_tracks = 1
3656
+ single_track_score = []
3657
+ score_num_ticks = 0
3658
+
3659
+ if type(score[0]) == int and len(score) > 1:
3660
+
3661
+ score_type = 'MIDI_PY'
3662
+ score_num_ticks = score[0]
3663
+
3664
+ while num_tracks < len(score):
3665
+ for event in score[num_tracks]:
3666
+ single_track_score.append(event)
3667
+ num_tracks += 1
3668
+
3669
+ else:
3670
+ score_type = 'CUSTOM'
3671
+ single_track_score = score
3672
+
3673
+ if single_track_score and single_track_score[0]:
3674
+
3675
+ try:
3676
+
3677
+ if type(single_track_score[0][0]) == str or single_track_score[0][0] == 'note':
3678
+ single_track_score.sort(key = lambda x: x[1])
3679
+ score_timings = [s[1] for s in single_track_score]
3680
+ else:
3681
+ score_timings = [s[0] for s in single_track_score]
3682
+
3683
+ is_score_time_absolute = lambda sct: all(x <= y for x, y in zip(sct, sct[1:]))
3684
+
3685
+ score_timings_type = ''
3686
+
3687
+ if is_score_time_absolute(score_timings):
3688
+ score_timings_type = 'ABS'
3689
+
3690
+ chords = []
3691
+ cho = []
3692
+
3693
+ if score_type == 'MIDI_PY':
3694
+ pe = single_track_score[0]
3695
+ else:
3696
+ pe = single_track_score[0]
3697
+
3698
+ for e in single_track_score:
3699
+
3700
+ if score_type == 'MIDI_PY':
3701
+ time = e[1]
3702
+ ptime = pe[1]
3703
+ else:
3704
+ time = e[0]
3705
+ ptime = pe[0]
3706
+
3707
+ if time == ptime:
3708
+ cho.append(e)
3709
+
3710
+ else:
3711
+ if len(cho) > 0:
3712
+ chords.append(cho)
3713
+ cho = []
3714
+ cho.append(e)
3715
+
3716
+ pe = e
3717
+
3718
+ if len(cho) > 0:
3719
+ chords.append(cho)
3720
+
3721
+ else:
3722
+ score_timings_type = 'REL'
3723
+
3724
+ chords = []
3725
+ cho = []
3726
+
3727
+ for e in single_track_score:
3728
+
3729
+ if score_type == 'MIDI_PY':
3730
+ time = e[1]
3731
+ else:
3732
+ time = e[0]
3733
+
3734
+ if time == 0:
3735
+ cho.append(e)
3736
+
3737
+ else:
3738
+ if len(cho) > 0:
3739
+ chords.append(cho)
3740
+ cho = []
3741
+ cho.append(e)
3742
+
3743
+ if len(cho) > 0:
3744
+ chords.append(cho)
3745
+
3746
+ requested_data = []
3747
+
3748
+ if return_detected_score_information:
3749
+
3750
+ detected_score_information = []
3751
+
3752
+ detected_score_information.append(['Score type', score_type])
3753
+ detected_score_information.append(['Score timings type', score_timings_type])
3754
+ detected_score_information.append(['Score tpq', score_num_ticks])
3755
+ detected_score_information.append(['Score number of tracks', num_tracks])
3756
+
3757
+ requested_data.append(detected_score_information)
3758
+
3759
+ if return_choridfied_score and return_detected_score_information:
3760
+ requested_data.append(chords)
3761
+
3762
+ if return_choridfied_score and not return_detected_score_information:
3763
+ requested_data.extend(chords)
3764
+
3765
+ return requested_data
3766
+
3767
+ except Exception as e:
3768
+ print('Error!')
3769
+ print('Check score for consistency and compatibility!')
3770
+ print('Exception detected:', e)
3771
+
3772
+ else:
3773
+ return None
3774
+
3775
+ else:
3776
+ return None
3777
+
3778
+ def fix_monophonic_score_durations(monophonic_score):
3779
+
3780
+ fixed_score = []
3781
+
3782
+ for i in range(len(monophonic_score)-1):
3783
+ note = monophonic_score[i]
3784
+
3785
+ nmt = monophonic_score[i+1][1]
3786
+
3787
+ if note[1]+note[2] >= nmt:
3788
+ note_dur = nmt-note[1]-1
3789
+ else:
3790
+ note_dur = note[2]
3791
+
3792
+ fixed_score.append([note[0], note[1], note_dur, note[3], note[4], note[5]])
3793
+
3794
+ fixed_score.append(monophonic_score[-1])
3795
+
3796
+ return fixed_score
3797
+
3798
+ ###################################################################################
3799
+
3800
+ from itertools import product
3801
+
3802
+ ALL_CHORDS = [[0], [7], [5], [9], [2], [4], [11], [10], [8], [6], [3], [1], [0, 9], [2, 5],
3803
+ [4, 7], [7, 10], [2, 11], [0, 3], [6, 9], [1, 4], [8, 11], [5, 8], [1, 10],
3804
+ [3, 6], [0, 4], [5, 9], [7, 11], [0, 7], [0, 5], [2, 10], [2, 7], [2, 9],
3805
+ [2, 6], [4, 11], [4, 9], [3, 7], [5, 10], [1, 9], [0, 8], [6, 11], [3, 11],
3806
+ [4, 8], [3, 10], [3, 8], [1, 5], [1, 8], [1, 6], [6, 10], [3, 9], [4, 10],
3807
+ [1, 7], [0, 6], [2, 8], [5, 11], [5, 7], [0, 10], [0, 2], [9, 11], [7, 9],
3808
+ [2, 4], [4, 6], [3, 5], [8, 10], [6, 8], [1, 3], [1, 11], [2, 7, 11],
3809
+ [0, 4, 7], [0, 5, 9], [2, 6, 9], [2, 5, 10], [1, 4, 9], [4, 8, 11], [3, 7, 10],
3810
+ [0, 3, 8], [3, 6, 11], [1, 5, 8], [1, 6, 10], [0, 4, 9], [2, 5, 9], [4, 7, 11],
3811
+ [2, 7, 10], [2, 6, 11], [0, 3, 7], [0, 5, 8], [1, 4, 8], [1, 6, 9], [3, 8, 11],
3812
+ [1, 5, 10], [3, 6, 10], [2, 5, 11], [4, 7, 10], [3, 6, 9], [0, 6, 9],
3813
+ [0, 3, 9], [2, 8, 11], [2, 5, 8], [1, 7, 10], [1, 4, 7], [0, 3, 6], [1, 4, 10],
3814
+ [5, 8, 11], [2, 5, 7], [0, 7, 10], [0, 2, 9], [0, 3, 5], [6, 9, 11], [4, 7, 9],
3815
+ [2, 4, 11], [5, 8, 10], [1, 3, 10], [1, 4, 6], [3, 6, 8], [1, 8, 11],
3816
+ [5, 7, 11], [0, 4, 10], [3, 5, 9], [0, 2, 6], [1, 7, 9], [0, 7, 9], [5, 7, 10],
3817
+ [2, 8, 10], [3, 9, 11], [0, 2, 5], [2, 4, 8], [2, 4, 7], [0, 2, 7], [2, 7, 9],
3818
+ [4, 9, 11], [4, 6, 9], [1, 3, 7], [2, 4, 9], [0, 5, 7], [0, 3, 10], [2, 9, 11],
3819
+ [0, 5, 10], [0, 6, 8], [4, 6, 10], [4, 6, 11], [1, 4, 11], [6, 8, 11],
3820
+ [1, 5, 11], [1, 6, 11], [1, 8, 10], [1, 6, 8], [3, 5, 8], [3, 8, 10],
3821
+ [1, 3, 8], [3, 5, 10], [1, 3, 6], [2, 5, 7, 10], [0, 3, 7, 10], [1, 4, 8, 11],
3822
+ [2, 4, 7, 11], [0, 4, 7, 9], [0, 2, 5, 9], [2, 6, 9, 11], [1, 5, 8, 10],
3823
+ [0, 3, 5, 8], [3, 6, 8, 11], [1, 3, 6, 10], [1, 4, 6, 9], [1, 5, 9], [0, 4, 8],
3824
+ [2, 6, 10], [3, 7, 11], [0, 3, 6, 9], [2, 5, 8, 11], [1, 4, 7, 10],
3825
+ [2, 5, 7, 11], [0, 2, 6, 9], [0, 4, 7, 10], [2, 4, 8, 11], [0, 3, 5, 9],
3826
+ [1, 4, 7, 9], [3, 6, 9, 11], [2, 5, 8, 10], [1, 4, 6, 10], [0, 3, 6, 8],
3827
+ [1, 3, 7, 10], [1, 5, 8, 11], [2, 4, 10], [5, 9, 11], [1, 5, 7], [0, 2, 8],
3828
+ [0, 4, 6], [1, 7, 11], [3, 7, 9], [1, 3, 9], [7, 9, 11], [5, 7, 9], [0, 6, 10],
3829
+ [0, 2, 10], [2, 6, 8], [0, 2, 4], [4, 8, 10], [1, 9, 11], [2, 4, 6],
3830
+ [3, 5, 11], [3, 5, 7], [0, 8, 10], [4, 6, 8], [1, 3, 11], [6, 8, 10],
3831
+ [1, 3, 5], [0, 2, 5, 10], [0, 5, 7, 9], [0, 3, 8, 10], [0, 2, 4, 7],
3832
+ [4, 6, 8, 11], [3, 5, 7, 10], [2, 7, 9, 11], [2, 4, 6, 9], [1, 6, 8, 10],
3833
+ [1, 4, 9, 11], [1, 3, 5, 8], [1, 3, 6, 11], [2, 5, 9, 11], [2, 4, 7, 10],
3834
+ [0, 2, 5, 8], [1, 5, 7, 10], [0, 4, 6, 9], [1, 3, 6, 9], [0, 3, 6, 10],
3835
+ [2, 6, 8, 11], [0, 2, 7, 9], [1, 4, 8, 10], [0, 3, 7, 9], [3, 5, 8, 11],
3836
+ [0, 5, 7, 10], [0, 2, 5, 7], [1, 4, 7, 11], [2, 4, 7, 9], [0, 3, 5, 10],
3837
+ [4, 6, 9, 11], [1, 4, 6, 11], [2, 4, 9, 11], [1, 6, 8, 11], [1, 3, 6, 8],
3838
+ [1, 3, 8, 10], [3, 5, 8, 10], [4, 7, 9, 11], [0, 2, 7, 10], [2, 5, 7, 9],
3839
+ [0, 2, 4, 9], [1, 6, 9, 11], [2, 4, 6, 11], [0, 3, 5, 7], [0, 5, 8, 10],
3840
+ [1, 4, 6, 8], [1, 3, 5, 10], [1, 3, 8, 11], [3, 6, 8, 10], [0, 2, 5, 7, 10],
3841
+ [0, 2, 4, 7, 9], [0, 2, 5, 7, 9], [1, 3, 7, 9], [1, 4, 6, 9, 11],
3842
+ [1, 3, 6, 8, 11], [3, 5, 9, 11], [1, 3, 6, 8, 10], [1, 4, 6, 8, 11],
3843
+ [1, 3, 5, 8, 10], [2, 4, 6, 9, 11], [2, 4, 8, 10], [2, 4, 7, 9, 11],
3844
+ [0, 3, 5, 7, 10], [1, 5, 7, 11], [0, 2, 6, 8], [0, 3, 5, 8, 10], [0, 4, 6, 10],
3845
+ [1, 3, 5, 9], [1, 5, 7, 9], [2, 6, 8, 10], [3, 7, 9, 11], [0, 2, 4, 8],
3846
+ [0, 4, 6, 8], [0, 4, 8, 10], [2, 4, 6, 10], [1, 3, 7, 11], [0, 2, 6, 10],
3847
+ [1, 5, 9, 11], [3, 5, 7, 11], [1, 7, 9, 11], [0, 2, 4, 6], [1, 3, 9, 11],
3848
+ [0, 2, 4, 10], [5, 7, 9, 11], [2, 4, 6, 8], [0, 2, 8, 10], [3, 5, 7, 9],
3849
+ [1, 3, 5, 7], [4, 6, 8, 10], [0, 6, 8, 10], [1, 3, 5, 11], [0, 3, 6, 8, 10],
3850
+ [0, 2, 4, 6, 9], [1, 4, 7, 9, 11], [2, 4, 6, 8, 11], [1, 3, 6, 9, 11],
3851
+ [1, 3, 5, 8, 11], [0, 2, 5, 8, 10], [1, 4, 6, 8, 10], [0, 3, 5, 7, 9],
3852
+ [2, 5, 7, 9, 11], [1, 3, 5, 7, 10], [0, 2, 4, 7, 10], [1, 3, 5, 7, 9],
3853
+ [1, 3, 5, 9, 11], [1, 5, 7, 9, 11], [1, 3, 7, 9, 11], [3, 5, 7, 9, 11],
3854
+ [2, 4, 6, 8, 10], [0, 4, 6, 8, 10], [0, 2, 6, 8, 10], [1, 3, 5, 7, 11],
3855
+ [0, 2, 4, 8, 10], [0, 2, 4, 6, 8], [0, 2, 4, 6, 10]]
3856
+
3857
+ def find_exact_match_variable_length(list_of_lists, target_list, uncertain_indices):
3858
+ # Infer possible values for each uncertain index
3859
+ possible_values = {idx: set() for idx in uncertain_indices}
3860
+ for sublist in list_of_lists:
3861
+ for idx in uncertain_indices:
3862
+ if idx < len(sublist):
3863
+ possible_values[idx].add(sublist[idx])
3864
+
3865
+ # Generate all possible combinations for the uncertain elements
3866
+ uncertain_combinations = product(*(possible_values[idx] for idx in uncertain_indices))
3867
+
3868
+ for combination in uncertain_combinations:
3869
+ # Create a copy of the target list and update the uncertain elements
3870
+ test_list = target_list[:]
3871
+ for idx, value in zip(uncertain_indices, combination):
3872
+ test_list[idx] = value
3873
+
3874
+ # Check if the modified target list is an exact match in the list of lists
3875
+ # Only consider sublists that are at least as long as the target list
3876
+ for sublist in list_of_lists:
3877
+ if len(sublist) >= len(test_list) and sublist[:len(test_list)] == test_list:
3878
+ return sublist # Return the matching sublist
3879
+
3880
+ return None # No exact match found
3881
+
3882
+
3883
+ def advanced_validate_chord_pitches(chord, channel_to_check = 0, return_sorted = True):
3884
+
3885
+ pitches_chord = sorted(list(set([x[4] for x in chord if 0 < x[4] < 128 and x[3] == channel_to_check])))
3886
+
3887
+ if pitches_chord:
3888
+
3889
+ tones_chord = sorted(list(set([c % 12 for c in sorted(list(set(pitches_chord)))])))
3890
+
3891
+ if not bad_chord(tones_chord):
3892
+ if return_sorted:
3893
+ chord.sort(key = lambda x: x[4], reverse=True)
3894
+ return chord
3895
+
3896
+ else:
3897
+ bad_chord_indices = list(set([i for s in [[tones_chord.index(a), tones_chord.index(b)] for a, b in zip(tones_chord, tones_chord[1:]) if b-a == 1] for i in s]))
3898
+
3899
+ good_tones_chord = find_exact_match_variable_length(ALL_CHORDS, tones_chord, bad_chord_indices)
3900
+
3901
+ if good_tones_chord is not None:
3902
+
3903
+ fixed_chord = []
3904
+
3905
+ for c in chord:
3906
+ if c[3] == channel_to_check:
3907
+ if (c[4] % 12) in good_tones_chord:
3908
+ fixed_chord.append(c)
3909
+ else:
3910
+ fixed_chord.append(c)
3911
+
3912
+ if return_sorted:
3913
+ fixed_chord.sort(key = lambda x: x[4], reverse=True)
3914
+
3915
+ else:
3916
+
3917
+ if 0 in tones_chord and 11 in tones_chord:
3918
+ tones_chord.remove(0)
3919
+
3920
+ fixed_tones = [[a, b] for a, b in zip(tones_chord, tones_chord[1:]) if b-a != 1]
3921
+
3922
+ fixed_tones_chord = []
3923
+ for f in fixed_tones:
3924
+ fixed_tones_chord.extend(f)
3925
+ fixed_tones_chord = list(set(fixed_tones_chord))
3926
+
3927
+ fixed_chord = []
3928
+
3929
+ for c in chord:
3930
+ if c[3] == channel_to_check:
3931
+ if (c[4] % 12) in fixed_tones_chord:
3932
+ fixed_chord.append(c)
3933
+ else:
3934
+ fixed_chord.append(c)
3935
+
3936
+ if return_sorted:
3937
+ fixed_chord.sort(key = lambda x: x[4], reverse=True)
3938
+
3939
+ return fixed_chord
3940
+
3941
+ else:
3942
+ chord.sort(key = lambda x: x[4], reverse=True)
3943
+ return chord
3944
+
3945
+ ###################################################################################
3946
+
3947
+ def analyze_score_pitches(score, channels_to_analyze=[0]):
3948
+
3949
+ analysis = {}
3950
+
3951
+ score_notes = [s for s in score if s[3] in channels_to_analyze]
3952
+
3953
+ cscore = chordify_score(score_notes)
3954
+
3955
+ chords_tones = []
3956
+
3957
+ all_tones = []
3958
+
3959
+ all_chords_good = True
3960
+
3961
+ bad_chords = []
3962
+
3963
+ for c in cscore:
3964
+ tones = sorted(list(set([t[4] % 12 for t in c])))
3965
+ chords_tones.append(tones)
3966
+ all_tones.extend(tones)
3967
+
3968
+ if tones not in ALL_CHORDS:
3969
+ all_chords_good = False
3970
+ bad_chords.append(tones)
3971
+
3972
+ analysis['Number of notes'] = len(score_notes)
3973
+ analysis['Number of chords'] = len(cscore)
3974
+ analysis['Score tones'] = sorted(list(set(all_tones)))
3975
+ analysis['Shortest chord'] = sorted(min(chords_tones, key=len))
3976
+ analysis['Longest chord'] = sorted(max(chords_tones, key=len))
3977
+ analysis['All chords good'] = all_chords_good
3978
+ analysis['Bad chords'] = bad_chords
3979
+
3980
+ return analysis
3981
+
3982
+ ###################################################################################
3983
+
3984
+ ALL_CHORDS_GROUPED = [
3985
+ [[0, 2, 5, 7, 10], [0, 2, 4, 7, 9], [0, 2, 5, 7, 9], [1, 4, 6, 9, 11],
3986
+ [1, 3, 6, 8, 11], [1, 3, 6, 8, 10], [1, 4, 6, 8, 11], [1, 3, 5, 8, 10],
3987
+ [2, 4, 6, 9, 11], [2, 4, 7, 9, 11], [0, 3, 5, 7, 10], [0, 3, 5, 8, 10],
3988
+ [0, 3, 6, 8, 10], [0, 2, 4, 6, 9], [1, 4, 7, 9, 11], [2, 4, 6, 8, 11],
3989
+ [1, 3, 6, 9, 11], [1, 3, 5, 8, 11], [0, 2, 5, 8, 10], [1, 4, 6, 8, 10],
3990
+ [0, 3, 5, 7, 9], [2, 5, 7, 9, 11], [1, 3, 5, 7, 10], [0, 2, 4, 7, 10],
3991
+ [1, 3, 5, 7, 9], [1, 3, 5, 9, 11], [1, 5, 7, 9, 11], [1, 3, 7, 9, 11],
3992
+ [3, 5, 7, 9, 11], [2, 4, 6, 8, 10], [0, 4, 6, 8, 10], [0, 2, 6, 8, 10],
3993
+ [1, 3, 5, 7, 11], [0, 2, 4, 8, 10], [0, 2, 4, 6, 8], [0, 2, 4, 6, 10]],
3994
+ [[2, 5, 7, 10], [0, 3, 7, 10], [1, 4, 8, 11], [2, 4, 7, 11], [0, 4, 7, 9],
3995
+ [0, 2, 5, 9], [2, 6, 9, 11], [1, 5, 8, 10], [0, 3, 5, 8], [3, 6, 8, 11],
3996
+ [1, 3, 6, 10], [1, 4, 6, 9], [0, 3, 6, 9], [2, 5, 8, 11], [1, 4, 7, 10],
3997
+ [2, 5, 7, 11], [0, 2, 6, 9], [0, 4, 7, 10], [2, 4, 8, 11], [0, 3, 5, 9],
3998
+ [1, 4, 7, 9], [3, 6, 9, 11], [2, 5, 8, 10], [1, 4, 6, 10], [0, 3, 6, 8],
3999
+ [1, 3, 7, 10], [1, 5, 8, 11], [0, 2, 5, 10], [0, 5, 7, 9], [0, 3, 8, 10],
4000
+ [0, 2, 4, 7], [4, 6, 8, 11], [3, 5, 7, 10], [2, 7, 9, 11], [2, 4, 6, 9],
4001
+ [1, 6, 8, 10], [1, 4, 9, 11], [1, 3, 5, 8], [1, 3, 6, 11], [2, 5, 9, 11],
4002
+ [2, 4, 7, 10], [0, 2, 5, 8], [1, 5, 7, 10], [0, 4, 6, 9], [1, 3, 6, 9],
4003
+ [0, 3, 6, 10], [2, 6, 8, 11], [0, 2, 7, 9], [1, 4, 8, 10], [0, 3, 7, 9],
4004
+ [3, 5, 8, 11], [0, 5, 7, 10], [0, 2, 5, 7], [1, 4, 7, 11], [2, 4, 7, 9],
4005
+ [0, 3, 5, 10], [4, 6, 9, 11], [1, 4, 6, 11], [2, 4, 9, 11], [1, 6, 8, 11],
4006
+ [1, 3, 6, 8], [1, 3, 8, 10], [3, 5, 8, 10], [4, 7, 9, 11], [0, 2, 7, 10],
4007
+ [2, 5, 7, 9], [0, 2, 4, 9], [1, 6, 9, 11], [2, 4, 6, 11], [0, 3, 5, 7],
4008
+ [0, 5, 8, 10], [1, 4, 6, 8], [1, 3, 5, 10], [1, 3, 8, 11], [3, 6, 8, 10],
4009
+ [1, 3, 7, 9], [3, 5, 9, 11], [2, 4, 8, 10], [1, 5, 7, 11], [0, 2, 6, 8],
4010
+ [0, 4, 6, 10], [1, 3, 5, 9], [1, 5, 7, 9], [2, 6, 8, 10], [3, 7, 9, 11],
4011
+ [0, 2, 4, 8], [0, 4, 6, 8], [0, 4, 8, 10], [2, 4, 6, 10], [1, 3, 7, 11],
4012
+ [0, 2, 6, 10], [1, 5, 9, 11], [3, 5, 7, 11], [1, 7, 9, 11], [0, 2, 4, 6],
4013
+ [1, 3, 9, 11], [0, 2, 4, 10], [5, 7, 9, 11], [2, 4, 6, 8], [0, 2, 8, 10],
4014
+ [3, 5, 7, 9], [1, 3, 5, 7], [4, 6, 8, 10], [0, 6, 8, 10], [1, 3, 5, 11]],
4015
+ [[2, 7, 11], [0, 4, 7], [0, 5, 9], [2, 6, 9], [2, 5, 10], [1, 4, 9],
4016
+ [4, 8, 11], [3, 7, 10], [0, 3, 8], [3, 6, 11], [1, 5, 8], [1, 6, 10],
4017
+ [0, 4, 9], [2, 5, 9], [4, 7, 11], [2, 7, 10], [2, 6, 11], [0, 3, 7],
4018
+ [0, 5, 8], [1, 4, 8], [1, 6, 9], [3, 8, 11], [1, 5, 10], [3, 6, 10],
4019
+ [2, 5, 11], [4, 7, 10], [3, 6, 9], [0, 6, 9], [0, 3, 9], [2, 8, 11],
4020
+ [2, 5, 8], [1, 7, 10], [1, 4, 7], [0, 3, 6], [1, 4, 10], [5, 8, 11],
4021
+ [2, 5, 7], [0, 7, 10], [0, 2, 9], [0, 3, 5], [6, 9, 11], [4, 7, 9],
4022
+ [2, 4, 11], [5, 8, 10], [1, 3, 10], [1, 4, 6], [3, 6, 8], [1, 8, 11],
4023
+ [5, 7, 11], [0, 4, 10], [3, 5, 9], [0, 2, 6], [1, 7, 9], [0, 7, 9],
4024
+ [5, 7, 10], [2, 8, 10], [3, 9, 11], [0, 2, 5], [2, 4, 8], [2, 4, 7],
4025
+ [0, 2, 7], [2, 7, 9], [4, 9, 11], [4, 6, 9], [1, 3, 7], [2, 4, 9], [0, 5, 7],
4026
+ [0, 3, 10], [2, 9, 11], [0, 5, 10], [0, 6, 8], [4, 6, 10], [4, 6, 11],
4027
+ [1, 4, 11], [6, 8, 11], [1, 5, 11], [1, 6, 11], [1, 8, 10], [1, 6, 8],
4028
+ [3, 5, 8], [3, 8, 10], [1, 3, 8], [3, 5, 10], [1, 3, 6], [1, 5, 9], [0, 4, 8],
4029
+ [2, 6, 10], [3, 7, 11], [2, 4, 10], [5, 9, 11], [1, 5, 7], [0, 2, 8],
4030
+ [0, 4, 6], [1, 7, 11], [3, 7, 9], [1, 3, 9], [7, 9, 11], [5, 7, 9],
4031
+ [0, 6, 10], [0, 2, 10], [2, 6, 8], [0, 2, 4], [4, 8, 10], [1, 9, 11],
4032
+ [2, 4, 6], [3, 5, 11], [3, 5, 7], [0, 8, 10], [4, 6, 8], [1, 3, 11],
4033
+ [6, 8, 10], [1, 3, 5]],
4034
+ [[0, 9], [2, 5], [4, 7], [7, 10], [2, 11], [0, 3], [6, 9], [1, 4], [8, 11],
4035
+ [5, 8], [1, 10], [3, 6], [0, 4], [5, 9], [7, 11], [0, 7], [0, 5], [2, 10],
4036
+ [2, 7], [2, 9], [2, 6], [4, 11], [4, 9], [3, 7], [5, 10], [1, 9], [0, 8],
4037
+ [6, 11], [3, 11], [4, 8], [3, 10], [3, 8], [1, 5], [1, 8], [1, 6], [6, 10],
4038
+ [3, 9], [4, 10], [1, 7], [0, 6], [2, 8], [5, 11], [5, 7], [0, 10], [0, 2],
4039
+ [9, 11], [7, 9], [2, 4], [4, 6], [3, 5], [8, 10], [6, 8], [1, 3], [1, 11]],
4040
+ [[0], [7], [5], [9], [2], [4], [11], [10], [8], [6], [3], [1]]
4041
+ ]
4042
+
4043
+ def group_sublists_by_length(lst):
4044
+ unique_lengths = sorted(list(set(map(len, lst))), reverse=True)
4045
+ return [[x for x in lst if len(x) == i] for i in unique_lengths]
4046
+
4047
+ def pitches_to_tones_chord(pitches):
4048
+ return sorted(set([p % 12 for p in pitches]))
4049
+
4050
+ def tones_chord_to_pitches(tones_chord, base_pitch=60):
4051
+ return [t+base_pitch for t in tones_chord if 0 <= t < 12]
4052
+
4053
+ ###################################################################################
4054
+
4055
+ def advanced_score_processor(raw_score,
4056
+ patches_to_analyze=list(range(129)),
4057
+ return_score_analysis=True,
4058
+ return_enhanced_score=False,
4059
+ return_enhanced_score_notes=False,
4060
+ return_enhanced_monophonic_melody=False,
4061
+ return_chordified_enhanced_score=False,
4062
+ return_chordified_enhanced_score_with_lyrics=False,
4063
+ return_score_tones_chords=False,
4064
+ return_text_and_lyric_events=False
4065
+ ):
4066
+
4067
+ '''TMIDIX Advanced Score Processor'''
4068
+
4069
+ # Score data types detection
4070
+
4071
+ if raw_score and type(raw_score) == list:
4072
+
4073
+ num_ticks = 0
4074
+ num_tracks = 1
4075
+
4076
+ basic_single_track_score = []
4077
+
4078
+ if type(raw_score[0]) != int:
4079
+ if len(raw_score[0]) < 5 and type(raw_score[0][0]) != str:
4080
+ return ['Check score for errors and compatibility!']
4081
+
4082
+ else:
4083
+ basic_single_track_score = copy.deepcopy(raw_score)
4084
+
4085
+ else:
4086
+ num_ticks = raw_score[0]
4087
+ while num_tracks < len(raw_score):
4088
+ for event in raw_score[num_tracks]:
4089
+ ev = copy.deepcopy(event)
4090
+ basic_single_track_score.append(ev)
4091
+ num_tracks += 1
4092
+
4093
+ basic_single_track_score.sort(key=lambda x: x[4] if x[0] == 'note' else 128, reverse=True)
4094
+ basic_single_track_score.sort(key=lambda x: x[1])
4095
+
4096
+ enhanced_single_track_score = []
4097
+ patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
4098
+ all_score_patches = []
4099
+ num_patch_changes = 0
4100
+
4101
+ for event in basic_single_track_score:
4102
+ if event[0] == 'patch_change':
4103
+ patches[event[2]] = event[3]
4104
+ enhanced_single_track_score.append(event)
4105
+ num_patch_changes += 1
4106
+
4107
+ if event[0] == 'note':
4108
+ if event[3] != 9:
4109
+ event.extend([patches[event[3]]])
4110
+ all_score_patches.extend([patches[event[3]]])
4111
+ else:
4112
+ event.extend([128])
4113
+ all_score_patches.extend([128])
4114
+
4115
+ if enhanced_single_track_score:
4116
+ if (event[1] == enhanced_single_track_score[-1][1]):
4117
+ if ([event[3], event[4]] != enhanced_single_track_score[-1][3:5]):
4118
+ enhanced_single_track_score.append(event)
4119
+ else:
4120
+ enhanced_single_track_score.append(event)
4121
+
4122
+ else:
4123
+ enhanced_single_track_score.append(event)
4124
+
4125
+ if event[0] not in ['note', 'patch_change']:
4126
+ enhanced_single_track_score.append(event)
4127
+
4128
+ enhanced_single_track_score.sort(key=lambda x: x[6] if x[0] == 'note' else -1)
4129
+ enhanced_single_track_score.sort(key=lambda x: x[4] if x[0] == 'note' else 128, reverse=True)
4130
+ enhanced_single_track_score.sort(key=lambda x: x[1])
4131
+
4132
+ # Analysis and chordification
4133
+
4134
+ cscore = []
4135
+ cescore = []
4136
+ chords_tones = []
4137
+ tones_chords = []
4138
+ all_tones = []
4139
+ all_chords_good = True
4140
+ bad_chords = []
4141
+ bad_chords_count = 0
4142
+ score_notes = []
4143
+ score_pitches = []
4144
+ score_patches = []
4145
+ num_text_events = 0
4146
+ num_lyric_events = 0
4147
+ num_other_events = 0
4148
+ text_and_lyric_events = []
4149
+ text_and_lyric_events_latin = None
4150
+
4151
+ analysis = {}
4152
+
4153
+ score_notes = [s for s in enhanced_single_track_score if s[0] == 'note' and s[6] in patches_to_analyze]
4154
+ score_patches = [sn[6] for sn in score_notes]
4155
+
4156
+ if return_text_and_lyric_events:
4157
+ text_and_lyric_events = [e for e in enhanced_single_track_score if e[0] in ['text_event', 'lyric']]
4158
+
4159
+ if text_and_lyric_events:
4160
+ text_and_lyric_events_latin = True
4161
+ for e in text_and_lyric_events:
4162
+ try:
4163
+ tle = str(e[2].decode())
4164
+ except:
4165
+ tle = str(e[2])
4166
+
4167
+ for c in tle:
4168
+ if not 0 <= ord(c) < 128:
4169
+ text_and_lyric_events_latin = False
4170
+
4171
+ if (return_chordified_enhanced_score or return_score_analysis) and any(elem in patches_to_analyze for elem in score_patches):
4172
+
4173
+ cescore = chordify_score([num_ticks, enhanced_single_track_score])
4174
+
4175
+ if return_score_analysis:
4176
+
4177
+ cscore = chordify_score(score_notes)
4178
+
4179
+ score_pitches = [sn[4] for sn in score_notes]
4180
+
4181
+ text_events = [e for e in enhanced_single_track_score if e[0] == 'text_event']
4182
+ num_text_events = len(text_events)
4183
+
4184
+ lyric_events = [e for e in enhanced_single_track_score if e[0] == 'lyric']
4185
+ num_lyric_events = len(lyric_events)
4186
+
4187
+ other_events = [e for e in enhanced_single_track_score if e[0] not in ['note', 'patch_change', 'text_event', 'lyric']]
4188
+ num_other_events = len(other_events)
4189
+
4190
+ for c in cscore:
4191
+ tones = sorted(set([t[4] % 12 for t in c if t[3] != 9]))
4192
+
4193
+ if tones:
4194
+ chords_tones.append(tones)
4195
+ all_tones.extend(tones)
4196
+
4197
+ if tones not in ALL_CHORDS:
4198
+ all_chords_good = False
4199
+ bad_chords.append(tones)
4200
+ bad_chords_count += 1
4201
+
4202
+ analysis['Number of ticks per quarter note'] = num_ticks
4203
+ analysis['Number of tracks'] = num_tracks
4204
+ analysis['Number of all events'] = len(enhanced_single_track_score)
4205
+ analysis['Number of patch change events'] = num_patch_changes
4206
+ analysis['Number of text events'] = num_text_events
4207
+ analysis['Number of lyric events'] = num_lyric_events
4208
+ analysis['All text and lyric events Latin'] = text_and_lyric_events_latin
4209
+ analysis['Number of other events'] = num_other_events
4210
+ analysis['Number of score notes'] = len(score_notes)
4211
+ analysis['Number of score chords'] = len(cscore)
4212
+ analysis['Score patches'] = sorted(set(score_patches))
4213
+ analysis['Score pitches'] = sorted(set(score_pitches))
4214
+ analysis['Score tones'] = sorted(set(all_tones))
4215
+ if chords_tones:
4216
+ analysis['Shortest chord'] = sorted(min(chords_tones, key=len))
4217
+ analysis['Longest chord'] = sorted(max(chords_tones, key=len))
4218
+ analysis['All chords good'] = all_chords_good
4219
+ analysis['Number of bad chords'] = bad_chords_count
4220
+ analysis['Bad chords'] = sorted([list(c) for c in set(tuple(bc) for bc in bad_chords)])
4221
+
4222
+ else:
4223
+ analysis['Error'] = 'Provided score does not have specified patches to analyse'
4224
+ analysis['Provided patches to analyse'] = sorted(patches_to_analyze)
4225
+ analysis['Patches present in the score'] = sorted(set(all_score_patches))
4226
+
4227
+ if return_enhanced_monophonic_melody:
4228
+
4229
+ score_notes_copy = copy.deepcopy(score_notes)
4230
+ chordified_score_notes = chordify_score(score_notes_copy)
4231
+
4232
+ melody = [c[0] for c in chordified_score_notes]
4233
+
4234
+ fixed_melody = []
4235
+
4236
+ for i in range(len(melody)-1):
4237
+ note = melody[i]
4238
+ nmt = melody[i+1][1]
4239
+
4240
+ if note[1]+note[2] >= nmt:
4241
+ note_dur = nmt-note[1]-1
4242
+ else:
4243
+ note_dur = note[2]
4244
+
4245
+ melody[i][2] = note_dur
4246
+
4247
+ fixed_melody.append(melody[i])
4248
+ fixed_melody.append(melody[-1])
4249
+
4250
+ if return_score_tones_chords:
4251
+ cscore = chordify_score(score_notes)
4252
+ for c in cscore:
4253
+ tones_chord = sorted(set([t[4] % 12 for t in c if t[3] != 9]))
4254
+ if tones_chord:
4255
+ tones_chords.append(tones_chord)
4256
+
4257
+ if return_chordified_enhanced_score_with_lyrics:
4258
+ score_with_lyrics = [e for e in enhanced_single_track_score if e[0] in ['note', 'text_event', 'lyric']]
4259
+ chordified_enhanced_score_with_lyrics = chordify_score(score_with_lyrics)
4260
+
4261
+ # Returned data
4262
+
4263
+ requested_data = []
4264
+
4265
+ if return_score_analysis and analysis:
4266
+ requested_data.append([[k, v] for k, v in analysis.items()])
4267
+
4268
+ if return_enhanced_score and enhanced_single_track_score:
4269
+ requested_data.append([num_ticks, enhanced_single_track_score])
4270
+
4271
+ if return_enhanced_score_notes and score_notes:
4272
+ requested_data.append(score_notes)
4273
+
4274
+ if return_enhanced_monophonic_melody and fixed_melody:
4275
+ requested_data.append(fixed_melody)
4276
+
4277
+ if return_chordified_enhanced_score and cescore:
4278
+ requested_data.append(cescore)
4279
+
4280
+ if return_chordified_enhanced_score_with_lyrics and chordified_enhanced_score_with_lyrics:
4281
+ requested_data.append(chordified_enhanced_score_with_lyrics)
4282
+
4283
+ if return_score_tones_chords and tones_chords:
4284
+ requested_data.append(tones_chords)
4285
+
4286
+ if return_text_and_lyric_events and text_and_lyric_events:
4287
+ requested_data.append(text_and_lyric_events)
4288
+
4289
+ return requested_data
4290
+
4291
+ else:
4292
+ return ['Check score for errors and compatibility!']
4293
+
4294
+ ###################################################################################
4295
+
4296
+ import random
4297
+ import copy
4298
+
4299
+ ###################################################################################
4300
+
4301
+ def replace_bad_tones_chord(bad_tones_chord):
4302
+ bad_chord_p = [0] * 12
4303
+ for b in bad_tones_chord:
4304
+ bad_chord_p[b] = 1
4305
+
4306
+ match_ratios = []
4307
+ good_chords = []
4308
+ for c in ALL_CHORDS:
4309
+ good_chord_p = [0] * 12
4310
+ for cc in c:
4311
+ good_chord_p[cc] = 1
4312
+
4313
+ good_chords.append(good_chord_p)
4314
+ match_ratios.append(sum(i == j for i, j in zip(good_chord_p, bad_chord_p)) / len(good_chord_p))
4315
+
4316
+ best_good_chord = good_chords[match_ratios.index(max(match_ratios))]
4317
+
4318
+ replaced_chord = []
4319
+ for i in range(len(best_good_chord)):
4320
+ if best_good_chord[i] == 1:
4321
+ replaced_chord.append(i)
4322
+
4323
+ return [replaced_chord, max(match_ratios)]
4324
+
4325
+ ###################################################################################
4326
+
4327
+ def check_and_fix_chord(chord,
4328
+ channel_index=3,
4329
+ pitch_index=4):
4330
+
4331
+ chord_notes = [x for x in chord if x[channel_index] != 9]
4332
+ chord_drums = [x for x in chord if x[channel_index] == 9]
4333
+ chord_pitches = [x[pitch_index] for x in chord_notes]
4334
+ tones_chord = sorted(set([x % 12 for x in chord_pitches]))
4335
+ good_tones_chord = replace_bad_tones_chord(tones_chord)[0]
4336
+ bad_tones = list(set(tones_chord) ^ set(good_tones_chord))
4337
+
4338
+ if bad_tones:
4339
+
4340
+ fixed_chord = []
4341
+
4342
+ for c in chord_notes:
4343
+ if (c[pitch_index] % 12) not in bad_tones:
4344
+ fixed_chord.append(c)
4345
+
4346
+ fixed_chord += chord_drums
4347
+
4348
+ return fixed_chord
4349
+
4350
+ else:
4351
+ return chord
4352
+
4353
+ ###################################################################################
4354
+
4355
+ def find_similar_tones_chord(tones_chord,
4356
+ max_match_threshold=1,
4357
+ randomize_chords_matches=False,
4358
+ custom_chords_list=[]):
4359
+ chord_p = [0] * 12
4360
+ for b in tones_chord:
4361
+ chord_p[b] = 1
4362
+
4363
+ match_ratios = []
4364
+ good_chords = []
4365
+
4366
+ if custom_chords_list:
4367
+ CHORDS = copy.deepcopy([list(x) for x in set(tuple(t) for t in custom_chords_list)])
4368
+ else:
4369
+ CHORDS = copy.deepcopy(ALL_CHORDS)
4370
+
4371
+ if randomize_chords_matches:
4372
+ random.shuffle(CHORDS)
4373
+
4374
+ for c in CHORDS:
4375
+ good_chord_p = [0] * 12
4376
+ for cc in c:
4377
+ good_chord_p[cc] = 1
4378
+
4379
+ good_chords.append(good_chord_p)
4380
+ match_ratio = sum(i == j for i, j in zip(good_chord_p, chord_p)) / len(good_chord_p)
4381
+ if match_ratio < max_match_threshold:
4382
+ match_ratios.append(match_ratio)
4383
+ else:
4384
+ match_ratios.append(0)
4385
+
4386
+ best_good_chord = good_chords[match_ratios.index(max(match_ratios))]
4387
+
4388
+ similar_chord = []
4389
+ for i in range(len(best_good_chord)):
4390
+ if best_good_chord[i] == 1:
4391
+ similar_chord.append(i)
4392
+
4393
+ return [similar_chord, max(match_ratios)]
4394
+
4395
+ ###################################################################################
4396
+
4397
+ def generate_tones_chords_progression(number_of_chords_to_generate=100,
4398
+ start_tones_chord=[],
4399
+ custom_chords_list=[]):
4400
+
4401
+ if start_tones_chord:
4402
+ start_chord = start_tones_chord
4403
+ else:
4404
+ start_chord = random.choice(ALL_CHORDS)
4405
+
4406
+ chord = []
4407
+
4408
+ chords_progression = [start_chord]
4409
+
4410
+ for i in range(number_of_chords_to_generate):
4411
+ if not chord:
4412
+ chord = start_chord
4413
+
4414
+ if custom_chords_list:
4415
+ chord = find_similar_tones_chord(chord, randomize_chords_matches=True, custom_chords_list=custom_chords_list)[0]
4416
+ else:
4417
+ chord = find_similar_tones_chord(chord, randomize_chords_matches=True)[0]
4418
+
4419
+ chords_progression.append(chord)
4420
+
4421
+ return chords_progression
4422
+
4423
+ ###################################################################################
4424
+
4425
+ def ascii_texts_search(texts = ['text1', 'text2', 'text3'],
4426
+ search_query = 'Once upon a time...',
4427
+ deterministic_matching = False
4428
+ ):
4429
+
4430
+ texts_copy = texts
4431
+
4432
+ if not deterministic_matching:
4433
+ texts_copy = copy.deepcopy(texts)
4434
+ random.shuffle(texts_copy)
4435
+
4436
+ clean_texts = []
4437
+
4438
+ for t in texts_copy:
4439
+ text_words_list = [at.split(chr(32)) for at in t.split(chr(10))]
4440
+
4441
+ clean_text_words_list = []
4442
+ for twl in text_words_list:
4443
+ for w in twl:
4444
+ clean_text_words_list.append(''.join(filter(str.isalpha, w.lower())))
4445
+
4446
+ clean_texts.append(clean_text_words_list)
4447
+
4448
+ text_search_query = [at.split(chr(32)) for at in search_query.split(chr(10))]
4449
+ clean_text_search_query = []
4450
+ for w in text_search_query:
4451
+ for ww in w:
4452
+ clean_text_search_query.append(''.join(filter(str.isalpha, ww.lower())))
4453
+
4454
+ if clean_texts[0] and clean_text_search_query:
4455
+ texts_match_ratios = []
4456
+ words_match_indexes = []
4457
+ for t in clean_texts:
4458
+ word_match_count = 0
4459
+ wmis = []
4460
+
4461
+ for c in clean_text_search_query:
4462
+ if c in t:
4463
+ word_match_count += 1
4464
+ wmis.append(t.index(c))
4465
+ else:
4466
+ wmis.append(-1)
4467
+
4468
+ words_match_indexes.append(wmis)
4469
+ words_match_indexes_consequtive = all(abs(b) - abs(a) == 1 for a, b in zip(wmis, wmis[1:]))
4470
+ words_match_indexes_consequtive_ratio = sum([abs(b) - abs(a) == 1 for a, b in zip(wmis, wmis[1:])]) / len(wmis)
4471
+
4472
+ if words_match_indexes_consequtive:
4473
+ texts_match_ratios.append(word_match_count / len(clean_text_search_query))
4474
+ else:
4475
+ texts_match_ratios.append(((word_match_count / len(clean_text_search_query)) + words_match_indexes_consequtive_ratio) / 2)
4476
+
4477
+ if texts_match_ratios:
4478
+ max_text_match_ratio = max(texts_match_ratios)
4479
+ max_match_ratio_text = texts_copy[texts_match_ratios.index(max_text_match_ratio)]
4480
+ max_text_words_match_indexes = words_match_indexes[texts_match_ratios.index(max_text_match_ratio)]
4481
+
4482
+ return [max_match_ratio_text, max_text_match_ratio, max_text_words_match_indexes]
4483
+
4484
+ else:
4485
+ return None
4486
+
4487
+ ###################################################################################
4488
+
4489
+ def ascii_text_words_counter(ascii_text):
4490
+
4491
+ text_words_list = [at.split(chr(32)) for at in ascii_text.split(chr(10))]
4492
+
4493
+ clean_text_words_list = []
4494
+ for twl in text_words_list:
4495
+ for w in twl:
4496
+ wo = ''
4497
+ for ww in w.lower():
4498
+ if 96 < ord(ww) < 123:
4499
+ wo += ww
4500
+ if wo != '':
4501
+ clean_text_words_list.append(wo)
4502
+
4503
+ words = {}
4504
+ for i in clean_text_words_list:
4505
+ words[i] = words.get(i, 0) + 1
4506
+
4507
+ words_sorted = dict(sorted(words.items(), key=lambda item: item[1], reverse=True))
4508
+
4509
+ return len(clean_text_words_list), words_sorted, clean_text_words_list
4510
+
4511
+ ###################################################################################
4512
+
4513
  # This is the end of the TMIDI X Python module
4514
 
4515
  ###################################################################################
los_angeles_midi_dataset_chords_data_decoder.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Los_Angeles_MIDI_Dataset_CHORDS_DATA_Decoder.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1HKcxhtWFX4zGE8ZxG59pzaWHDxnaxctR
8
+
9
+ # Los Angeles MIDI Dataset CHORDS DATA Decoder (ver. 1.0)
10
+
11
+ ***
12
+
13
+ Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools
14
+
15
+ ***
16
+
17
+ #### Project Los Angeles
18
+
19
+ #### Tegridy Code 2024
20
+
21
+ ***
22
+
23
+ # (SETUP ENVIRONMENT)
24
+ """
25
+
26
+ #@title Install all dependencies (run only once per session)
27
+
28
+ !git clone https://github.com/asigalov61/tegridy-tools
29
+ !pip install tqdm
30
+
31
+ #@title Import all needed modules
32
+
33
+ print('Loading core modules... Please wait...')
34
+ import os
35
+
36
+ import math
37
+ import statistics
38
+ import random
39
+ from collections import Counter
40
+ import shutil
41
+ import hashlib
42
+ from tqdm import tqdm
43
+
44
+ print('Creating IO dirs...')
45
+
46
+ if not os.path.exists('/content/CHORDS_DATA'):
47
+ os.makedirs('/content/CHORDS_DATA')
48
+
49
+ print('Loading TMIDIX module...')
50
+ os.chdir('/content/tegridy-tools/tegridy-tools')
51
+
52
+ import TMIDIX
53
+
54
+ print('Done!')
55
+
56
+ os.chdir('/content/')
57
+ print('Enjoy! :)')
58
+
59
+ """# (Load sample LAMDa CHORDS DATA file)"""
60
+
61
+ chords_data = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/CHORDS_DATA/LAMDa_CHORDS_DATA_2500')
62
+ print('Done!')
63
+
64
+ """# (Decode to MIDI)"""
65
+
66
+ data = random.choice(chords_data)
67
+
68
+ file_name = data[0]
69
+ song = data[1]
70
+
71
+ song_f = []
72
+
73
+ time = 0
74
+ dur = 0
75
+ vel = 90
76
+ pitch = 0
77
+ channel = 0
78
+
79
+ patches = [-1] * 16
80
+
81
+ channels = [0] * 16
82
+ channels[9] = 1
83
+
84
+ for ss in song:
85
+
86
+ time += ss[0] * 16
87
+
88
+ for i in range(0, len(ss[1:]), 4):
89
+
90
+ s = ss[1:][i:i+4]
91
+
92
+ dur = s[0] * 16
93
+
94
+ patch = s[1]
95
+
96
+ if patch < 128:
97
+ if patch not in patches:
98
+ if 0 in channels:
99
+ cha = channels.index(0)
100
+ channels[cha] = 1
101
+ else:
102
+ cha = 15
103
+ patches[cha] = patch
104
+ channel = patches.index(patch)
105
+ else:
106
+ channel = patches.index(patch)
107
+
108
+ if patch == 128:
109
+ channel = 9
110
+
111
+ pitch = s[2]
112
+
113
+ vel = s[3]
114
+
115
+ song_f.append(['note', time, dur, channel, pitch, vel, patch ])
116
+
117
+ patches = [0 if x==-1 else x for x in patches]
118
+
119
+
120
+ detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
121
+ output_signature = file_name+'.mid',
122
+ output_file_name = '/content/Los-Angeles-MIDI-Dataset-Composition',
123
+ track_name='Project Los Angeles',
124
+ list_of_MIDI_patches=patches
125
+ )
126
+
127
+ """# Congrats! You did it! :)"""