Spaces:
Running on Zero
Running on Zero
Upload TMIDIX.py
Browse files
TMIDIX.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
#! /usr/bin/python3
|
| 2 |
|
| 3 |
-
r'''
|
| 4 |
-
###############################################################################
|
| 5 |
-
#
|
| 6 |
#
|
| 7 |
# Tegridy MIDI X Module (TMIDI X / tee-midi eks)
|
| 8 |
#
|
| 9 |
-
# NOTE: TMIDI X Module starts after the partial MIDI.py module @ line
|
| 10 |
#
|
| 11 |
# Based upon MIDI.py module v.6.7. by Peter Billam / pjb.com.au
|
| 12 |
#
|
|
@@ -16,9 +15,8 @@ r'''############################################################################
|
|
| 16 |
#
|
| 17 |
# https://github.com/Tegridy-Code/Project-Los-Angeles
|
| 18 |
#
|
| 19 |
-
#
|
| 20 |
-
###################################################################################
|
| 21 |
###################################################################################
|
|
|
|
| 22 |
# Copyright 2026 Project Los Angeles / Tegridy Code
|
| 23 |
#
|
| 24 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -32,7 +30,7 @@ r'''############################################################################
|
|
| 32 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 33 |
# See the License for the specific language governing permissions and
|
| 34 |
# limitations under the License.
|
| 35 |
-
#
|
| 36 |
###################################################################################
|
| 37 |
#
|
| 38 |
# PARTIAL MIDI.py Module v.6.7. by Peter Billam
|
|
@@ -46,12 +44,13 @@ r'''############################################################################
|
|
| 46 |
# Copyright 2020 Peter Billam
|
| 47 |
#
|
| 48 |
###################################################################################
|
| 49 |
-
###################################################################################
|
| 50 |
'''
|
| 51 |
|
| 52 |
###################################################################################
|
| 53 |
|
| 54 |
-
__version__ = "26.4.
|
|
|
|
|
|
|
| 55 |
|
| 56 |
print('=' * 70)
|
| 57 |
print('TMIDIX Python module')
|
|
@@ -63,13 +62,25 @@ print('Loading module...')
|
|
| 63 |
|
| 64 |
import sys, struct, copy
|
| 65 |
|
|
|
|
|
|
|
| 66 |
Version = '6.7'
|
| 67 |
VersionDate = '20201120'
|
| 68 |
|
|
|
|
|
|
|
| 69 |
_previous_warning = '' # 5.4
|
| 70 |
_previous_times = 0 # 5.4
|
| 71 |
_no_warning = False
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
#------------------------------- Encoding stuff --------------------------
|
| 74 |
|
| 75 |
def opus2midi(opus=[], text_encoding='ISO-8859-1'):
|
|
@@ -1447,8 +1458,6 @@ def _encode(events_lol, unknown_callback=None, never_add_eot=False,
|
|
| 1447 |
|
| 1448 |
return b''.join(data)
|
| 1449 |
|
| 1450 |
-
###################################################################################
|
| 1451 |
-
###################################################################################
|
| 1452 |
###################################################################################
|
| 1453 |
#
|
| 1454 |
# Tegridy MIDI X Module (TMIDI X / tee-midi eks)
|
|
@@ -1457,13 +1466,11 @@ def _encode(events_lol, unknown_callback=None, never_add_eot=False,
|
|
| 1457 |
# pjb.com.au
|
| 1458 |
#
|
| 1459 |
# Project Los Angeles
|
| 1460 |
-
# Tegridy Code
|
| 1461 |
#
|
| 1462 |
# https://github.com/Tegridy-Code/Project-Los-Angeles
|
| 1463 |
#
|
| 1464 |
###################################################################################
|
| 1465 |
-
###################################################################################
|
| 1466 |
-
###################################################################################
|
| 1467 |
|
| 1468 |
import os
|
| 1469 |
|
|
@@ -7156,7 +7163,8 @@ def escore_notes_to_binary_matrix(escore_notes,
|
|
| 7156 |
patch=0,
|
| 7157 |
flip_matrix=False,
|
| 7158 |
reverse_matrix=False,
|
| 7159 |
-
encode_velocities=False
|
|
|
|
| 7160 |
):
|
| 7161 |
|
| 7162 |
escore = [e for e in escore_notes if e[3] == channel and e[6] == patch]
|
|
@@ -7167,44 +7175,68 @@ def escore_notes_to_binary_matrix(escore_notes,
|
|
| 7167 |
max_last_dur = max([e[2] for e in last_notes])
|
| 7168 |
|
| 7169 |
time_range = last_time+max_last_dur
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7170 |
|
| 7171 |
-
|
| 7172 |
|
| 7173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7174 |
|
| 7175 |
-
|
| 7176 |
|
| 7177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7178 |
|
| 7179 |
-
|
| 7180 |
-
duration = max(1, duration)
|
| 7181 |
-
chan = max(0, min(15, chan))
|
| 7182 |
-
pitch = max(0, min(127, pitch))
|
| 7183 |
-
velocity = max(1, min(127, velocity))
|
| 7184 |
-
pat = max(0, min(128, pat))
|
| 7185 |
|
| 7186 |
-
|
| 7187 |
|
| 7188 |
-
|
| 7189 |
-
|
| 7190 |
-
|
| 7191 |
-
|
| 7192 |
-
|
| 7193 |
-
|
| 7194 |
|
| 7195 |
-
|
| 7196 |
|
| 7197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7198 |
|
| 7199 |
-
|
| 7200 |
-
temp_matrix.append(m[::-1])
|
| 7201 |
|
| 7202 |
-
|
| 7203 |
|
| 7204 |
-
|
| 7205 |
-
|
| 7206 |
|
| 7207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7208 |
|
| 7209 |
else:
|
| 7210 |
return None
|
|
@@ -7223,6 +7255,7 @@ def binary_matrix_to_original_escore_notes(binary_matrix,
|
|
| 7223 |
for j in range(len(binary_matrix[0])):
|
| 7224 |
|
| 7225 |
count = 1
|
|
|
|
| 7226 |
|
| 7227 |
for i in range(1, len(binary_matrix)):
|
| 7228 |
|
|
@@ -18608,6 +18641,27 @@ def values_percentile(data, p):
|
|
| 18608 |
|
| 18609 |
###################################################################################
|
| 18610 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18611 |
print('Module loaded!')
|
| 18612 |
print('=' * 70)
|
| 18613 |
print('Enjoy! :)')
|
|
|
|
| 1 |
#! /usr/bin/python3
|
| 2 |
|
| 3 |
+
r'''
|
| 4 |
+
###############################################################################
|
|
|
|
| 5 |
#
|
| 6 |
# Tegridy MIDI X Module (TMIDI X / tee-midi eks)
|
| 7 |
#
|
| 8 |
+
# NOTE: TMIDI X Module starts after the partial MIDI.py module @ line 1461
|
| 9 |
#
|
| 10 |
# Based upon MIDI.py module v.6.7. by Peter Billam / pjb.com.au
|
| 11 |
#
|
|
|
|
| 15 |
#
|
| 16 |
# https://github.com/Tegridy-Code/Project-Los-Angeles
|
| 17 |
#
|
|
|
|
|
|
|
| 18 |
###################################################################################
|
| 19 |
+
#
|
| 20 |
# Copyright 2026 Project Los Angeles / Tegridy Code
|
| 21 |
#
|
| 22 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
| 30 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 31 |
# See the License for the specific language governing permissions and
|
| 32 |
# limitations under the License.
|
| 33 |
+
#
|
| 34 |
###################################################################################
|
| 35 |
#
|
| 36 |
# PARTIAL MIDI.py Module v.6.7. by Peter Billam
|
|
|
|
| 44 |
# Copyright 2020 Peter Billam
|
| 45 |
#
|
| 46 |
###################################################################################
|
|
|
|
| 47 |
'''
|
| 48 |
|
| 49 |
###################################################################################
|
| 50 |
|
| 51 |
+
__version__ = "26.4.27" # TMIDIX version
|
| 52 |
+
|
| 53 |
+
###################################################################################
|
| 54 |
|
| 55 |
print('=' * 70)
|
| 56 |
print('TMIDIX Python module')
|
|
|
|
| 62 |
|
| 63 |
import sys, struct, copy
|
| 64 |
|
| 65 |
+
###################################################################################
|
| 66 |
+
|
| 67 |
Version = '6.7'
|
| 68 |
VersionDate = '20201120'
|
| 69 |
|
| 70 |
+
###################################################################################
|
| 71 |
+
|
| 72 |
_previous_warning = '' # 5.4
|
| 73 |
_previous_times = 0 # 5.4
|
| 74 |
_no_warning = False
|
| 75 |
|
| 76 |
+
###################################################################################
|
| 77 |
+
|
| 78 |
+
def set_no_warning(value: bool):
|
| 79 |
+
global _no_warning
|
| 80 |
+
_no_warning = value
|
| 81 |
+
|
| 82 |
+
###################################################################################
|
| 83 |
+
|
| 84 |
#------------------------------- Encoding stuff --------------------------
|
| 85 |
|
| 86 |
def opus2midi(opus=[], text_encoding='ISO-8859-1'):
|
|
|
|
| 1458 |
|
| 1459 |
return b''.join(data)
|
| 1460 |
|
|
|
|
|
|
|
| 1461 |
###################################################################################
|
| 1462 |
#
|
| 1463 |
# Tegridy MIDI X Module (TMIDI X / tee-midi eks)
|
|
|
|
| 1466 |
# pjb.com.au
|
| 1467 |
#
|
| 1468 |
# Project Los Angeles
|
| 1469 |
+
# Tegridy Code 2026
|
| 1470 |
#
|
| 1471 |
# https://github.com/Tegridy-Code/Project-Los-Angeles
|
| 1472 |
#
|
| 1473 |
###################################################################################
|
|
|
|
|
|
|
| 1474 |
|
| 1475 |
import os
|
| 1476 |
|
|
|
|
| 7163 |
patch=0,
|
| 7164 |
flip_matrix=False,
|
| 7165 |
reverse_matrix=False,
|
| 7166 |
+
encode_velocities=False,
|
| 7167 |
+
dry_run=False
|
| 7168 |
):
|
| 7169 |
|
| 7170 |
escore = [e for e in escore_notes if e[3] == channel and e[6] == patch]
|
|
|
|
| 7175 |
max_last_dur = max([e[2] for e in last_notes])
|
| 7176 |
|
| 7177 |
time_range = last_time+max_last_dur
|
| 7178 |
+
|
| 7179 |
+
if dry_run:
|
| 7180 |
+
escore_matrix = [[] for _ in range(time_range)]
|
| 7181 |
+
|
| 7182 |
+
for note in escore:
|
| 7183 |
|
| 7184 |
+
etype, time, duration, chan, pitch, velocity, pat = note
|
| 7185 |
|
| 7186 |
+
time = max(0, time)
|
| 7187 |
+
duration = max(1, duration)
|
| 7188 |
+
chan = max(0, min(15, chan))
|
| 7189 |
+
pitch = max(0, min(127, pitch))
|
| 7190 |
+
velocity = max(1, min(127, velocity))
|
| 7191 |
+
pat = max(0, min(128, pat))
|
| 7192 |
|
| 7193 |
+
if channel == chan and patch == pat:
|
| 7194 |
|
| 7195 |
+
for t in range(time, min(time + duration, time_range)):
|
| 7196 |
+
if encode_velocities:
|
| 7197 |
+
escore_matrix[t].extend([pitch, velocity])
|
| 7198 |
+
|
| 7199 |
+
else:
|
| 7200 |
+
escore_matrix[t].append(pitch)
|
| 7201 |
+
|
| 7202 |
+
return escore_matrix
|
| 7203 |
+
|
| 7204 |
+
else:
|
| 7205 |
+
escore_matrix = [[0] * 128 for _ in range(time_range)]
|
| 7206 |
|
| 7207 |
+
for note in escore:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7208 |
|
| 7209 |
+
etype, time, duration, chan, pitch, velocity, pat = note
|
| 7210 |
|
| 7211 |
+
time = max(0, time)
|
| 7212 |
+
duration = max(1, duration)
|
| 7213 |
+
chan = max(0, min(15, chan))
|
| 7214 |
+
pitch = max(0, min(127, pitch))
|
| 7215 |
+
velocity = max(1, min(127, velocity))
|
| 7216 |
+
pat = max(0, min(128, pat))
|
| 7217 |
|
| 7218 |
+
if channel == chan and patch == pat:
|
| 7219 |
|
| 7220 |
+
for t in range(time, min(time + duration, time_range)):
|
| 7221 |
+
if encode_velocities:
|
| 7222 |
+
escore_matrix[t][pitch] = velocity
|
| 7223 |
+
|
| 7224 |
+
else:
|
| 7225 |
+
escore_matrix[t][pitch] = 1
|
| 7226 |
|
| 7227 |
+
if flip_matrix:
|
|
|
|
| 7228 |
|
| 7229 |
+
temp_matrix = []
|
| 7230 |
|
| 7231 |
+
for m in escore_matrix:
|
| 7232 |
+
temp_matrix.append(m[::-1])
|
| 7233 |
|
| 7234 |
+
escore_matrix = temp_matrix
|
| 7235 |
+
|
| 7236 |
+
if reverse_matrix:
|
| 7237 |
+
escore_matrix = escore_matrix[::-1]
|
| 7238 |
+
|
| 7239 |
+
return escore_matrix
|
| 7240 |
|
| 7241 |
else:
|
| 7242 |
return None
|
|
|
|
| 7255 |
for j in range(len(binary_matrix[0])):
|
| 7256 |
|
| 7257 |
count = 1
|
| 7258 |
+
i = 1
|
| 7259 |
|
| 7260 |
for i in range(1, len(binary_matrix)):
|
| 7261 |
|
|
|
|
| 18641 |
|
| 18642 |
###################################################################################
|
| 18643 |
|
| 18644 |
+
def set_of_sublists(list_of_lists):
|
| 18645 |
+
seen = set()
|
| 18646 |
+
add = seen.add
|
| 18647 |
+
|
| 18648 |
+
for sub in list_of_lists:
|
| 18649 |
+
add(tuple(sub))
|
| 18650 |
+
|
| 18651 |
+
return [list(t) for t in seen]
|
| 18652 |
+
|
| 18653 |
+
###################################################################################
|
| 18654 |
+
|
| 18655 |
+
def escore_notes_run_time(escore_notes, time_idx=1, dur_idx=2):
|
| 18656 |
+
|
| 18657 |
+
last_time = escore_notes[-1][time_idx]
|
| 18658 |
+
last_notes = [e for e in escore_notes if e[time_idx] == last_time]
|
| 18659 |
+
max_dur = max([e[dur_idx] for e in last_notes])
|
| 18660 |
+
|
| 18661 |
+
return [last_time+max_dur, last_time]
|
| 18662 |
+
|
| 18663 |
+
###################################################################################
|
| 18664 |
+
|
| 18665 |
print('Module loaded!')
|
| 18666 |
print('=' * 70)
|
| 18667 |
print('Enjoy! :)')
|