Files
MIDIFoundationModel/data_representation/permute.py
2025-11-27 15:44:17 +08:00

40 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from itertools import groupby, chain
from random import shuffle, seed
def shuffled(seq):
shuffle(seq)
return seq
def permute_inside_and_across_tracks(seq):
seq_sorted = sorted(seq, key=lambda x: x[5])
tracks = [list(g) for _, g in groupby(seq_sorted, key=lambda x: x[5])] # 5 is program
return list(chain.from_iterable(shuffled(t) for t in shuffled(tracks)))
# (PitchDrum, Position, Bar, Velocity, Duration, Program, Tempo, TimeSignature)
seq = [
# Program 0
(60, 0, 0, 90, 96, 0, 120, 16),
(64, 48,0, 88, 96, 0, 120, 16),
(67, 96,0, 92, 96, 0, 120, 16),
# Program 32
(40, 0, 0, 80, 192, 32, 120, 16),
(43, 0, 1, 78, 192, 32, 120, 16),
# Program 40
(72, 24,0, 85, 72, 40, 120, 16),
(74, 72,0, 83, 72, 40, 120, 16),
(76, 24,1, 86, 72, 40, 120, 16),
]
# seed(42)
inside_track_permuted_and_track_permuted = permute_inside_and_across_tracks(seq)
print("原始 seq")
for e in seq:
print(e)
print("\n打乱后的 seq")
for e in inside_track_permuted_and_track_permuted:
print(e)