diff --git a/.gitattributes b/.gitattributes index a6344aac8c09253b3b630fb776ae94478aa0275b..96394be90d0b671495bff321e9d64814cdfd1bfd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text *.zst filter=lfs diff=lfs merge=lfs -text *tfevents* filter=lfs diff=lfs merge=lfs -text +SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2 filter=lfs diff=lfs merge=lfs -text diff --git a/SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2 b/SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2 new file mode 100644 index 0000000000000000000000000000000000000000..85f5f34fb7a368ad300978d2a9a4e41f4c3b1670 --- /dev/null +++ b/SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cd41a4639c9e7a96413b4b22540d48e6741e24bcdabcb2eff22cd65929df3cfa +size 553961496 diff --git a/TMIDIX.py b/TMIDIX.py new file mode 100644 index 0000000000000000000000000000000000000000..f57d309618e7fb0aa7be9204af63d952e0d94ab1 --- /dev/null +++ b/TMIDIX.py @@ -0,0 +1,6396 @@ +#! /usr/bin/python3 + + +r'''############################################################################### +################################################################################### +# +# +# Tegridy MIDI X Module (TMIDI X / tee-midi eks) +# Version 1.0 +# +# NOTE: TMIDI X Module starts after the partial MIDI.py module @ line 1342 +# +# Based upon MIDI.py module v.6.7. by Peter Billam / pjb.com.au +# +# Project Los Angeles +# +# Tegridy Code 2021 +# +# https://github.com/Tegridy-Code/Project-Los-Angeles +# +# +################################################################################### +################################################################################### +# Copyright 2021 Project Los Angeles / Tegridy Code +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################### +################################################################################### +# +# PARTIAL MIDI.py Module v.6.7. by Peter Billam +# Please see TMIDI 2.3/tegridy-tools repo for full MIDI.py module code +# +# Or you can always download the latest full version from: +# +# https://pjb.com.au/ +# https://peterbillam.gitlab.io/miditools/ +# +# Copyright 2020 Peter Billam +# +################################################################################### +###################################################################################''' + +import sys, struct, copy +Version = '6.7' +VersionDate = '20201120' + +_previous_warning = '' # 5.4 +_previous_times = 0 # 5.4 +#------------------------------- Encoding stuff -------------------------- + +def opus2midi(opus=[], text_encoding='ISO-8859-1'): + r'''The argument is a list: the first item in the list is the "ticks" +parameter, the others are the tracks. Each track is a list +of midi-events, and each event is itself a list; see above. +opus2midi() returns a bytestring of the MIDI, which can then be +written either to a file opened in binary mode (mode='wb'), +or to stdout by means of: sys.stdout.buffer.write() + +my_opus = [ + 96, + [ # track 0: + ['patch_change', 0, 1, 8], # and these are the events... + ['note_on', 5, 1, 25, 96], + ['note_off', 96, 1, 25, 0], + ['note_on', 0, 1, 29, 96], + ['note_off', 96, 1, 29, 0], + ], # end of track 0 +] +my_midi = opus2midi(my_opus) +sys.stdout.buffer.write(my_midi) +''' + if len(opus) < 2: + opus=[1000, [],] + tracks = copy.deepcopy(opus) + ticks = int(tracks.pop(0)) + ntracks = len(tracks) + if ntracks == 1: + format = 0 + else: + format = 1 + + my_midi = b"MThd\x00\x00\x00\x06"+struct.pack('>HHH',format,ntracks,ticks) + for track in tracks: + events = _encode(track, text_encoding=text_encoding) + my_midi += b'MTrk' + struct.pack('>I',len(events)) + events + _clean_up_warnings() + return my_midi + + +def score2opus(score=None, text_encoding='ISO-8859-1'): + r''' +The argument is a list: the first item in the list is the "ticks" +parameter, the others are the tracks. Each track is a list +of score-events, and each event is itself a list. A score-event +is similar to an opus-event (see above), except that in a score: + 1) the times are expressed as an absolute number of ticks + from the track's start time + 2) the pairs of 'note_on' and 'note_off' events in an "opus" + are abstracted into a single 'note' event in a "score": + ['note', start_time, duration, channel, pitch, velocity] +score2opus() returns a list specifying the equivalent "opus". + +my_score = [ + 96, + [ # track 0: + ['patch_change', 0, 1, 8], + ['note', 5, 96, 1, 25, 96], + ['note', 101, 96, 1, 29, 96] + ], # end of track 0 +] +my_opus = score2opus(my_score) +''' + if len(score) < 2: + score=[1000, [],] + tracks = copy.deepcopy(score) + ticks = int(tracks.pop(0)) + opus_tracks = [] + for scoretrack in tracks: + time2events = dict([]) + for scoreevent in scoretrack: + if scoreevent[0] == 'note': + note_on_event = ['note_on',scoreevent[1], + scoreevent[3],scoreevent[4],scoreevent[5]] + note_off_event = ['note_off',scoreevent[1]+scoreevent[2], + scoreevent[3],scoreevent[4],scoreevent[5]] + if time2events.get(note_on_event[1]): + time2events[note_on_event[1]].append(note_on_event) + else: + time2events[note_on_event[1]] = [note_on_event,] + if time2events.get(note_off_event[1]): + time2events[note_off_event[1]].append(note_off_event) + else: + time2events[note_off_event[1]] = [note_off_event,] + continue + if time2events.get(scoreevent[1]): + time2events[scoreevent[1]].append(scoreevent) + else: + time2events[scoreevent[1]] = [scoreevent,] + + sorted_times = [] # list of keys + for k in time2events.keys(): + sorted_times.append(k) + sorted_times.sort() + + sorted_events = [] # once-flattened list of values sorted by key + for time in sorted_times: + sorted_events.extend(time2events[time]) + + abs_time = 0 + for event in sorted_events: # convert abs times => delta times + delta_time = event[1] - abs_time + abs_time = event[1] + event[1] = delta_time + opus_tracks.append(sorted_events) + opus_tracks.insert(0,ticks) + _clean_up_warnings() + return opus_tracks + +def score2midi(score=None, text_encoding='ISO-8859-1'): + r''' +Translates a "score" into MIDI, using score2opus() then opus2midi() +''' + return opus2midi(score2opus(score, text_encoding), text_encoding) + +#--------------------------- Decoding stuff ------------------------ + +def midi2opus(midi=b'', do_not_check_MIDI_signature=False): + r'''Translates MIDI into a "opus". For a description of the +"opus" format, see opus2midi() +''' + my_midi=bytearray(midi) + if len(my_midi) < 4: + _clean_up_warnings() + return [1000,[],] + id = bytes(my_midi[0:4]) + if id != b'MThd': + _warn("midi2opus: midi starts with "+str(id)+" instead of 'MThd'") + _clean_up_warnings() + if do_not_check_MIDI_signature == False: + return [1000,[],] + [length, format, tracks_expected, ticks] = struct.unpack( + '>IHHH', bytes(my_midi[4:14])) + if length != 6: + _warn("midi2opus: midi header length was "+str(length)+" instead of 6") + _clean_up_warnings() + return [1000,[],] + my_opus = [ticks,] + my_midi = my_midi[14:] + track_num = 1 # 5.1 + while len(my_midi) >= 8: + track_type = bytes(my_midi[0:4]) + if track_type != b'MTrk': + #_warn('midi2opus: Warning: track #'+str(track_num)+' type is '+str(track_type)+" instead of b'MTrk'") + pass + [track_length] = struct.unpack('>I', my_midi[4:8]) + my_midi = my_midi[8:] + if track_length > len(my_midi): + _warn('midi2opus: track #'+str(track_num)+' length '+str(track_length)+' is too large') + _clean_up_warnings() + return my_opus # 5.0 + my_midi_track = my_midi[0:track_length] + my_track = _decode(my_midi_track) + my_opus.append(my_track) + my_midi = my_midi[track_length:] + track_num += 1 # 5.1 + _clean_up_warnings() + return my_opus + +def opus2score(opus=[]): + r'''For a description of the "opus" and "score" formats, +see opus2midi() and score2opus(). +''' + if len(opus) < 2: + _clean_up_warnings() + return [1000,[],] + tracks = copy.deepcopy(opus) # couple of slices probably quicker... + ticks = int(tracks.pop(0)) + score = [ticks,] + for opus_track in tracks: + ticks_so_far = 0 + score_track = [] + chapitch2note_on_events = dict([]) # 4.0 + for opus_event in opus_track: + ticks_so_far += opus_event[1] + if opus_event[0] == 'note_off' or (opus_event[0] == 'note_on' and opus_event[4] == 0): # 4.8 + cha = opus_event[2] + pitch = opus_event[3] + key = cha*128 + pitch + if chapitch2note_on_events.get(key): + new_event = chapitch2note_on_events[key].pop(0) + new_event[2] = ticks_so_far - new_event[1] + score_track.append(new_event) + elif pitch > 127: + pass #_warn('opus2score: note_off with no note_on, bad pitch='+str(pitch)) + else: + pass #_warn('opus2score: note_off with no note_on cha='+str(cha)+' pitch='+str(pitch)) + elif opus_event[0] == 'note_on': + cha = opus_event[2] + pitch = opus_event[3] + key = cha*128 + pitch + new_event = ['note',ticks_so_far,0,cha,pitch, opus_event[4]] + if chapitch2note_on_events.get(key): + chapitch2note_on_events[key].append(new_event) + else: + chapitch2note_on_events[key] = [new_event,] + else: + opus_event[1] = ticks_so_far + score_track.append(opus_event) + # check for unterminated notes (Oisín) -- 5.2 + for chapitch in chapitch2note_on_events: + note_on_events = chapitch2note_on_events[chapitch] + for new_e in note_on_events: + new_e[2] = ticks_so_far - new_e[1] + score_track.append(new_e) + pass #_warn("opus2score: note_on with no note_off cha="+str(new_e[3])+' pitch='+str(new_e[4])+'; adding note_off at end') + score.append(score_track) + _clean_up_warnings() + return score + +def midi2score(midi=b'', do_not_check_MIDI_signature=False): + r''' +Translates MIDI into a "score", using midi2opus() then opus2score() +''' + return opus2score(midi2opus(midi, do_not_check_MIDI_signature)) + +def midi2ms_score(midi=b'', do_not_check_MIDI_signature=False): + r''' +Translates MIDI into a "score" with one beat per second and one +tick per millisecond, using midi2opus() then to_millisecs() +then opus2score() +''' + return opus2score(to_millisecs(midi2opus(midi, do_not_check_MIDI_signature))) + +def midi2single_track_ms_score(midi_path_or_bytes, + recalculate_channels = False, + pass_old_timings_events= False, + verbose = False, + do_not_check_MIDI_signature=False + ): + r''' +Translates MIDI into a single track "score" with 16 instruments and one beat per second and one +tick per millisecond +''' + + if type(midi_path_or_bytes) == bytes: + midi_data = midi_path_or_bytes + + elif type(midi_path_or_bytes) == str: + midi_data = open(midi_path_or_bytes, 'rb').read() + + score = midi2score(midi_data, do_not_check_MIDI_signature) + + if recalculate_channels: + + events_matrixes = [] + + itrack = 1 + events_matrixes_channels = [] + while itrack < len(score): + events_matrix = [] + for event in score[itrack]: + if event[0] == 'note' and event[3] != 9: + event[3] = (16 * (itrack-1)) + event[3] + if event[3] not in events_matrixes_channels: + events_matrixes_channels.append(event[3]) + + events_matrix.append(event) + events_matrixes.append(events_matrix) + itrack += 1 + + events_matrix1 = [] + for e in events_matrixes: + events_matrix1.extend(e) + + if verbose: + if len(events_matrixes_channels) > 16: + print('MIDI has', len(events_matrixes_channels), 'instruments!', len(events_matrixes_channels) - 16, 'instrument(s) will be removed!') + + for e in events_matrix1: + if e[0] == 'note' and e[3] != 9: + if e[3] in events_matrixes_channels[:15]: + if events_matrixes_channels[:15].index(e[3]) < 9: + e[3] = events_matrixes_channels[:15].index(e[3]) + else: + e[3] = events_matrixes_channels[:15].index(e[3])+1 + else: + events_matrix1.remove(e) + + if e[0] in ['patch_change', 'control_change', 'channel_after_touch', 'key_after_touch', 'pitch_wheel_change'] and e[2] != 9: + if e[2] in [e % 16 for e in events_matrixes_channels[:15]]: + if [e % 16 for e in events_matrixes_channels[:15]].index(e[2]) < 9: + e[2] = [e % 16 for e in events_matrixes_channels[:15]].index(e[2]) + else: + e[2] = [e % 16 for e in events_matrixes_channels[:15]].index(e[2])+1 + else: + events_matrix1.remove(e) + + else: + events_matrix1 = [] + itrack = 1 + + while itrack < len(score): + for event in score[itrack]: + events_matrix1.append(event) + itrack += 1 + + opus = score2opus([score[0], events_matrix1]) + ms_score = opus2score(to_millisecs(opus, pass_old_timings_events=pass_old_timings_events)) + + return ms_score + +#------------------------ Other Transformations --------------------- + +def to_millisecs(old_opus=None, desired_time_in_ms=1, pass_old_timings_events = False): + r'''Recallibrates all the times in an "opus" to use one beat +per second and one tick per millisecond. This makes it +hard to retrieve any information about beats or barlines, +but it does make it easy to mix different scores together. +''' + if old_opus == None: + return [1000 * desired_time_in_ms,[],] + try: + old_tpq = int(old_opus[0]) + except IndexError: # 5.0 + _warn('to_millisecs: the opus '+str(type(old_opus))+' has no elements') + return [1000 * desired_time_in_ms,[],] + new_opus = [1000 * desired_time_in_ms,] + # 6.7 first go through building a table of set_tempos by absolute-tick + ticks2tempo = {} + itrack = 1 + while itrack < len(old_opus): + ticks_so_far = 0 + for old_event in old_opus[itrack]: + if old_event[0] == 'note': + raise TypeError('to_millisecs needs an opus, not a score') + ticks_so_far += old_event[1] + if old_event[0] == 'set_tempo': + ticks2tempo[ticks_so_far] = old_event[2] + itrack += 1 + # then get the sorted-array of their keys + tempo_ticks = [] # list of keys + for k in ticks2tempo.keys(): + tempo_ticks.append(k) + tempo_ticks.sort() + # then go through converting to millisec, testing if the next + # set_tempo lies before the next track-event, and using it if so. + itrack = 1 + while itrack < len(old_opus): + ms_per_old_tick = 400 / old_tpq # float: will round later 6.3 + i_tempo_ticks = 0 + ticks_so_far = 0 + ms_so_far = 0.0 + previous_ms_so_far = 0.0 + + if pass_old_timings_events: + new_track = [['set_tempo',0,1000000 * desired_time_in_ms],['old_tpq', 0, old_tpq]] # new "crochet" is 1 sec + else: + new_track = [['set_tempo',0,1000000 * desired_time_in_ms],] # new "crochet" is 1 sec + for old_event in old_opus[itrack]: + # detect if ticks2tempo has something before this event + # 20160702 if ticks2tempo is at the same time, leave it + event_delta_ticks = old_event[1] * desired_time_in_ms + if (i_tempo_ticks < len(tempo_ticks) and + tempo_ticks[i_tempo_ticks] < (ticks_so_far + old_event[1]) * desired_time_in_ms): + delta_ticks = tempo_ticks[i_tempo_ticks] - ticks_so_far + ms_so_far += (ms_per_old_tick * delta_ticks * desired_time_in_ms) + ticks_so_far = tempo_ticks[i_tempo_ticks] + ms_per_old_tick = ticks2tempo[ticks_so_far] / (1000.0*old_tpq * desired_time_in_ms) + i_tempo_ticks += 1 + event_delta_ticks -= delta_ticks + new_event = copy.deepcopy(old_event) # now handle the new event + ms_so_far += (ms_per_old_tick * old_event[1] * desired_time_in_ms) + new_event[1] = round(ms_so_far - previous_ms_so_far) + + if pass_old_timings_events: + if old_event[0] != 'set_tempo': + previous_ms_so_far = ms_so_far + new_track.append(new_event) + else: + new_event[0] = 'old_set_tempo' + previous_ms_so_far = ms_so_far + new_track.append(new_event) + else: + if old_event[0] != 'set_tempo': + previous_ms_so_far = ms_so_far + new_track.append(new_event) + ticks_so_far += event_delta_ticks + new_opus.append(new_track) + itrack += 1 + _clean_up_warnings() + return new_opus + +def event2alsaseq(event=None): # 5.5 + r'''Converts an event into the format needed by the alsaseq module, +http://pp.com.mx/python/alsaseq +The type of track (opus or score) is autodetected. +''' + pass + +def grep(score=None, channels=None): + r'''Returns a "score" containing only the channels specified +''' + if score == None: + return [1000,[],] + ticks = score[0] + new_score = [ticks,] + if channels == None: + return new_score + channels = set(channels) + global Event2channelindex + itrack = 1 + while itrack < len(score): + new_score.append([]) + for event in score[itrack]: + channel_index = Event2channelindex.get(event[0], False) + if channel_index: + if event[channel_index] in channels: + new_score[itrack].append(event) + else: + new_score[itrack].append(event) + itrack += 1 + return new_score + +def play_score(score=None): + r'''Converts the "score" to midi, and feeds it into 'aplaymidi -' +''' + if score == None: + return + import subprocess + pipe = subprocess.Popen(['aplaymidi','-'], stdin=subprocess.PIPE) + if score_type(score) == 'opus': + pipe.stdin.write(opus2midi(score)) + else: + pipe.stdin.write(score2midi(score)) + pipe.stdin.close() + +def score2stats(opus_or_score=None): + r'''Returns a dict of some basic stats about the score, like +bank_select (list of tuples (msb,lsb)), +channels_by_track (list of lists), channels_total (set), +general_midi_mode (list), +ntracks, nticks, patch_changes_by_track (list of dicts), +num_notes_by_channel (list of numbers), +patch_changes_total (set), +percussion (dict histogram of channel 9 events), +pitches (dict histogram of pitches on channels other than 9), +pitch_range_by_track (list, by track, of two-member-tuples), +pitch_range_sum (sum over tracks of the pitch_ranges), +''' + bank_select_msb = -1 + bank_select_lsb = -1 + bank_select = [] + channels_by_track = [] + channels_total = set([]) + general_midi_mode = [] + num_notes_by_channel = dict([]) + patches_used_by_track = [] + patches_used_total = set([]) + patch_changes_by_track = [] + patch_changes_total = set([]) + percussion = dict([]) # histogram of channel 9 "pitches" + pitches = dict([]) # histogram of pitch-occurrences channels 0-8,10-15 + pitch_range_sum = 0 # u pitch-ranges of each track + pitch_range_by_track = [] + is_a_score = True + if opus_or_score == None: + return {'bank_select':[], 'channels_by_track':[], 'channels_total':[], + 'general_midi_mode':[], 'ntracks':0, 'nticks':0, + 'num_notes_by_channel':dict([]), + 'patch_changes_by_track':[], 'patch_changes_total':[], + 'percussion':{}, 'pitches':{}, 'pitch_range_by_track':[], + 'ticks_per_quarter':0, 'pitch_range_sum':0} + ticks_per_quarter = opus_or_score[0] + i = 1 # ignore first element, which is ticks + nticks = 0 + while i < len(opus_or_score): + highest_pitch = 0 + lowest_pitch = 128 + channels_this_track = set([]) + patch_changes_this_track = dict({}) + for event in opus_or_score[i]: + if event[0] == 'note': + num_notes_by_channel[event[3]] = num_notes_by_channel.get(event[3],0) + 1 + if event[3] == 9: + percussion[event[4]] = percussion.get(event[4],0) + 1 + else: + pitches[event[4]] = pitches.get(event[4],0) + 1 + if event[4] > highest_pitch: + highest_pitch = event[4] + if event[4] < lowest_pitch: + lowest_pitch = event[4] + channels_this_track.add(event[3]) + channels_total.add(event[3]) + finish_time = event[1] + event[2] + if finish_time > nticks: + nticks = finish_time + elif event[0] == 'note_off' or (event[0] == 'note_on' and event[4] == 0): # 4.8 + finish_time = event[1] + if finish_time > nticks: + nticks = finish_time + elif event[0] == 'note_on': + is_a_score = False + num_notes_by_channel[event[2]] = num_notes_by_channel.get(event[2],0) + 1 + if event[2] == 9: + percussion[event[3]] = percussion.get(event[3],0) + 1 + else: + pitches[event[3]] = pitches.get(event[3],0) + 1 + if event[3] > highest_pitch: + highest_pitch = event[3] + if event[3] < lowest_pitch: + lowest_pitch = event[3] + channels_this_track.add(event[2]) + channels_total.add(event[2]) + elif event[0] == 'patch_change': + patch_changes_this_track[event[2]] = event[3] + patch_changes_total.add(event[3]) + elif event[0] == 'control_change': + if event[3] == 0: # bank select MSB + bank_select_msb = event[4] + elif event[3] == 32: # bank select LSB + bank_select_lsb = event[4] + if bank_select_msb >= 0 and bank_select_lsb >= 0: + bank_select.append((bank_select_msb,bank_select_lsb)) + bank_select_msb = -1 + bank_select_lsb = -1 + elif event[0] == 'sysex_f0': + if _sysex2midimode.get(event[2], -1) >= 0: + general_midi_mode.append(_sysex2midimode.get(event[2])) + if is_a_score: + if event[1] > nticks: + nticks = event[1] + else: + nticks += event[1] + if lowest_pitch == 128: + lowest_pitch = 0 + channels_by_track.append(channels_this_track) + patch_changes_by_track.append(patch_changes_this_track) + pitch_range_by_track.append((lowest_pitch,highest_pitch)) + pitch_range_sum += (highest_pitch-lowest_pitch) + i += 1 + + return {'bank_select':bank_select, + 'channels_by_track':channels_by_track, + 'channels_total':channels_total, + 'general_midi_mode':general_midi_mode, + 'ntracks':len(opus_or_score)-1, + 'nticks':nticks, + 'num_notes_by_channel':num_notes_by_channel, + 'patch_changes_by_track':patch_changes_by_track, + 'patch_changes_total':patch_changes_total, + 'percussion':percussion, + 'pitches':pitches, + 'pitch_range_by_track':pitch_range_by_track, + 'pitch_range_sum':pitch_range_sum, + 'ticks_per_quarter':ticks_per_quarter} + +#----------------------------- Event stuff -------------------------- + +_sysex2midimode = { + "\x7E\x7F\x09\x01\xF7": 1, + "\x7E\x7F\x09\x02\xF7": 0, + "\x7E\x7F\x09\x03\xF7": 2, +} + +# Some public-access tuples: +MIDI_events = tuple('''note_off note_on key_after_touch +control_change patch_change channel_after_touch +pitch_wheel_change'''.split()) + +Text_events = tuple('''text_event copyright_text_event +track_name instrument_name lyric marker cue_point text_event_08 +text_event_09 text_event_0a text_event_0b text_event_0c +text_event_0d text_event_0e text_event_0f'''.split()) + +Nontext_meta_events = tuple('''end_track set_tempo +smpte_offset time_signature key_signature sequencer_specific +raw_meta_event sysex_f0 sysex_f7 song_position song_select +tune_request'''.split()) +# unsupported: raw_data + +# Actually, 'tune_request' is is F-series event, not strictly a meta-event... +Meta_events = Text_events + Nontext_meta_events +All_events = MIDI_events + Meta_events + +# And three dictionaries: +Number2patch = { # General MIDI patch numbers: +0:'Acoustic Grand', +1:'Bright Acoustic', +2:'Electric Grand', +3:'Honky-Tonk', +4:'Electric Piano 1', +5:'Electric Piano 2', +6:'Harpsichord', +7:'Clav', +8:'Celesta', +9:'Glockenspiel', +10:'Music Box', +11:'Vibraphone', +12:'Marimba', +13:'Xylophone', +14:'Tubular Bells', +15:'Dulcimer', +16:'Drawbar Organ', +17:'Percussive Organ', +18:'Rock Organ', +19:'Church Organ', +20:'Reed Organ', +21:'Accordion', +22:'Harmonica', +23:'Tango Accordion', +24:'Acoustic Guitar(nylon)', +25:'Acoustic Guitar(steel)', +26:'Electric Guitar(jazz)', +27:'Electric Guitar(clean)', +28:'Electric Guitar(muted)', +29:'Overdriven Guitar', +30:'Distortion Guitar', +31:'Guitar Harmonics', +32:'Acoustic Bass', +33:'Electric Bass(finger)', +34:'Electric Bass(pick)', +35:'Fretless Bass', +36:'Slap Bass 1', +37:'Slap Bass 2', +38:'Synth Bass 1', +39:'Synth Bass 2', +40:'Violin', +41:'Viola', +42:'Cello', +43:'Contrabass', +44:'Tremolo Strings', +45:'Pizzicato Strings', +46:'Orchestral Harp', +47:'Timpani', +48:'String Ensemble 1', +49:'String Ensemble 2', +50:'SynthStrings 1', +51:'SynthStrings 2', +52:'Choir Aahs', +53:'Voice Oohs', +54:'Synth Voice', +55:'Orchestra Hit', +56:'Trumpet', +57:'Trombone', +58:'Tuba', +59:'Muted Trumpet', +60:'French Horn', +61:'Brass Section', +62:'SynthBrass 1', +63:'SynthBrass 2', +64:'Soprano Sax', +65:'Alto Sax', +66:'Tenor Sax', +67:'Baritone Sax', +68:'Oboe', +69:'English Horn', +70:'Bassoon', +71:'Clarinet', +72:'Piccolo', +73:'Flute', +74:'Recorder', +75:'Pan Flute', +76:'Blown Bottle', +77:'Skakuhachi', +78:'Whistle', +79:'Ocarina', +80:'Lead 1 (square)', +81:'Lead 2 (sawtooth)', +82:'Lead 3 (calliope)', +83:'Lead 4 (chiff)', +84:'Lead 5 (charang)', +85:'Lead 6 (voice)', +86:'Lead 7 (fifths)', +87:'Lead 8 (bass+lead)', +88:'Pad 1 (new age)', +89:'Pad 2 (warm)', +90:'Pad 3 (polysynth)', +91:'Pad 4 (choir)', +92:'Pad 5 (bowed)', +93:'Pad 6 (metallic)', +94:'Pad 7 (halo)', +95:'Pad 8 (sweep)', +96:'FX 1 (rain)', +97:'FX 2 (soundtrack)', +98:'FX 3 (crystal)', +99:'FX 4 (atmosphere)', +100:'FX 5 (brightness)', +101:'FX 6 (goblins)', +102:'FX 7 (echoes)', +103:'FX 8 (sci-fi)', +104:'Sitar', +105:'Banjo', +106:'Shamisen', +107:'Koto', +108:'Kalimba', +109:'Bagpipe', +110:'Fiddle', +111:'Shanai', +112:'Tinkle Bell', +113:'Agogo', +114:'Steel Drums', +115:'Woodblock', +116:'Taiko Drum', +117:'Melodic Tom', +118:'Synth Drum', +119:'Reverse Cymbal', +120:'Guitar Fret Noise', +121:'Breath Noise', +122:'Seashore', +123:'Bird Tweet', +124:'Telephone Ring', +125:'Helicopter', +126:'Applause', +127:'Gunshot', +} +Notenum2percussion = { # General MIDI Percussion (on Channel 9): +35:'Acoustic Bass Drum', +36:'Bass Drum 1', +37:'Side Stick', +38:'Acoustic Snare', +39:'Hand Clap', +40:'Electric Snare', +41:'Low Floor Tom', +42:'Closed Hi-Hat', +43:'High Floor Tom', +44:'Pedal Hi-Hat', +45:'Low Tom', +46:'Open Hi-Hat', +47:'Low-Mid Tom', +48:'Hi-Mid Tom', +49:'Crash Cymbal 1', +50:'High Tom', +51:'Ride Cymbal 1', +52:'Chinese Cymbal', +53:'Ride Bell', +54:'Tambourine', +55:'Splash Cymbal', +56:'Cowbell', +57:'Crash Cymbal 2', +58:'Vibraslap', +59:'Ride Cymbal 2', +60:'Hi Bongo', +61:'Low Bongo', +62:'Mute Hi Conga', +63:'Open Hi Conga', +64:'Low Conga', +65:'High Timbale', +66:'Low Timbale', +67:'High Agogo', +68:'Low Agogo', +69:'Cabasa', +70:'Maracas', +71:'Short Whistle', +72:'Long Whistle', +73:'Short Guiro', +74:'Long Guiro', +75:'Claves', +76:'Hi Wood Block', +77:'Low Wood Block', +78:'Mute Cuica', +79:'Open Cuica', +80:'Mute Triangle', +81:'Open Triangle', +} + +Event2channelindex = { 'note':3, 'note_off':2, 'note_on':2, + 'key_after_touch':2, 'control_change':2, 'patch_change':2, + 'channel_after_touch':2, 'pitch_wheel_change':2 +} + +################################################################ +# The code below this line is full of frightening things, all to +# do with the actual encoding and decoding of binary MIDI data. + +def _twobytes2int(byte_a): + r'''decode a 16 bit quantity from two bytes,''' + return (byte_a[1] | (byte_a[0] << 8)) + +def _int2twobytes(int_16bit): + r'''encode a 16 bit quantity into two bytes,''' + return bytes([(int_16bit>>8) & 0xFF, int_16bit & 0xFF]) + +def _read_14_bit(byte_a): + r'''decode a 14 bit quantity from two bytes,''' + return (byte_a[0] | (byte_a[1] << 7)) + +def _write_14_bit(int_14bit): + r'''encode a 14 bit quantity into two bytes,''' + return bytes([int_14bit & 0x7F, (int_14bit>>7) & 0x7F]) + +def _ber_compressed_int(integer): + r'''BER compressed integer (not an ASN.1 BER, see perlpacktut for +details). Its bytes represent an unsigned integer in base 128, +most significant digit first, with as few digits as possible. +Bit eight (the high bit) is set on each byte except the last. +''' + ber = bytearray(b'') + seven_bits = 0x7F & integer + ber.insert(0, seven_bits) # XXX surely should convert to a char ? + integer >>= 7 + while integer > 0: + seven_bits = 0x7F & integer + ber.insert(0, 0x80|seven_bits) # XXX surely should convert to a char ? + integer >>= 7 + return ber + +def _unshift_ber_int(ba): + r'''Given a bytearray, returns a tuple of (the ber-integer at the +start, and the remainder of the bytearray). +''' + if not len(ba): # 6.7 + _warn('_unshift_ber_int: no integer found') + return ((0, b"")) + byte = ba.pop(0) + integer = 0 + while True: + integer += (byte & 0x7F) + if not (byte & 0x80): + return ((integer, ba)) + if not len(ba): + _warn('_unshift_ber_int: no end-of-integer found') + return ((0, ba)) + byte = ba.pop(0) + integer <<= 7 + +def _clean_up_warnings(): # 5.4 + # Call this before returning from any publicly callable function + # whenever there's a possibility that a warning might have been printed + # by the function, or by any private functions it might have called. + global _previous_times + global _previous_warning + if _previous_times > 1: + # E:1176, 0: invalid syntax (, line 1176) (syntax-error) ??? + # print(' previous message repeated '+str(_previous_times)+' times', file=sys.stderr) + # 6.7 + sys.stderr.write(' previous message repeated {0} times\n'.format(_previous_times)) + elif _previous_times > 0: + sys.stderr.write(' previous message repeated\n') + _previous_times = 0 + _previous_warning = '' + +def _warn(s=''): + global _previous_times + global _previous_warning + if s == _previous_warning: # 5.4 + _previous_times = _previous_times + 1 + else: + _clean_up_warnings() + sys.stderr.write(str(s)+"\n") + _previous_warning = s + +def _some_text_event(which_kind=0x01, text=b'some_text', text_encoding='ISO-8859-1'): + if str(type(text)).find("'str'") >= 0: # 6.4 test for back-compatibility + data = bytes(text, encoding=text_encoding) + else: + data = bytes(text) + return b'\xFF'+bytes((which_kind,))+_ber_compressed_int(len(data))+data + +def _consistentise_ticks(scores): # 3.6 + # used by mix_scores, merge_scores, concatenate_scores + if len(scores) == 1: + return copy.deepcopy(scores) + are_consistent = True + ticks = scores[0][0] + iscore = 1 + while iscore < len(scores): + if scores[iscore][0] != ticks: + are_consistent = False + break + iscore += 1 + if are_consistent: + return copy.deepcopy(scores) + new_scores = [] + iscore = 0 + while iscore < len(scores): + score = scores[iscore] + new_scores.append(opus2score(to_millisecs(score2opus(score)))) + iscore += 1 + return new_scores + + +########################################################################### + +def _decode(trackdata=b'', exclude=None, include=None, + event_callback=None, exclusive_event_callback=None, no_eot_magic=False): + r'''Decodes MIDI track data into an opus-style list of events. +The options: + 'exclude' is a list of event types which will be ignored SHOULD BE A SET + 'include' (and no exclude), makes exclude a list + of all possible events, /minus/ what include specifies + 'event_callback' is a coderef + 'exclusive_event_callback' is a coderef +''' + trackdata = bytearray(trackdata) + if exclude == None: + exclude = [] + if include == None: + include = [] + if include and not exclude: + exclude = All_events + include = set(include) + exclude = set(exclude) + + # Pointer = 0; not used here; we eat through the bytearray instead. + event_code = -1; # used for running status + event_count = 0; + events = [] + + while(len(trackdata)): + # loop while there's anything to analyze ... + eot = False # When True, the event registrar aborts this loop + event_count += 1 + + E = [] + # E for events - we'll feed it to the event registrar at the end. + + # Slice off the delta time code, and analyze it + [time, remainder] = _unshift_ber_int(trackdata) + + # Now let's see what we can make of the command + first_byte = trackdata.pop(0) & 0xFF + + if (first_byte < 0xF0): # It's a MIDI event + if (first_byte & 0x80): + event_code = first_byte + else: + # It wants running status; use last event_code value + trackdata.insert(0, first_byte) + if (event_code == -1): + _warn("Running status not set; Aborting track.") + return [] + + command = event_code & 0xF0 + channel = event_code & 0x0F + + if (command == 0xF6): # 0-byte argument + pass + elif (command == 0xC0 or command == 0xD0): # 1-byte argument + parameter = trackdata.pop(0) # could be B + else: # 2-byte argument could be BB or 14-bit + parameter = (trackdata.pop(0), trackdata.pop(0)) + + ################################################################# + # MIDI events + + if (command == 0x80): + if 'note_off' in exclude: + continue + E = ['note_off', time, channel, parameter[0], parameter[1]] + elif (command == 0x90): + if 'note_on' in exclude: + continue + E = ['note_on', time, channel, parameter[0], parameter[1]] + elif (command == 0xA0): + if 'key_after_touch' in exclude: + continue + E = ['key_after_touch',time,channel,parameter[0],parameter[1]] + elif (command == 0xB0): + if 'control_change' in exclude: + continue + E = ['control_change',time,channel,parameter[0],parameter[1]] + elif (command == 0xC0): + if 'patch_change' in exclude: + continue + E = ['patch_change', time, channel, parameter] + elif (command == 0xD0): + if 'channel_after_touch' in exclude: + continue + E = ['channel_after_touch', time, channel, parameter] + elif (command == 0xE0): + if 'pitch_wheel_change' in exclude: + continue + E = ['pitch_wheel_change', time, channel, + _read_14_bit(parameter)-0x2000] + else: + _warn("Shouldn't get here; command="+hex(command)) + + elif (first_byte == 0xFF): # It's a Meta-Event! ################## + #[command, length, remainder] = + # unpack("xCwa*", substr(trackdata, $Pointer, 6)); + #Pointer += 6 - len(remainder); + # # Move past JUST the length-encoded. + command = trackdata.pop(0) & 0xFF + [length, trackdata] = _unshift_ber_int(trackdata) + if (command == 0x00): + if (length == 2): + E = ['set_sequence_number',time,_twobytes2int(trackdata)] + else: + _warn('set_sequence_number: length must be 2, not '+str(length)) + E = ['set_sequence_number', time, 0] + + elif command >= 0x01 and command <= 0x0f: # Text events + # 6.2 take it in bytes; let the user get the right encoding. + # text_str = trackdata[0:length].decode('ascii','ignore') + # text_str = trackdata[0:length].decode('ISO-8859-1') + # 6.4 take it in bytes; let the user get the right encoding. + text_data = bytes(trackdata[0:length]) # 6.4 + # Defined text events + if (command == 0x01): + E = ['text_event', time, text_data] + elif (command == 0x02): + E = ['copyright_text_event', time, text_data] + elif (command == 0x03): + E = ['track_name', time, text_data] + elif (command == 0x04): + E = ['instrument_name', time, text_data] + elif (command == 0x05): + E = ['lyric', time, text_data] + elif (command == 0x06): + E = ['marker', time, text_data] + elif (command == 0x07): + E = ['cue_point', time, text_data] + # Reserved but apparently unassigned text events + elif (command == 0x08): + E = ['text_event_08', time, text_data] + elif (command == 0x09): + E = ['text_event_09', time, text_data] + elif (command == 0x0a): + E = ['text_event_0a', time, text_data] + elif (command == 0x0b): + E = ['text_event_0b', time, text_data] + elif (command == 0x0c): + E = ['text_event_0c', time, text_data] + elif (command == 0x0d): + E = ['text_event_0d', time, text_data] + elif (command == 0x0e): + E = ['text_event_0e', time, text_data] + elif (command == 0x0f): + E = ['text_event_0f', time, text_data] + + # Now the sticky events ------------------------------------- + elif (command == 0x2F): + E = ['end_track', time] + # The code for handling this, oddly, comes LATER, + # in the event registrar. + elif (command == 0x51): # DTime, Microseconds/Crochet + if length != 3: + _warn('set_tempo event, but length='+str(length)) + E = ['set_tempo', time, + struct.unpack(">I", b'\x00'+trackdata[0:3])[0]] + elif (command == 0x54): + if length != 5: # DTime, HR, MN, SE, FR, FF + _warn('smpte_offset event, but length='+str(length)) + E = ['smpte_offset',time] + list(struct.unpack(">BBBBB",trackdata[0:5])) + elif (command == 0x58): + if length != 4: # DTime, NN, DD, CC, BB + _warn('time_signature event, but length='+str(length)) + E = ['time_signature', time]+list(trackdata[0:4]) + elif (command == 0x59): + if length != 2: # DTime, SF(signed), MI + _warn('key_signature event, but length='+str(length)) + E = ['key_signature',time] + list(struct.unpack(">bB",trackdata[0:2])) + elif (command == 0x7F): # 6.4 + E = ['sequencer_specific',time, bytes(trackdata[0:length])] + else: + E = ['raw_meta_event', time, command, + bytes(trackdata[0:length])] # 6.0 + #"[uninterpretable meta-event command of length length]" + # DTime, Command, Binary Data + # It's uninterpretable; record it as raw_data. + + # Pointer += length; # Now move Pointer + trackdata = trackdata[length:] + + ###################################################################### + elif (first_byte == 0xF0 or first_byte == 0xF7): + # Note that sysexes in MIDI /files/ are different than sysexes + # in MIDI transmissions!! The vast majority of system exclusive + # messages will just use the F0 format. For instance, the + # transmitted message F0 43 12 00 07 F7 would be stored in a + # MIDI file as F0 05 43 12 00 07 F7. As mentioned above, it is + # required to include the F7 at the end so that the reader of the + # MIDI file knows that it has read the entire message. (But the F7 + # is omitted if this is a non-final block in a multiblock sysex; + # but the F7 (if there) is counted in the message's declared + # length, so we don't have to think about it anyway.) + #command = trackdata.pop(0) + [length, trackdata] = _unshift_ber_int(trackdata) + if first_byte == 0xF0: + # 20091008 added ISO-8859-1 to get an 8-bit str + # 6.4 return bytes instead + E = ['sysex_f0', time, bytes(trackdata[0:length])] + else: + E = ['sysex_f7', time, bytes(trackdata[0:length])] + trackdata = trackdata[length:] + + ###################################################################### + # Now, the MIDI file spec says: + # = + + # = + # = | | + # I know that, on the wire, can include note_on, + # note_off, and all the other 8x to Ex events, AND Fx events + # other than F0, F7, and FF -- namely, , + # , and . + # + # Whether these can occur in MIDI files is not clear specified + # from the MIDI file spec. So, I'm going to assume that + # they CAN, in practice, occur. I don't know whether it's + # proper for you to actually emit these into a MIDI file. + + elif (first_byte == 0xF2): # DTime, Beats + # ::= F2 + E = ['song_position', time, _read_14_bit(trackdata[:2])] + trackdata = trackdata[2:] + + elif (first_byte == 0xF3): # ::= F3 + # E = ['song_select', time, struct.unpack('>B',trackdata.pop(0))[0]] + E = ['song_select', time, trackdata[0]] + trackdata = trackdata[1:] + # DTime, Thing (what?! song number? whatever ...) + + elif (first_byte == 0xF6): # DTime + E = ['tune_request', time] + # What would a tune request be doing in a MIDI /file/? + + ######################################################### + # ADD MORE META-EVENTS HERE. TODO: + # f1 -- MTC Quarter Frame Message. One data byte follows + # the Status; it's the time code value, from 0 to 127. + # f8 -- MIDI clock. no data. + # fa -- MIDI start. no data. + # fb -- MIDI continue. no data. + # fc -- MIDI stop. no data. + # fe -- Active sense. no data. + # f4 f5 f9 fd -- unallocated + + r''' + elif (first_byte > 0xF0) { # Some unknown kinda F-series event #### + # Here we only produce a one-byte piece of raw data. + # But the encoder for 'raw_data' accepts any length of it. + E = [ 'raw_data', + time, substr(trackdata,Pointer,1) ] + # DTime and the Data (in this case, the one Event-byte) + ++Pointer; # itself + +''' + elif first_byte > 0xF0: # Some unknown F-series event + # Here we only produce a one-byte piece of raw data. + # E = ['raw_data', time, bytest(trackdata[0])] # 6.4 + E = ['raw_data', time, trackdata[0]] # 6.4 6.7 + trackdata = trackdata[1:] + else: # Fallthru. + _warn("Aborting track. Command-byte first_byte="+hex(first_byte)) + break + # End of the big if-group + + + ###################################################################### + # THE EVENT REGISTRAR... + if E and (E[0] == 'end_track'): + # This is the code for exceptional handling of the EOT event. + eot = True + if not no_eot_magic: + if E[1] > 0: # a null text-event to carry the delta-time + E = ['text_event', E[1], ''] + else: + E = [] # EOT with a delta-time of 0; ignore it. + + if E and not (E[0] in exclude): + #if ( $exclusive_event_callback ): + # &{ $exclusive_event_callback }( @E ); + #else: + # &{ $event_callback }( @E ) if $event_callback; + events.append(E) + if eot: + break + + # End of the big "Event" while-block + + return events + + +########################################################################### +def _encode(events_lol, unknown_callback=None, never_add_eot=False, + no_eot_magic=False, no_running_status=False, text_encoding='ISO-8859-1'): + # encode an event structure, presumably for writing to a file + # Calling format: + # $data_r = MIDI::Event::encode( \@event_lol, { options } ); + # Takes a REFERENCE to an event structure (a LoL) + # Returns an (unblessed) REFERENCE to track data. + + # If you want to use this to encode a /single/ event, + # you still have to do it as a reference to an event structure (a LoL) + # that just happens to have just one event. I.e., + # encode( [ $event ] ) or encode( [ [ 'note_on', 100, 5, 42, 64] ] ) + # If you're doing this, consider the never_add_eot track option, as in + # print MIDI ${ encode( [ $event], { 'never_add_eot' => 1} ) }; + + data = [] # what I'll store the chunks of byte-data in + + # This is so my end_track magic won't corrupt the original + events = copy.deepcopy(events_lol) + + if not never_add_eot: + # One way or another, tack on an 'end_track' + if events: + last = events[-1] + if not (last[0] == 'end_track'): # no end_track already + if (last[0] == 'text_event' and len(last[2]) == 0): + # 0-length text event at track-end. + if no_eot_magic: + # Exceptional case: don't mess with track-final + # 0-length text_events; just peg on an end_track + events.append(['end_track', 0]) + else: + # NORMAL CASE: replace with an end_track, leaving DTime + last[0] = 'end_track' + else: + # last event was neither 0-length text_event nor end_track + events.append(['end_track', 0]) + else: # an eventless track! + events = [['end_track', 0],] + + # maybe_running_status = not no_running_status # unused? 4.7 + last_status = -1 + + for event_r in (events): + E = copy.deepcopy(event_r) + # otherwise the shifting'd corrupt the original + if not E: + continue + + event = E.pop(0) + if not len(event): + continue + + dtime = int(E.pop(0)) + # print('event='+str(event)+' dtime='+str(dtime)) + + event_data = '' + + if ( # MIDI events -- eligible for running status + event == 'note_on' + or event == 'note_off' + or event == 'control_change' + or event == 'key_after_touch' + or event == 'patch_change' + or event == 'channel_after_touch' + or event == 'pitch_wheel_change' ): + + # This block is where we spend most of the time. Gotta be tight. + if (event == 'note_off'): + status = 0x80 | (int(E[0]) & 0x0F) + parameters = struct.pack('>BB', int(E[1])&0x7F, int(E[2])&0x7F) + elif (event == 'note_on'): + status = 0x90 | (int(E[0]) & 0x0F) + parameters = struct.pack('>BB', int(E[1])&0x7F, int(E[2])&0x7F) + elif (event == 'key_after_touch'): + status = 0xA0 | (int(E[0]) & 0x0F) + parameters = struct.pack('>BB', int(E[1])&0x7F, int(E[2])&0x7F) + elif (event == 'control_change'): + status = 0xB0 | (int(E[0]) & 0x0F) + parameters = struct.pack('>BB', int(E[1])&0xFF, int(E[2])&0xFF) + elif (event == 'patch_change'): + status = 0xC0 | (int(E[0]) & 0x0F) + parameters = struct.pack('>B', int(E[1]) & 0xFF) + elif (event == 'channel_after_touch'): + status = 0xD0 | (int(E[0]) & 0x0F) + parameters = struct.pack('>B', int(E[1]) & 0xFF) + elif (event == 'pitch_wheel_change'): + status = 0xE0 | (int(E[0]) & 0x0F) + parameters = _write_14_bit(int(E[1]) + 0x2000) + else: + _warn("BADASS FREAKOUT ERROR 31415!") + + # And now the encoding + # w = BER compressed integer (not ASN.1 BER, see perlpacktut for + # details). Its bytes represent an unsigned integer in base 128, + # most significant digit first, with as few digits as possible. + # Bit eight (the high bit) is set on each byte except the last. + + data.append(_ber_compressed_int(dtime)) + if (status != last_status) or no_running_status: + data.append(struct.pack('>B', status)) + data.append(parameters) + + last_status = status + continue + else: + # Not a MIDI event. + # All the code in this block could be more efficient, + # but this is not where the code needs to be tight. + # print "zaz $event\n"; + last_status = -1 + + if event == 'raw_meta_event': + event_data = _some_text_event(int(E[0]), E[1], text_encoding) + elif (event == 'set_sequence_number'): # 3.9 + event_data = b'\xFF\x00\x02'+_int2twobytes(E[0]) + + # Text meta-events... + # a case for a dict, I think (pjb) ... + elif (event == 'text_event'): + event_data = _some_text_event(0x01, E[0], text_encoding) + elif (event == 'copyright_text_event'): + event_data = _some_text_event(0x02, E[0], text_encoding) + elif (event == 'track_name'): + event_data = _some_text_event(0x03, E[0], text_encoding) + elif (event == 'instrument_name'): + event_data = _some_text_event(0x04, E[0], text_encoding) + elif (event == 'lyric'): + event_data = _some_text_event(0x05, E[0], text_encoding) + elif (event == 'marker'): + event_data = _some_text_event(0x06, E[0], text_encoding) + elif (event == 'cue_point'): + event_data = _some_text_event(0x07, E[0], text_encoding) + elif (event == 'text_event_08'): + event_data = _some_text_event(0x08, E[0], text_encoding) + elif (event == 'text_event_09'): + event_data = _some_text_event(0x09, E[0], text_encoding) + elif (event == 'text_event_0a'): + event_data = _some_text_event(0x0A, E[0], text_encoding) + elif (event == 'text_event_0b'): + event_data = _some_text_event(0x0B, E[0], text_encoding) + elif (event == 'text_event_0c'): + event_data = _some_text_event(0x0C, E[0], text_encoding) + elif (event == 'text_event_0d'): + event_data = _some_text_event(0x0D, E[0], text_encoding) + elif (event == 'text_event_0e'): + event_data = _some_text_event(0x0E, E[0], text_encoding) + elif (event == 'text_event_0f'): + event_data = _some_text_event(0x0F, E[0], text_encoding) + # End of text meta-events + + elif (event == 'end_track'): + event_data = b"\xFF\x2F\x00" + + elif (event == 'set_tempo'): + #event_data = struct.pack(">BBwa*", 0xFF, 0x51, 3, + # substr( struct.pack('>I', E[0]), 1, 3)) + event_data = b'\xFF\x51\x03'+struct.pack('>I',E[0])[1:] + elif (event == 'smpte_offset'): + # event_data = struct.pack(">BBwBBBBB", 0xFF, 0x54, 5, E[0:5] ) + event_data = struct.pack(">BBBbBBBB", 0xFF,0x54,0x05,E[0],E[1],E[2],E[3],E[4]) + elif (event == 'time_signature'): + # event_data = struct.pack(">BBwBBBB", 0xFF, 0x58, 4, E[0:4] ) + event_data = struct.pack(">BBBbBBB", 0xFF, 0x58, 0x04, E[0],E[1],E[2],E[3]) + elif (event == 'key_signature'): + event_data = struct.pack(">BBBbB", 0xFF, 0x59, 0x02, E[0],E[1]) + elif (event == 'sequencer_specific'): + # event_data = struct.pack(">BBwa*", 0xFF,0x7F, len(E[0]), E[0]) + event_data = _some_text_event(0x7F, E[0], text_encoding) + # End of Meta-events + + # Other Things... + elif (event == 'sysex_f0'): + #event_data = struct.pack(">Bwa*", 0xF0, len(E[0]), E[0]) + #B=bitstring w=BER-compressed-integer a=null-padded-ascii-str + event_data = bytearray(b'\xF0')+_ber_compressed_int(len(E[0]))+bytearray(E[0]) + elif (event == 'sysex_f7'): + #event_data = struct.pack(">Bwa*", 0xF7, len(E[0]), E[0]) + event_data = bytearray(b'\xF7')+_ber_compressed_int(len(E[0]))+bytearray(E[0]) + + elif (event == 'song_position'): + event_data = b"\xF2" + _write_14_bit( E[0] ) + elif (event == 'song_select'): + event_data = struct.pack('>BB', 0xF3, E[0] ) + elif (event == 'tune_request'): + event_data = b"\xF6" + elif (event == 'raw_data'): + _warn("_encode: raw_data event not supported") + # event_data = E[0] + continue + # End of Other Stuff + + else: + # The Big Fallthru + if unknown_callback: + # push(@data, &{ $unknown_callback }( @$event_r )) + pass + else: + _warn("Unknown event: "+str(event)) + # To surpress complaint here, just set + # 'unknown_callback' => sub { return () } + continue + + #print "Event $event encoded part 2\n" + if str(type(event_data)).find("'str'") >= 0: + event_data = bytearray(event_data.encode('Latin1', 'ignore')) + if len(event_data): # how could $event_data be empty + # data.append(struct.pack('>wa*', dtime, event_data)) + # print(' event_data='+str(event_data)) + data.append(_ber_compressed_int(dtime)+event_data) + + return b''.join(data) + +################################################################################### +################################################################################### +################################################################################### +# +# Tegridy MIDI X Module (TMIDI X / tee-midi eks) +# Version 1.0 +# +# Based upon and includes the amazing MIDI.py module v.6.7. by Peter Billam +# pjb.com.au +# +# Project Los Angeles +# Tegridy Code 2021 +# https://github.com/Tegridy-Code/Project-Los-Angeles +# +################################################################################### +################################################################################### +################################################################################### + +import os + +import datetime + +import copy + +from datetime import datetime + +import secrets + +import random + +import pickle + +import csv + +import tqdm + +from itertools import zip_longest +from itertools import groupby +from collections import Counter + +from operator import itemgetter + +import sys + +from abc import ABC, abstractmethod + +from difflib import SequenceMatcher as SM + +import statistics + +import matplotlib.pyplot as plt + +################################################################################### +# +# Original TMIDI Tegridy helper functions +# +################################################################################### + +def Tegridy_TXT_to_INT_Converter(input_TXT_string, line_by_line_INT_string=True, max_INT = 0): + + '''Tegridy TXT to Intergers Converter + + Input: Input TXT string in the TMIDI-TXT format + + Type of output TXT INT string: line-by-line or one long string + + Maximum absolute integer to process. Maximum is inclusive + Default = process all integers. This helps to remove outliers/unwanted ints + + Output: List of pure intergers + String of intergers in the specified format: line-by-line or one long string + Number of processed integers + Number of skipped integers + + Project Los Angeles + Tegridy Code 2021''' + + print('Tegridy TXT to Intergers Converter') + + output_INT_list = [] + + npi = 0 + nsi = 0 + + TXT_List = list(input_TXT_string) + for char in TXT_List: + if max_INT != 0: + if abs(ord(char)) <= max_INT: + output_INT_list.append(ord(char)) + npi += 1 + else: + nsi += 1 + else: + output_INT_list.append(ord(char)) + npi += 1 + + if line_by_line_INT_string: + output_INT_string = '\n'.join([str(elem) for elem in output_INT_list]) + else: + output_INT_string = ' '.join([str(elem) for elem in output_INT_list]) + + print('Converted TXT to INTs:', npi, ' / ', nsi) + + return output_INT_list, output_INT_string, npi, nsi + +################################################################################### + +def Tegridy_INT_to_TXT_Converter(input_INT_list): + + '''Tegridy Intergers to TXT Converter + + Input: List of intergers in TMIDI-TXT-INT format + Output: Decoded TXT string in TMIDI-TXT format + Project Los Angeles + Tegridy Code 2020''' + + output_TXT_string = '' + + for i in input_INT_list: + output_TXT_string += chr(int(i)) + + return output_TXT_string + +################################################################################### + +def Tegridy_INT_String_to_TXT_Converter(input_INT_String, line_by_line_input=True): + + '''Tegridy Intergers String to TXT Converter + + Input: List of intergers in TMIDI-TXT-INT-String format + Output: Decoded TXT string in TMIDI-TXT format + Project Los Angeles + Tegridy Code 2020''' + + print('Tegridy Intergers String to TXT Converter') + + if line_by_line_input: + input_string = input_INT_String.split('\n') + else: + input_string = input_INT_String.split(' ') + + output_TXT_string = '' + + for i in input_string: + try: + output_TXT_string += chr(abs(int(i))) + except: + print('Bad note:', i) + continue + + print('Done!') + + return output_TXT_string + +################################################################################### + +def Tegridy_SONG_to_MIDI_Converter(SONG, + output_signature = 'Tegridy TMIDI Module', + track_name = 'Composition Track', + number_of_ticks_per_quarter = 425, + list_of_MIDI_patches = [0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 0, 0, 0, 0, 0, 0], + output_file_name = 'TMIDI-Composition', + text_encoding='ISO-8859-1', + verbose=True): + + '''Tegridy SONG to MIDI Converter + + Input: Input SONG in TMIDI SONG/MIDI.py Score format + Output MIDI Track 0 name / MIDI Signature + Output MIDI Track 1 name / Composition track name + Number of ticks per quarter for the output MIDI + List of 16 MIDI patch numbers for output MIDI. Def. is MuseNet compatible patches. + Output file name w/o .mid extension. + Optional text encoding if you are working with text_events/lyrics. This is especially useful for Karaoke. Please note that anything but ISO-8859-1 is a non-standard way of encoding text_events according to MIDI specs. + + Output: MIDI File + Detailed MIDI stats + + Project Los Angeles + Tegridy Code 2020''' + + if verbose: + print('Converting to MIDI. Please stand-by...') + + output_header = [number_of_ticks_per_quarter, + [['track_name', 0, bytes(output_signature, text_encoding)]]] + + patch_list = [['patch_change', 0, 0, list_of_MIDI_patches[0]], + ['patch_change', 0, 1, list_of_MIDI_patches[1]], + ['patch_change', 0, 2, list_of_MIDI_patches[2]], + ['patch_change', 0, 3, list_of_MIDI_patches[3]], + ['patch_change', 0, 4, list_of_MIDI_patches[4]], + ['patch_change', 0, 5, list_of_MIDI_patches[5]], + ['patch_change', 0, 6, list_of_MIDI_patches[6]], + ['patch_change', 0, 7, list_of_MIDI_patches[7]], + ['patch_change', 0, 8, list_of_MIDI_patches[8]], + ['patch_change', 0, 9, list_of_MIDI_patches[9]], + ['patch_change', 0, 10, list_of_MIDI_patches[10]], + ['patch_change', 0, 11, list_of_MIDI_patches[11]], + ['patch_change', 0, 12, list_of_MIDI_patches[12]], + ['patch_change', 0, 13, list_of_MIDI_patches[13]], + ['patch_change', 0, 14, list_of_MIDI_patches[14]], + ['patch_change', 0, 15, list_of_MIDI_patches[15]], + ['track_name', 0, bytes(track_name, text_encoding)]] + + output = output_header + [patch_list + SONG] + + midi_data = score2midi(output, text_encoding) + detailed_MIDI_stats = score2stats(output) + + with open(output_file_name + '.mid', 'wb') as midi_file: + midi_file.write(midi_data) + midi_file.close() + + if verbose: + print('Done! Enjoy! :)') + + return detailed_MIDI_stats + +################################################################################### + +def Tegridy_ms_SONG_to_MIDI_Converter(ms_SONG, + output_signature = 'Tegridy TMIDI Module', + track_name = 'Composition Track', + list_of_MIDI_patches = [0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 0, 0, 0, 0, 0, 0], + output_file_name = 'TMIDI-Composition', + text_encoding='ISO-8859-1', + timings_multiplier=1, + verbose=True + ): + + '''Tegridy milisecond SONG to MIDI Converter + + Input: Input ms SONG in TMIDI ms SONG/MIDI.py ms Score format + Output MIDI Track 0 name / MIDI Signature + Output MIDI Track 1 name / Composition track name + List of 16 MIDI patch numbers for output MIDI. Def. is MuseNet compatible patches. + Output file name w/o .mid extension. + Optional text encoding if you are working with text_events/lyrics. This is especially useful for Karaoke. Please note that anything but ISO-8859-1 is a non-standard way of encoding text_events according to MIDI specs. + Optional timings multiplier + Optional verbose output + + Output: MIDI File + Detailed MIDI stats + + Project Los Angeles + Tegridy Code 2024''' + + if verbose: + print('Converting to MIDI. Please stand-by...') + + output_header = [1000, + [['set_tempo', 0, 1000000], + ['time_signature', 0, 4, 2, 24, 8], + ['track_name', 0, bytes(output_signature, text_encoding)]]] + + patch_list = [['patch_change', 0, 0, list_of_MIDI_patches[0]], + ['patch_change', 0, 1, list_of_MIDI_patches[1]], + ['patch_change', 0, 2, list_of_MIDI_patches[2]], + ['patch_change', 0, 3, list_of_MIDI_patches[3]], + ['patch_change', 0, 4, list_of_MIDI_patches[4]], + ['patch_change', 0, 5, list_of_MIDI_patches[5]], + ['patch_change', 0, 6, list_of_MIDI_patches[6]], + ['patch_change', 0, 7, list_of_MIDI_patches[7]], + ['patch_change', 0, 8, list_of_MIDI_patches[8]], + ['patch_change', 0, 9, list_of_MIDI_patches[9]], + ['patch_change', 0, 10, list_of_MIDI_patches[10]], + ['patch_change', 0, 11, list_of_MIDI_patches[11]], + ['patch_change', 0, 12, list_of_MIDI_patches[12]], + ['patch_change', 0, 13, list_of_MIDI_patches[13]], + ['patch_change', 0, 14, list_of_MIDI_patches[14]], + ['patch_change', 0, 15, list_of_MIDI_patches[15]], + ['track_name', 0, bytes(track_name, text_encoding)]] + + SONG = copy.deepcopy(ms_SONG) + + if timings_multiplier != 1: + for S in SONG: + S[1] = S[1] * timings_multiplier + if S[0] == 'note': + S[2] = S[2] * timings_multiplier + + output = output_header + [patch_list + SONG] + + midi_data = score2midi(output, text_encoding) + detailed_MIDI_stats = score2stats(output) + + with open(output_file_name + '.mid', 'wb') as midi_file: + midi_file.write(midi_data) + midi_file.close() + + if verbose: + print('Done! Enjoy! :)') + + return detailed_MIDI_stats + +################################################################################### + +def hsv_to_rgb(h, s, v): + if s == 0.0: + return v, v, v + i = int(h*6.0) + f = (h*6.0) - i + p = v*(1.0 - s) + q = v*(1.0 - s*f) + t = v*(1.0 - s*(1.0-f)) + i = i%6 + return [(v, t, p), (q, v, p), (p, v, t), (p, q, v), (t, p, v), (v, p, q)][i] + +def generate_colors(n): + return [hsv_to_rgb(i/n, 1, 1) for i in range(n)] + +def add_arrays(a, b): + return [sum(pair) for pair in zip(a, b)] + +#------------------------------------------------------------------------------- + +def plot_ms_SONG(ms_song, + preview_length_in_notes=0, + block_lines_times_list = None, + plot_title='ms Song', + max_num_colors=129, + drums_color_num=128, + plot_size=(11,4), + note_height = 0.75, + show_grid_lines=False, + return_plt = False, + timings_multiplier=1 + ): + + '''Tegridy ms SONG plotter/vizualizer''' + + notes = [s for s in ms_song if s[0] == 'note'] + + if (len(max(notes, key=len)) != 7) and (len(min(notes, key=len)) != 7): + print('The song notes do not have patches information') + print('Ploease add patches to the notes in the song') + + else: + + start_times = [(s[1] * timings_multiplier) / 1000 for s in notes] + durations = [(s[2] * timings_multiplier) / 1000 for s in notes] + pitches = [s[4] for s in notes] + patches = [s[6] for s in notes] + + colors = generate_colors(max_num_colors) + colors[drums_color_num] = (1, 1, 1) + + pbl = (notes[preview_length_in_notes][1] * timings_multiplier) / 1000 + + fig, ax = plt.subplots(figsize=plot_size) + #fig, ax = plt.subplots() + + # Create a rectangle for each note with color based on patch number + for start, duration, pitch, patch in zip(start_times, durations, pitches, patches): + rect = plt.Rectangle((start, pitch), duration, note_height, facecolor=colors[patch]) + ax.add_patch(rect) + + # Set the limits of the plot + ax.set_xlim([min(start_times), max(add_arrays(start_times, durations))]) + ax.set_ylim([min(pitches)-1, max(pitches)+1]) + + # Set the background color to black + ax.set_facecolor('black') + fig.patch.set_facecolor('white') + + if preview_length_in_notes > 0: + ax.axvline(x=pbl, c='white') + + if block_lines_times_list: + for bl in block_lines_times_list: + ax.axvline(x=bl, c='white') + + if show_grid_lines: + ax.grid(color='white') + + plt.xlabel('Time (s)', c='black') + plt.ylabel('MIDI Pitch', c='black') + + plt.title(plot_title) + + if return_plt: + return fig + + plt.show() + +################################################################################### + +def Tegridy_SONG_to_Full_MIDI_Converter(SONG, + output_signature = 'Tegridy TMIDI Module', + track_name = 'Composition Track', + number_of_ticks_per_quarter = 1000, + output_file_name = 'TMIDI-Composition', + text_encoding='ISO-8859-1', + verbose=True): + + '''Tegridy SONG to Full MIDI Converter + + Input: Input SONG in Full TMIDI SONG/MIDI.py Score format + Output MIDI Track 0 name / MIDI Signature + Output MIDI Track 1 name / Composition track name + Number of ticks per quarter for the output MIDI + Output file name w/o .mid extension. + Optional text encoding if you are working with text_events/lyrics. This is especially useful for Karaoke. Please note that anything but ISO-8859-1 is a non-standard way of encoding text_events according to MIDI specs. + + Output: MIDI File + Detailed MIDI stats + + Project Los Angeles + Tegridy Code 2023''' + + if verbose: + print('Converting to MIDI. Please stand-by...') + + output_header = [number_of_ticks_per_quarter, + [['set_tempo', 0, 1000000], + ['track_name', 0, bytes(output_signature, text_encoding)]]] + + song_track = [['track_name', 0, bytes(track_name, text_encoding)]] + + output = output_header + [song_track + SONG] + + midi_data = score2midi(output, text_encoding) + detailed_MIDI_stats = score2stats(output) + + with open(output_file_name + '.mid', 'wb') as midi_file: + midi_file.write(midi_data) + midi_file.close() + + if verbose: + print('Done! Enjoy! :)') + + return detailed_MIDI_stats + +################################################################################### + +def Tegridy_File_Time_Stamp(input_file_name='File_Created_on_', ext = ''): + + '''Tegridy File Time Stamp + + Input: Full path and file name without extention + File extension + + Output: File name string with time-stamp and extension (time-stamped file name) + + Project Los Angeles + Tegridy Code 2021''' + + print('Time-stamping output file...') + + now = '' + now_n = str(datetime.now()) + now_n = now_n.replace(' ', '_') + now_n = now_n.replace(':', '_') + now = now_n.replace('.', '_') + + fname = input_file_name + str(now) + ext + + return(fname) + +################################################################################### + +def Tegridy_Any_Pickle_File_Writer(Data, input_file_name='TMIDI_Pickle_File'): + + '''Tegridy Pickle File Writer + + Input: Data to write (I.e. a list) + Full path and file name without extention + + Output: Named Pickle file + + Project Los Angeles + Tegridy Code 2021''' + + print('Tegridy Pickle File Writer') + + full_path_to_output_dataset_to = input_file_name + '.pickle' + + if os.path.exists(full_path_to_output_dataset_to): + os.remove(full_path_to_output_dataset_to) + print('Removing old Dataset...') + else: + print("Creating new Dataset file...") + + with open(full_path_to_output_dataset_to, 'wb') as filehandle: + # store the data as binary data stream + pickle.dump(Data, filehandle, protocol=pickle.HIGHEST_PROTOCOL) + + print('Dataset was saved as:', full_path_to_output_dataset_to) + print('Task complete. Enjoy! :)') + +################################################################################### + +def Tegridy_Any_Pickle_File_Reader(input_file_name='TMIDI_Pickle_File', ext='.pickle', verbose=True): + + '''Tegridy Pickle File Loader + + Input: Full path and file name without extention + File extension if different from default .pickle + + Output: Standard Python 3 unpickled data object + + Project Los Angeles + Tegridy Code 2021''' + + if verbose: + print('Tegridy Pickle File Loader') + print('Loading the pickle file. Please wait...') + + with open(input_file_name + ext, 'rb') as pickle_file: + content = pickle.load(pickle_file) + + if verbose: + print('Done!') + + return content + +################################################################################### + +# TMIDI X Code is below + +################################################################################### + +def Optimus_MIDI_TXT_Processor(MIDI_file, + line_by_line_output=True, + chordify_TXT=False, + dataset_MIDI_events_time_denominator=1, + output_velocity=True, + output_MIDI_channels = False, + MIDI_channel=0, + MIDI_patch=[0, 1], + char_offset = 30000, + transpose_by = 0, + flip=False, + melody_conditioned_encoding=False, + melody_pitch_baseline = 0, + number_of_notes_to_sample = -1, + sampling_offset_from_start = 0, + karaoke=False, + karaoke_language_encoding='utf-8', + song_name='Song', + perfect_timings=False, + musenet_encoding=False, + transform=0, + zero_token=False, + reset_timings=False): + + '''Project Los Angeles + Tegridy Code 2021''' + +########### + + debug = False + + ev = 0 + + chords_list_final = [] + chords_list = [] + events_matrix = [] + melody = [] + melody1 = [] + + itrack = 1 + + min_note = 0 + max_note = 0 + ev = 0 + patch = 0 + + score = [] + rec_event = [] + + txt = '' + txtc = '' + chords = [] + melody_chords = [] + + karaoke_events_matrix = [] + karaokez = [] + + sample = 0 + start_sample = 0 + + bass_melody = [] + + INTS = [] + bints = 0 + +########### + + def list_average(num): + sum_num = 0 + for t in num: + sum_num = sum_num + t + + avg = sum_num / len(num) + return avg + +########### + + #print('Loading MIDI file...') + midi_file = open(MIDI_file, 'rb') + if debug: print('Processing File:', file_address) + + try: + opus = midi2opus(midi_file.read()) + + except: + print('Problematic MIDI. Skipping...') + print('File name:', MIDI_file) + midi_file.close() + return txt, melody, chords + + midi_file.close() + + score1 = to_millisecs(opus) + score2 = opus2score(score1) + + # score2 = opus2score(opus) # TODO Improve score timings when it will be possible. + + if MIDI_channel == 16: # Process all MIDI channels + score = score2 + + if MIDI_channel >= 0 and MIDI_channel <= 15: # Process only a selected single MIDI channel + score = grep(score2, [MIDI_channel]) + + if MIDI_channel == -1: # Process all channels except drums (except channel 9) + score = grep(score2, [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15]) + + #print('Reading all MIDI events from the MIDI file...') + while itrack < len(score): + for event in score[itrack]: + + if perfect_timings: + if event[0] == 'note': + event[1] = round(event[1], -1) + event[2] = round(event[2], -1) + + if event[0] == 'text_event' or event[0] == 'lyric' or event[0] == 'note': + if perfect_timings: + event[1] = round(event[1], -1) + karaokez.append(event) + + if event[0] == 'text_event' or event[0] == 'lyric': + if perfect_timings: + event[1] = round(event[1], -1) + try: + event[2] = str(event[2].decode(karaoke_language_encoding, 'replace')).replace('/', '').replace(' ', '').replace('\\', '') + except: + event[2] = str(event[2]).replace('/', '').replace(' ', '').replace('\\', '') + continue + karaoke_events_matrix.append(event) + + if event[0] == 'patch_change': + patch = event[3] + + if event[0] == 'note' and patch in MIDI_patch: + if len(event) == 6: # Checking for bad notes... + eve = copy.deepcopy(event) + + eve[1] = int(event[1] / dataset_MIDI_events_time_denominator) + eve[2] = int(event[2] / dataset_MIDI_events_time_denominator) + + eve[4] = int(event[4] + transpose_by) + + if flip == True: + eve[4] = int(127 - (event[4] + transpose_by)) + + if number_of_notes_to_sample > -1: + if sample <= number_of_notes_to_sample: + if start_sample >= sampling_offset_from_start: + events_matrix.append(eve) + sample += 1 + ev += 1 + else: + start_sample += 1 + + else: + events_matrix.append(eve) + ev += 1 + start_sample += 1 + + itrack +=1 # Going to next track... + + #print('Doing some heavy pythonic sorting...Please stand by...') + + fn = os.path.basename(MIDI_file) + song_name = song_name.replace(' ', '_').replace('=', '_').replace('\'', '-') + if song_name == 'Song': + sng_name = fn.split('.')[0].replace(' ', '_').replace('=', '_').replace('\'', '-') + song_name = sng_name + + # Zero token + if zero_token: + txt += chr(char_offset) + chr(char_offset) + if output_MIDI_channels: + txt += chr(char_offset) + if output_velocity: + txt += chr(char_offset) + chr(char_offset) + else: + txt += chr(char_offset) + + txtc += chr(char_offset) + chr(char_offset) + if output_MIDI_channels: + txtc += chr(char_offset) + if output_velocity: + txtc += chr(char_offset) + chr(char_offset) + else: + txtc += chr(char_offset) + + txt += '=' + song_name + '_with_' + str(len(events_matrix)-1) + '_notes' + txtc += '=' + song_name + '_with_' + str(len(events_matrix)-1) + '_notes' + + else: + # Song stamp + txt += 'SONG=' + song_name + '_with_' + str(len(events_matrix)-1) + '_notes' + txtc += 'SONG=' + song_name + '_with_' + str(len(events_matrix)-1) + '_notes' + + if line_by_line_output: + txt += chr(10) + txtc += chr(10) + else: + txt += chr(32) + txtc += chr(32) + + #print('Sorting input by start time...') + events_matrix.sort(key=lambda x: x[1]) # Sorting input by start time + + #print('Timings converter') + if reset_timings: + ev_matrix = Tegridy_Timings_Converter(events_matrix)[0] + else: + ev_matrix = events_matrix + + chords.extend(ev_matrix) + #print(chords) + + #print('Extracting melody...') + melody_list = [] + + #print('Grouping by start time. This will take a while...') + values = set(map(lambda x:x[1], ev_matrix)) # Non-multithreaded function version just in case + + groups = [[y for y in ev_matrix if y[1]==x and len(y) == 6] for x in values] # Grouping notes into chords while discarting bad notes... + + #print('Sorting events...') + for items in groups: + + items.sort(reverse=True, key=lambda x: x[4]) # Sorting events by pitch + + if melody_conditioned_encoding: items[0][3] = 0 # Melody should always bear MIDI Channel 0 for code to work + + melody_list.append(items[0]) # Creating final melody list + melody_chords.append(items) # Creating final chords list + bass_melody.append(items[-1]) # Creating final bass melody list + + # [WIP] Melody-conditioned chords list + if melody_conditioned_encoding == True: + if not karaoke: + + previous_event = copy.deepcopy(melody_chords[0][0]) + + for ev in melody_chords: + hp = True + ev.sort(reverse=False, key=lambda x: x[4]) # Sorting chord events by pitch + for event in ev: + + # Computing events details + start_time = int(abs(event[1] - previous_event[1])) + + duration = int(previous_event[2]) + + if hp == True: + if int(previous_event[4]) >= melody_pitch_baseline: + channel = int(0) + hp = False + else: + channel = int(previous_event[3]+1) + hp = False + else: + channel = int(previous_event[3]+1) + hp = False + + pitch = int(previous_event[4]) + + velocity = int(previous_event[5]) + + # Writing INTergerS... + try: + INTS.append([(start_time)+char_offset, (duration)+char_offset, channel+char_offset, pitch+char_offset, velocity+char_offset]) + except: + bints += 1 + + # Converting to TXT if possible... + try: + txtc += str(chr(start_time + char_offset)) + txtc += str(chr(duration + char_offset)) + txtc += str(chr(pitch + char_offset)) + if output_velocity: + txtc += str(chr(velocity + char_offset)) + if output_MIDI_channels: + txtc += str(chr(channel + char_offset)) + + if line_by_line_output: + + + txtc += chr(10) + else: + + txtc += chr(32) + + previous_event = copy.deepcopy(event) + + except: + # print('Problematic MIDI event! Skipping...') + continue + + if not line_by_line_output: + txtc += chr(10) + + txt = txtc + chords = melody_chords + + # Default stuff (not melody-conditioned/not-karaoke) + else: + if not karaoke: + melody_chords.sort(reverse=False, key=lambda x: x[0][1]) + mel_chords = [] + for mc in melody_chords: + mel_chords.extend(mc) + + if transform != 0: + chords = Tegridy_Transform(mel_chords, transform) + else: + chords = mel_chords + + # TXT Stuff + previous_event = copy.deepcopy(chords[0]) + for event in chords: + + # Computing events details + start_time = int(abs(event[1] - previous_event[1])) + + duration = int(previous_event[2]) + + channel = int(previous_event[3]) + + pitch = int(previous_event[4] + transpose_by) + if flip == True: + pitch = 127 - int(previous_event[4] + transpose_by) + + velocity = int(previous_event[5]) + + # Writing INTergerS... + try: + INTS.append([(start_time)+char_offset, (duration)+char_offset, channel+char_offset, pitch+char_offset, velocity+char_offset]) + except: + bints += 1 + + # Converting to TXT if possible... + try: + txt += str(chr(start_time + char_offset)) + txt += str(chr(duration + char_offset)) + txt += str(chr(pitch + char_offset)) + if output_velocity: + txt += str(chr(velocity + char_offset)) + if output_MIDI_channels: + txt += str(chr(channel + char_offset)) + + + if chordify_TXT == True and int(event[1] - previous_event[1]) == 0: + txt += '' + else: + if line_by_line_output: + txt += chr(10) + else: + txt += chr(32) + + previous_event = copy.deepcopy(event) + + except: + # print('Problematic MIDI event. Skipping...') + continue + + if not line_by_line_output: + txt += chr(10) + + # Karaoke stuff + if karaoke: + + melody_chords.sort(reverse=False, key=lambda x: x[0][1]) + mel_chords = [] + for mc in melody_chords: + mel_chords.extend(mc) + + if transform != 0: + chords = Tegridy_Transform(mel_chords, transform) + else: + chords = mel_chords + + previous_event = copy.deepcopy(chords[0]) + for event in chords: + + # Computing events details + start_time = int(abs(event[1] - previous_event[1])) + + duration = int(previous_event[2]) + + channel = int(previous_event[3]) + + pitch = int(previous_event[4] + transpose_by) + + velocity = int(previous_event[5]) + + # Converting to TXT + txt += str(chr(start_time + char_offset)) + txt += str(chr(duration + char_offset)) + txt += str(chr(pitch + char_offset)) + + txt += str(chr(velocity + char_offset)) + txt += str(chr(channel + char_offset)) + + if start_time > 0: + for k in karaoke_events_matrix: + if event[1] == k[1]: + txt += str('=') + txt += str(k[2]) + break + + if line_by_line_output: + txt += chr(10) + else: + txt += chr(32) + + previous_event = copy.deepcopy(event) + + if not line_by_line_output: + txt += chr(10) + + # Final processing code... + # ======================================================================= + + # Helper aux/backup function for Karaoke + karaokez.sort(reverse=False, key=lambda x: x[1]) + + # MuseNet sorting + if musenet_encoding and not melody_conditioned_encoding and not karaoke: + chords.sort(key=lambda x: (x[1], x[3])) + + # Final melody sort + melody_list.sort() + + # auxs for future use + aux1 = [None] + aux2 = [None] + + return txt, melody_list, chords, bass_melody, karaokez, INTS, aux1, aux2 # aux1 and aux2 are not used atm + +################################################################################### + +def Optimus_TXT_to_Notes_Converter(Optimus_TXT_String, + line_by_line_dataset = True, + has_velocities = True, + has_MIDI_channels = True, + dataset_MIDI_events_time_denominator = 1, + char_encoding_offset = 30000, + save_only_first_composition = True, + simulate_velocity=True, + karaoke=False, + zero_token=False): + + '''Project Los Angeles + Tegridy Code 2020''' + + print('Tegridy Optimus TXT to Notes Converter') + print('Converting TXT to Notes list...Please wait...') + + song_name = '' + + if line_by_line_dataset: + input_string = Optimus_TXT_String.split('\n') + else: + input_string = Optimus_TXT_String.split(' ') + + if line_by_line_dataset: + name_string = Optimus_TXT_String.split('\n')[0].split('=') + else: + name_string = Optimus_TXT_String.split(' ')[0].split('=') + + # Zero token + zt = '' + + zt += chr(char_encoding_offset) + chr(char_encoding_offset) + + if has_MIDI_channels: + zt += chr(char_encoding_offset) + + if has_velocities: + zt += chr(char_encoding_offset) + chr(char_encoding_offset) + + else: + zt += chr(char_encoding_offset) + + if zero_token: + if name_string[0] == zt: + song_name = name_string[1] + + else: + if name_string[0] == 'SONG': + song_name = name_string[1] + + output_list = [] + st = 0 + + for i in range(2, len(input_string)-1): + + if save_only_first_composition: + if zero_token: + if input_string[i].split('=')[0] == zt: + + song_name = name_string[1] + break + + else: + if input_string[i].split('=')[0] == 'SONG': + + song_name = name_string[1] + break + try: + istring = input_string[i] + + if has_MIDI_channels == False: + step = 4 + + if has_MIDI_channels == True: + step = 5 + + if has_velocities == False: + step -= 1 + + st += int(ord(istring[0]) - char_encoding_offset) * dataset_MIDI_events_time_denominator + + if not karaoke: + for s in range(0, len(istring), step): + if has_MIDI_channels==True: + if step > 3 and len(istring) > 2: + out = [] + out.append('note') + + out.append(st) # Start time + + out.append(int(ord(istring[s+1]) - char_encoding_offset) * dataset_MIDI_events_time_denominator) # Duration + + if has_velocities: + out.append(int(ord(istring[s+4]) - char_encoding_offset)) # Channel + else: + out.append(int(ord(istring[s+3]) - char_encoding_offset)) # Channel + + out.append(int(ord(istring[s+2]) - char_encoding_offset)) # Pitch + + if simulate_velocity: + if s == 0: + sim_vel = int(ord(istring[s+2]) - char_encoding_offset) + out.append(sim_vel) # Simulated Velocity (= highest note's pitch) + else: + out.append(int(ord(istring[s+3]) - char_encoding_offset)) # Velocity + + if has_MIDI_channels==False: + if step > 3 and len(istring) > 2: + out = [] + out.append('note') + + out.append(st) # Start time + out.append(int(ord(istring[s+1]) - char_encoding_offset) * dataset_MIDI_events_time_denominator) # Duration + out.append(0) # Channel + out.append(int(ord(istring[s+2]) - char_encoding_offset)) # Pitch + + if simulate_velocity: + if s == 0: + sim_vel = int(ord(istring[s+2]) - char_encoding_offset) + out.append(sim_vel) # Simulated Velocity (= highest note's pitch) + else: + out.append(int(ord(istring[s+3]) - char_encoding_offset)) # Velocity + + if step == 3 and len(istring) > 2: + out = [] + out.append('note') + + out.append(st) # Start time + out.append(int(ord(istring[s+1]) - char_encoding_offset) * dataset_MIDI_events_time_denominator) # Duration + out.append(0) # Channel + out.append(int(ord(istring[s+2]) - char_encoding_offset)) # Pitch + + out.append(int(ord(istring[s+2]) - char_encoding_offset)) # Velocity = Pitch + + output_list.append(out) + + if karaoke: + try: + out = [] + out.append('note') + + out.append(st) # Start time + out.append(int(ord(istring[1]) - char_encoding_offset) * dataset_MIDI_events_time_denominator) # Duration + out.append(int(ord(istring[4]) - char_encoding_offset)) # Channel + out.append(int(ord(istring[2]) - char_encoding_offset)) # Pitch + + if simulate_velocity: + if s == 0: + sim_vel = int(ord(istring[2]) - char_encoding_offset) + out.append(sim_vel) # Simulated Velocity (= highest note's pitch) + else: + out.append(int(ord(istring[3]) - char_encoding_offset)) # Velocity + output_list.append(out) + out = [] + if istring.split('=')[1] != '': + out.append('lyric') + out.append(st) + out.append(istring.split('=')[1]) + output_list.append(out) + except: + continue + + + except: + print('Bad note string:', istring) + continue + + # Simple error control just in case + S = [] + for x in output_list: + if len(x) == 6 or len(x) == 3: + S.append(x) + + output_list.clear() + output_list = copy.deepcopy(S) + + + print('Task complete! Enjoy! :)') + + return output_list, song_name + +################################################################################### + +def Optimus_Data2TXT_Converter(data, + dataset_time_denominator=1, + transpose_by = 0, + char_offset = 33, + line_by_line_output = True, + output_velocity = False, + output_MIDI_channels = False): + + + '''Input: data as a flat chords list of flat chords lists + + Output: TXT string + INTs + + Project Los Angeles + Tegridy Code 2021''' + + txt = '' + TXT = '' + + quit = False + counter = 0 + + INTs = [] + INTs_f = [] + + for d in tqdm.tqdm(sorted(data)): + + if quit == True: + break + + txt = 'SONG=' + str(counter) + counter += 1 + + if line_by_line_output: + txt += chr(10) + else: + txt += chr(32) + + INTs = [] + + # TXT Stuff + previous_event = copy.deepcopy(d[0]) + for event in sorted(d): + + # Computing events details + start_time = int(abs(event[1] - previous_event[1]) / dataset_time_denominator) + + duration = int(previous_event[2] / dataset_time_denominator) + + channel = int(previous_event[3]) + + pitch = int(previous_event[4] + transpose_by) + + velocity = int(previous_event[5]) + + INTs.append([start_time, duration, pitch]) + + # Converting to TXT if possible... + try: + txt += str(chr(start_time + char_offset)) + txt += str(chr(duration + char_offset)) + txt += str(chr(pitch + char_offset)) + if output_velocity: + txt += str(chr(velocity + char_offset)) + if output_MIDI_channels: + txt += str(chr(channel + char_offset)) + + if line_by_line_output: + txt += chr(10) + else: + txt += chr(32) + + previous_event = copy.deepcopy(event) + except KeyboardInterrupt: + quit = True + break + except: + print('Problematic MIDI data. Skipping...') + continue + + if not line_by_line_output: + txt += chr(10) + + TXT += txt + INTs_f.extend(INTs) + + return TXT, INTs_f + +################################################################################### + +def Optimus_Squash(chords_list, simulate_velocity=True, mono_compression=False): + + '''Input: Flat chords list + Simulate velocity or not + Mono-compression enabled or disabled + + Default is almost lossless 25% compression, otherwise, lossy 50% compression (mono-compression) + + Output: Squashed chords list + Resulting compression level + + Please note that if drums are passed through as is + + Project Los Angeles + Tegridy Code 2021''' + + output = [] + ptime = 0 + vel = 0 + boost = 15 + stptc = [] + ocount = 0 + rcount = 0 + + for c in chords_list: + + cc = copy.deepcopy(c) + ocount += 1 + + if [cc[1], cc[3], (cc[4] % 12) + 60] not in stptc: + stptc.append([cc[1], cc[3], (cc[4] % 12) + 60]) + + if cc[3] != 9: + cc[4] = (c[4] % 12) + 60 + + if simulate_velocity and c[1] != ptime: + vel = c[4] + boost + + if cc[3] != 9: + cc[5] = vel + + if mono_compression: + if c[1] != ptime: + output.append(cc) + rcount += 1 + else: + output.append(cc) + rcount += 1 + + ptime = c[1] + + output.sort(key=lambda x: (x[1], x[4])) + + comp_level = 100 - int((rcount * 100) / ocount) + + return output, comp_level + +################################################################################### + +def Optimus_Signature(chords_list, calculate_full_signature=False): + + '''Optimus Signature + + ---In the name of the search for a perfect score slice signature--- + + Input: Flat chords list to evaluate + + Output: Full Optimus Signature as a list + Best/recommended Optimus Signature as a list + + Project Los Angeles + Tegridy Code 2021''' + + # Pitches + + ## StDev + if calculate_full_signature: + psd = statistics.stdev([int(y[4]) for y in chords_list]) + else: + psd = 0 + + ## Median + pmh = statistics.median_high([int(y[4]) for y in chords_list]) + pm = statistics.median([int(y[4]) for y in chords_list]) + pml = statistics.median_low([int(y[4]) for y in chords_list]) + + ## Mean + if calculate_full_signature: + phm = statistics.harmonic_mean([int(y[4]) for y in chords_list]) + else: + phm = 0 + + # Durations + dur = statistics.median([int(y[2]) for y in chords_list]) + + # Velocities + + vel = statistics.median([int(y[5]) for y in chords_list]) + + # Beats + mtds = statistics.median([int(abs(chords_list[i-1][1]-chords_list[i][1])) for i in range(1, len(chords_list))]) + if calculate_full_signature: + hmtds = statistics.harmonic_mean([int(abs(chords_list[i-1][1]-chords_list[i][1])) for i in range(1, len(chords_list))]) + else: + hmtds = 0 + + # Final Optimus signatures + full_Optimus_signature = [round(psd), round(pmh), round(pm), round(pml), round(phm), round(dur), round(vel), round(mtds), round(hmtds)] + ######################## PStDev PMedianH PMedian PMedianL PHarmoMe Duration Velocity Beat HarmoBeat + + best_Optimus_signature = [round(pmh), round(pm), round(pml), round(dur, -1), round(vel, -1), round(mtds, -1)] + ######################## PMedianH PMedian PMedianL Duration Velocity Beat + + # Return... + return full_Optimus_signature, best_Optimus_signature + + +################################################################################### +# +# TMIDI 2.0 Helper functions +# +################################################################################### + +def Tegridy_FastSearch(needle, haystack, randomize = False): + + ''' + + Input: Needle iterable + Haystack iterable + Randomize search range (this prevents determinism) + + Output: Start index of the needle iterable in a haystack iterable + If nothing found, -1 is returned + + Project Los Angeles + Tegridy Code 2021''' + + need = copy.deepcopy(needle) + + try: + if randomize: + idx = haystack.index(need, secrets.randbelow(len(haystack)-len(need))) + else: + idx = haystack.index(need) + + except KeyboardInterrupt: + return -1 + + except: + return -1 + + return idx + +################################################################################### + +def Tegridy_Chord_Match(chord1, chord2, match_type=2): + + '''Tegridy Chord Match + + Input: Two chords to evaluate + Match type: 2 = duration, channel, pitch, velocity + 3 = channel, pitch, velocity + 4 = pitch, velocity + 5 = velocity + + Output: Match rating (0-100) + NOTE: Match rating == -1 means identical source chords + NOTE: Match rating == 100 means mutual shortest chord + + Project Los Angeles + Tegridy Code 2021''' + + match_rating = 0 + + if chord1 == []: + return 0 + if chord2 == []: + return 0 + + if chord1 == chord2: + return -1 + + else: + zipped_pairs = list(zip(chord1, chord2)) + zipped_diff = abs(len(chord1) - len(chord2)) + + short_match = [False] + for pair in zipped_pairs: + cho1 = ' '.join([str(y) for y in pair[0][match_type:]]) + cho2 = ' '.join([str(y) for y in pair[1][match_type:]]) + if cho1 == cho2: + short_match.append(True) + else: + short_match.append(False) + + if True in short_match: + return 100 + + pairs_ratings = [] + + for pair in zipped_pairs: + cho1 = ' '.join([str(y) for y in pair[0][match_type:]]) + cho2 = ' '.join([str(y) for y in pair[1][match_type:]]) + pairs_ratings.append(SM(None, cho1, cho2).ratio()) + + match_rating = sum(pairs_ratings) / len(pairs_ratings) * 100 + + return match_rating + +################################################################################### + +def Tegridy_Last_Chord_Finder(chords_list): + + '''Tegridy Last Chord Finder + + Input: Flat chords list + + Output: Last detected chord of the chords list + Last chord start index in the original chords list + First chord end index in the original chords list + + Project Los Angeles + Tegridy Code 2021''' + + chords = [] + cho = [] + + ptime = 0 + + i = 0 + + pc_idx = 0 + fc_idx = 0 + + chords_list.sort(reverse=False, key=lambda x: x[1]) + + for cc in chords_list: + + if cc[1] == ptime: + + cho.append(cc) + + ptime = cc[1] + + else: + if pc_idx == 0: + fc_idx = chords_list.index(cc) + pc_idx = chords_list.index(cc) + + chords.append(cho) + + cho = [] + + cho.append(cc) + + ptime = cc[1] + + i += 1 + + if cho != []: + chords.append(cho) + i += 1 + + return chords_list[pc_idx:], pc_idx, fc_idx + +################################################################################### + +def Tegridy_Chords_Generator(chords_list, shuffle_pairs = True, remove_single_notes=False): + + '''Tegridy Score Chords Pairs Generator + + Input: Flat chords list + Shuffle pairs (recommended) + + Output: List of chords + + Average time(ms) per chord + Average time(ms) per pitch + Average chords delta time + + Average duration + Average channel + Average pitch + Average velocity + + Project Los Angeles + Tegridy Code 2021''' + + chords = [] + cho = [] + + i = 0 + + # Sort by start time + chords_list.sort(reverse=False, key=lambda x: x[1]) + + # Main loop + pcho = chords_list[0] + for cc in chords_list: + if cc[1] == pcho[1]: + + cho.append(cc) + pcho = copy.deepcopy(cc) + + else: + if not remove_single_notes: + chords.append(cho) + cho = [] + cho.append(cc) + pcho = copy.deepcopy(cc) + + i += 1 + else: + if len(cho) > 1: + chords.append(cho) + cho = [] + cho.append(cc) + pcho = copy.deepcopy(cc) + + i += 1 + + # Averages + t0 = chords[0][0][1] + t1 = chords[-1][-1][1] + tdel = abs(t1 - t0) + avg_ms_per_chord = int(tdel / i) + avg_ms_per_pitch = int(tdel / len(chords_list)) + + # Delta time + tds = [int(abs(chords_list[i-1][1]-chords_list[i][1]) / 1) for i in range(1, len(chords_list))] + if len(tds) != 0: avg_delta_time = int(sum(tds) / len(tds)) + + # Chords list attributes + p = int(sum([int(y[4]) for y in chords_list]) / len(chords_list)) + d = int(sum([int(y[2]) for y in chords_list]) / len(chords_list)) + c = int(sum([int(y[3]) for y in chords_list]) / len(chords_list)) + v = int(sum([int(y[5]) for y in chords_list]) / len(chords_list)) + + # Final shuffle + if shuffle_pairs: + random.shuffle(chords) + + return chords, [avg_ms_per_chord, avg_ms_per_pitch, avg_delta_time], [d, c, p, v] + +################################################################################### + +def Tegridy_Chords_List_Music_Features(chords_list, st_dur_div = 1, pitch_div = 1, vel_div = 1): + + '''Tegridy Chords List Music Features + + Input: Flat chords list + + Output: A list of the extracted chords list's music features + + Project Los Angeles + Tegridy Code 2021''' + + chords_list1 = [x for x in chords_list if x] + chords_list1.sort(reverse=False, key=lambda x: x[1]) + + # Features extraction code + + melody_list = [] + bass_melody = [] + melody_chords = [] + mel_avg_tds = [] + mel_chrd_avg_tds = [] + bass_melody_avg_tds = [] + + #print('Grouping by start time. This will take a while...') + values = set(map(lambda x:x[1], chords_list1)) # Non-multithreaded function version just in case + + groups = [[y for y in chords_list1 if y[1]==x and len(y) == 6] for x in values] # Grouping notes into chords while discarting bad notes... + + #print('Sorting events...') + for items in groups: + items.sort(reverse=True, key=lambda x: x[4]) # Sorting events by pitch + melody_list.append(items[0]) # Creating final melody list + melody_chords.append(items) # Creating final chords list + bass_melody.append(items[-1]) # Creating final bass melody list + + #print('Final sorting by start time...') + melody_list.sort(reverse=False, key=lambda x: x[1]) # Sorting events by start time + melody_chords.sort(reverse=False, key=lambda x: x[0][1]) # Sorting events by start time + bass_melody.sort(reverse=False, key=lambda x: x[1]) # Sorting events by start time + + # Extracting music features from the chords list + + # Melody features + mel_avg_pitch = int(sum([y[4] for y in melody_list]) / len(melody_list) / pitch_div) + mel_avg_dur = int(sum([int(y[2] / st_dur_div) for y in melody_list]) / len(melody_list)) + mel_avg_vel = int(sum([int(y[5] / vel_div) for y in melody_list]) / len(melody_list)) + mel_avg_chan = int(sum([int(y[3]) for y in melody_list]) / len(melody_list)) + + mel_tds = [int(abs(melody_list[i-1][1]-melody_list[i][1])) for i in range(1, len(melody_list))] + if len(mel_tds) != 0: mel_avg_tds = int(sum(mel_tds) / len(mel_tds) / st_dur_div) + + melody_features = [mel_avg_tds, mel_avg_dur, mel_avg_chan, mel_avg_pitch, mel_avg_vel] + + # Chords list features + mel_chrd_avg_pitch = int(sum([y[4] for y in chords_list1]) / len(chords_list1) / pitch_div) + mel_chrd_avg_dur = int(sum([int(y[2] / st_dur_div) for y in chords_list1]) / len(chords_list1)) + mel_chrd_avg_vel = int(sum([int(y[5] / vel_div) for y in chords_list1]) / len(chords_list1)) + mel_chrd_avg_chan = int(sum([int(y[3]) for y in chords_list1]) / len(chords_list1)) + + mel_chrd_tds = [int(abs(chords_list1[i-1][1]-chords_list1[i][1])) for i in range(1, len(chords_list1))] + if len(mel_tds) != 0: mel_chrd_avg_tds = int(sum(mel_chrd_tds) / len(mel_chrd_tds) / st_dur_div) + + chords_list_features = [mel_chrd_avg_tds, mel_chrd_avg_dur, mel_chrd_avg_chan, mel_chrd_avg_pitch, mel_chrd_avg_vel] + + # Bass melody features + bass_melody_avg_pitch = int(sum([y[4] for y in bass_melody]) / len(bass_melody) / pitch_div) + bass_melody_avg_dur = int(sum([int(y[2] / st_dur_div) for y in bass_melody]) / len(bass_melody)) + bass_melody_avg_vel = int(sum([int(y[5] / vel_div) for y in bass_melody]) / len(bass_melody)) + bass_melody_avg_chan = int(sum([int(y[3]) for y in bass_melody]) / len(bass_melody)) + + bass_melody_tds = [int(abs(bass_melody[i-1][1]-bass_melody[i][1])) for i in range(1, len(bass_melody))] + if len(bass_melody_tds) != 0: bass_melody_avg_tds = int(sum(bass_melody_tds) / len(bass_melody_tds) / st_dur_div) + + bass_melody_features = [bass_melody_avg_tds, bass_melody_avg_dur, bass_melody_avg_chan, bass_melody_avg_pitch, bass_melody_avg_vel] + + # A list to return all features + music_features = [] + + music_features.extend([len(chords_list1)]) # Count of the original chords list notes + + music_features.extend(melody_features) # Extracted melody features + music_features.extend(chords_list_features) # Extracted chords list features + music_features.extend(bass_melody_features) # Extracted bass melody features + music_features.extend([sum([y[4] for y in chords_list1])]) # Sum of all pitches in the original chords list + + return music_features + +################################################################################### + +def Tegridy_Transform(chords_list, to_pitch=60, to_velocity=-1): + + '''Tegridy Transform + + Input: Flat chords list + Desired average pitch (-1 == no change) + Desired average velocity (-1 == no change) + + Output: Transformed flat chords list + + Project Los Angeles + Tegridy Code 2021''' + + transformed_chords_list = [] + + chords_list.sort(reverse=False, key=lambda x: x[1]) + + chords_list_features = Optimus_Signature(chords_list)[1] + + pitch_diff = int((chords_list_features[0] + chords_list_features[1] + chords_list_features[2]) / 3) - to_pitch + velocity_diff = chords_list_features[4] - to_velocity + + for c in chords_list: + cc = copy.deepcopy(c) + if c[3] != 9: # Except the drums + if to_pitch != -1: + cc[4] = c[4] - pitch_diff + + if to_velocity != -1: + cc[5] = c[5] - velocity_diff + + transformed_chords_list.append(cc) + + return transformed_chords_list + +################################################################################### + +def Tegridy_MIDI_Zip_Notes_Summarizer(chords_list, match_type = 4): + + '''Tegridy MIDI Zip Notes Summarizer + + Input: Flat chords list / SONG + Match type according to 'note' event of MIDI.py + + Output: Summarized chords list + Number of summarized notes + Number of dicarted notes + + Project Los Angeles + Tegridy Code 2021''' + + i = 0 + j = 0 + out1 = [] + pout = [] + + + for o in chords_list: + + # MIDI Zip + + if o[match_type:] not in pout: + pout.append(o[match_type:]) + + out1.append(o) + j += 1 + + else: + i += 1 + + return out1, i + +################################################################################### + +def Tegridy_Score_Chords_Pairs_Generator(chords_list, shuffle_pairs = True, remove_single_notes=False): + + '''Tegridy Score Chords Pairs Generator + + Input: Flat chords list + Shuffle pairs (recommended) + + Output: Score chords pairs list + Number of created pairs + Number of detected chords + + Project Los Angeles + Tegridy Code 2021''' + + chords = [] + cho = [] + + i = 0 + j = 0 + + chords_list.sort(reverse=False, key=lambda x: x[1]) + pcho = chords_list[0] + for cc in chords_list: + if cc[1] == pcho[1]: + + cho.append(cc) + pcho = copy.deepcopy(cc) + + else: + if not remove_single_notes: + chords.append(cho) + cho = [] + cho.append(cc) + pcho = copy.deepcopy(cc) + + i += 1 + else: + if len(cho) > 1: + chords.append(cho) + cho = [] + cho.append(cc) + pcho = copy.deepcopy(cc) + + i += 1 + + chords_pairs = [] + for i in range(len(chords)-1): + chords_pairs.append([chords[i], chords[i+1]]) + j += 1 + if shuffle_pairs: random.shuffle(chords_pairs) + + return chords_pairs, j, i + +################################################################################### + +def Tegridy_Sliced_Score_Pairs_Generator(chords_list, number_of_miliseconds_per_slice=2000, shuffle_pairs = False): + + '''Tegridy Sliced Score Pairs Generator + + Input: Flat chords list + Number of miliseconds per slice + + Output: Sliced score pairs list + Number of created slices + + Project Los Angeles + Tegridy Code 2021''' + + chords = [] + cho = [] + + time = number_of_miliseconds_per_slice + + i = 0 + + chords_list1 = [x for x in chords_list if x] + chords_list1.sort(reverse=False, key=lambda x: x[1]) + pcho = chords_list1[0] + for cc in chords_list1[1:]: + + if cc[1] <= time: + + cho.append(cc) + + else: + if cho != [] and pcho != []: chords.append([pcho, cho]) + pcho = copy.deepcopy(cho) + cho = [] + cho.append(cc) + time += number_of_miliseconds_per_slice + i += 1 + + if cho != [] and pcho != []: + chords.append([pcho, cho]) + pcho = copy.deepcopy(cho) + i += 1 + + if shuffle_pairs: random.shuffle(chords) + + return chords, i + +################################################################################### + +def Tegridy_Timings_Converter(chords_list, + max_delta_time = 1000, + fixed_start_time = 250, + start_time = 0, + start_time_multiplier = 1, + durations_multiplier = 1): + + '''Tegridy Timings Converter + + Input: Flat chords list + Max delta time allowed between notes + Fixed start note time for excessive gaps + + Output: Converted flat chords list + + Project Los Angeles + Tegridy Code 2021''' + + song = chords_list + + song1 = [] + + p = song[0] + + p[1] = start_time + + time = start_time + + delta = [0] + + for i in range(len(song)): + if song[i][0] == 'note': + ss = copy.deepcopy(song[i]) + if song[i][1] != p[1]: + + if abs(song[i][1] - p[1]) > max_delta_time: + time += fixed_start_time + else: + time += abs(song[i][1] - p[1]) + delta.append(abs(song[i][1] - p[1])) + + ss[1] = int(round(time * start_time_multiplier, -1)) + ss[2] = int(round(song[i][2] * durations_multiplier, -1)) + song1.append(ss) + + p = copy.deepcopy(song[i]) + else: + + ss[1] = int(round(time * start_time_multiplier, -1)) + ss[2] = int(round(song[i][2] * durations_multiplier, -1)) + song1.append(ss) + + p = copy.deepcopy(song[i]) + + else: + ss = copy.deepcopy(song[i]) + ss[1] = time + song1.append(ss) + + average_delta_st = int(sum(delta) / len(delta)) + average_duration = int(sum([y[2] for y in song1 if y[0] == 'note']) / len([y[2] for y in song1 if y[0] == 'note'])) + + song1.sort(reverse=False, key=lambda x: x[1]) + + return song1, time, average_delta_st, average_duration + +################################################################################### + +def Tegridy_Score_Slicer(chords_list, number_of_miliseconds_per_slice=2000, overlap_notes = 0, overlap_chords=False): + + '''Tegridy Score Slicer + + Input: Flat chords list + Number of miliseconds per slice + + Output: Sliced chords list + Number of created slices + + Project Los Angeles + Tegridy Code 2021''' + + chords = [] + cho = [] + + time = number_of_miliseconds_per_slice + ptime = 0 + + i = 0 + + pc_idx = 0 + + chords_list.sort(reverse=False, key=lambda x: x[1]) + + for cc in chords_list: + + if cc[1] <= time: + + cho.append(cc) + + if ptime != cc[1]: + pc_idx = cho.index(cc) + + ptime = cc[1] + + + else: + + if overlap_chords: + chords.append(cho) + cho.extend(chords[-1][pc_idx:]) + + else: + chords.append(cho[:pc_idx]) + + cho = [] + + cho.append(cc) + + time += number_of_miliseconds_per_slice + ptime = cc[1] + + i += 1 + + if cho != []: + chords.append(cho) + i += 1 + + return [x for x in chords if x], i + +################################################################################### + +def Tegridy_TXT_Tokenizer(input_TXT_string, line_by_line_TXT_string=True): + + '''Tegridy TXT Tokenizer + + Input: TXT String + + Output: Tokenized TXT string + forward and reverse dics + + Project Los Angeles + Tegridy Code 2021''' + + print('Tegridy TXT Tokenizer') + + if line_by_line_TXT_string: + T = input_TXT_string.split() + else: + T = input_TXT_string.split(' ') + + DIC = dict(zip(T, range(len(T)))) + RDIC = dict(zip(range(len(T)), T)) + + TXTT = '' + + for t in T: + try: + TXTT += chr(DIC[t]) + except: + print('Error. Could not finish.') + return TXTT, DIC, RDIC + + print('Done!') + + return TXTT, DIC, RDIC + +################################################################################### + +def Tegridy_TXT_DeTokenizer(input_Tokenized_TXT_string, RDIC): + + '''Tegridy TXT Tokenizer + + Input: Tokenized TXT String + + + Output: DeTokenized TXT string + + Project Los Angeles + Tegridy Code 2021''' + + print('Tegridy TXT DeTokenizer') + + Q = list(input_Tokenized_TXT_string) + c = 0 + RTXT = '' + for q in Q: + try: + RTXT += RDIC[ord(q)] + chr(10) + except: + c+=1 + + print('Number of errors:', c) + + print('Done!') + + return RTXT + +################################################################################### + +def Tegridy_List_Slicer(input_list, slices_length_in_notes=20): + + '''Input: List to slice + Desired slices length in notes + + Output: Sliced list of lists + + Project Los Angeles + Tegridy Code 2021''' + + for i in range(0, len(input_list), slices_length_in_notes): + yield input_list[i:i + slices_length_in_notes] + +################################################################################### + +def Tegridy_Split_List(list_to_split, split_value=0): + + # src courtesy of www.geeksforgeeks.org + + # using list comprehension + zip() + slicing + enumerate() + # Split list into lists by particular value + size = len(list_to_split) + idx_list = [idx + 1 for idx, val in + enumerate(list_to_split) if val == split_value] + + + res = [list_to_split[i: j] for i, j in + zip([0] + idx_list, idx_list + + ([size] if idx_list[-1] != size else []))] + + # print result + # print("The list after splitting by a value : " + str(res)) + + return res + +################################################################################### + +# Binary chords functions + +def tones_chord_to_bits(chord): + bits = [0] * 12 + for num in chord: + bits[num] = 1 + + return bits + +def bits_to_tones_chord(bits): + return [i for i, bit in enumerate(bits) if bit == 1] + +def shift_bits(bits, n): + return bits[-n:] + bits[:-n] + +def bits_to_int(bits, shift_bits_value=0): + bits = shift_bits(bits, shift_bits_value) + result = 0 + for bit in bits: + result = (result << 1) | bit + + return result + +def int_to_bits(n): + bits = [0] * 12 + for i in range(12): + bits[11 - i] = n % 2 + n //= 2 + + return bits + +def bad_chord(chord): + bad = any(b - a == 1 for a, b in zip(chord, chord[1:])) + if (0 in chord) and (11 in chord): + bad = True + + return bad + +def pitches_chord_to_int(pitches_chord, tones_transpose_value=0): + + pitches_chord = [x for x in pitches_chord if 0 < x < 128] + + if not (-12 < tones_transpose_value < 12): + tones_transpose_value = 0 + + tones_chord = sorted(list(set([c % 12 for c in sorted(list(set(pitches_chord)))]))) + bits = tones_chord_to_bits(tones_chord) + integer = bits_to_int(bits, shift_bits_value=tones_transpose_value) + + return integer + +def int_to_pitches_chord(integer, chord_base_pitch=60): + if 0 < integer < 4096: + bits = int_to_bits(integer) + tones_chord = bits_to_tones_chord(bits) + if not bad_chord(tones_chord): + pitches_chord = [t+chord_base_pitch for t in tones_chord] + return [pitches_chord, tones_chord] + + else: + return 0 # Bad chord code + + else: + return -1 # Bad integer code + +################################################################################### + +def bad_chord(chord): + bad = any(b - a == 1 for a, b in zip(chord, chord[1:])) + if (0 in chord) and (11 in chord): + bad = True + + return bad + +def validate_pitches_chord(pitches_chord, return_sorted = True): + + pitches_chord = sorted(list(set([x for x in pitches_chord if 0 < x < 128]))) + + tones_chord = sorted(list(set([c % 12 for c in sorted(list(set(pitches_chord)))]))) + + if not bad_chord(tones_chord): + if return_sorted: + pitches_chord.sort(reverse=True) + return pitches_chord + + else: + if 0 in tones_chord and 11 in tones_chord: + tones_chord.remove(0) + + fixed_tones = [[a, b] for a, b in zip(tones_chord, tones_chord[1:]) if b-a != 1] + + fixed_tones_chord = [] + for f in fixed_tones: + fixed_tones_chord.extend(f) + fixed_tones_chord = list(set(fixed_tones_chord)) + + fixed_pitches_chord = [] + + for p in pitches_chord: + if (p % 12) in fixed_tones_chord: + fixed_pitches_chord.append(p) + + if return_sorted: + fixed_pitches_chord.sort(reverse=True) + + return fixed_pitches_chord + +def validate_pitches(chord, channel_to_check = 0, return_sorted = True): + + pitches_chord = sorted(list(set([x[4] for x in chord if 0 < x[4] < 128 and x[3] == channel_to_check]))) + + if pitches_chord: + + tones_chord = sorted(list(set([c % 12 for c in sorted(list(set(pitches_chord)))]))) + + if not bad_chord(tones_chord): + if return_sorted: + chord.sort(key = lambda x: x[4], reverse=True) + return chord + + else: + if 0 in tones_chord and 11 in tones_chord: + tones_chord.remove(0) + + fixed_tones = [[a, b] for a, b in zip(tones_chord, tones_chord[1:]) if b-a != 1] + + fixed_tones_chord = [] + for f in fixed_tones: + fixed_tones_chord.extend(f) + fixed_tones_chord = list(set(fixed_tones_chord)) + + fixed_chord = [] + + for c in chord: + if c[3] == channel_to_check: + if (c[4] % 12) in fixed_tones_chord: + fixed_chord.append(c) + else: + fixed_chord.append(c) + + if return_sorted: + fixed_chord.sort(key = lambda x: x[4], reverse=True) + + return fixed_chord + + else: + chord.sort(key = lambda x: x[4], reverse=True) + return chord + +def adjust_score_velocities(score, max_velocity): + + min_velocity = min([c[5] for c in score]) + max_velocity_all_channels = max([c[5] for c in score]) + min_velocity_ratio = min_velocity / max_velocity_all_channels + + max_channel_velocity = max([c[5] for c in score]) + if max_channel_velocity < min_velocity: + factor = max_velocity / min_velocity + else: + factor = max_velocity / max_channel_velocity + for i in range(len(score)): + score[i][5] = int(score[i][5] * factor) + +def chordify_score(score, + return_choridfied_score=True, + return_detected_score_information=False + ): + + if score: + + num_tracks = 1 + single_track_score = [] + score_num_ticks = 0 + + if type(score[0]) == int and len(score) > 1: + + score_type = 'MIDI_PY' + score_num_ticks = score[0] + + while num_tracks < len(score): + for event in score[num_tracks]: + single_track_score.append(event) + num_tracks += 1 + + else: + score_type = 'CUSTOM' + single_track_score = score + + if single_track_score and single_track_score[0]: + + try: + + if type(single_track_score[0][0]) == str or single_track_score[0][0] == 'note': + single_track_score.sort(key = lambda x: x[1]) + score_timings = [s[1] for s in single_track_score] + else: + score_timings = [s[0] for s in single_track_score] + + is_score_time_absolute = lambda sct: all(x <= y for x, y in zip(sct, sct[1:])) + + score_timings_type = '' + + if is_score_time_absolute(score_timings): + score_timings_type = 'ABS' + + chords = [] + cho = [] + + if score_type == 'MIDI_PY': + pe = single_track_score[0] + else: + pe = single_track_score[0] + + for e in single_track_score: + + if score_type == 'MIDI_PY': + time = e[1] + ptime = pe[1] + else: + time = e[0] + ptime = pe[0] + + if time == ptime: + cho.append(e) + + else: + if len(cho) > 0: + chords.append(cho) + cho = [] + cho.append(e) + + pe = e + + if len(cho) > 0: + chords.append(cho) + + else: + score_timings_type = 'REL' + + chords = [] + cho = [] + + for e in single_track_score: + + if score_type == 'MIDI_PY': + time = e[1] + else: + time = e[0] + + if time == 0: + cho.append(e) + + else: + if len(cho) > 0: + chords.append(cho) + cho = [] + cho.append(e) + + if len(cho) > 0: + chords.append(cho) + + requested_data = [] + + if return_detected_score_information: + + detected_score_information = [] + + detected_score_information.append(['Score type', score_type]) + detected_score_information.append(['Score timings type', score_timings_type]) + detected_score_information.append(['Score tpq', score_num_ticks]) + detected_score_information.append(['Score number of tracks', num_tracks]) + + requested_data.append(detected_score_information) + + if return_choridfied_score and return_detected_score_information: + requested_data.append(chords) + + if return_choridfied_score and not return_detected_score_information: + requested_data.extend(chords) + + return requested_data + + except Exception as e: + print('Error!') + print('Check score for consistency and compatibility!') + print('Exception detected:', e) + + else: + return None + + else: + return None + +def fix_monophonic_score_durations(monophonic_score): + + fixed_score = [] + + if monophonic_score[0][0] == 'note': + + for i in range(len(monophonic_score)-1): + note = monophonic_score[i] + + nmt = monophonic_score[i+1][1] + + if note[1]+note[2] >= nmt: + note_dur = nmt-note[1]-1 + else: + note_dur = note[2] + + new_note = [note[0], note[1], note_dur] + note[3:] + + fixed_score.append(new_note) + + fixed_score.append(monophonic_score[-1]) + + elif type(monophonic_score[0][0]) == int: + + for i in range(len(monophonic_score)-1): + note = monophonic_score[i] + + nmt = monophonic_score[i+1][0] + + if note[0]+note[1] >= nmt: + note_dur = nmt-note[0]-1 + else: + note_dur = note[1] + + new_note = [note[0], note_dur] + note[2:] + + fixed_score.append(new_note) + + fixed_score.append(monophonic_score[-1]) + + return fixed_score + +################################################################################### + +from itertools import product + +ALL_CHORDS = [[0], [7], [5], [9], [2], [4], [11], [10], [8], [6], [3], [1], [0, 9], [2, 5], + [4, 7], [7, 10], [2, 11], [0, 3], [6, 9], [1, 4], [8, 11], [5, 8], [1, 10], + [3, 6], [0, 4], [5, 9], [7, 11], [0, 7], [0, 5], [2, 10], [2, 7], [2, 9], + [2, 6], [4, 11], [4, 9], [3, 7], [5, 10], [1, 9], [0, 8], [6, 11], [3, 11], + [4, 8], [3, 10], [3, 8], [1, 5], [1, 8], [1, 6], [6, 10], [3, 9], [4, 10], + [1, 7], [0, 6], [2, 8], [5, 11], [5, 7], [0, 10], [0, 2], [9, 11], [7, 9], + [2, 4], [4, 6], [3, 5], [8, 10], [6, 8], [1, 3], [1, 11], [2, 7, 11], + [0, 4, 7], [0, 5, 9], [2, 6, 9], [2, 5, 10], [1, 4, 9], [4, 8, 11], [3, 7, 10], + [0, 3, 8], [3, 6, 11], [1, 5, 8], [1, 6, 10], [0, 4, 9], [2, 5, 9], [4, 7, 11], + [2, 7, 10], [2, 6, 11], [0, 3, 7], [0, 5, 8], [1, 4, 8], [1, 6, 9], [3, 8, 11], + [1, 5, 10], [3, 6, 10], [2, 5, 11], [4, 7, 10], [3, 6, 9], [0, 6, 9], + [0, 3, 9], [2, 8, 11], [2, 5, 8], [1, 7, 10], [1, 4, 7], [0, 3, 6], [1, 4, 10], + [5, 8, 11], [2, 5, 7], [0, 7, 10], [0, 2, 9], [0, 3, 5], [6, 9, 11], [4, 7, 9], + [2, 4, 11], [5, 8, 10], [1, 3, 10], [1, 4, 6], [3, 6, 8], [1, 8, 11], + [5, 7, 11], [0, 4, 10], [3, 5, 9], [0, 2, 6], [1, 7, 9], [0, 7, 9], [5, 7, 10], + [2, 8, 10], [3, 9, 11], [0, 2, 5], [2, 4, 8], [2, 4, 7], [0, 2, 7], [2, 7, 9], + [4, 9, 11], [4, 6, 9], [1, 3, 7], [2, 4, 9], [0, 5, 7], [0, 3, 10], [2, 9, 11], + [0, 5, 10], [0, 6, 8], [4, 6, 10], [4, 6, 11], [1, 4, 11], [6, 8, 11], + [1, 5, 11], [1, 6, 11], [1, 8, 10], [1, 6, 8], [3, 5, 8], [3, 8, 10], + [1, 3, 8], [3, 5, 10], [1, 3, 6], [2, 5, 7, 10], [0, 3, 7, 10], [1, 4, 8, 11], + [2, 4, 7, 11], [0, 4, 7, 9], [0, 2, 5, 9], [2, 6, 9, 11], [1, 5, 8, 10], + [0, 3, 5, 8], [3, 6, 8, 11], [1, 3, 6, 10], [1, 4, 6, 9], [1, 5, 9], [0, 4, 8], + [2, 6, 10], [3, 7, 11], [0, 3, 6, 9], [2, 5, 8, 11], [1, 4, 7, 10], + [2, 5, 7, 11], [0, 2, 6, 9], [0, 4, 7, 10], [2, 4, 8, 11], [0, 3, 5, 9], + [1, 4, 7, 9], [3, 6, 9, 11], [2, 5, 8, 10], [1, 4, 6, 10], [0, 3, 6, 8], + [1, 3, 7, 10], [1, 5, 8, 11], [2, 4, 10], [5, 9, 11], [1, 5, 7], [0, 2, 8], + [0, 4, 6], [1, 7, 11], [3, 7, 9], [1, 3, 9], [7, 9, 11], [5, 7, 9], [0, 6, 10], + [0, 2, 10], [2, 6, 8], [0, 2, 4], [4, 8, 10], [1, 9, 11], [2, 4, 6], + [3, 5, 11], [3, 5, 7], [0, 8, 10], [4, 6, 8], [1, 3, 11], [6, 8, 10], + [1, 3, 5], [0, 2, 5, 10], [0, 5, 7, 9], [0, 3, 8, 10], [0, 2, 4, 7], + [4, 6, 8, 11], [3, 5, 7, 10], [2, 7, 9, 11], [2, 4, 6, 9], [1, 6, 8, 10], + [1, 4, 9, 11], [1, 3, 5, 8], [1, 3, 6, 11], [2, 5, 9, 11], [2, 4, 7, 10], + [0, 2, 5, 8], [1, 5, 7, 10], [0, 4, 6, 9], [1, 3, 6, 9], [0, 3, 6, 10], + [2, 6, 8, 11], [0, 2, 7, 9], [1, 4, 8, 10], [0, 3, 7, 9], [3, 5, 8, 11], + [0, 5, 7, 10], [0, 2, 5, 7], [1, 4, 7, 11], [2, 4, 7, 9], [0, 3, 5, 10], + [4, 6, 9, 11], [1, 4, 6, 11], [2, 4, 9, 11], [1, 6, 8, 11], [1, 3, 6, 8], + [1, 3, 8, 10], [3, 5, 8, 10], [4, 7, 9, 11], [0, 2, 7, 10], [2, 5, 7, 9], + [0, 2, 4, 9], [1, 6, 9, 11], [2, 4, 6, 11], [0, 3, 5, 7], [0, 5, 8, 10], + [1, 4, 6, 8], [1, 3, 5, 10], [1, 3, 8, 11], [3, 6, 8, 10], [0, 2, 5, 7, 10], + [0, 2, 4, 7, 9], [0, 2, 5, 7, 9], [1, 3, 7, 9], [1, 4, 6, 9, 11], + [1, 3, 6, 8, 11], [3, 5, 9, 11], [1, 3, 6, 8, 10], [1, 4, 6, 8, 11], + [1, 3, 5, 8, 10], [2, 4, 6, 9, 11], [2, 4, 8, 10], [2, 4, 7, 9, 11], + [0, 3, 5, 7, 10], [1, 5, 7, 11], [0, 2, 6, 8], [0, 3, 5, 8, 10], [0, 4, 6, 10], + [1, 3, 5, 9], [1, 5, 7, 9], [2, 6, 8, 10], [3, 7, 9, 11], [0, 2, 4, 8], + [0, 4, 6, 8], [0, 4, 8, 10], [2, 4, 6, 10], [1, 3, 7, 11], [0, 2, 6, 10], + [1, 5, 9, 11], [3, 5, 7, 11], [1, 7, 9, 11], [0, 2, 4, 6], [1, 3, 9, 11], + [0, 2, 4, 10], [5, 7, 9, 11], [2, 4, 6, 8], [0, 2, 8, 10], [3, 5, 7, 9], + [1, 3, 5, 7], [4, 6, 8, 10], [0, 6, 8, 10], [1, 3, 5, 11], [0, 3, 6, 8, 10], + [0, 2, 4, 6, 9], [1, 4, 7, 9, 11], [2, 4, 6, 8, 11], [1, 3, 6, 9, 11], + [1, 3, 5, 8, 11], [0, 2, 5, 8, 10], [1, 4, 6, 8, 10], [0, 3, 5, 7, 9], + [2, 5, 7, 9, 11], [1, 3, 5, 7, 10], [0, 2, 4, 7, 10], [1, 3, 5, 7, 9], + [1, 3, 5, 9, 11], [1, 5, 7, 9, 11], [1, 3, 7, 9, 11], [3, 5, 7, 9, 11], + [2, 4, 6, 8, 10], [0, 4, 6, 8, 10], [0, 2, 6, 8, 10], [1, 3, 5, 7, 11], + [0, 2, 4, 8, 10], [0, 2, 4, 6, 8], [0, 2, 4, 6, 10], [0, 2, 4, 6, 8, 10], + [1, 3, 5, 7, 9, 11]] + +def find_exact_match_variable_length(list_of_lists, target_list, uncertain_indices): + # Infer possible values for each uncertain index + possible_values = {idx: set() for idx in uncertain_indices} + for sublist in list_of_lists: + for idx in uncertain_indices: + if idx < len(sublist): + possible_values[idx].add(sublist[idx]) + + # Generate all possible combinations for the uncertain elements + uncertain_combinations = product(*(possible_values[idx] for idx in uncertain_indices)) + + for combination in uncertain_combinations: + # Create a copy of the target list and update the uncertain elements + test_list = target_list[:] + for idx, value in zip(uncertain_indices, combination): + test_list[idx] = value + + # Check if the modified target list is an exact match in the list of lists + # Only consider sublists that are at least as long as the target list + for sublist in list_of_lists: + if len(sublist) >= len(test_list) and sublist[:len(test_list)] == test_list: + return sublist # Return the matching sublist + + return None # No exact match found + + +def advanced_validate_chord_pitches(chord, channel_to_check = 0, return_sorted = True): + + pitches_chord = sorted(list(set([x[4] for x in chord if 0 < x[4] < 128 and x[3] == channel_to_check]))) + + if pitches_chord: + + tones_chord = sorted(list(set([c % 12 for c in sorted(list(set(pitches_chord)))]))) + + if not bad_chord(tones_chord): + if return_sorted: + chord.sort(key = lambda x: x[4], reverse=True) + return chord + + else: + 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])) + + good_tones_chord = find_exact_match_variable_length(ALL_CHORDS, tones_chord, bad_chord_indices) + + if good_tones_chord is not None: + + fixed_chord = [] + + for c in chord: + if c[3] == channel_to_check: + if (c[4] % 12) in good_tones_chord: + fixed_chord.append(c) + else: + fixed_chord.append(c) + + if return_sorted: + fixed_chord.sort(key = lambda x: x[4], reverse=True) + + else: + + if 0 in tones_chord and 11 in tones_chord: + tones_chord.remove(0) + + fixed_tones = [[a, b] for a, b in zip(tones_chord, tones_chord[1:]) if b-a != 1] + + fixed_tones_chord = [] + for f in fixed_tones: + fixed_tones_chord.extend(f) + fixed_tones_chord = list(set(fixed_tones_chord)) + + fixed_chord = [] + + for c in chord: + if c[3] == channel_to_check: + if (c[4] % 12) in fixed_tones_chord: + fixed_chord.append(c) + else: + fixed_chord.append(c) + + if return_sorted: + fixed_chord.sort(key = lambda x: x[4], reverse=True) + + return fixed_chord + + else: + chord.sort(key = lambda x: x[4], reverse=True) + return chord + +################################################################################### + +def analyze_score_pitches(score, channels_to_analyze=[0]): + + analysis = {} + + score_notes = [s for s in score if s[3] in channels_to_analyze] + + cscore = chordify_score(score_notes) + + chords_tones = [] + + all_tones = [] + + all_chords_good = True + + bad_chords = [] + + for c in cscore: + tones = sorted(list(set([t[4] % 12 for t in c]))) + chords_tones.append(tones) + all_tones.extend(tones) + + if tones not in ALL_CHORDS: + all_chords_good = False + bad_chords.append(tones) + + analysis['Number of notes'] = len(score_notes) + analysis['Number of chords'] = len(cscore) + analysis['Score tones'] = sorted(list(set(all_tones))) + analysis['Shortest chord'] = sorted(min(chords_tones, key=len)) + analysis['Longest chord'] = sorted(max(chords_tones, key=len)) + analysis['All chords good'] = all_chords_good + analysis['Bad chords'] = bad_chords + + return analysis + +################################################################################### + +ALL_CHORDS_GROUPED = [[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]], + [[0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9], [0, 10], + [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [1, 11], + [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [2, 10], [2, 11], [3, 5], + [3, 6], [3, 7], [3, 8], [3, 9], [3, 10], [3, 11], [4, 6], [4, 7], [4, 8], + [4, 9], [4, 10], [4, 11], [5, 7], [5, 8], [5, 9], [5, 10], [5, 11], [6, 8], + [6, 9], [6, 10], [6, 11], [7, 9], [7, 10], [7, 11], [8, 10], [8, 11], + [9, 11]], + [[0, 2, 4], [0, 2, 5], [0, 3, 5], [0, 2, 6], [0, 3, 6], [0, 4, 6], [0, 2, 7], + [0, 3, 7], [0, 4, 7], [0, 5, 7], [0, 2, 8], [0, 3, 8], [0, 4, 8], [0, 5, 8], + [0, 6, 8], [0, 2, 9], [0, 3, 9], [0, 4, 9], [0, 5, 9], [0, 6, 9], [0, 7, 9], + [0, 2, 10], [0, 3, 10], [0, 4, 10], [0, 5, 10], [0, 6, 10], [0, 7, 10], + [0, 8, 10], [1, 3, 5], [1, 3, 6], [1, 4, 6], [1, 3, 7], [1, 4, 7], [1, 5, 7], + [1, 3, 8], [1, 4, 8], [1, 5, 8], [1, 6, 8], [1, 3, 9], [1, 4, 9], [1, 5, 9], + [1, 6, 9], [1, 7, 9], [1, 3, 10], [1, 4, 10], [1, 5, 10], [1, 6, 10], + [1, 7, 10], [1, 8, 10], [1, 3, 11], [1, 4, 11], [1, 5, 11], [1, 6, 11], + [1, 7, 11], [1, 8, 11], [1, 9, 11], [2, 4, 6], [2, 4, 7], [2, 5, 7], + [2, 4, 8], [2, 5, 8], [2, 6, 8], [2, 4, 9], [2, 5, 9], [2, 6, 9], [2, 7, 9], + [2, 4, 10], [2, 5, 10], [2, 6, 10], [2, 7, 10], [2, 8, 10], [2, 4, 11], + [2, 5, 11], [2, 6, 11], [2, 7, 11], [2, 8, 11], [2, 9, 11], [3, 5, 7], + [3, 5, 8], [3, 6, 8], [3, 5, 9], [3, 6, 9], [3, 7, 9], [3, 5, 10], [3, 6, 10], + [3, 7, 10], [3, 8, 10], [3, 5, 11], [3, 6, 11], [3, 7, 11], [3, 8, 11], + [3, 9, 11], [4, 6, 8], [4, 6, 9], [4, 7, 9], [4, 6, 10], [4, 7, 10], + [4, 8, 10], [4, 6, 11], [4, 7, 11], [4, 8, 11], [4, 9, 11], [5, 7, 9], + [5, 7, 10], [5, 8, 10], [5, 7, 11], [5, 8, 11], [5, 9, 11], [6, 8, 10], + [6, 8, 11], [6, 9, 11], [7, 9, 11]], + [[0, 2, 4, 6], [0, 2, 4, 7], [0, 2, 5, 7], [0, 3, 5, 7], [0, 2, 4, 8], + [0, 2, 5, 8], [0, 2, 6, 8], [0, 3, 5, 8], [0, 3, 6, 8], [0, 4, 6, 8], + [0, 2, 4, 9], [0, 2, 5, 9], [0, 2, 6, 9], [0, 2, 7, 9], [0, 3, 5, 9], + [0, 3, 6, 9], [0, 3, 7, 9], [0, 4, 6, 9], [0, 4, 7, 9], [0, 5, 7, 9], + [0, 2, 4, 10], [0, 2, 5, 10], [0, 2, 6, 10], [0, 2, 7, 10], [0, 2, 8, 10], + [0, 3, 5, 10], [0, 3, 6, 10], [0, 3, 7, 10], [0, 3, 8, 10], [0, 4, 6, 10], + [0, 4, 7, 10], [0, 4, 8, 10], [0, 5, 7, 10], [0, 5, 8, 10], [0, 6, 8, 10], + [1, 3, 5, 7], [1, 3, 5, 8], [1, 3, 6, 8], [1, 4, 6, 8], [1, 3, 5, 9], + [1, 3, 6, 9], [1, 3, 7, 9], [1, 4, 6, 9], [1, 4, 7, 9], [1, 5, 7, 9], + [1, 3, 5, 10], [1, 3, 6, 10], [1, 3, 7, 10], [1, 3, 8, 10], [1, 4, 6, 10], + [1, 4, 7, 10], [1, 4, 8, 10], [1, 5, 7, 10], [1, 5, 8, 10], [1, 6, 8, 10], + [1, 3, 5, 11], [1, 3, 6, 11], [1, 3, 7, 11], [1, 3, 8, 11], [1, 3, 9, 11], + [1, 4, 6, 11], [1, 4, 7, 11], [1, 4, 8, 11], [1, 4, 9, 11], [1, 5, 7, 11], + [1, 5, 8, 11], [1, 5, 9, 11], [1, 6, 8, 11], [1, 6, 9, 11], [1, 7, 9, 11], + [2, 4, 6, 8], [2, 4, 6, 9], [2, 4, 7, 9], [2, 5, 7, 9], [2, 4, 6, 10], + [2, 4, 7, 10], [2, 4, 8, 10], [2, 5, 7, 10], [2, 5, 8, 10], [2, 6, 8, 10], + [2, 4, 6, 11], [2, 4, 7, 11], [2, 4, 8, 11], [2, 4, 9, 11], [2, 5, 7, 11], + [2, 5, 8, 11], [2, 5, 9, 11], [2, 6, 8, 11], [2, 6, 9, 11], [2, 7, 9, 11], + [3, 5, 7, 9], [3, 5, 7, 10], [3, 5, 8, 10], [3, 6, 8, 10], [3, 5, 7, 11], + [3, 5, 8, 11], [3, 5, 9, 11], [3, 6, 8, 11], [3, 6, 9, 11], [3, 7, 9, 11], + [4, 6, 8, 10], [4, 6, 8, 11], [4, 6, 9, 11], [4, 7, 9, 11], [5, 7, 9, 11]], + [[0, 2, 4, 6, 8], [0, 2, 4, 6, 9], [0, 2, 4, 7, 9], [0, 2, 5, 7, 9], + [0, 3, 5, 7, 9], [0, 2, 4, 6, 10], [0, 2, 4, 7, 10], [0, 2, 4, 8, 10], + [0, 2, 5, 7, 10], [0, 2, 5, 8, 10], [0, 2, 6, 8, 10], [0, 3, 5, 7, 10], + [0, 3, 5, 8, 10], [0, 3, 6, 8, 10], [0, 4, 6, 8, 10], [1, 3, 5, 7, 9], + [1, 3, 5, 7, 10], [1, 3, 5, 8, 10], [1, 3, 6, 8, 10], [1, 4, 6, 8, 10], + [1, 3, 5, 7, 11], [1, 3, 5, 8, 11], [1, 3, 5, 9, 11], [1, 3, 6, 8, 11], + [1, 3, 6, 9, 11], [1, 3, 7, 9, 11], [1, 4, 6, 8, 11], [1, 4, 6, 9, 11], + [1, 4, 7, 9, 11], [1, 5, 7, 9, 11], [2, 4, 6, 8, 10], [2, 4, 6, 8, 11], + [2, 4, 6, 9, 11], [2, 4, 7, 9, 11], [2, 5, 7, 9, 11], [3, 5, 7, 9, 11]], + [[0, 2, 4, 6, 8, 10], [1, 3, 5, 7, 9, 11]]] + +def group_sublists_by_length(lst): + unique_lengths = sorted(list(set(map(len, lst))), reverse=True) + return [[x for x in lst if len(x) == i] for i in unique_lengths] + +def pitches_to_tones_chord(pitches): + return sorted(set([p % 12 for p in pitches])) + +def tones_chord_to_pitches(tones_chord, base_pitch=60): + return [t+base_pitch for t in tones_chord if 0 <= t < 12] + +################################################################################### + +def advanced_score_processor(raw_score, + patches_to_analyze=list(range(129)), + return_score_analysis=False, + return_enhanced_score=False, + return_enhanced_score_notes=False, + return_enhanced_monophonic_melody=False, + return_chordified_enhanced_score=False, + return_chordified_enhanced_score_with_lyrics=False, + return_score_tones_chords=False, + return_text_and_lyric_events=False + ): + + '''TMIDIX Advanced Score Processor''' + + # Score data types detection + + if raw_score and type(raw_score) == list: + + num_ticks = 0 + num_tracks = 1 + + basic_single_track_score = [] + + if type(raw_score[0]) != int: + if len(raw_score[0]) < 5 and type(raw_score[0][0]) != str: + return ['Check score for errors and compatibility!'] + + else: + basic_single_track_score = copy.deepcopy(raw_score) + + else: + num_ticks = raw_score[0] + while num_tracks < len(raw_score): + for event in raw_score[num_tracks]: + ev = copy.deepcopy(event) + basic_single_track_score.append(ev) + num_tracks += 1 + + basic_single_track_score.sort(key=lambda x: x[4] if x[0] == 'note' else 128, reverse=True) + basic_single_track_score.sort(key=lambda x: x[1]) + + enhanced_single_track_score = [] + patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + all_score_patches = [] + num_patch_changes = 0 + + for event in basic_single_track_score: + if event[0] == 'patch_change': + patches[event[2]] = event[3] + enhanced_single_track_score.append(event) + num_patch_changes += 1 + + if event[0] == 'note': + if event[3] != 9: + event.extend([patches[event[3]]]) + all_score_patches.extend([patches[event[3]]]) + else: + event.extend([128]) + all_score_patches.extend([128]) + + if enhanced_single_track_score: + if (event[1] == enhanced_single_track_score[-1][1]): + if ([event[3], event[4]] != enhanced_single_track_score[-1][3:5]): + enhanced_single_track_score.append(event) + else: + enhanced_single_track_score.append(event) + + else: + enhanced_single_track_score.append(event) + + if event[0] not in ['note', 'patch_change']: + enhanced_single_track_score.append(event) + + enhanced_single_track_score.sort(key=lambda x: x[6] if x[0] == 'note' else -1) + enhanced_single_track_score.sort(key=lambda x: x[4] if x[0] == 'note' else 128, reverse=True) + enhanced_single_track_score.sort(key=lambda x: x[1]) + + # Analysis and chordification + + cscore = [] + cescore = [] + chords_tones = [] + tones_chords = [] + all_tones = [] + all_chords_good = True + bad_chords = [] + bad_chords_count = 0 + score_notes = [] + score_pitches = [] + score_patches = [] + num_text_events = 0 + num_lyric_events = 0 + num_other_events = 0 + text_and_lyric_events = [] + text_and_lyric_events_latin = None + + analysis = {} + + score_notes = [s for s in enhanced_single_track_score if s[0] == 'note' and s[6] in patches_to_analyze] + score_patches = [sn[6] for sn in score_notes] + + if return_text_and_lyric_events: + text_and_lyric_events = [e for e in enhanced_single_track_score if e[0] in ['text_event', 'lyric']] + + if text_and_lyric_events: + text_and_lyric_events_latin = True + for e in text_and_lyric_events: + try: + tle = str(e[2].decode()) + except: + tle = str(e[2]) + + for c in tle: + if not 0 <= ord(c) < 128: + text_and_lyric_events_latin = False + + if (return_chordified_enhanced_score or return_score_analysis) and any(elem in patches_to_analyze for elem in score_patches): + + cescore = chordify_score([num_ticks, enhanced_single_track_score]) + + if return_score_analysis: + + cscore = chordify_score(score_notes) + + score_pitches = [sn[4] for sn in score_notes] + + text_events = [e for e in enhanced_single_track_score if e[0] == 'text_event'] + num_text_events = len(text_events) + + lyric_events = [e for e in enhanced_single_track_score if e[0] == 'lyric'] + num_lyric_events = len(lyric_events) + + other_events = [e for e in enhanced_single_track_score if e[0] not in ['note', 'patch_change', 'text_event', 'lyric']] + num_other_events = len(other_events) + + for c in cscore: + tones = sorted(set([t[4] % 12 for t in c if t[3] != 9])) + + if tones: + chords_tones.append(tones) + all_tones.extend(tones) + + if tones not in ALL_CHORDS: + all_chords_good = False + bad_chords.append(tones) + bad_chords_count += 1 + + analysis['Number of ticks per quarter note'] = num_ticks + analysis['Number of tracks'] = num_tracks + analysis['Number of all events'] = len(enhanced_single_track_score) + analysis['Number of patch change events'] = num_patch_changes + analysis['Number of text events'] = num_text_events + analysis['Number of lyric events'] = num_lyric_events + analysis['All text and lyric events Latin'] = text_and_lyric_events_latin + analysis['Number of other events'] = num_other_events + analysis['Number of score notes'] = len(score_notes) + analysis['Number of score chords'] = len(cscore) + analysis['Score patches'] = sorted(set(score_patches)) + analysis['Score pitches'] = sorted(set(score_pitches)) + analysis['Score tones'] = sorted(set(all_tones)) + if chords_tones: + analysis['Shortest chord'] = sorted(min(chords_tones, key=len)) + analysis['Longest chord'] = sorted(max(chords_tones, key=len)) + analysis['All chords good'] = all_chords_good + analysis['Number of bad chords'] = bad_chords_count + analysis['Bad chords'] = sorted([list(c) for c in set(tuple(bc) for bc in bad_chords)]) + + else: + analysis['Error'] = 'Provided score does not have specified patches to analyse' + analysis['Provided patches to analyse'] = sorted(patches_to_analyze) + analysis['Patches present in the score'] = sorted(set(all_score_patches)) + + if return_enhanced_monophonic_melody: + + score_notes_copy = copy.deepcopy(score_notes) + chordified_score_notes = chordify_score(score_notes_copy) + + melody = [c[0] for c in chordified_score_notes] + + fixed_melody = [] + + for i in range(len(melody)-1): + note = melody[i] + nmt = melody[i+1][1] + + if note[1]+note[2] >= nmt: + note_dur = nmt-note[1]-1 + else: + note_dur = note[2] + + melody[i][2] = note_dur + + fixed_melody.append(melody[i]) + fixed_melody.append(melody[-1]) + + if return_score_tones_chords: + cscore = chordify_score(score_notes) + for c in cscore: + tones_chord = sorted(set([t[4] % 12 for t in c if t[3] != 9])) + if tones_chord: + tones_chords.append(tones_chord) + + if return_chordified_enhanced_score_with_lyrics: + score_with_lyrics = [e for e in enhanced_single_track_score if e[0] in ['note', 'text_event', 'lyric']] + chordified_enhanced_score_with_lyrics = chordify_score(score_with_lyrics) + + # Returned data + + requested_data = [] + + if return_score_analysis and analysis: + requested_data.append([[k, v] for k, v in analysis.items()]) + + if return_enhanced_score and enhanced_single_track_score: + requested_data.append([num_ticks, enhanced_single_track_score]) + + if return_enhanced_score_notes and score_notes: + requested_data.append(score_notes) + + if return_enhanced_monophonic_melody and fixed_melody: + requested_data.append(fixed_melody) + + if return_chordified_enhanced_score and cescore: + requested_data.append(cescore) + + if return_chordified_enhanced_score_with_lyrics and chordified_enhanced_score_with_lyrics: + requested_data.append(chordified_enhanced_score_with_lyrics) + + if return_score_tones_chords and tones_chords: + requested_data.append(tones_chords) + + if return_text_and_lyric_events and text_and_lyric_events: + requested_data.append(text_and_lyric_events) + + return requested_data + + else: + return ['Check score for errors and compatibility!'] + +################################################################################### + +import random +import copy + +################################################################################### + +def replace_bad_tones_chord(bad_tones_chord): + bad_chord_p = [0] * 12 + for b in bad_tones_chord: + bad_chord_p[b] = 1 + + match_ratios = [] + good_chords = [] + for c in ALL_CHORDS: + good_chord_p = [0] * 12 + for cc in c: + good_chord_p[cc] = 1 + + good_chords.append(good_chord_p) + match_ratios.append(sum(i == j for i, j in zip(good_chord_p, bad_chord_p)) / len(good_chord_p)) + + best_good_chord = good_chords[match_ratios.index(max(match_ratios))] + + replaced_chord = [] + for i in range(len(best_good_chord)): + if best_good_chord[i] == 1: + replaced_chord.append(i) + + return [replaced_chord, max(match_ratios)] + +################################################################################### + +def check_and_fix_chord(chord, + channel_index=3, + pitch_index=4 + ): + + tones_chord = sorted(set([t[pitch_index] % 12 for t in chord if t[channel_index] != 9])) + + notes_events = [t for t in chord if t[channel_index] != 9] + notes_events.sort(key=lambda x: x[pitch_index], reverse=True) + + drums_events = [t for t in chord if t[channel_index] == 9] + + checked_and_fixed_chord = [] + + if tones_chord: + + new_tones_chord = advanced_check_and_fix_tones_chord(tones_chord, high_pitch=notes_events[0][pitch_index]) + + if new_tones_chord != tones_chord: + + if len(notes_events) > 1: + checked_and_fixed_chord.extend([notes_events[0]]) + for cc in notes_events[1:]: + if cc[channel_index] != 9: + if (cc[pitch_index] % 12) in new_tones_chord: + checked_and_fixed_chord.extend([cc]) + checked_and_fixed_chord.extend(drums_events) + else: + checked_and_fixed_chord.extend([notes_events[0]]) + else: + checked_and_fixed_chord.extend(chord) + else: + checked_and_fixed_chord.extend(chord) + + checked_and_fixed_chord.sort(key=lambda x: x[pitch_index], reverse=True) + + return checked_and_fixed_chord + +################################################################################### + +def find_similar_tones_chord(tones_chord, + max_match_threshold=1, + randomize_chords_matches=False, + custom_chords_list=[]): + chord_p = [0] * 12 + for b in tones_chord: + chord_p[b] = 1 + + match_ratios = [] + good_chords = [] + + if custom_chords_list: + CHORDS = copy.deepcopy([list(x) for x in set(tuple(t) for t in custom_chords_list)]) + else: + CHORDS = copy.deepcopy(ALL_CHORDS) + + if randomize_chords_matches: + random.shuffle(CHORDS) + + for c in CHORDS: + good_chord_p = [0] * 12 + for cc in c: + good_chord_p[cc] = 1 + + good_chords.append(good_chord_p) + match_ratio = sum(i == j for i, j in zip(good_chord_p, chord_p)) / len(good_chord_p) + if match_ratio < max_match_threshold: + match_ratios.append(match_ratio) + else: + match_ratios.append(0) + + best_good_chord = good_chords[match_ratios.index(max(match_ratios))] + + similar_chord = [] + for i in range(len(best_good_chord)): + if best_good_chord[i] == 1: + similar_chord.append(i) + + return [similar_chord, max(match_ratios)] + +################################################################################### + +def generate_tones_chords_progression(number_of_chords_to_generate=100, + start_tones_chord=[], + custom_chords_list=[]): + + if start_tones_chord: + start_chord = start_tones_chord + else: + start_chord = random.choice(ALL_CHORDS) + + chord = [] + + chords_progression = [start_chord] + + for i in range(number_of_chords_to_generate): + if not chord: + chord = start_chord + + if custom_chords_list: + chord = find_similar_tones_chord(chord, randomize_chords_matches=True, custom_chords_list=custom_chords_list)[0] + else: + chord = find_similar_tones_chord(chord, randomize_chords_matches=True)[0] + + chords_progression.append(chord) + + return chords_progression + +################################################################################### + +def ascii_texts_search(texts = ['text1', 'text2', 'text3'], + search_query = 'Once upon a time...', + deterministic_matching = False + ): + + texts_copy = texts + + if not deterministic_matching: + texts_copy = copy.deepcopy(texts) + random.shuffle(texts_copy) + + clean_texts = [] + + for t in texts_copy: + text_words_list = [at.split(chr(32)) for at in t.split(chr(10))] + + clean_text_words_list = [] + for twl in text_words_list: + for w in twl: + clean_text_words_list.append(''.join(filter(str.isalpha, w.lower()))) + + clean_texts.append(clean_text_words_list) + + text_search_query = [at.split(chr(32)) for at in search_query.split(chr(10))] + clean_text_search_query = [] + for w in text_search_query: + for ww in w: + clean_text_search_query.append(''.join(filter(str.isalpha, ww.lower()))) + + if clean_texts[0] and clean_text_search_query: + texts_match_ratios = [] + words_match_indexes = [] + for t in clean_texts: + word_match_count = 0 + wmis = [] + + for c in clean_text_search_query: + if c in t: + word_match_count += 1 + wmis.append(t.index(c)) + else: + wmis.append(-1) + + words_match_indexes.append(wmis) + words_match_indexes_consequtive = all(abs(b) - abs(a) == 1 for a, b in zip(wmis, wmis[1:])) + words_match_indexes_consequtive_ratio = sum([abs(b) - abs(a) == 1 for a, b in zip(wmis, wmis[1:])]) / len(wmis) + + if words_match_indexes_consequtive: + texts_match_ratios.append(word_match_count / len(clean_text_search_query)) + else: + texts_match_ratios.append(((word_match_count / len(clean_text_search_query)) + words_match_indexes_consequtive_ratio) / 2) + + if texts_match_ratios: + max_text_match_ratio = max(texts_match_ratios) + max_match_ratio_text = texts_copy[texts_match_ratios.index(max_text_match_ratio)] + max_text_words_match_indexes = words_match_indexes[texts_match_ratios.index(max_text_match_ratio)] + + return [max_match_ratio_text, max_text_match_ratio, max_text_words_match_indexes] + + else: + return None + +################################################################################### + +def ascii_text_words_counter(ascii_text): + + text_words_list = [at.split(chr(32)) for at in ascii_text.split(chr(10))] + + clean_text_words_list = [] + for twl in text_words_list: + for w in twl: + wo = '' + for ww in w.lower(): + if 96 < ord(ww) < 123: + wo += ww + if wo != '': + clean_text_words_list.append(wo) + + words = {} + for i in clean_text_words_list: + words[i] = words.get(i, 0) + 1 + + words_sorted = dict(sorted(words.items(), key=lambda item: item[1], reverse=True)) + + return len(clean_text_words_list), words_sorted, clean_text_words_list + +################################################################################### + +def check_and_fix_tones_chord(tones_chord): + + lst = tones_chord + + if len(lst) == 2: + if lst[1] - lst[0] == 1: + return [lst[-1]] + else: + if 0 in lst and 11 in lst: + lst.remove(0) + return lst + + non_consecutive = [lst[0]] + + if len(lst) > 2: + for i in range(1, len(lst) - 1): + if lst[i-1] + 1 != lst[i] and lst[i] + 1 != lst[i+1]: + non_consecutive.append(lst[i]) + non_consecutive.append(lst[-1]) + + if 0 in non_consecutive and 11 in non_consecutive: + non_consecutive.remove(0) + + return non_consecutive + +################################################################################### + +def find_closest_tone(tones, tone): + return min(tones, key=lambda x:abs(x-tone)) + +def advanced_check_and_fix_tones_chord(tones_chord, high_pitch=0): + + lst = tones_chord + + if 0 < high_pitch < 128: + ht = high_pitch % 12 + else: + ht = 12 + + cht = find_closest_tone(lst, ht) + + if len(lst) == 2: + if lst[1] - lst[0] == 1: + return [cht] + else: + if 0 in lst and 11 in lst: + if find_closest_tone([0, 11], cht) == 11: + lst.remove(0) + else: + lst.remove(11) + return lst + + non_consecutive = [] + + if len(lst) > 2: + for i in range(0, len(lst) - 1): + if lst[i] + 1 != lst[i+1]: + non_consecutive.append(lst[i]) + if lst[-1] - lst[-2] > 1: + non_consecutive.append(lst[-1]) + + if cht not in non_consecutive: + non_consecutive.append(cht) + non_consecutive.sort() + if any(abs(non_consecutive[i+1] - non_consecutive[i]) == 1 for i in range(len(non_consecutive) - 1)): + final_list = [x for x in non_consecutive if x == cht or abs(x - cht) > 1] + else: + final_list = non_consecutive + + else: + final_list = non_consecutive + + if 0 in final_list and 11 in final_list: + if find_closest_tone([0, 11], cht) == 11: + final_list.remove(0) + else: + final_list.remove(11) + + if cht in final_list or ht in final_list: + return final_list + else: + return ['Error'] + +################################################################################### + +def create_similarity_matrix(list_of_values, matrix_length=0): + + counts = Counter(list_of_values).items() + + if matrix_length > 0: + sim_matrix = [0] * max(matrix_length, len(list_of_values)) + else: + sim_matrix = [0] * len(counts) + + for c in counts: + sim_matrix[c[0]] = c[1] + + similarity_matrix = [[0] * len(sim_matrix) for _ in range(len(sim_matrix))] + + for i in range(len(sim_matrix)): + for j in range(len(sim_matrix)): + if max(sim_matrix[i], sim_matrix[j]) != 0: + similarity_matrix[i][j] = min(sim_matrix[i], sim_matrix[j]) / max(sim_matrix[i], sim_matrix[j]) + + return similarity_matrix, sim_matrix + +################################################################################### + +def augment_enhanced_score_notes(enhanced_score_notes, + timings_divider=16, + full_sorting=True, + timings_shift=0, + pitch_shift=0 + ): + + esn = copy.deepcopy(enhanced_score_notes) + + for e in esn: + e[1] = int(e[1] / timings_divider) + timings_shift + e[2] = int(e[2] / timings_divider) + timings_shift + e[4] = e[4] + pitch_shift + + if full_sorting: + + # Sorting by patch, pitch, then by start-time + esn.sort(key=lambda x: x[6]) + esn.sort(key=lambda x: x[4], reverse=True) + esn.sort(key=lambda x: x[1]) + + return esn + +################################################################################### + +def stack_list(lst, base=12): + return sum(j * base**i for i, j in enumerate(lst[::-1])) + +def destack_list(num, base=12): + lst = [] + while num: + lst.append(num % base) + num //= base + return lst[::-1] + +################################################################################### + +def extract_melody(chordified_enhanced_score, + melody_range=[48, 84], + melody_channel=0, + melody_patch=0, + melody_velocity=0, + stacked_melody=False, + stacked_melody_base_pitch=60 + ): + + if stacked_melody: + + + all_pitches_chords = [] + for e in chordified_enhanced_score: + all_pitches_chords.append(sorted(set([p[4] for p in e]), reverse=True)) + + melody_score = [] + for i, chord in enumerate(chordified_enhanced_score): + + if melody_velocity > 0: + vel = melody_velocity + else: + vel = chord[0][5] + + melody_score.append(['note', chord[0][1], chord[0][2], melody_channel, stacked_melody_base_pitch+(stack_list([p % 12 for p in all_pitches_chords[i]]) % 12), vel, melody_patch]) + + else: + + melody_score = copy.deepcopy([c[0] for c in chordified_enhanced_score if c[0][3] != 9]) + + for e in melody_score: + + e[3] = melody_channel + + if melody_velocity > 0: + e[5] = melody_velocity + + e[6] = melody_patch + + if e[4] < melody_range[0]: + e[4] = (e[4] % 12) + melody_range[0] + + if e[4] >= melody_range[1]: + e[4] = (e[4] % 12) + (melody_range[1]-12) + + return fix_monophonic_score_durations(melody_score) + +################################################################################### + +def flip_enhanced_score_notes(enhanced_score_notes): + + min_pitch = min([e[4] for e in enhanced_score_notes if e[3] != 9]) + + fliped_score_pitches = [127 - e[4]for e in enhanced_score_notes if e[3] != 9] + + delta_min_pitch = min_pitch - min([p for p in fliped_score_pitches]) + + output_score = copy.deepcopy(enhanced_score_notes) + + for e in output_score: + if e[3] != 9: + e[4] = (127 - e[4]) + delta_min_pitch + + return output_score + +################################################################################### + +ALL_CHORDS_SORTED = [[0], [0, 2], [0, 3], [0, 4], [0, 2, 4], [0, 5], [0, 2, 5], [0, 3, 5], [0, 6], + [0, 2, 6], [0, 3, 6], [0, 4, 6], [0, 2, 4, 6], [0, 7], [0, 2, 7], [0, 3, 7], + [0, 4, 7], [0, 5, 7], [0, 2, 4, 7], [0, 2, 5, 7], [0, 3, 5, 7], [0, 8], + [0, 2, 8], [0, 3, 8], [0, 4, 8], [0, 5, 8], [0, 6, 8], [0, 2, 4, 8], + [0, 2, 5, 8], [0, 2, 6, 8], [0, 3, 5, 8], [0, 3, 6, 8], [0, 4, 6, 8], + [0, 2, 4, 6, 8], [0, 9], [0, 2, 9], [0, 3, 9], [0, 4, 9], [0, 5, 9], [0, 6, 9], + [0, 7, 9], [0, 2, 4, 9], [0, 2, 5, 9], [0, 2, 6, 9], [0, 2, 7, 9], + [0, 3, 5, 9], [0, 3, 6, 9], [0, 3, 7, 9], [0, 4, 6, 9], [0, 4, 7, 9], + [0, 5, 7, 9], [0, 2, 4, 6, 9], [0, 2, 4, 7, 9], [0, 2, 5, 7, 9], + [0, 3, 5, 7, 9], [0, 10], [0, 2, 10], [0, 3, 10], [0, 4, 10], [0, 5, 10], + [0, 6, 10], [0, 7, 10], [0, 8, 10], [0, 2, 4, 10], [0, 2, 5, 10], + [0, 2, 6, 10], [0, 2, 7, 10], [0, 2, 8, 10], [0, 3, 5, 10], [0, 3, 6, 10], + [0, 3, 7, 10], [0, 3, 8, 10], [0, 4, 6, 10], [0, 4, 7, 10], [0, 4, 8, 10], + [0, 5, 7, 10], [0, 5, 8, 10], [0, 6, 8, 10], [0, 2, 4, 6, 10], + [0, 2, 4, 7, 10], [0, 2, 4, 8, 10], [0, 2, 5, 7, 10], [0, 2, 5, 8, 10], + [0, 2, 6, 8, 10], [0, 3, 5, 7, 10], [0, 3, 5, 8, 10], [0, 3, 6, 8, 10], + [0, 4, 6, 8, 10], [0, 2, 4, 6, 8, 10], [1], [1, 3], [1, 4], [1, 5], [1, 3, 5], + [1, 6], [1, 3, 6], [1, 4, 6], [1, 7], [1, 3, 7], [1, 4, 7], [1, 5, 7], + [1, 3, 5, 7], [1, 8], [1, 3, 8], [1, 4, 8], [1, 5, 8], [1, 6, 8], [1, 3, 5, 8], + [1, 3, 6, 8], [1, 4, 6, 8], [1, 9], [1, 3, 9], [1, 4, 9], [1, 5, 9], [1, 6, 9], + [1, 7, 9], [1, 3, 5, 9], [1, 3, 6, 9], [1, 3, 7, 9], [1, 4, 6, 9], + [1, 4, 7, 9], [1, 5, 7, 9], [1, 3, 5, 7, 9], [1, 10], [1, 3, 10], [1, 4, 10], + [1, 5, 10], [1, 6, 10], [1, 7, 10], [1, 8, 10], [1, 3, 5, 10], [1, 3, 6, 10], + [1, 3, 7, 10], [1, 3, 8, 10], [1, 4, 6, 10], [1, 4, 7, 10], [1, 4, 8, 10], + [1, 5, 7, 10], [1, 5, 8, 10], [1, 6, 8, 10], [1, 3, 5, 7, 10], + [1, 3, 5, 8, 10], [1, 3, 6, 8, 10], [1, 4, 6, 8, 10], [1, 11], [1, 3, 11], + [1, 4, 11], [1, 5, 11], [1, 6, 11], [1, 7, 11], [1, 8, 11], [1, 9, 11], + [1, 3, 5, 11], [1, 3, 6, 11], [1, 3, 7, 11], [1, 3, 8, 11], [1, 3, 9, 11], + [1, 4, 6, 11], [1, 4, 7, 11], [1, 4, 8, 11], [1, 4, 9, 11], [1, 5, 7, 11], + [1, 5, 8, 11], [1, 5, 9, 11], [1, 6, 8, 11], [1, 6, 9, 11], [1, 7, 9, 11], + [1, 3, 5, 7, 11], [1, 3, 5, 8, 11], [1, 3, 5, 9, 11], [1, 3, 6, 8, 11], + [1, 3, 6, 9, 11], [1, 3, 7, 9, 11], [1, 4, 6, 8, 11], [1, 4, 6, 9, 11], + [1, 4, 7, 9, 11], [1, 5, 7, 9, 11], [1, 3, 5, 7, 9, 11], [2], [2, 4], [2, 5], + [2, 6], [2, 4, 6], [2, 7], [2, 4, 7], [2, 5, 7], [2, 8], [2, 4, 8], [2, 5, 8], + [2, 6, 8], [2, 4, 6, 8], [2, 9], [2, 4, 9], [2, 5, 9], [2, 6, 9], [2, 7, 9], + [2, 4, 6, 9], [2, 4, 7, 9], [2, 5, 7, 9], [2, 10], [2, 4, 10], [2, 5, 10], + [2, 6, 10], [2, 7, 10], [2, 8, 10], [2, 4, 6, 10], [2, 4, 7, 10], + [2, 4, 8, 10], [2, 5, 7, 10], [2, 5, 8, 10], [2, 6, 8, 10], [2, 4, 6, 8, 10], + [2, 11], [2, 4, 11], [2, 5, 11], [2, 6, 11], [2, 7, 11], [2, 8, 11], + [2, 9, 11], [2, 4, 6, 11], [2, 4, 7, 11], [2, 4, 8, 11], [2, 4, 9, 11], + [2, 5, 7, 11], [2, 5, 8, 11], [2, 5, 9, 11], [2, 6, 8, 11], [2, 6, 9, 11], + [2, 7, 9, 11], [2, 4, 6, 8, 11], [2, 4, 6, 9, 11], [2, 4, 7, 9, 11], + [2, 5, 7, 9, 11], [3], [3, 5], [3, 6], [3, 7], [3, 5, 7], [3, 8], [3, 5, 8], + [3, 6, 8], [3, 9], [3, 5, 9], [3, 6, 9], [3, 7, 9], [3, 5, 7, 9], [3, 10], + [3, 5, 10], [3, 6, 10], [3, 7, 10], [3, 8, 10], [3, 5, 7, 10], [3, 5, 8, 10], + [3, 6, 8, 10], [3, 11], [3, 5, 11], [3, 6, 11], [3, 7, 11], [3, 8, 11], + [3, 9, 11], [3, 5, 7, 11], [3, 5, 8, 11], [3, 5, 9, 11], [3, 6, 8, 11], + [3, 6, 9, 11], [3, 7, 9, 11], [3, 5, 7, 9, 11], [4], [4, 6], [4, 7], [4, 8], + [4, 6, 8], [4, 9], [4, 6, 9], [4, 7, 9], [4, 10], [4, 6, 10], [4, 7, 10], + [4, 8, 10], [4, 6, 8, 10], [4, 11], [4, 6, 11], [4, 7, 11], [4, 8, 11], + [4, 9, 11], [4, 6, 8, 11], [4, 6, 9, 11], [4, 7, 9, 11], [5], [5, 7], [5, 8], + [5, 9], [5, 7, 9], [5, 10], [5, 7, 10], [5, 8, 10], [5, 11], [5, 7, 11], + [5, 8, 11], [5, 9, 11], [5, 7, 9, 11], [6], [6, 8], [6, 9], [6, 10], + [6, 8, 10], [6, 11], [6, 8, 11], [6, 9, 11], [7], [7, 9], [7, 10], [7, 11], + [7, 9, 11], [8], [8, 10], [8, 11], [9], [9, 11], [10], [11]] + +################################################################################### + +MIDI_Instruments_Families = { + 0: 'Piano Family', + 1: 'Chromatic Percussion Family', + 2: 'Organ Family', + 3: 'Guitar Family', + 4: 'Bass Family', + 5: 'Strings Family', + 6: 'Ensemble Family', + 7: 'Brass Family', + 8: 'Reed Family', + 9: 'Pipe Family', + 10: 'Synth Lead Family', + 11: 'Synth Pad Family', + 12: 'Synth Effects Family', + 13: 'Ethnic Family', + 14: 'Percussive Family', + 15: 'Sound Effects Family', + 16: 'Drums Family', + -1: 'Unknown Family', + } + +################################################################################### + +def patch_to_instrument_family(MIDI_patch, drums_patch=128): + + if 0 <= MIDI_patch < 128: + return MIDI_patch // 8, MIDI_Instruments_Families[MIDI_patch // 8] + + elif MIDI_patch == drums_patch: + return MIDI_patch // 8, MIDI_Instruments_Families[16] + + else: + return -1, MIDI_Instruments_Families[-1] + +################################################################################### + +def patch_list_from_enhanced_score_notes(enhanced_score_notes, + default_patch=0, + drums_patch=9, + verbose=False + ): + + patches = [-1] * 16 + + for idx, e in enumerate(enhanced_score_notes): + if e[3] != 9: + if patches[e[3]] == -1: + patches[e[3]] = e[6] + else: + if patches[e[3]] != e[6]: + if e[6] in patches: + e[3] = patches.index(e[6]) + else: + if -1 in patches: + patches[patches.index(-1)] = e[6] + else: + patches[-1] = e[6] + + if verbose: + print('=' * 70) + print('WARNING! Composition has more than 15 patches!') + print('Conflict note number:', idx) + print('Conflict channel number:', e[3]) + print('Conflict patch number:', e[6]) + + patches = [p if p != -1 else default_patch for p in patches] + + patches[9] = drums_patch + + if verbose: + print('=' * 70) + print('Composition patches') + print('=' * 70) + for c, p in enumerate(patches): + print('Cha', str(c).zfill(2), '---', str(p).zfill(3), Number2patch[p]) + print('=' * 70) + + return patches + +################################################################################### + +def patch_enhanced_score_notes(enhanced_score_notes, + default_patch=0, + drums_patch=9, + verbose=False + ): + + #=========================================================================== + + enhanced_score_notes_with_patch_changes = [] + + patches = [-1] * 16 + + overflow_idx = -1 + + for idx, e in enumerate(enhanced_score_notes): + if e[3] != 9: + if patches[e[3]] == -1: + patches[e[3]] = e[6] + else: + if patches[e[3]] != e[6]: + if e[6] in patches: + e[3] = patches.index(e[6]) + else: + if -1 in patches: + patches[patches.index(-1)] = e[6] + else: + overflow_idx = idx + break + + enhanced_score_notes_with_patch_changes.append(e) + + #=========================================================================== + + overflow_patches = [] + + if overflow_idx != -1: + for idx, e in enumerate(enhanced_score_notes[overflow_idx:]): + if e[3] != 9: + if e[6] not in patches: + if e[6] not in overflow_patches: + overflow_patches.append(e[6]) + enhanced_score_notes_with_patch_changes.append(['patch_change', e[1], e[3], e[6]]) + else: + e[3] = patches.index(e[6]) + + enhanced_score_notes_with_patch_changes.append(e) + + #=========================================================================== + + patches = [p if p != -1 else default_patch for p in patches] + + patches[9] = drums_patch + + #=========================================================================== + + if verbose: + print('=' * 70) + print('Composition patches') + print('=' * 70) + for c, p in enumerate(patches): + print('Cha', str(c).zfill(2), '---', str(p).zfill(3), Number2patch[p]) + print('=' * 70) + + if overflow_patches: + print('Extra composition patches') + print('=' * 70) + for c, p in enumerate(overflow_patches): + print(str(p).zfill(3), Number2patch[p]) + print('=' * 70) + + return enhanced_score_notes_with_patch_changes, patches, overflow_patches + +################################################################################### + +def create_enhanced_monophonic_melody(monophonic_melody): + + enhanced_monophonic_melody = [] + + for i, note in enumerate(monophonic_melody[:-1]): + + enhanced_monophonic_melody.append(note) + + if note[1]+note[2] < monophonic_melody[i+1][1]: + + delta_time = monophonic_melody[i+1][1] - (note[1]+note[2]) + enhanced_monophonic_melody.append(['silence', note[1]+note[2], delta_time, note[3], 0, 0, note[6]]) + + enhanced_monophonic_melody.append(monophonic_melody[-1]) + + return enhanced_monophonic_melody + +################################################################################### + +def frame_monophonic_melody(monophonic_melody, min_frame_time_threshold=10): + + mzip = list(zip(monophonic_melody[:-1], monophonic_melody[1:])) + + times_counts = Counter([(b[1]-a[1]) for a, b in mzip]).most_common() + + mc_time = next((item for item, count in times_counts if item >= min_frame_time_threshold), min_frame_time_threshold) + + times = [(b[1]-a[1]) // mc_time for a, b in mzip] + [monophonic_melody[-1][2] // mc_time] + + framed_melody = [] + + for i, note in enumerate(monophonic_melody): + + stime = note[1] + count = times[i] + + if count != 0: + for j in range(count): + + new_note = copy.deepcopy(note) + new_note[1] = stime + (j * mc_time) + new_note[2] = mc_time + framed_melody.append(new_note) + + else: + framed_melody.append(note) + + return [framed_melody, mc_time] + +################################################################################### + +def delta_score_notes(score_notes, + timings_clip_value=255, + even_timings=False, + compress_timings=False + ): + + delta_score = [] + + pe = score_notes[0] + + for n in score_notes: + + note = copy.deepcopy(n) + + time = n[1] - pe[1] + dur = n[2] + + if even_timings: + if time != 0 and time % 2 != 0: + time += 1 + if dur % 2 != 0: + dur += 1 + + time = max(0, min(timings_clip_value, time)) + dur = max(0, min(timings_clip_value, dur)) + + if compress_timings: + time /= 2 + dur /= 2 + + note[1] = int(time) + note[2] = int(dur) + + delta_score.append(note) + + pe = n + + return delta_score + +################################################################################### + +def check_and_fix_chords_in_chordified_score(chordified_score, + channels_index=3, + pitches_index=4 + ): + fixed_chordified_score = [] + + bad_chords_counter = 0 + + for c in chordified_score: + + tones_chord = sorted(set([t[pitches_index] % 12 for t in c if t[channels_index] != 9])) + + if tones_chord: + + if tones_chord not in ALL_CHORDS_SORTED: + bad_chords_counter += 1 + + while tones_chord not in ALL_CHORDS_SORTED: + tones_chord.pop(0) + + new_chord = [] + + c.sort(key = lambda x: x[pitches_index], reverse=True) + + for e in c: + if e[channels_index] != 9: + if e[pitches_index] % 12 in tones_chord: + new_chord.append(e) + + else: + new_chord.append(e) + + fixed_chordified_score.append(new_chord) + + return fixed_chordified_score, bad_chords_counter + +################################################################################### + +from itertools import combinations, groupby + +################################################################################### + +def advanced_check_and_fix_chords_in_chordified_score(chordified_score, + channels_index=3, + pitches_index=4, + patches_index=6, + use_filtered_chords=True, + remove_duplicate_pitches=True, + skip_drums=False + ): + fixed_chordified_score = [] + + bad_chords_counter = 0 + duplicate_pitches_counter = 0 + + if use_filtered_chords: + CHORDS = ALL_CHORDS_FILTERED + else: + CHORDS = ALL_CHORDS_SORTED + + for c in chordified_score: + + if remove_duplicate_pitches: + + c.sort(key = lambda x: x[pitches_index], reverse=True) + + seen = set() + ddchord = [] + + for cc in c: + if cc[channels_index] != 9: + + if tuple([cc[pitches_index], cc[patches_index]]) not in seen: + ddchord.append(cc) + seen.add(tuple([cc[pitches_index], cc[patches_index]])) + else: + duplicate_pitches_counter += 1 + + else: + ddchord.append(cc) + + c = copy.deepcopy(ddchord) + + tones_chord = sorted(set([t[pitches_index] % 12 for t in c if t[channels_index] != 9])) + + if tones_chord: + + if tones_chord not in CHORDS: + + pitches_chord = sorted(set([p[pitches_index] for p in c if p[channels_index] != 9]), reverse=True) + + if len(tones_chord) == 2: + tones_counts = Counter([p % 12 for p in pitches_chord]).most_common() + + if tones_counts[0][1] > 1: + tones_chord = [tones_counts[0][0]] + elif tones_counts[1][1] > 1: + tones_chord = [tones_counts[1][0]] + else: + tones_chord = [pitches_chord[0] % 12] + + else: + tones_chord_combs = [list(comb) for i in range(len(tones_chord)-2, 0, -1) for comb in combinations(tones_chord, i+1)] + + for co in tones_chord_combs: + if co in CHORDS: + tones_chord = co + break + + bad_chords_counter += 1 + + new_chord = [] + + c.sort(key = lambda x: x[pitches_index], reverse=True) + + for e in c: + if e[channels_index] != 9: + if e[pitches_index] % 12 in tones_chord: + new_chord.append(e) + + else: + if not skip_drums: + new_chord.append(e) + + fixed_chordified_score.append(new_chord) + + return fixed_chordified_score, bad_chords_counter, duplicate_pitches_counter + +################################################################################### + +def score_chord_to_tones_chord(chord, + transpose_value=0, + channels_index=3, + pitches_index=4): + + return sorted(set([(p[4]+transpose_value) % 12 for p in chord if p[channels_index] != 9])) + +################################################################################### + +def grouped_set(seq): + return [k for k, v in groupby(seq)] + +################################################################################### + +def ordered_set(seq): + dic = {} + return [k for k, v in dic.fromkeys(seq).items()] + +################################################################################### + +def add_melody_to_enhanced_score_notes(enhanced_score_notes, + melody_start_time=0, + melody_start_chord=0, + melody_notes_min_duration=-1, + melody_notes_max_duration=255, + melody_duration_overlap_tolerance=4, + melody_avg_duration_divider=2, + melody_base_octave=5, + melody_channel=3, + melody_patch=40, + melody_max_velocity=110, + acc_max_velocity=90, + pass_drums=True + ): + + if pass_drums: + score = copy.deepcopy(enhanced_score_notes) + else: + score = [e for e in copy.deepcopy(enhanced_score_notes) if e[3] !=9] + + if melody_notes_min_duration > 0: + min_duration = melody_notes_min_duration + else: + durs = [d[2] for d in score] + min_duration = Counter(durs).most_common()[0][0] + + adjust_score_velocities(score, acc_max_velocity) + + cscore = chordify_score([1000, score]) + + melody_score = [] + acc_score = [] + + pt = melody_start_time + + for c in cscore[:melody_start_chord]: + acc_score.extend(c) + + for c in cscore[melody_start_chord:]: + + durs = [d[2] if d[3] != 9 else -1 for d in c] + + if not all(d == -1 for d in durs): + ndurs = [d for d in durs if d != -1] + avg_dur = (sum(ndurs) / len(ndurs)) / melody_avg_duration_divider + best_dur = min(durs, key=lambda x:abs(x-avg_dur)) + pidx = durs.index(best_dur) + + cc = copy.deepcopy(c[pidx]) + + if c[0][1] >= pt - melody_duration_overlap_tolerance and best_dur >= min_duration: + + cc[3] = melody_channel + cc[4] = (c[pidx][4] % 24) + cc[5] = 100 + ((c[pidx][4] % 12) * 2) + cc[6] = melody_patch + + melody_score.append(cc) + acc_score.extend(c) + + pt = c[0][1]+c[pidx][2] + + else: + acc_score.extend(c) + + else: + acc_score.extend(c) + + values = [e[4] % 24 for e in melody_score] + smoothed = [values[0]] + for i in range(1, len(values)): + if abs(smoothed[-1] - values[i]) >= 12: + if smoothed[-1] < values[i]: + smoothed.append(values[i] - 12) + else: + smoothed.append(values[i] + 12) + else: + smoothed.append(values[i]) + + smoothed_melody = copy.deepcopy(melody_score) + + for i, e in enumerate(smoothed_melody): + e[4] = (melody_base_octave * 12) + smoothed[i] + + for i, m in enumerate(smoothed_melody[1:]): + if m[1] - smoothed_melody[i][1] < melody_notes_max_duration: + smoothed_melody[i][2] = m[1] - smoothed_melody[i][1] + + adjust_score_velocities(smoothed_melody, melody_max_velocity) + + final_score = sorted(smoothed_melody + acc_score, key=lambda x: (x[1], -x[4])) + + return final_score + +################################################################################### + +def find_paths(list_of_lists, path=[]): + if not list_of_lists: + return [path] + return [p for sublist in list_of_lists[0] for p in find_paths(list_of_lists[1:], path+[sublist])] + +################################################################################### + +def recalculate_score_timings(score, start_time=0): + + rscore = copy.deepcopy(score) + + pe = rscore[0] + + abs_time = start_time + + for e in rscore: + + dtime = e[1] - pe[1] + pe = copy.deepcopy(e) + abs_time += dtime + e[1] = abs_time + + return rscore + +################################################################################### + +WHITE_NOTES = [0, 2, 4, 5, 7, 9, 11] +BLACK_NOTES = [1, 3, 6, 8, 10] + +################################################################################### + +ALL_CHORDS_FILTERED = [[0], [0, 3], [0, 3, 5], [0, 3, 5, 8], [0, 3, 5, 9], [0, 3, 5, 10], [0, 3, 7], + [0, 3, 7, 10], [0, 3, 8], [0, 3, 9], [0, 3, 10], [0, 4], [0, 4, 6], + [0, 4, 6, 9], [0, 4, 6, 10], [0, 4, 7], [0, 4, 7, 10], [0, 4, 8], [0, 4, 9], + [0, 4, 10], [0, 5], [0, 5, 8], [0, 5, 9], [0, 5, 10], [0, 6], [0, 6, 9], + [0, 6, 10], [0, 7], [0, 7, 10], [0, 8], [0, 9], [0, 10], [1], [1, 4], + [1, 4, 6], [1, 4, 6, 9], [1, 4, 6, 10], [1, 4, 6, 11], [1, 4, 7], + [1, 4, 7, 10], [1, 4, 7, 11], [1, 4, 8], [1, 4, 8, 11], [1, 4, 9], [1, 4, 10], + [1, 4, 11], [1, 5], [1, 5, 8], [1, 5, 8, 11], [1, 5, 9], [1, 5, 10], + [1, 5, 11], [1, 6], [1, 6, 9], [1, 6, 10], [1, 6, 11], [1, 7], [1, 7, 10], + [1, 7, 11], [1, 8], [1, 8, 11], [1, 9], [1, 10], [1, 11], [2], [2, 5], + [2, 5, 8], [2, 5, 8, 11], [2, 5, 9], [2, 5, 10], [2, 5, 11], [2, 6], [2, 6, 9], + [2, 6, 10], [2, 6, 11], [2, 7], [2, 7, 10], [2, 7, 11], [2, 8], [2, 8, 11], + [2, 9], [2, 10], [2, 11], [3], [3, 5], [3, 5, 8], [3, 5, 8, 11], [3, 5, 9], + [3, 5, 10], [3, 5, 11], [3, 7], [3, 7, 10], [3, 7, 11], [3, 8], [3, 8, 11], + [3, 9], [3, 10], [3, 11], [4], [4, 6], [4, 6, 9], [4, 6, 10], [4, 6, 11], + [4, 7], [4, 7, 10], [4, 7, 11], [4, 8], [4, 8, 11], [4, 9], [4, 10], [4, 11], + [5], [5, 8], [5, 8, 11], [5, 9], [5, 10], [5, 11], [6], [6, 9], [6, 10], + [6, 11], [7], [7, 10], [7, 11], [8], [8, 11], [9], [10], [11]] + +################################################################################### + +def harmonize_enhanced_melody_score_notes(enhanced_melody_score_notes): + + mel_tones = [e[4] % 12 for e in enhanced_melody_score_notes] + + cur_chord = [] + + song = [] + + for i, m in enumerate(mel_tones): + cur_chord.append(m) + cc = sorted(set(cur_chord)) + + if cc in ALL_CHORDS_FILTERED: + song.append(cc) + + else: + while sorted(set(cur_chord)) not in ALL_CHORDS_FILTERED: + cur_chord.pop(0) + cc = sorted(set(cur_chord)) + song.append(cc) + + return song + +################################################################################### + +def split_melody(enhanced_melody_score_notes, + split_time=-1, + max_score_time=255 + ): + + mel_chunks = [] + + if split_time == -1: + + durs = [max(0, min(max_score_time, e[2])) for e in enhanced_melody_score_notes] + stime = max(durs) + + else: + stime = split_time + + pe = enhanced_melody_score_notes[0] + chu = [] + + for e in enhanced_melody_score_notes: + dtime = max(0, min(max_score_time, e[1]-pe[1])) + + if dtime > max(durs): + if chu: + mel_chunks.append(chu) + chu = [] + chu.append(e) + else: + chu.append(e) + + pe = e + + if chu: + mel_chunks.append(chu) + + return mel_chunks, [[m[0][1], m[-1][1]] for m in mel_chunks], len(mel_chunks) + +################################################################################### + +def flatten(list_of_lists): + return [x for y in list_of_lists for x in y] + +################################################################################### + +def enhanced_delta_score_notes(enhanced_score_notes, + start_time=0, + max_score_time=255 + ): + + delta_score = [] + + pe = ['note', max(0, enhanced_score_notes[0][1]-start_time)] + + for e in enhanced_score_notes: + + dtime = max(0, min(max_score_time, e[1]-pe[1])) + dur = max(1, min(max_score_time, e[2])) + cha = max(0, min(15, e[3])) + ptc = max(1, min(127, e[4])) + vel = max(1, min(127, e[5])) + pat = max(0, min(128, e[6])) + + delta_score.append([dtime, dur, cha, ptc, vel, pat]) + + pe = e + + return delta_score + +################################################################################### + +def basic_enhanced_delta_score_notes_tokenizer(enhanced_delta_score_notes, + tokenize_start_times=True, + tokenize_durations=True, + tokenize_channels=True, + tokenize_pitches=True, + tokenize_velocities=True, + tokenize_patches=True, + score_timings_range=256, + max_seq_len=-1, + seq_pad_value=-1 + ): + + + + score_tokens_ints_seq = [] + + tokens_shifts = [-1] * 7 + + for d in enhanced_delta_score_notes: + + seq = [] + shift = 0 + + if tokenize_start_times: + seq.append(d[0]) + tokens_shifts[0] = shift + shift += score_timings_range + + if tokenize_durations: + seq.append(d[1]+shift) + tokens_shifts[1] = shift + shift += score_timings_range + + if tokenize_channels: + tokens_shifts[2] = shift + seq.append(d[2]+shift) + shift += 16 + + if tokenize_pitches: + tokens_shifts[3] = shift + seq.append(d[3]+shift) + shift += 128 + + if tokenize_velocities: + tokens_shifts[4] = shift + seq.append(d[4]+shift) + shift += 128 + + if tokenize_patches: + tokens_shifts[5] = shift + seq.append(d[5]+shift) + shift += 129 + + tokens_shifts[6] = shift + score_tokens_ints_seq.append(seq) + + final_score_tokens_ints_seq = flatten(score_tokens_ints_seq) + + if max_seq_len > -1: + final_score_tokens_ints_seq = flat_score_tokens_ints_seq[:max_seq_len] + + if seq_pad_value > -1: + final_score_tokens_ints_seq += [seq_pad_value] * (max_seq_len - len(final_score_tokens_ints_seq)) + + return [score_tokens_ints_seq, + final_score_tokens_ints_seq, + tokens_shifts, + seq_pad_value, + max_seq_len, + len(score_tokens_ints_seq), + len(final_score_tokens_ints_seq) + ] + +################################################################################### + +def basic_enhanced_delta_score_notes_detokenizer(tokenized_seq, + tokens_shifts, + timings_multiplier=16 + ): + + song_f = [] + + time = 0 + dur = 16 + channel = 0 + pitch = 60 + vel = 90 + pat = 0 + + note_seq_len = len([t for t in tokens_shifts if t > -1])-1 + tok_shifts_idxs = [i for i in range(len(tokens_shifts[:-1])) if tokens_shifts[i] > - 1] + + song = [] + + for i in range(0, len(tokenized_seq), note_seq_len): + note = tokenized_seq[i:i+note_seq_len] + song.append(note) + + for note in song: + for i, idx in enumerate(tok_shifts_idxs): + if idx == 0: + time += (note[i]-tokens_shifts[0]) * timings_multiplier + elif idx == 1: + dur = (note[i]-tokens_shifts[1]) * timings_multiplier + elif idx == 2: + channel = (note[i]-tokens_shifts[2]) + elif idx == 3: + pitch = (note[i]-tokens_shifts[3]) + elif idx == 4: + vel = (note[i]-tokens_shifts[4]) + elif idx == 5: + pat = (note[i]-tokens_shifts[5]) + + song_f.append(['note', time, dur, channel, pitch, vel, pat ]) + + return song_f + +################################################################################### + +def enhanced_chord_to_chord_token(enhanced_chord, + channels_index=3, + pitches_index=4, + use_filtered_chords=True + ): + + bad_chords_counter = 0 + duplicate_pitches_counter = 0 + + if use_filtered_chords: + CHORDS = ALL_CHORDS_FILTERED + else: + CHORDS = ALL_CHORDS_SORTED + + tones_chord = sorted(set([t[pitches_index] % 12 for t in enhanced_chord if t[channels_index] != 9])) + + original_tones_chord = copy.deepcopy(tones_chord) + + if tones_chord: + + if tones_chord not in CHORDS: + + pitches_chord = sorted(set([p[pitches_index] for p in enhanced_chord if p[channels_index] != 9]), reverse=True) + + if len(tones_chord) == 2: + tones_counts = Counter([p % 12 for p in pitches_chord]).most_common() + + if tones_counts[0][1] > 1: + tones_chord = [tones_counts[0][0]] + elif tones_counts[1][1] > 1: + tones_chord = [tones_counts[1][0]] + else: + tones_chord = [pitches_chord[0] % 12] + + else: + tones_chord_combs = [list(comb) for i in range(len(tones_chord)-2, 0, -1) for comb in combinations(tones_chord, i+1)] + + for co in tones_chord_combs: + if co in CHORDS: + tones_chord = co + break + + if use_filtered_chords: + chord_token = ALL_CHORDS_FILTERED.index(tones_chord) + else: + chord_token = ALL_CHORDS_SORTED.index(tones_chord) + + return [chord_token, tones_chord, original_tones_chord, sorted(set(original_tones_chord) ^ set(tones_chord))] + +################################################################################### + +def enhanced_chord_to_tones_chord(enhanced_chord): + return sorted(set([t[4] % 12 for t in enhanced_chord if t[3] != 9])) + +################################################################################### + +import hashlib + +################################################################################### + +def md5_hash(file_path_or_data=None, original_md5_hash=None): + + if type(file_path_or_data) == str: + + with open(file_path_or_data, 'rb') as file_to_check: + data = file_to_check.read() + + if data: + md5 = hashlib.md5(data).hexdigest() + + else: + if file_path_or_data: + md5 = hashlib.md5(file_path_or_data).hexdigest() + + if md5: + + if original_md5_hash: + + if md5 == original_md5_hash: + check = True + else: + check = False + + else: + check = None + + return [md5, check] + + else: + + md5 = None + check = None + + return [md5, check] + +################################################################################### + +ALL_PITCHES_CHORDS_FILTERED = [[67], [64], [62], [69], [60], [65], [59], [70], [66], [63], [68], [61], + [64, 60], [67, 64], [65, 62], [62, 59], [69, 65], [60, 57], [66, 62], [59, 55], + [62, 57], [67, 62], [64, 59], [64, 60, 55], [60, 55], [65, 60], [64, 61], + [69, 64], [66, 62, 57], [69, 66], [62, 59, 55], [64, 60, 57], [62, 58], + [65, 60, 57], [70, 67], [67, 63], [64, 61, 57], [61, 57], [63, 60], [68, 64], + [65, 62, 58], [65, 62, 57], [59, 56], [63, 58], [68, 65], [59, 54, 47, 35], + [70, 65], [66, 61], [64, 59, 56], [65, 61], [64, 59, 55], [63, 59], [61, 58], + [68, 63], [60, 56], [67, 63, 60], [67, 63, 58], [66, 62, 59], [61, 56], + [70, 66], [67, 62, 58], [63, 60, 56], [65, 61, 56], [66, 61, 58], [66, 61, 57], + [65, 60, 56], [65, 61, 58], [65, 59], [68, 64, 61], [66, 60], [64, 58], + [62, 56], [63, 57], [61, 55], [66, 64], [60, 58], [65, 63], [63, 59, 56], + [65, 62, 59], [61, 59], [66, 60, 57], [64, 61, 55], [64, 58, 55], [62, 59, 56], + [64, 60, 58], [63, 60, 57], [64, 60, 58, 55], [65, 62, 56], [64, 61, 58], + [66, 64, 59], [60, 58, 55], [65, 63, 60], [63, 57, 53], [65, 63, 60, 57], + [65, 59, 56], [63, 60, 58, 55], [67, 61, 58], [64, 61, 57, 54], [64, 61, 59], + [70, 65, 60], [68, 65, 63, 60], [63, 60, 58], [65, 63, 58], [69, 66, 64], + [64, 60, 54], [64, 60, 57, 54], [66, 64, 61], [66, 61, 59], [67, 63, 59], + [65, 61, 57], [68, 65, 63], [64, 61, 59, 56], [65, 61, 59], [66, 64, 61, 58], + [64, 61, 58, 55], [64, 60, 56], [65, 61, 59, 56], [66, 62, 58], [61, 59, 56], + [64, 58, 54], [63, 59, 53], [65, 62, 59, 56], [61, 59, 55], [64, 61, 59, 55], + [68, 65, 63, 59], [70, 66, 60], [65, 63, 60, 58], [64, 61, 59, 54], + [70, 64, 60, 54]] + +################################################################################### + +ALL_PITCHES_CHORDS_SORTED = [[60], [62, 60], [63, 60], [64, 60], [64, 62, 60], [65, 60], [65, 62, 60], + [65, 63, 60], [66, 60], [66, 62, 60], [66, 63, 60], [64, 60, 54], + [64, 60, 54, 50], [60, 55], [67, 62, 60], [67, 63, 60], [64, 60, 55], + [65, 60, 55], [64, 62, 60, 55], [67, 65, 62, 60], [67, 65, 63, 60], [60, 56], + [62, 60, 56], [63, 60, 56], [64, 60, 56], [65, 60, 56], [66, 60, 56], + [72, 68, 64, 62], [65, 62, 60, 56], [66, 62, 60, 56], [68, 65, 63, 60], + [68, 66, 63, 60], [60, 44, 42, 40], [88, 80, 74, 66, 60, 56], [60, 57], + [62, 60, 57], [63, 60, 57], [64, 60, 57], [65, 60, 57], [66, 60, 57], + [67, 60, 57], [64, 62, 60, 57], [65, 62, 60, 57], [69, 66, 62, 60], + [67, 62, 60, 57], [65, 63, 60, 57], [66, 63, 60, 57], [67, 63, 60, 57], + [64, 60, 57, 54], [67, 64, 60, 57], [67, 65, 60, 57], [69, 64, 60, 54, 38], + [67, 64, 62, 60, 57], [67, 65, 62, 60, 57], [67, 65, 63, 60, 57], [60, 58], + [62, 60, 58], [63, 60, 58], [64, 60, 58], [70, 65, 60], [70, 66, 60], + [60, 58, 55], [70, 60, 56], [74, 64, 60, 58], [65, 62, 60, 58], + [70, 66, 62, 60], [62, 60, 58, 55], [72, 68, 62, 58], [65, 63, 60, 58], + [70, 66, 63, 60], [63, 60, 58, 55], [70, 63, 60, 56], [70, 64, 60, 54], + [64, 60, 58, 55], [68, 64, 60, 58], [65, 60, 58, 55], [70, 65, 60, 56], + [70, 66, 60, 56], [78, 76, 74, 72, 70, 66], [67, 64, 62, 58, 36], + [74, 68, 64, 58, 48], [65, 62, 58, 55, 36], [65, 62, 60, 56, 46], + [72, 66, 62, 56, 46], [79, 65, 63, 58, 53, 36], [65, 60, 56, 51, 46, 41], + [70, 66, 63, 60, 44], [68, 66, 64, 58, 56, 48], + [94, 92, 90, 88, 86, 84, 82, 80, 78, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, + 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24], + [61], [63, 61], [64, 61], [65, 61], [65, 63, 61], [66, 61], [66, 63, 61], + [66, 64, 61], [61, 55], [67, 63, 61], [64, 61, 55], [65, 61, 55], + [65, 61, 55, 39], [61, 56], [63, 61, 56], [68, 64, 61], [65, 61, 56], + [66, 61, 56], [68, 65, 63, 61], [54, 49, 44, 39], [68, 64, 61, 42], [61, 57], + [63, 61, 57], [64, 61, 57], [65, 61, 57], [66, 61, 57], [67, 61, 57], + [69, 65, 63, 61], [66, 63, 61, 57], [67, 63, 61, 57], [64, 61, 57, 54], + [67, 64, 61, 57], [65, 61, 55, 45], [67, 65, 63, 61, 57], [61, 58], + [63, 61, 58], [64, 61, 58], [65, 61, 58], [66, 61, 58], [67, 61, 58], + [61, 58, 56], [65, 63, 61, 58], [66, 63, 61, 58], [67, 63, 61, 58], + [63, 61, 58, 56], [66, 64, 61, 58], [64, 61, 58, 55], [68, 64, 61, 58], + [65, 61, 58, 55], [65, 61, 58, 56], [58, 54, 49, 44], [70, 65, 61, 55, 39], + [80, 68, 65, 63, 61, 58], [63, 58, 54, 49, 44, 39], [73, 68, 64, 58, 54], + [61, 59], [63, 61, 59], [64, 61, 59], [65, 61, 59], [66, 61, 59], [61, 59, 55], + [61, 59, 56], [61, 59, 57], [63, 59, 53, 49], [66, 63, 61, 59], + [71, 67, 63, 61], [63, 61, 59, 56], [61, 57, 51, 47], [64, 61, 59, 54], + [64, 61, 59, 55], [64, 61, 59, 56], [64, 61, 59, 57], [65, 61, 59, 55], + [65, 61, 59, 56], [69, 65, 61, 59], [66, 61, 59, 56], [71, 66, 61, 57], + [71, 67, 61, 57], [67, 63, 59, 53, 49], [68, 65, 63, 59, 37], + [65, 63, 61, 59, 57], [66, 63, 61, 59, 56], [73, 69, 66, 63, 59], + [79, 75, 73, 61, 59, 33], [61, 56, 52, 47, 42, 35], [76, 73, 69, 66, 35], + [71, 67, 64, 61, 57], [73, 71, 69, 67, 65], + [95, 93, 91, 89, 87, 85, 83, 81, 79, 77, 75, 73, 71, 69, 67, 65, 63, 61, 59, + 57, 55, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25], + [62], [64, 62], [65, 62], [66, 62], [66, 64, 62], [67, 62], [67, 64, 62], + [67, 65, 62], [62, 56], [68, 64, 62], [65, 62, 56], [66, 62, 56], + [66, 62, 56, 52], [62, 57], [50, 45, 40], [65, 62, 57], [66, 62, 57], + [55, 50, 45], [66, 64, 62, 57], [55, 50, 45, 40], [69, 67, 65, 62], [62, 58], + [64, 62, 58], [65, 62, 58], [66, 62, 58], [67, 62, 58], [62, 58, 56], + [66, 64, 62, 58], [67, 64, 62, 58], [64, 62, 58, 56], [65, 62, 58, 55], + [65, 62, 58, 56], [66, 62, 58, 56], [66, 64, 58, 44, 38], [62, 59], + [64, 62, 59], [65, 62, 59], [66, 62, 59], [62, 59, 55], [62, 59, 56], + [62, 59, 57], [66, 64, 62, 59], [67, 64, 62, 59], [64, 62, 59, 56], + [64, 62, 59, 57], [67, 65, 62, 59], [65, 62, 59, 56], [69, 65, 62, 59], + [66, 62, 59, 56], [69, 66, 62, 59], [59, 55, 50, 45], [64, 62, 59, 56, 54], + [69, 66, 62, 59, 40], [64, 59, 55, 50, 45, 40], [69, 65, 62, 59, 55], [63], + [65, 63], [66, 63], [67, 63], [67, 65, 63], [68, 63], [68, 65, 63], + [68, 66, 63], [63, 57], [63, 57, 53], [66, 63, 57], [67, 63, 57], + [67, 63, 57, 53], [63, 58], [65, 63, 58], [66, 63, 58], [67, 63, 58], + [68, 63, 58], [67, 65, 63, 58], [63, 58, 56, 53], [70, 68, 66, 63], [63, 59], + [63, 59, 53], [66, 63, 59], [67, 63, 59], [63, 59, 56], [63, 59, 57], + [63, 59, 55, 53], [68, 65, 63, 59], [69, 65, 63, 59], [66, 63, 59, 56], + [66, 63, 59, 57], [67, 63, 59, 57], [67, 63, 59, 57, 41], [64], [66, 64], + [67, 64], [68, 64], [68, 66, 64], [69, 64], [69, 66, 64], [69, 67, 64], + [64, 58], [64, 58, 54], [64, 58, 55], [68, 64, 58], [68, 64, 58, 42], [64, 59], + [66, 64, 59], [64, 59, 55], [64, 59, 56], [64, 59, 57], [64, 59, 56, 54], + [64, 59, 57, 54], [69, 64, 59, 55], [65], [67, 65], [68, 65], [69, 65], + [69, 67, 65], [70, 65], [65, 58, 55], [70, 68, 65], [65, 59], [65, 59, 55], + [65, 59, 56], [59, 57, 53], [69, 65, 59, 55], [66], [68, 66], [69, 66], + [70, 66], [80, 70, 54], [59, 54, 47, 35], [66, 59, 56], [71, 69, 66], [67], + [69, 67], [70, 67], [59, 55], [71, 69, 67], [68], [70, 68], [59, 56], [69], + [71, 69], [70], [59]] + +################################################################################### + +def sort_list_by_other(list1, list2): + return sorted(list1, key=lambda x: list2.index(x) if x in list2 else len(list2)) + +################################################################################### + +ALL_CHORDS_PAIRS_SORTED = [[[0], [0, 4, 7]], [[0, 2], [0, 4, 7]], [[0, 3], [0, 3, 7]], + [[0, 4], [0, 4, 7]], [[0, 2, 4], [0, 2, 4, 7]], [[0, 5], [0, 5, 9]], + [[0, 2, 5], [0, 2, 5, 9]], [[0, 3, 5], [0, 3, 5, 9]], [[0, 6], [0, 2, 6, 9]], + [[0, 2, 6], [0, 2, 6, 9]], [[0, 3, 6], [0, 3, 6, 8]], + [[0, 4, 6], [0, 4, 6, 9]], [[0, 2, 4, 6], [0, 2, 4, 6, 9]], + [[0, 7], [0, 4, 7]], [[0, 2, 7], [0, 2, 4, 7]], [[0, 3, 7], [0, 3, 7, 10]], + [[0, 4, 7], [0, 4, 7, 9]], [[0, 5, 7], [0, 5, 7, 9]], + [[0, 2, 4, 7], [0, 2, 4, 7, 9]], [[0, 2, 5, 7], [0, 2, 5, 7, 9]], + [[0, 3, 5, 7], [0, 3, 5, 7, 10]], [[0, 8], [0, 3, 8]], + [[0, 2, 8], [0, 2, 5, 8]], [[0, 3, 8], [0, 3, 5, 8]], + [[0, 4, 8], [2, 4, 8, 11]], [[0, 5, 8], [0, 3, 5, 8]], + [[0, 6, 8], [0, 3, 6, 8]], [[0, 2, 4, 8], [0, 2, 4, 6, 8]], + [[0, 2, 5, 8], [0, 2, 5, 8, 10]], [[0, 2, 6, 8], [0, 2, 6, 8, 10]], + [[0, 3, 5, 8], [0, 3, 5, 8, 10]], [[0, 3, 6, 8], [0, 3, 6, 8, 10]], + [[0, 4, 6, 8], [2, 4, 6, 8, 11]], [[0, 2, 4, 6, 8], [2, 4, 6, 8, 11]], + [[0, 9], [0, 4, 9]], [[0, 2, 9], [0, 2, 6, 9]], [[0, 3, 9], [0, 3, 5, 9]], + [[0, 4, 9], [0, 4, 7, 9]], [[0, 5, 9], [0, 2, 5, 9]], + [[0, 6, 9], [0, 2, 6, 9]], [[0, 7, 9], [0, 4, 7, 9]], + [[0, 2, 4, 9], [0, 2, 4, 7, 9]], [[0, 2, 5, 9], [0, 2, 5, 7, 9]], + [[0, 2, 6, 9], [0, 2, 4, 6, 9]], [[0, 2, 7, 9], [0, 2, 4, 7, 9]], + [[0, 3, 5, 9], [0, 3, 5, 7, 9]], [[0, 3, 6, 9], [0, 2, 4, 6, 9]], + [[0, 3, 7, 9], [0, 3, 5, 7, 9]], [[0, 4, 6, 9], [0, 2, 4, 6, 9]], + [[0, 4, 7, 9], [0, 2, 4, 7, 9]], [[0, 5, 7, 9], [0, 2, 5, 7, 9]], + [[0, 2, 4, 6, 9], [2, 4, 6, 9, 11]], [[0, 2, 4, 7, 9], [2, 4, 7, 9, 11]], + [[0, 2, 5, 7, 9], [2, 5, 7, 9, 11]], [[0, 3, 5, 7, 9], [2, 4, 6, 8, 11]], + [[0, 10], [2, 5, 10]], [[0, 2, 10], [0, 2, 5, 10]], + [[0, 3, 10], [0, 3, 7, 10]], [[0, 4, 10], [0, 4, 7, 10]], + [[0, 5, 10], [0, 2, 5, 10]], [[0, 6, 10], [0, 3, 6, 10]], + [[0, 7, 10], [0, 4, 7, 10]], [[0, 8, 10], [0, 3, 8, 10]], + [[0, 2, 4, 10], [0, 2, 4, 7, 10]], [[0, 2, 5, 10], [0, 2, 5, 7, 10]], + [[0, 2, 6, 10], [0, 2, 6, 8, 10]], [[0, 2, 7, 10], [0, 2, 5, 7, 10]], + [[0, 2, 8, 10], [0, 2, 5, 8, 10]], [[0, 3, 5, 10], [0, 3, 5, 7, 10]], + [[0, 3, 6, 10], [0, 3, 6, 8, 10]], [[0, 3, 7, 10], [0, 3, 5, 7, 10]], + [[0, 3, 8, 10], [0, 3, 5, 8, 10]], [[0, 4, 6, 10], [0, 2, 4, 6, 10]], + [[0, 4, 7, 10], [0, 2, 4, 7, 10]], [[0, 4, 8, 10], [0, 2, 4, 8, 10]], + [[0, 5, 7, 10], [0, 3, 5, 7, 10]], [[0, 5, 8, 10], [0, 3, 5, 8, 10]], + [[0, 6, 8, 10], [0, 3, 6, 8, 10]], [[0, 2, 4, 6, 10], [0, 2, 4, 8, 10]], + [[0, 2, 4, 7, 10], [1, 3, 6, 9, 11]], [[0, 2, 4, 8, 10], [1, 3, 7, 9, 11]], + [[0, 2, 5, 7, 10], [0, 3, 5, 7, 10]], [[0, 2, 5, 8, 10], [1, 4, 7, 9, 11]], + [[0, 2, 6, 8, 10], [2, 4, 6, 8, 10]], [[0, 3, 5, 7, 10], [0, 2, 5, 7, 10]], + [[0, 3, 5, 8, 10], [1, 3, 5, 8, 10]], [[0, 3, 6, 8, 10], [1, 3, 6, 8, 10]], + [[0, 4, 6, 8, 10], [0, 2, 4, 6, 9]], + [[0, 2, 4, 6, 8, 10], [1, 3, 5, 7, 9, 11]], [[1], [1, 8]], [[1, 3], [1, 5, 8]], + [[1, 4], [1, 4, 9]], [[1, 5], [1, 5, 8]], [[1, 3, 5], [1, 3, 5, 10]], + [[1, 6], [1, 6, 10]], [[1, 3, 6], [1, 3, 6, 10]], [[1, 4, 6], [1, 4, 6, 9]], + [[1, 7], [1, 4, 7]], [[1, 3, 7], [1, 3, 7, 10]], [[1, 4, 7], [1, 4, 7, 9]], + [[1, 5, 7], [1, 5, 7, 10]], [[1, 3, 5, 7], [1, 3, 5, 7, 10]], + [[1, 8], [1, 5, 8]], [[1, 3, 8], [1, 3, 5, 8]], [[1, 4, 8], [1, 4, 8, 11]], + [[1, 5, 8], [1, 5, 8, 10]], [[1, 6, 8], [1, 3, 6, 8]], + [[1, 3, 5, 8], [1, 3, 5, 8, 10]], [[1, 3, 6, 8], [1, 3, 6, 8, 10]], + [[1, 4, 6, 8], [1, 4, 6, 8, 11]], [[1, 9], [1, 4, 9]], + [[1, 3, 9], [1, 3, 6, 9]], [[1, 4, 9], [1, 4, 6, 9]], + [[1, 5, 9], [0, 3, 5, 9]], [[1, 6, 9], [1, 4, 6, 9]], + [[1, 7, 9], [1, 4, 7, 9]], [[1, 3, 5, 9], [0, 3, 5, 7, 9]], + [[1, 3, 6, 9], [1, 3, 6, 9, 11]], [[1, 3, 7, 9], [1, 3, 5, 7, 9]], + [[1, 4, 6, 9], [1, 4, 6, 9, 11]], [[1, 4, 7, 9], [1, 4, 7, 9, 11]], + [[1, 5, 7, 9], [1, 3, 7, 9, 11]], [[1, 3, 5, 7, 9], [2, 4, 6, 8, 11]], + [[1, 10], [1, 5, 10]], [[1, 3, 10], [1, 3, 7, 10]], + [[1, 4, 10], [1, 4, 6, 10]], [[1, 5, 10], [1, 5, 8, 10]], + [[1, 6, 10], [1, 4, 6, 10]], [[1, 7, 10], [1, 3, 7, 10]], + [[1, 8, 10], [1, 5, 8, 10]], [[1, 3, 5, 10], [1, 3, 5, 8, 10]], + [[1, 3, 6, 10], [1, 3, 6, 8, 10]], [[1, 3, 7, 10], [1, 3, 5, 7, 10]], + [[1, 3, 8, 10], [1, 3, 5, 8, 10]], [[1, 4, 6, 10], [1, 4, 6, 8, 10]], + [[1, 4, 7, 10], [0, 2, 4, 7, 10]], [[1, 4, 8, 10], [1, 4, 6, 8, 10]], + [[1, 5, 7, 10], [1, 3, 5, 7, 10]], [[1, 5, 8, 10], [1, 3, 5, 8, 10]], + [[1, 6, 8, 10], [1, 3, 6, 8, 10]], [[1, 3, 5, 7, 10], [2, 4, 6, 8, 11]], + [[1, 3, 5, 8, 10], [0, 3, 5, 8, 10]], [[1, 3, 6, 8, 10], [0, 3, 6, 8, 10]], + [[1, 4, 6, 8, 10], [0, 3, 5, 7, 9]], [[1, 11], [2, 6, 11]], + [[1, 3, 11], [1, 3, 6, 11]], [[1, 4, 11], [1, 4, 8, 11]], + [[1, 5, 11], [1, 5, 8, 11]], [[1, 6, 11], [1, 4, 6, 11]], + [[1, 7, 11], [1, 4, 7, 11]], [[1, 8, 11], [1, 4, 8, 11]], + [[1, 9, 11], [1, 4, 9, 11]], [[1, 3, 5, 11], [1, 3, 5, 8, 11]], + [[1, 3, 6, 11], [1, 3, 6, 8, 11]], [[1, 3, 7, 11], [1, 3, 7, 9, 11]], + [[1, 3, 8, 11], [1, 3, 6, 8, 11]], [[1, 3, 9, 11], [1, 3, 6, 9, 11]], + [[1, 4, 6, 11], [1, 4, 6, 9, 11]], [[1, 4, 7, 11], [1, 4, 7, 9, 11]], + [[1, 4, 8, 11], [1, 4, 6, 8, 11]], [[1, 4, 9, 11], [1, 4, 6, 9, 11]], + [[1, 5, 7, 11], [0, 4, 6, 8, 10]], [[1, 5, 8, 11], [1, 3, 5, 8, 11]], + [[1, 5, 9, 11], [1, 5, 7, 9, 11]], [[1, 6, 8, 11], [1, 3, 6, 8, 11]], + [[1, 6, 9, 11], [1, 4, 6, 9, 11]], [[1, 7, 9, 11], [1, 4, 7, 9, 11]], + [[1, 3, 5, 7, 11], [0, 2, 4, 6, 8]], [[1, 3, 5, 8, 11], [0, 2, 4, 7, 10]], + [[1, 3, 5, 9, 11], [1, 3, 7, 9, 11]], [[1, 3, 6, 8, 11], [1, 4, 6, 8, 11]], + [[1, 3, 6, 9, 11], [0, 2, 5, 8, 10]], [[1, 3, 7, 9, 11], [1, 3, 6, 9, 11]], + [[1, 4, 6, 8, 11], [1, 4, 6, 9, 11]], [[1, 4, 6, 9, 11], [2, 4, 6, 9, 11]], + [[1, 4, 7, 9, 11], [2, 4, 7, 9, 11]], [[1, 5, 7, 9, 11], [2, 4, 7, 9, 11]], + [[1, 3, 5, 7, 9, 11], [0, 2, 4, 6, 8, 10]], [[2], [2, 9]], [[2, 4], [2, 6, 9]], + [[2, 5], [2, 5, 9]], [[2, 6], [2, 6, 9]], [[2, 4, 6], [2, 4, 6, 9]], + [[2, 7], [2, 7, 11]], [[2, 4, 7], [2, 4, 7, 11]], [[2, 5, 7], [2, 5, 7, 11]], + [[2, 8], [4, 8, 11]], [[2, 4, 8], [2, 4, 8, 11]], [[2, 5, 8], [2, 5, 8, 10]], + [[2, 6, 8], [2, 6, 8, 11]], [[2, 4, 6, 8], [2, 4, 6, 8, 11]], + [[2, 9], [2, 6, 9]], [[2, 4, 9], [2, 4, 6, 9]], [[2, 5, 9], [0, 2, 5, 9]], + [[2, 6, 9], [2, 6, 9, 11]], [[2, 7, 9], [2, 7, 9, 11]], + [[2, 4, 6, 9], [2, 4, 6, 9, 11]], [[2, 4, 7, 9], [2, 4, 7, 9, 11]], + [[2, 5, 7, 9], [0, 2, 5, 7, 9]], [[2, 10], [2, 5, 10]], + [[2, 4, 10], [2, 4, 7, 10]], [[2, 5, 10], [2, 5, 7, 10]], + [[2, 6, 10], [1, 4, 6, 10]], [[2, 7, 10], [2, 5, 7, 10]], + [[2, 8, 10], [2, 5, 8, 10]], [[2, 4, 6, 10], [0, 2, 4, 6, 10]], + [[2, 4, 7, 10], [0, 2, 4, 7, 10]], [[2, 4, 8, 10], [2, 4, 7, 9, 11]], + [[2, 5, 7, 10], [0, 2, 5, 7, 10]], [[2, 5, 8, 10], [0, 2, 5, 8, 10]], + [[2, 6, 8, 10], [1, 3, 5, 7, 10]], [[2, 4, 6, 8, 10], [0, 2, 6, 8, 10]], + [[2, 11], [2, 7, 11]], [[2, 4, 11], [2, 4, 8, 11]], + [[2, 5, 11], [2, 5, 7, 11]], [[2, 6, 11], [2, 6, 9, 11]], + [[2, 7, 11], [2, 4, 7, 11]], [[2, 8, 11], [2, 4, 8, 11]], + [[2, 9, 11], [2, 6, 9, 11]], [[2, 4, 6, 11], [2, 4, 6, 9, 11]], + [[2, 4, 7, 11], [2, 4, 7, 9, 11]], [[2, 4, 8, 11], [2, 4, 6, 8, 11]], + [[2, 4, 9, 11], [2, 4, 7, 9, 11]], [[2, 5, 7, 11], [2, 5, 7, 9, 11]], + [[2, 5, 8, 11], [1, 3, 5, 8, 11]], [[2, 5, 9, 11], [2, 5, 7, 9, 11]], + [[2, 6, 8, 11], [2, 4, 6, 8, 11]], [[2, 6, 9, 11], [2, 4, 6, 9, 11]], + [[2, 7, 9, 11], [2, 4, 7, 9, 11]], [[2, 4, 6, 8, 11], [2, 4, 6, 9, 11]], + [[2, 4, 6, 9, 11], [2, 4, 7, 9, 11]], [[2, 4, 7, 9, 11], [0, 2, 4, 7, 9]], + [[2, 5, 7, 9, 11], [2, 4, 7, 9, 11]], [[3], [3, 10]], [[3, 5], [3, 7, 10]], + [[3, 6], [3, 6, 11]], [[3, 7], [3, 7, 10]], [[3, 5, 7], [3, 5, 7, 10]], + [[3, 8], [0, 3, 8]], [[3, 5, 8], [0, 3, 5, 8]], [[3, 6, 8], [0, 3, 6, 8]], + [[3, 9], [0, 3, 9]], [[3, 5, 9], [0, 3, 5, 9]], [[3, 6, 9], [3, 6, 9, 11]], + [[3, 7, 9], [0, 3, 7, 9]], [[3, 5, 7, 9], [0, 3, 5, 7, 9]], + [[3, 10], [3, 7, 10]], [[3, 5, 10], [3, 5, 7, 10]], + [[3, 6, 10], [1, 3, 6, 10]], [[3, 7, 10], [0, 3, 7, 10]], + [[3, 8, 10], [0, 3, 8, 10]], [[3, 5, 7, 10], [0, 3, 5, 7, 10]], + [[3, 5, 8, 10], [0, 3, 5, 8, 10]], [[3, 6, 8, 10], [1, 3, 6, 8, 10]], + [[3, 11], [3, 6, 11]], [[3, 5, 11], [3, 5, 8, 11]], + [[3, 6, 11], [3, 6, 9, 11]], [[3, 7, 11], [2, 5, 7, 11]], + [[3, 8, 11], [3, 6, 8, 11]], [[3, 9, 11], [3, 6, 9, 11]], + [[3, 5, 7, 11], [3, 5, 7, 9, 11]], [[3, 5, 8, 11], [1, 3, 5, 8, 11]], + [[3, 5, 9, 11], [3, 5, 7, 9, 11]], [[3, 6, 8, 11], [1, 3, 6, 8, 11]], + [[3, 6, 9, 11], [1, 3, 6, 9, 11]], [[3, 7, 9, 11], [2, 4, 7, 9, 11]], + [[3, 5, 7, 9, 11], [2, 5, 7, 9, 11]], [[4], [4, 11]], [[4, 6], [4, 7, 11]], + [[4, 7], [0, 4, 7]], [[4, 8], [4, 8, 11]], [[4, 6, 8], [4, 6, 8, 11]], + [[4, 9], [1, 4, 9]], [[4, 6, 9], [1, 4, 6, 9]], [[4, 7, 9], [1, 4, 7, 9]], + [[4, 10], [4, 7, 10]], [[4, 6, 10], [1, 4, 6, 10]], + [[4, 7, 10], [0, 4, 7, 10]], [[4, 8, 10], [1, 4, 8, 10]], + [[4, 6, 8, 10], [1, 4, 6, 8, 10]], [[4, 11], [4, 8, 11]], + [[4, 6, 11], [4, 6, 8, 11]], [[4, 7, 11], [2, 4, 7, 11]], + [[4, 8, 11], [2, 4, 8, 11]], [[4, 9, 11], [2, 4, 9, 11]], + [[4, 6, 8, 11], [1, 4, 6, 8, 11]], [[4, 6, 9, 11], [2, 4, 6, 9, 11]], + [[4, 7, 9, 11], [2, 4, 7, 9, 11]], [[5], [0, 5, 9]], [[5, 7], [0, 4, 7]], + [[5, 8], [0, 5, 8]], [[5, 9], [0, 5, 9]], [[5, 7, 9], [0, 4, 7, 9]], + [[5, 10], [2, 5, 10]], [[5, 7, 10], [2, 5, 7, 10]], + [[5, 8, 10], [2, 5, 8, 10]], [[5, 11], [0, 5, 9]], [[5, 7, 11], [2, 5, 7, 11]], + [[5, 8, 11], [1, 5, 8, 11]], [[5, 9, 11], [2, 5, 9, 11]], + [[5, 7, 9, 11], [2, 5, 7, 9, 11]], [[6], [1, 6]], [[6, 8], [1, 5, 8]], + [[6, 9], [2, 6, 9]], [[6, 10], [1, 6, 10]], [[6, 8, 10], [1, 5, 8, 10]], + [[6, 11], [3, 6, 11]], [[6, 8, 11], [3, 6, 8, 11]], + [[6, 9, 11], [3, 6, 9, 11]], [[7], [2, 7, 11]], [[7, 9], [2, 6, 9]], + [[7, 10], [2, 7, 10]], [[7, 11], [2, 7, 11]], [[7, 9, 11], [2, 7, 9, 11]], + [[8], [3, 8]], [[8, 10], [3, 7, 10]], [[8, 11], [4, 8, 11]], [[9], [4, 9]], + [[9, 11], [4, 8, 11]], [[10], [2, 5, 10]], [[11], [6, 11]]] + +################################################################################### + +ALL_CHORDS_PAIRS_FILTERED = [[[0], [0, 4, 7]], [[0, 3], [0, 3, 7]], [[0, 3, 5], [0, 3, 5, 9]], + [[0, 3, 5, 8], [0, 3, 7, 10]], [[0, 3, 5, 9], [0, 3, 7, 10]], + [[0, 3, 5, 10], [0, 3, 5, 9]], [[0, 3, 7], [0, 3, 7, 10]], + [[0, 3, 7, 10], [0, 3, 5, 9]], [[0, 3, 8], [0, 3, 5, 8]], + [[0, 3, 9], [0, 3, 5, 9]], [[0, 3, 10], [0, 3, 7, 10]], [[0, 4], [0, 4, 7]], + [[0, 4, 6], [0, 4, 6, 9]], [[0, 4, 6, 9], [1, 4, 6, 9]], + [[0, 4, 6, 10], [0, 4, 7, 10]], [[0, 4, 7], [0, 4, 7, 10]], + [[0, 4, 7, 10], [1, 4, 7, 10]], [[0, 4, 8], [0, 4, 7, 10]], + [[0, 4, 9], [0, 4, 6, 9]], [[0, 4, 10], [0, 4, 7, 10]], [[0, 5], [0, 5, 9]], + [[0, 5, 8], [0, 3, 5, 8]], [[0, 5, 9], [0, 3, 5, 9]], + [[0, 5, 10], [0, 3, 5, 10]], [[0, 6], [0, 6, 9]], [[0, 6, 9], [0, 4, 6, 9]], + [[0, 6, 10], [0, 4, 7, 10]], [[0, 7], [0, 4, 7]], [[0, 7, 10], [0, 4, 7, 10]], + [[0, 8], [0, 3, 8]], [[0, 9], [0, 4, 9]], [[0, 10], [2, 5, 10]], [[1], [1, 8]], + [[1, 4], [1, 4, 9]], [[1, 4, 6], [1, 4, 6, 9]], [[1, 4, 6, 9], [1, 4, 8, 11]], + [[1, 4, 6, 10], [0, 3, 5, 9]], [[1, 4, 6, 11], [1, 4, 6, 9]], + [[1, 4, 7], [1, 4, 7, 10]], [[1, 4, 7, 10], [0, 4, 7, 10]], + [[1, 4, 7, 11], [1, 4, 6, 10]], [[1, 4, 8], [1, 4, 8, 11]], + [[1, 4, 8, 11], [1, 4, 6, 9]], [[1, 4, 9], [1, 4, 6, 9]], + [[1, 4, 10], [1, 4, 6, 10]], [[1, 4, 11], [1, 4, 8, 11]], [[1, 5], [1, 5, 8]], + [[1, 5, 8], [1, 5, 8, 11]], [[1, 5, 8, 11], [2, 5, 8, 11]], + [[1, 5, 9], [0, 3, 5, 9]], [[1, 5, 10], [0, 4, 7, 10]], + [[1, 5, 11], [1, 5, 8, 11]], [[1, 6], [1, 6, 10]], [[1, 6, 9], [1, 4, 6, 9]], + [[1, 6, 10], [1, 4, 6, 10]], [[1, 6, 11], [1, 4, 6, 11]], [[1, 7], [1, 4, 7]], + [[1, 7, 10], [1, 4, 7, 10]], [[1, 7, 11], [1, 4, 7, 11]], [[1, 8], [1, 5, 8]], + [[1, 8, 11], [1, 4, 8, 11]], [[1, 9], [1, 4, 9]], [[1, 10], [1, 5, 10]], + [[1, 11], [2, 6, 11]], [[2], [2, 9]], [[2, 5], [2, 5, 9]], + [[2, 5, 8], [2, 5, 8, 11]], [[2, 5, 8, 11], [1, 4, 7, 10]], + [[2, 5, 9], [0, 3, 5, 9]], [[2, 5, 10], [0, 3, 5, 9]], + [[2, 5, 11], [2, 5, 8, 11]], [[2, 6], [2, 6, 9]], [[2, 6, 9], [1, 4, 6, 9]], + [[2, 6, 10], [1, 4, 6, 10]], [[2, 6, 11], [1, 4, 6, 10]], [[2, 7], [2, 7, 11]], + [[2, 7, 10], [0, 4, 7, 10]], [[2, 7, 11], [1, 4, 6, 9]], [[2, 8], [4, 8, 11]], + [[2, 8, 11], [2, 5, 8, 11]], [[2, 9], [2, 6, 9]], [[2, 10], [2, 5, 10]], + [[2, 11], [2, 7, 11]], [[3], [3, 10]], [[3, 5], [3, 7, 10]], + [[3, 5, 8], [0, 3, 5, 8]], [[3, 5, 8, 11], [2, 5, 8, 11]], + [[3, 5, 9], [0, 3, 5, 9]], [[3, 5, 10], [0, 3, 5, 10]], + [[3, 5, 11], [3, 5, 8, 11]], [[3, 7], [3, 7, 10]], [[3, 7, 10], [0, 3, 7, 10]], + [[3, 7, 11], [0, 3, 7, 10]], [[3, 8], [0, 3, 8]], [[3, 8, 11], [3, 5, 8, 11]], + [[3, 9], [0, 3, 9]], [[3, 10], [3, 7, 10]], [[3, 11], [3, 8, 11]], + [[4], [4, 11]], [[4, 6], [4, 7, 11]], [[4, 6, 9], [1, 4, 6, 9]], + [[4, 6, 10], [1, 4, 6, 10]], [[4, 6, 11], [1, 4, 6, 11]], [[4, 7], [0, 4, 7]], + [[4, 7, 10], [0, 4, 7, 10]], [[4, 7, 11], [1, 4, 7, 11]], [[4, 8], [4, 8, 11]], + [[4, 8, 11], [1, 4, 8, 11]], [[4, 9], [1, 4, 9]], [[4, 10], [4, 7, 10]], + [[4, 11], [4, 8, 11]], [[5], [0, 5, 9]], [[5, 8], [0, 5, 8]], + [[5, 8, 11], [1, 5, 8, 11]], [[5, 9], [0, 5, 9]], [[5, 10], [2, 5, 10]], + [[5, 11], [0, 5, 9]], [[6], [1, 6]], [[6, 9], [2, 6, 9]], + [[6, 10], [1, 6, 10]], [[6, 11], [2, 6, 11]], [[7], [2, 7, 11]], + [[7, 10], [2, 7, 10]], [[7, 11], [2, 7, 11]], [[8], [3, 8]], + [[8, 11], [4, 8, 11]], [[9], [4, 9]], [[10], [2, 5, 10]], [[11], [6, 11]]] + +################################################################################### + +ALL_CHORDS_TRIPLETS_SORTED = [[[0], [0, 4, 7], [0]], [[0, 2], [0, 4, 7], [0]], [[0, 3], [0, 3, 7], [0]], + [[0, 4], [0, 4, 7], [0, 4]], [[0, 2, 4], [0, 2, 4, 7], [0]], + [[0, 5], [0, 5, 9], [0, 5]], [[0, 2, 5], [0, 2, 5, 9], [0, 2, 5]], + [[0, 3, 5], [0, 3, 5, 9], [0, 3, 5]], [[0, 6], [0, 2, 6, 9], [2]], + [[0, 2, 6], [0, 2, 6, 9], [0, 2, 6]], [[0, 3, 6], [0, 3, 6, 8], [0, 3, 6]], + [[0, 4, 6], [0, 4, 6, 9], [0, 4, 6]], + [[0, 2, 4, 6], [0, 2, 4, 6, 9], [0, 2, 4, 6]], [[0, 7], [0, 4, 7], [0, 7]], + [[0, 2, 7], [0, 2, 4, 7], [0, 2, 7]], [[0, 3, 7], [0, 3, 7, 10], [0, 3, 7]], + [[0, 4, 7], [0, 4, 7, 9], [0, 4, 7]], [[0, 5, 7], [0, 5, 7, 9], [0, 5, 7]], + [[0, 2, 4, 7], [0, 2, 4, 7, 9], [0, 2, 4, 7]], + [[0, 2, 5, 7], [0, 2, 5, 7, 9], [0, 2, 5, 7]], + [[0, 3, 5, 7], [0, 3, 5, 7, 10], [0, 3, 5, 7]], [[0, 8], [0, 3, 8], [8]], + [[0, 2, 8], [0, 2, 5, 8], [0, 2, 8]], [[0, 3, 8], [0, 3, 5, 8], [0, 3, 8]], + [[0, 4, 8], [2, 4, 8, 11], [0, 4, 9]], [[0, 5, 8], [0, 3, 5, 8], [0, 5, 8]], + [[0, 6, 8], [0, 3, 6, 8], [0, 6, 8]], + [[0, 2, 4, 8], [0, 2, 4, 6, 8], [0, 2, 4, 8]], + [[0, 2, 5, 8], [0, 2, 5, 8, 10], [0, 2, 5, 8]], + [[0, 2, 6, 8], [0, 2, 6, 8, 10], [0, 2, 6, 8]], + [[0, 3, 5, 8], [0, 3, 5, 8, 10], [0, 3, 5, 8]], + [[0, 3, 6, 8], [0, 3, 6, 8, 10], [0, 3, 6, 8]], + [[0, 4, 6, 8], [2, 4, 6, 8, 11], [2, 6, 8, 11]], + [[0, 2, 4, 6, 8], [2, 4, 6, 8, 11], [2, 6, 8, 11]], [[0, 9], [0, 4, 9], [9]], + [[0, 2, 9], [0, 2, 6, 9], [0, 2, 9]], [[0, 3, 9], [0, 3, 5, 9], [0, 3, 9]], + [[0, 4, 9], [0, 4, 7, 9], [0, 4, 9]], [[0, 5, 9], [0, 2, 5, 9], [0, 5, 9]], + [[0, 6, 9], [0, 2, 6, 9], [0, 6, 9]], [[0, 7, 9], [0, 4, 7, 9], [0, 7, 9]], + [[0, 2, 4, 9], [0, 2, 4, 7, 9], [0, 2, 4, 9]], + [[0, 2, 5, 9], [0, 2, 5, 7, 9], [0, 2, 5, 9]], + [[0, 2, 6, 9], [0, 2, 4, 6, 9], [0, 2, 6, 9]], + [[0, 2, 7, 9], [0, 2, 4, 7, 9], [0, 2, 7, 9]], + [[0, 3, 5, 9], [0, 3, 5, 7, 9], [0, 3, 5, 9]], + [[0, 3, 6, 9], [0, 2, 4, 6, 9], [4, 6, 9]], + [[0, 3, 7, 9], [0, 3, 5, 7, 9], [0, 3, 7, 9]], + [[0, 4, 6, 9], [0, 2, 4, 6, 9], [0, 4, 6, 9]], + [[0, 4, 7, 9], [0, 2, 4, 7, 9], [0, 4, 7, 9]], + [[0, 5, 7, 9], [0, 2, 5, 7, 9], [0, 5, 7, 9]], + [[0, 2, 4, 6, 9], [2, 4, 6, 9, 11], [0, 2, 4, 6, 9]], + [[0, 2, 4, 7, 9], [2, 4, 7, 9, 11], [0, 2, 4, 7, 9]], + [[0, 2, 5, 7, 9], [2, 5, 7, 9, 11], [7]], + [[0, 3, 5, 7, 9], [2, 4, 6, 8, 11], [1, 4, 6, 8, 10]], + [[0, 10], [2, 5, 10], [10]], [[0, 2, 10], [0, 2, 5, 10], [10]], + [[0, 3, 10], [0, 3, 7, 10], [0, 3, 10]], + [[0, 4, 10], [0, 4, 7, 10], [0, 4, 10]], + [[0, 5, 10], [0, 2, 5, 10], [0, 5, 10]], + [[0, 6, 10], [0, 3, 6, 10], [0, 6, 10]], + [[0, 7, 10], [0, 4, 7, 10], [0, 7, 10]], [[0, 8, 10], [0, 3, 8, 10], [8]], + [[0, 2, 4, 10], [0, 2, 4, 7, 10], [0, 4, 10]], + [[0, 2, 5, 10], [0, 2, 5, 7, 10], [0, 2, 5, 10]], + [[0, 2, 6, 10], [0, 2, 6, 8, 10], [8]], + [[0, 2, 7, 10], [0, 2, 5, 7, 10], [2, 7, 10]], + [[0, 2, 8, 10], [0, 2, 5, 8, 10], [8, 10]], + [[0, 3, 5, 10], [0, 3, 5, 7, 10], [0, 3, 5, 10]], + [[0, 3, 6, 10], [0, 3, 6, 8, 10], [0, 3, 6, 10]], + [[0, 3, 7, 10], [0, 3, 5, 7, 10], [0, 3, 7, 10]], + [[0, 3, 8, 10], [0, 3, 5, 8, 10], [0, 3, 8, 10]], + [[0, 4, 6, 10], [0, 2, 4, 6, 10], [2]], + [[0, 4, 7, 10], [0, 2, 4, 7, 10], [0, 4, 7, 10]], + [[0, 4, 8, 10], [0, 2, 4, 8, 10], [0, 4, 8, 10]], + [[0, 5, 7, 10], [0, 3, 5, 7, 10], [0, 5, 7, 10]], + [[0, 5, 8, 10], [0, 3, 5, 8, 10], [10]], + [[0, 6, 8, 10], [0, 3, 6, 8, 10], [6]], + [[0, 2, 4, 6, 10], [0, 2, 4, 8, 10], [0, 2, 6, 8, 10]], + [[0, 2, 4, 7, 10], [1, 3, 6, 9, 11], [0, 2, 5, 8, 10]], + [[0, 2, 4, 8, 10], [1, 3, 7, 9, 11], [0, 2, 6, 8, 10]], + [[0, 2, 5, 7, 10], [0, 3, 5, 7, 10], [5, 10]], + [[0, 2, 5, 8, 10], [1, 4, 7, 9, 11], [8]], + [[0, 2, 6, 8, 10], [2, 4, 6, 8, 10], [0, 2, 6, 8, 10]], + [[0, 3, 5, 7, 10], [0, 2, 5, 7, 10], [9]], + [[0, 3, 5, 8, 10], [1, 3, 5, 8, 10], [0, 3, 5, 8, 10]], + [[0, 3, 6, 8, 10], [1, 3, 6, 8, 10], [0, 3, 6, 8, 10]], + [[0, 4, 6, 8, 10], [0, 2, 4, 6, 9], [1, 3, 5, 8, 10]], + [[0, 2, 4, 6, 8, 10], [1, 3, 5, 7, 9, 11], [0, 2, 4, 6, 8, 10]], + [[1], [1, 8], [1]], [[1, 3], [1, 5, 8], [1]], [[1, 4], [1, 4, 9], [9]], + [[1, 5], [1, 5, 8], [1, 5]], [[1, 3, 5], [1, 3, 5, 10], [1, 3, 5]], + [[1, 6], [1, 6, 10], [1, 6]], [[1, 3, 6], [1, 3, 6, 10], [1, 3, 6]], + [[1, 4, 6], [1, 4, 6, 9], [1, 4, 6]], [[1, 7], [1, 4, 7], [1, 7]], + [[1, 3, 7], [1, 3, 7, 10], [1, 3, 7]], [[1, 4, 7], [1, 4, 7, 9], [1, 4, 7]], + [[1, 5, 7], [1, 5, 7, 10], [1, 5, 7]], [[1, 3, 5, 7], [1, 3, 5, 7, 10], [7]], + [[1, 8], [1, 5, 8], [1, 8]], [[1, 3, 8], [1, 3, 5, 8], [1, 3, 8]], + [[1, 4, 8], [1, 4, 8, 11], [1, 4, 8]], [[1, 5, 8], [1, 5, 8, 10], [1, 5, 8]], + [[1, 6, 8], [1, 3, 6, 8], [1, 6, 8]], + [[1, 3, 5, 8], [1, 3, 5, 8, 10], [1, 3, 5, 8]], + [[1, 3, 6, 8], [1, 3, 6, 8, 10], [1, 3, 6, 8]], + [[1, 4, 6, 8], [1, 4, 6, 8, 11], [1, 4, 6, 8]], [[1, 9], [1, 4, 9], [9]], + [[1, 3, 9], [1, 3, 6, 9], [1, 3, 9]], [[1, 4, 9], [1, 4, 6, 9], [1, 4, 9]], + [[1, 5, 9], [0, 3, 5, 9], [0, 5, 9]], [[1, 6, 9], [1, 4, 6, 9], [1, 6, 9]], + [[1, 7, 9], [1, 4, 7, 9], [1, 7, 9]], + [[1, 3, 5, 9], [0, 3, 5, 7, 9], [1, 5, 9]], + [[1, 3, 6, 9], [1, 3, 6, 9, 11], [1, 3, 6, 9]], + [[1, 3, 7, 9], [1, 3, 5, 7, 9], [1, 7]], + [[1, 4, 6, 9], [1, 4, 6, 9, 11], [1, 4, 6, 9]], + [[1, 4, 7, 9], [1, 4, 7, 9, 11], [1, 4, 7, 9]], + [[1, 5, 7, 9], [1, 3, 7, 9, 11], [1, 5, 7, 9]], + [[1, 3, 5, 7, 9], [2, 4, 6, 8, 11], [9]], [[1, 10], [1, 5, 10], [10]], + [[1, 3, 10], [1, 3, 7, 10], [1, 3, 10]], + [[1, 4, 10], [1, 4, 6, 10], [1, 4, 10]], + [[1, 5, 10], [1, 5, 8, 10], [1, 5, 10]], + [[1, 6, 10], [1, 4, 6, 10], [1, 6, 10]], + [[1, 7, 10], [1, 3, 7, 10], [1, 7, 10]], [[1, 8, 10], [1, 5, 8, 10], [10]], + [[1, 3, 5, 10], [1, 3, 5, 8, 10], [1, 3, 5, 10]], + [[1, 3, 6, 10], [1, 3, 6, 8, 10], [1, 3, 6, 10]], + [[1, 3, 7, 10], [1, 3, 5, 7, 10], [1, 3, 7, 10]], + [[1, 3, 8, 10], [1, 3, 5, 8, 10], [1, 3, 8, 10]], + [[1, 4, 6, 10], [1, 4, 6, 8, 10], [1, 4, 6, 10]], + [[1, 4, 7, 10], [0, 2, 4, 7, 10], [0, 4, 7, 10]], + [[1, 4, 8, 10], [1, 4, 6, 8, 10], [1, 4, 8, 10]], + [[1, 5, 7, 10], [1, 3, 5, 7, 10], [1, 5, 7, 10]], + [[1, 5, 8, 10], [1, 3, 5, 8, 10], [1, 5, 8, 10]], + [[1, 6, 8, 10], [1, 3, 6, 8, 10], [1, 6, 8, 10]], + [[1, 3, 5, 7, 10], [2, 4, 6, 8, 11], [0, 3, 5, 7, 9]], + [[1, 3, 5, 8, 10], [0, 3, 5, 8, 10], [6, 8, 10]], + [[1, 3, 6, 8, 10], [0, 3, 6, 8, 10], [8]], + [[1, 4, 6, 8, 10], [0, 3, 5, 7, 9], [2, 4, 6, 8, 11]], + [[1, 11], [2, 6, 11], [11]], [[1, 3, 11], [1, 3, 6, 11], [11]], + [[1, 4, 11], [1, 4, 8, 11], [1]], [[1, 5, 11], [1, 5, 8, 11], [1, 5, 11]], + [[1, 6, 11], [1, 4, 6, 11], [1, 6, 11]], + [[1, 7, 11], [1, 4, 7, 11], [1, 7, 11]], + [[1, 8, 11], [1, 4, 8, 11], [1, 8, 11]], [[1, 9, 11], [1, 4, 9, 11], [9]], + [[1, 3, 5, 11], [1, 3, 5, 8, 11], [1, 3, 5, 11]], + [[1, 3, 6, 11], [1, 3, 6, 8, 11], [1, 3, 6, 11]], + [[1, 3, 7, 11], [1, 3, 7, 9, 11], [0]], + [[1, 3, 8, 11], [1, 3, 6, 8, 11], [1, 3, 8, 11]], + [[1, 3, 9, 11], [1, 3, 6, 9, 11], [1, 3, 9, 11]], + [[1, 4, 6, 11], [1, 4, 6, 9, 11], [1, 4, 6, 11]], + [[1, 4, 7, 11], [1, 4, 7, 9, 11], [1, 4, 7, 11]], + [[1, 4, 8, 11], [1, 4, 6, 8, 11], [1, 4, 8, 11]], + [[1, 4, 9, 11], [1, 4, 6, 9, 11], [1, 4, 9, 11]], + [[1, 5, 7, 11], [0, 4, 6, 8, 10], [5, 7, 9, 11]], + [[1, 5, 8, 11], [1, 3, 5, 8, 11], [1, 5, 8, 11]], + [[1, 5, 9, 11], [1, 5, 7, 9, 11], [9]], + [[1, 6, 8, 11], [1, 3, 6, 8, 11], [1, 6, 8, 11]], + [[1, 6, 9, 11], [1, 4, 6, 9, 11], [1, 6, 9, 11]], + [[1, 7, 9, 11], [1, 4, 7, 9, 11], [1, 7, 9, 11]], + [[1, 3, 5, 7, 11], [0, 2, 4, 6, 8], [7, 9]], + [[1, 3, 5, 8, 11], [0, 2, 4, 7, 10], [1, 3, 6, 9, 11]], + [[1, 3, 5, 9, 11], [1, 3, 7, 9, 11], [0, 2, 6, 8, 10]], + [[1, 3, 6, 8, 11], [1, 4, 6, 8, 11], [6, 8, 11]], + [[1, 3, 6, 9, 11], [0, 2, 5, 8, 10], [1, 4, 7, 9, 11]], + [[1, 3, 7, 9, 11], [1, 3, 6, 9, 11], [11]], + [[1, 4, 6, 8, 11], [1, 4, 6, 9, 11], [9, 11]], + [[1, 4, 6, 9, 11], [2, 4, 6, 9, 11], [1, 4, 6, 9, 11]], + [[1, 4, 7, 9, 11], [2, 4, 7, 9, 11], [7, 9, 11]], + [[1, 5, 7, 9, 11], [2, 4, 7, 9, 11], [5, 7, 9]], + [[1, 3, 5, 7, 9, 11], [0, 2, 4, 6, 8, 10], [1, 3, 5, 7, 9, 11]], + [[2], [2, 9], [2]], [[2, 4], [2, 6, 9], [2]], [[2, 5], [2, 5, 9], [2]], + [[2, 6], [2, 6, 9], [2]], [[2, 4, 6], [2, 4, 6, 9], [2, 4, 6]], + [[2, 7], [2, 7, 11], [2, 7]], [[2, 4, 7], [2, 4, 7, 11], [2, 4, 7]], + [[2, 5, 7], [2, 5, 7, 11], [2, 5, 7]], [[2, 8], [4, 8, 11], [4]], + [[2, 4, 8], [2, 4, 8, 11], [2, 4, 8]], [[2, 5, 8], [2, 5, 8, 10], [2, 5, 8]], + [[2, 6, 8], [2, 6, 8, 11], [2, 6, 8]], + [[2, 4, 6, 8], [2, 4, 6, 8, 11], [2, 4, 6, 8]], [[2, 9], [2, 6, 9], [2, 9]], + [[2, 4, 9], [2, 4, 6, 9], [2, 4, 9]], [[2, 5, 9], [0, 2, 5, 9], [2, 5, 9]], + [[2, 6, 9], [2, 6, 9, 11], [2, 6, 9]], [[2, 7, 9], [2, 7, 9, 11], [2, 7, 9]], + [[2, 4, 6, 9], [2, 4, 6, 9, 11], [2, 4, 6, 9]], + [[2, 4, 7, 9], [2, 4, 7, 9, 11], [2, 4, 7, 9]], + [[2, 5, 7, 9], [0, 2, 5, 7, 9], [2, 5, 7, 9]], [[2, 10], [2, 5, 10], [10]], + [[2, 4, 10], [2, 4, 7, 10], [2, 4, 10]], + [[2, 5, 10], [2, 5, 7, 10], [2, 5, 10]], + [[2, 6, 10], [1, 4, 6, 10], [1, 6, 10]], + [[2, 7, 10], [2, 5, 7, 10], [2, 7, 10]], + [[2, 8, 10], [2, 5, 8, 10], [2, 8, 10]], + [[2, 4, 6, 10], [0, 2, 4, 6, 10], [2, 4, 6, 10]], + [[2, 4, 7, 10], [0, 2, 4, 7, 10], [2, 4, 7, 10]], + [[2, 4, 8, 10], [2, 4, 7, 9, 11], [2, 4, 7, 11]], + [[2, 5, 7, 10], [0, 2, 5, 7, 10], [2, 5, 7, 10]], + [[2, 5, 8, 10], [0, 2, 5, 8, 10], [2, 5, 8, 10]], + [[2, 6, 8, 10], [1, 3, 5, 7, 10], [1, 7]], + [[2, 4, 6, 8, 10], [0, 2, 6, 8, 10], [2, 4, 6, 8, 10]], + [[2, 11], [2, 7, 11], [7]], [[2, 4, 11], [2, 4, 8, 11], [2, 4, 11]], + [[2, 5, 11], [2, 5, 7, 11], [2, 5, 11]], + [[2, 6, 11], [2, 6, 9, 11], [2, 6, 11]], + [[2, 7, 11], [2, 4, 7, 11], [2, 7, 11]], + [[2, 8, 11], [2, 4, 8, 11], [2, 8, 11]], + [[2, 9, 11], [2, 6, 9, 11], [2, 9, 11]], + [[2, 4, 6, 11], [2, 4, 6, 9, 11], [2, 4, 6, 11]], + [[2, 4, 7, 11], [2, 4, 7, 9, 11], [2, 4, 7, 11]], + [[2, 4, 8, 11], [2, 4, 6, 8, 11], [2, 4, 8, 11]], + [[2, 4, 9, 11], [2, 4, 7, 9, 11], [2, 4, 9, 11]], + [[2, 5, 7, 11], [2, 5, 7, 9, 11], [2, 5, 7, 11]], + [[2, 5, 8, 11], [1, 3, 5, 8, 11], [1, 5, 8, 11]], + [[2, 5, 9, 11], [2, 5, 7, 9, 11], [2, 5, 9, 11]], + [[2, 6, 8, 11], [2, 4, 6, 8, 11], [2, 6, 8, 11]], + [[2, 6, 9, 11], [2, 4, 6, 9, 11], [2, 6, 9, 11]], + [[2, 7, 9, 11], [2, 4, 7, 9, 11], [2, 7, 9, 11]], + [[2, 4, 6, 8, 11], [2, 4, 6, 9, 11], [2, 4, 6, 8, 11]], + [[2, 4, 6, 9, 11], [2, 4, 7, 9, 11], [2, 7, 9]], + [[2, 4, 7, 9, 11], [0, 2, 4, 7, 9], [11]], + [[2, 5, 7, 9, 11], [2, 4, 7, 9, 11], [2, 7, 9, 11]], [[3], [3, 10], [3]], + [[3, 5], [3, 7, 10], [3]], [[3, 6], [3, 6, 11], [11]], + [[3, 7], [3, 7, 10], [3]], [[3, 5, 7], [3, 5, 7, 10], [3, 5, 7]], + [[3, 8], [0, 3, 8], [3, 8]], [[3, 5, 8], [0, 3, 5, 8], [8]], + [[3, 6, 8], [0, 3, 6, 8], [3, 6, 8]], [[3, 9], [0, 3, 9], [3, 9]], + [[3, 5, 9], [0, 3, 5, 9], [3, 5, 9]], [[3, 6, 9], [3, 6, 9, 11], [3, 6, 9]], + [[3, 7, 9], [0, 3, 7, 9], [3, 7, 9]], + [[3, 5, 7, 9], [0, 3, 5, 7, 9], [0, 3, 5, 9]], [[3, 10], [3, 7, 10], [3, 10]], + [[3, 5, 10], [3, 5, 7, 10], [3, 5, 10]], + [[3, 6, 10], [1, 3, 6, 10], [3, 6, 10]], + [[3, 7, 10], [0, 3, 7, 10], [3, 7, 10]], + [[3, 8, 10], [0, 3, 8, 10], [3, 8, 10]], + [[3, 5, 7, 10], [0, 3, 5, 7, 10], [3, 5, 7, 10]], + [[3, 5, 8, 10], [0, 3, 5, 8, 10], [3, 5, 8, 10]], + [[3, 6, 8, 10], [1, 3, 6, 8, 10], [3, 6, 8, 10]], [[3, 11], [3, 6, 11], [11]], + [[3, 5, 11], [3, 5, 8, 11], [3, 5, 11]], + [[3, 6, 11], [3, 6, 9, 11], [3, 6, 11]], + [[3, 7, 11], [2, 5, 7, 11], [2, 7, 11]], + [[3, 8, 11], [3, 6, 8, 11], [3, 8, 11]], + [[3, 9, 11], [3, 6, 9, 11], [3, 9, 11]], + [[3, 5, 7, 11], [3, 5, 7, 9, 11], [3, 5, 7, 11]], + [[3, 5, 8, 11], [1, 3, 5, 8, 11], [3, 5, 8, 11]], + [[3, 5, 9, 11], [3, 5, 7, 9, 11], [5, 7, 9, 11]], + [[3, 6, 8, 11], [1, 3, 6, 8, 11], [3, 6, 8, 11]], + [[3, 6, 9, 11], [1, 3, 6, 9, 11], [3, 6, 9, 11]], + [[3, 7, 9, 11], [2, 4, 7, 9, 11], [7, 9, 11]], + [[3, 5, 7, 9, 11], [2, 5, 7, 9, 11], [2, 5, 7, 11]], [[4], [4, 11], [4]], + [[4, 6], [4, 7, 11], [4]], [[4, 7], [0, 4, 7], [0]], [[4, 8], [4, 8, 11], [4]], + [[4, 6, 8], [4, 6, 8, 11], [4]], [[4, 9], [1, 4, 9], [4, 9]], + [[4, 6, 9], [1, 4, 6, 9], [4, 6, 9]], [[4, 7, 9], [1, 4, 7, 9], [4, 7, 9]], + [[4, 10], [4, 7, 10], [4, 10]], [[4, 6, 10], [1, 4, 6, 10], [4, 6, 10]], + [[4, 7, 10], [0, 4, 7, 10], [4, 7, 10]], [[4, 8, 10], [1, 4, 8, 10], [1]], + [[4, 6, 8, 10], [1, 4, 6, 8, 10], [6]], [[4, 11], [4, 8, 11], [4, 11]], + [[4, 6, 11], [4, 6, 8, 11], [4, 6, 11]], + [[4, 7, 11], [2, 4, 7, 11], [4, 7, 11]], + [[4, 8, 11], [2, 4, 8, 11], [4, 8, 11]], + [[4, 9, 11], [2, 4, 9, 11], [4, 9, 11]], + [[4, 6, 8, 11], [1, 4, 6, 8, 11], [4, 6, 8, 11]], + [[4, 6, 9, 11], [2, 4, 6, 9, 11], [4, 6, 9, 11]], + [[4, 7, 9, 11], [2, 4, 7, 9, 11], [4, 7, 9, 11]], [[5], [0, 5, 9], [5]], + [[5, 7], [0, 4, 7], [0]], [[5, 8], [0, 5, 8], [5]], [[5, 9], [0, 5, 9], [5]], + [[5, 7, 9], [0, 4, 7, 9], [5]], [[5, 10], [2, 5, 10], [5, 10]], + [[5, 7, 10], [2, 5, 7, 10], [7]], [[5, 8, 10], [2, 5, 8, 10], [5, 8, 10]], + [[5, 11], [0, 5, 9], [5]], [[5, 7, 11], [2, 5, 7, 11], [5, 7, 11]], + [[5, 8, 11], [1, 5, 8, 11], [5, 8, 11]], + [[5, 9, 11], [2, 5, 9, 11], [5, 9, 11]], + [[5, 7, 9, 11], [2, 5, 7, 9, 11], [5, 7, 9]], [[6], [1, 6], [6]], + [[6, 8], [1, 5, 8], [8]], [[6, 9], [2, 6, 9], [2]], [[6, 10], [1, 6, 10], [6]], + [[6, 8, 10], [1, 5, 8, 10], [6, 8, 10]], [[6, 11], [3, 6, 11], [6, 11]], + [[6, 8, 11], [3, 6, 8, 11], [6, 8, 11]], + [[6, 9, 11], [3, 6, 9, 11], [6, 9, 11]], [[7], [2, 7, 11], [7]], + [[7, 9], [2, 6, 9], [2]], [[7, 10], [2, 7, 10], [7]], + [[7, 11], [2, 7, 11], [7]], [[7, 9, 11], [2, 7, 9, 11], [7, 9, 11]], + [[8], [3, 8], [8]], [[8, 10], [3, 7, 10], [3]], [[8, 11], [4, 8, 11], [4]], + [[9], [4, 9], [9]], [[9, 11], [4, 8, 11], [4]], [[10], [2, 5, 10], [10]], + [[11], [6, 11], [11]]] + +################################################################################### + +ALL_CHORDS_TRIPLETS_FILTERED = [[[0], [0, 4, 7], [7]], [[0, 3], [0, 3, 7], [0]], + [[0, 3, 5], [0, 3, 5, 9], [5]], [[0, 3, 5, 8], [0, 3, 7, 10], [0]], + [[0, 3, 5, 9], [0, 3, 7, 10], [10]], [[0, 3, 5, 10], [0, 3, 5, 9], [5]], + [[0, 3, 7], [0, 3, 7, 10], [0]], [[0, 3, 7, 10], [0, 3, 5, 9], [2, 5, 10]], + [[0, 3, 8], [0, 3, 5, 8], [8]], [[0, 3, 9], [0, 3, 5, 9], [5]], + [[0, 3, 10], [0, 3, 7, 10], [0]], [[0, 4], [0, 4, 7], [0]], + [[0, 4, 6], [0, 4, 6, 9], [4]], [[0, 4, 6, 9], [1, 4, 6, 9], [9]], + [[0, 4, 6, 10], [0, 4, 7, 10], [0, 4, 10]], [[0, 4, 7], [0, 4, 7, 10], [0]], + [[0, 4, 7, 10], [1, 4, 7, 10], [0]], [[0, 4, 8], [0, 4, 7, 10], [0, 5, 8]], + [[0, 4, 9], [0, 4, 6, 9], [9]], [[0, 4, 10], [0, 4, 7, 10], [0]], + [[0, 5], [0, 5, 9], [5]], [[0, 5, 8], [0, 3, 5, 8], [5]], + [[0, 5, 9], [0, 3, 5, 9], [5]], [[0, 5, 10], [0, 3, 5, 10], [10]], + [[0, 6], [0, 6, 9], [9]], [[0, 6, 9], [0, 4, 6, 9], [6]], + [[0, 6, 10], [0, 4, 7, 10], [10]], [[0, 7], [0, 4, 7], [0]], + [[0, 7, 10], [0, 4, 7, 10], [0]], [[0, 8], [0, 3, 8], [8]], + [[0, 9], [0, 4, 9], [9]], [[0, 10], [2, 5, 10], [10]], [[1], [1, 8], [8]], + [[1, 4], [1, 4, 9], [9]], [[1, 4, 6], [1, 4, 6, 9], [6]], + [[1, 4, 6, 9], [1, 4, 8, 11], [4]], [[1, 4, 6, 10], [0, 3, 5, 9], [5]], + [[1, 4, 6, 11], [1, 4, 6, 9], [6]], [[1, 4, 7], [1, 4, 7, 10], [10]], + [[1, 4, 7, 10], [0, 4, 7, 10], [0]], + [[1, 4, 7, 11], [1, 4, 6, 10], [1, 6, 10]], [[1, 4, 8], [1, 4, 8, 11], [1]], + [[1, 4, 8, 11], [1, 4, 6, 9], [1, 4, 9]], [[1, 4, 9], [1, 4, 6, 9], [9]], + [[1, 4, 10], [1, 4, 6, 10], [6]], [[1, 4, 11], [1, 4, 8, 11], [1]], + [[1, 5], [1, 5, 8], [1]], [[1, 5, 8], [1, 5, 8, 11], [1]], + [[1, 5, 8, 11], [2, 5, 8, 11], [1]], [[1, 5, 9], [0, 3, 5, 9], [0, 5, 9]], + [[1, 5, 10], [0, 4, 7, 10], [0]], [[1, 5, 11], [1, 5, 8, 11], [11]], + [[1, 6], [1, 6, 10], [6]], [[1, 6, 9], [1, 4, 6, 9], [6]], + [[1, 6, 10], [1, 4, 6, 10], [6]], [[1, 6, 11], [1, 4, 6, 11], [11]], + [[1, 7], [1, 4, 7], [4]], [[1, 7, 10], [1, 4, 7, 10], [4]], + [[1, 7, 11], [1, 4, 7, 11], [7]], [[1, 8], [1, 5, 8], [1]], + [[1, 8, 11], [1, 4, 8, 11], [1]], [[1, 9], [1, 4, 9], [9]], + [[1, 10], [1, 5, 10], [10]], [[1, 11], [2, 6, 11], [11]], [[2], [2, 9], [9]], + [[2, 5], [2, 5, 9], [2]], [[2, 5, 8], [2, 5, 8, 11], [2]], + [[2, 5, 8, 11], [1, 4, 7, 10], [0, 3, 8]], + [[2, 5, 9], [0, 3, 5, 9], [2, 5, 10]], [[2, 5, 10], [0, 3, 5, 9], [2, 10]], + [[2, 5, 11], [2, 5, 8, 11], [8]], [[2, 6], [2, 6, 9], [2]], + [[2, 6, 9], [1, 4, 6, 9], [1, 4, 9]], [[2, 6, 10], [1, 4, 6, 10], [1, 6, 10]], + [[2, 6, 11], [1, 4, 6, 10], [1, 6, 10]], [[2, 7], [2, 7, 11], [7]], + [[2, 7, 10], [0, 4, 7, 10], [0]], [[2, 7, 11], [1, 4, 6, 9], [1, 4, 9]], + [[2, 8], [4, 8, 11], [4]], [[2, 8, 11], [2, 5, 8, 11], [4]], + [[2, 9], [2, 6, 9], [2]], [[2, 10], [2, 5, 10], [10]], + [[2, 11], [2, 7, 11], [7]], [[3], [3, 10], [10]], [[3, 5], [3, 7, 10], [3]], + [[3, 5, 8], [0, 3, 5, 8], [8]], [[3, 5, 8, 11], [2, 5, 8, 11], [2]], + [[3, 5, 9], [0, 3, 5, 9], [5]], [[3, 5, 10], [0, 3, 5, 10], [5, 10]], + [[3, 5, 11], [3, 5, 8, 11], [5]], [[3, 7], [3, 7, 10], [3]], + [[3, 7, 10], [0, 3, 7, 10], [10]], [[3, 7, 11], [0, 3, 7, 10], [3, 7, 10]], + [[3, 8], [0, 3, 8], [8]], [[3, 8, 11], [3, 5, 8, 11], [11]], + [[3, 9], [0, 3, 9], [9]], [[3, 10], [3, 7, 10], [3]], + [[3, 11], [3, 8, 11], [8]], [[4], [4, 11], [11]], [[4, 6], [4, 7, 11], [4]], + [[4, 6, 9], [1, 4, 6, 9], [9]], [[4, 6, 10], [1, 4, 6, 10], [6]], + [[4, 6, 11], [1, 4, 6, 11], [11]], [[4, 7], [0, 4, 7], [0]], + [[4, 7, 10], [0, 4, 7, 10], [0]], [[4, 7, 11], [1, 4, 7, 11], [11]], + [[4, 8], [4, 8, 11], [4]], [[4, 8, 11], [1, 4, 8, 11], [4]], + [[4, 9], [1, 4, 9], [9]], [[4, 10], [4, 7, 10], [7]], + [[4, 11], [4, 8, 11], [4]], [[5], [0, 5, 9], [0]], [[5, 8], [0, 5, 8], [5]], + [[5, 8, 11], [1, 5, 8, 11], [1]], [[5, 9], [0, 5, 9], [5]], + [[5, 10], [2, 5, 10], [10]], [[5, 11], [0, 5, 9], [5]], [[6], [1, 6], [1]], + [[6, 9], [2, 6, 9], [2]], [[6, 10], [1, 6, 10], [6]], + [[6, 11], [2, 6, 11], [11]], [[7], [2, 7, 11], [2]], + [[7, 10], [2, 7, 10], [7]], [[7, 11], [2, 7, 11], [7]], [[8], [3, 8], [3]], + [[8, 11], [4, 8, 11], [4]], [[9], [4, 9], [4]], [[10], [2, 5, 10], [5]], + [[11], [6, 11], [6]]] + +################################################################################### + +def pitches_to_tones(pitches): + return [p % 12 for p in pitches] + +################################################################################### + +def tones_to_pitches(tones, base_octave=5): + return [(base_octave * 12) + t for t in tones] + +################################################################################### + +def find_closest_value(lst, val): + + closest_value = min(lst, key=lambda x: abs(val - x)) + closest_value_indexes = [i for i in range(len(lst)) if lst[i] == closest_value] + + return [closest_value, abs(val - closest_value), closest_value_indexes] + +################################################################################### + +def transpose_tones_chord(tones_chord, transpose_value=0): + return sorted([((60+t)+transpose_value) % 12 for t in sorted(set(tones_chord))]) + +################################################################################### + +def transpose_tones(tones, transpose_value=0): + return [((60+t)+transpose_value) % 12 for t in tones] + +################################################################################### + +def transpose_pitches_chord(pitches_chord, transpose_value=0): + return [max(1, min(127, p+transpose_value)) for p in sorted(set(pitches_chord), reverse=True)] + +################################################################################### + +def transpose_pitches(pitches, transpose_value=0): + return [max(1, min(127, p+transpose_value)) for p in pitches] + +################################################################################### + +def reverse_enhanced_score_notes(enhanced_score_notes): + + score = recalculate_score_timings(enhanced_score_notes) + + cscore = chordify_score([1000, score]) + + abs_dtimes = [] + + for i, t in enumerate(cscore[:-1]): + abs_dtimes.append(cscore[i+1][0][1]) + abs_dtimes.append(cscore[-1][0][1]+cscore[-1][0][2]) + + new_dtimes = [] + pt = abs_dtimes[-1] + + for t in abs_dtimes[::-1]: + new_dtimes.append(abs(pt-t)) + pt = t + + new_mel = copy.deepcopy(cscore[::-1]) + + time = 0 + + for i, t in enumerate(new_mel): + time += new_dtimes[i] + for tt in t: + tt[1] = time + + return recalculate_score_timings(flatten(new_mel)) + +################################################################################### + +# This is the end of the TMIDI X Python module + +################################################################################### \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..10f37715ef3c5b6a97b0a9a8e53a9cf2d91fd6f0 --- /dev/null +++ b/app.py @@ -0,0 +1,229 @@ +# https://huggingface.co/spaces/asigalov61/Melody2Song-Seq2Seq-Music-Transformer + +import os +import time as reqtime +import datetime +from pytz import timezone + +import torch + +import spaces +import gradio as gr + +from x_transformer_1_23_2 import * +import random +import tqdm + +from midi_to_colab_audio import midi_to_colab_audio +import TMIDIX + +import matplotlib.pyplot as plt + +in_space = os.getenv("SYSTEM") == "spaces" + +# ================================================================================================= + +@spaces.GPU +def GenerateSong(input_melody_seed_number): + print('=' * 70) + print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) + start_time = reqtime.time() + + print('Loading model...') + + SEQ_LEN = 2560 + PAD_IDX = 514 + DEVICE = 'cuda' # 'cuda' + + # instantiate the model + + model = TransformerWrapper( + num_tokens = PAD_IDX+1, + max_seq_len = SEQ_LEN, + attn_layers = Decoder(dim = 1024, depth = 24, heads = 16, attn_flash = True) + ) + + model = AutoregressiveWrapper(model, ignore_index = PAD_IDX) + + model.to(DEVICE) + print('=' * 70) + + print('Loading model checkpoint...') + + model.load_state_dict( + torch.load('Melody2Song_Seq2Seq_Music_Transformer_Trained_Model_28482_steps_0.719_loss_0.7865_acc.pth', + map_location=DEVICE)) + print('=' * 70) + + model.eval() + + if DEVICE == 'cpu': + dtype = torch.bfloat16 + else: + dtype = torch.bfloat16 + + ctx = torch.amp.autocast(device_type=DEVICE, dtype=dtype) + + print('Done!') + print('=' * 70) + seed_melody = seed_melodies_data[input_melody_seed_number] + print('Input melody seed number:', input_melody_seed_number) + print('-' * 70) + + #================================================================== + + print('=' * 70) + + print('Sample output events', seed_melody[:16]) + print('=' * 70) + print('Generating...') + + x = (torch.tensor(seed_melody, dtype=torch.long, device='cuda')[None, ...]) + + with ctx: + out = model.generate(x, + 1536, + temperature=0.9, + return_prime=False, + verbose=False) + + output = out[0].tolist() + + print('=' * 70) + print('Done!') + print('=' * 70) + + #=============================================================================== + print('Rendering results...') + + print('=' * 70) + print('Sample INTs', output[:15]) + print('=' * 70) + + out1 = output + + if len(out1) != 0: + + song = out1 + song_f = [] + + time = 0 + dur = 0 + vel = 90 + pitch = 0 + channel = 0 + + patches = [0] * 16 + patches[3] = 40 + + for ss in song: + + if 0 < ss < 128: + + time += (ss * 32) + + if 128 < ss < 256: + + dur = (ss-128) * 32 + + if 256 < ss < 512: + + pitch = (ss-256) % 128 + + channel = (ss-256) // 128 + + if channel == 1: + channel = 3 + vel = 110 + (pitch % 12) + song_f.append(['note', time, dur, channel, pitch, vel, 40]) + + else: + vel = 80 + (pitch % 12) + channel = 0 + song_f.append(['note', time, dur, channel, pitch, vel, 0]) + + fn1 = "Melody2Song-Seq2Seq-Music-Transformer-Composition" + + detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f, + output_signature = 'Melody2Song Seq2Seq Music Transformer', + output_file_name = fn1, + track_name='Project Los Angeles', + list_of_MIDI_patches=patches + ) + + new_fn = fn1+'.mid' + + + audio = midi_to_colab_audio(new_fn, + soundfont_path=soundfont, + sample_rate=16000, + volume_scale=10, + output_for_gradio=True + ) + + print('Done!') + print('=' * 70) + + #======================================================== + + output_midi_title = str(fn1) + output_midi_summary = str(song_f[:3]) + output_midi = str(new_fn) + output_audio = (16000, audio) + + output_plot = TMIDIX.plot_ms_SONG(song_f, plot_title=output_midi, return_plt=True) + + print('Output MIDI file name:', output_midi) + print('Output MIDI title:', output_midi_title) + print('Output MIDI summary:', output_midi_summary) + print('=' * 70) + + + #======================================================== + + print('-' * 70) + print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) + print('-' * 70) + print('Req execution time:', (reqtime.time() - start_time), 'sec') + + return output_midi_title, output_midi_summary, output_midi, output_audio, output_plot + +# ================================================================================================= + +if __name__ == "__main__": + + PDT = timezone('US/Pacific') + + print('=' * 70) + print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) + print('=' * 70) + + soundfont = "SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2" + + print('Loading seed meldoies data...') + seed_melodies_data = TMIDIX.Tegridy_Any_Pickle_File_Reader('Melody2Song_Seq2Seq_Music_Transformer_Seed_Melodies_Data') + print('=' * 70) + + app = gr.Blocks() + with app: + gr.Markdown("

Melody2Song Seq2Seq Music Transformer

") + gr.Markdown("

Generate unique songs from melodies with seq2seq music transformer

") + gr.Markdown( + "![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Melody2Song-Seq2Seq-Music-Transformer&style=flat)\n\n") + + input_melody_seed_number = gr.Slider(0, 203664, value=0, step=1, label="Select seed melody number") + + run_btn = gr.Button("generate", variant="primary") + + gr.Markdown("## Generation results") + + output_midi_title = gr.Textbox(label="Output MIDI title") + output_midi_summary = gr.Textbox(label="Output MIDI summary") + output_audio = gr.Audio(label="Output MIDI audio", format="wav", elem_id="midi_audio") + output_plot = gr.Plot(label="Output MIDI score plot") + output_midi = gr.File(label="Output MIDI file", file_types=[".mid"]) + + run_event = run_btn.click(GenerateSong, [input_melody_seed_number], + [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot]) + + app.queue().launch() \ No newline at end of file diff --git a/midi_to_colab_audio.py b/midi_to_colab_audio.py new file mode 100644 index 0000000000000000000000000000000000000000..65babf64516d2a45abce4a6fe7adb7ec8df0d25c --- /dev/null +++ b/midi_to_colab_audio.py @@ -0,0 +1,3090 @@ +r'''#=================================================================================================================== +# +# MIDI to Colab AUdio Python Module +# +# Converts any MIDI file to raw audio which is compatible +# with Google Colab or HUgging Face Gradio +# +# Version 1.0 +# +# Includes full source code of MIDI, pyfluidsynth, and midi_synthesizer Python modules +# +# Original source code for all modules was retrieved on 10/23/2023 +# +# Project Los Angeles +# Tegridy Code 2023 +# +#=================================================================================================================== +# +# Critical dependencies +# +# pip install numpy +# sudo apt install fluidsynth +# +#=================================================================================================================== +# +# Example usage: +# +# from midi_to_colab_audio import midi_to_colab_audio +# from IPython.display import display, Audio +# +# raw_audio = midi_to_colab_audio('/content/input.mid') +# +# display(Audio(raw_audio, rate=16000, normalize=False)) +# +#=================================================================================================================== +#! /usr/bin/python3 +# unsupported 20091104 ... +# ['set_sequence_number', dtime, sequence] +# ['raw_data', dtime, raw] + +# 20150914 jimbo1qaz MIDI.py str/bytes bug report +# I found a MIDI file which had Shift-JIS titles. When midi.py decodes it as +# latin-1, it produces a string which cannot even be accessed without raising +# a UnicodeDecodeError. Maybe, when converting raw byte strings from MIDI, +# you should keep them as bytes, not improperly decode them. However, this +# would change the API. (ie: text = a "string" ? of 0 or more bytes). It +# could break compatiblity, but there's not much else you can do to fix the bug +# https://en.wikipedia.org/wiki/Shift_JIS + +This module offers functions: concatenate_scores(), grep(), +merge_scores(), mix_scores(), midi2opus(), midi2score(), opus2midi(), +opus2score(), play_score(), score2midi(), score2opus(), score2stats(), +score_type(), segment(), timeshift() and to_millisecs(), +where "midi" means the MIDI-file bytes (as can be put in a .mid file, +or piped into aplaymidi), and "opus" and "score" are list-structures +as inspired by Sean Burke's MIDI-Perl CPAN module. + +Warning: Version 6.4 is not necessarily backward-compatible with +previous versions, in that text-data is now bytes, not strings. +This reflects the fact that many MIDI files have text data in +encodings other that ISO-8859-1, for example in Shift-JIS. + +Download MIDI.py from http://www.pjb.com.au/midi/free/MIDI.py +and put it in your PYTHONPATH. MIDI.py depends on Python3. + +There is also a call-compatible translation into Lua of this +module: see http://www.pjb.com.au/comp/lua/MIDI.html + +Backup web site: https://peterbillam.gitlab.io/miditools/ + +The "opus" is a direct translation of the midi-file-events, where +the times are delta-times, in ticks, since the previous event. + +The "score" is more human-centric; it uses absolute times, and +combines the separate note_on and note_off events into one "note" +event, with a duration: + ['note', start_time, duration, channel, note, velocity] # in a "score" + + EVENTS (in an "opus" structure) + ['note_off', dtime, channel, note, velocity] # in an "opus" + ['note_on', dtime, channel, note, velocity] # in an "opus" + ['key_after_touch', dtime, channel, note, velocity] + ['control_change', dtime, channel, controller(0-127), value(0-127)] + ['patch_change', dtime, channel, patch] + ['channel_after_touch', dtime, channel, velocity] + ['pitch_wheel_change', dtime, channel, pitch_wheel] + ['text_event', dtime, text] + ['copyright_text_event', dtime, text] + ['track_name', dtime, text] + ['instrument_name', dtime, text] + ['lyric', dtime, text] + ['marker', dtime, text] + ['cue_point', dtime, text] + ['text_event_08', dtime, text] + ['text_event_09', dtime, text] + ['text_event_0a', dtime, text] + ['text_event_0b', dtime, text] + ['text_event_0c', dtime, text] + ['text_event_0d', dtime, text] + ['text_event_0e', dtime, text] + ['text_event_0f', dtime, text] + ['end_track', dtime] + ['set_tempo', dtime, tempo] + ['smpte_offset', dtime, hr, mn, se, fr, ff] + ['time_signature', dtime, nn, dd, cc, bb] + ['key_signature', dtime, sf, mi] + ['sequencer_specific', dtime, raw] + ['raw_meta_event', dtime, command(0-255), raw] + ['sysex_f0', dtime, raw] + ['sysex_f7', dtime, raw] + ['song_position', dtime, song_pos] + ['song_select', dtime, song_number] + ['tune_request', dtime] + + DATA TYPES + channel = a value 0 to 15 + controller = 0 to 127 (see http://www.pjb.com.au/muscript/gm.html#cc ) + dtime = time measured in "ticks", 0 to 268435455 + velocity = a value 0 (soft) to 127 (loud) + note = a value 0 to 127 (middle-C is 60) + patch = 0 to 127 (see http://www.pjb.com.au/muscript/gm.html ) + pitch_wheel = a value -8192 to 8191 (0x1FFF) + raw = bytes, of length 0 or more (for sysex events see below) + sequence_number = a value 0 to 65,535 (0xFFFF) + song_pos = a value 0 to 16,383 (0x3FFF) + song_number = a value 0 to 127 + tempo = microseconds per crochet (quarter-note), 0 to 16777215 + text = bytes, of length 0 or more + ticks = the number of ticks per crochet (quarter-note) + + In sysex_f0 events, the raw data must not start with a \xF0 byte, + since this gets added automatically; + but it must end with an explicit \xF7 byte! + In the very unlikely case that you ever need to split sysex data + into one sysex_f0 followed by one or more sysex_f7s, then only the + last of those sysex_f7 events must end with the explicit \xF7 byte + (again, the raw data of individual sysex_f7 events must not start + with any \xF7 byte, since this gets added automatically). + + Since version 6.4, text data is in bytes, not in a ISO-8859-1 string. + + + GOING THROUGH A SCORE WITHIN A PYTHON PROGRAM + channels = {2,3,5,8,13} + itrack = 1 # skip 1st element which is ticks + while itrack < len(score): + for event in score[itrack]: + if event[0] == 'note': # for example, + pass # do something to all notes + # or, to work on events in only particular channels... + channel_index = MIDI.Event2channelindex.get(event[0], False) + if channel_index and (event[channel_index] in channels): + pass # do something to channels 2,3,5,8 and 13 + itrack += 1 + +''' + +import sys, struct, copy +# sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb') +Version = '6.7' +VersionDate = '20201120' +# 20201120 6.7 call to bytest() removed, and protect _unshift_ber_int +# 20160702 6.6 to_millisecs() now handles set_tempo across multiple Tracks +# 20150921 6.5 segment restores controllers as well as patch and tempo +# 20150914 6.4 text data is bytes or bytearray, not ISO-8859-1 strings +# 20150628 6.3 absent any set_tempo, default is 120bpm (see MIDI file spec 1.1) +# 20150101 6.2 all text events can be 8-bit; let user get the right encoding +# 20141231 6.1 fix _some_text_event; sequencer_specific data can be 8-bit +# 20141230 6.0 synth_specific data can be 8-bit +# 20120504 5.9 add the contents of mid_opus_tracks() +# 20120208 5.8 fix num_notes_by_channel() ; should be a dict +# 20120129 5.7 _encode handles empty tracks; score2stats num_notes_by_channel +# 20111111 5.6 fix patch 45 and 46 in Number2patch, should be Harp +# 20110129 5.5 add mix_opus_tracks() and event2alsaseq() +# 20110126 5.4 "previous message repeated N times" to save space on stderr +# 20110125 5.2 opus2score terminates unended notes at the end of the track +# 20110124 5.1 the warnings in midi2opus display track_num +# 21110122 5.0 if garbage, midi2opus returns the opus so far +# 21110119 4.9 non-ascii chars stripped out of the text_events +# 21110110 4.8 note_on with velocity=0 treated as a note-off +# 21110108 4.6 unknown F-series event correctly eats just one byte +# 21011010 4.2 segment() uses start_time, end_time named params +# 21011005 4.1 timeshift() must not pad the set_tempo command +# 21011003 4.0 pitch2note_event must be chapitch2note_event +# 21010918 3.9 set_sequence_number supported, FWIW +# 20100913 3.7 many small bugfixes; passes all tests +# 20100910 3.6 concatenate_scores enforce ticks=1000, just like merge_scores +# 20100908 3.5 minor bugs fixed in score2stats +# 20091104 3.4 tune_request now supported +# 20091104 3.3 fixed bug in decoding song_position and song_select +# 20091104 3.2 unsupported: set_sequence_number tune_request raw_data +# 20091101 3.1 document how to traverse a score within Python +# 20091021 3.0 fixed bug in score2stats detecting GM-mode = 0 +# 20091020 2.9 score2stats reports GM-mode and bank msb,lsb events +# 20091019 2.8 in merge_scores, channel 9 must remain channel 9 (in GM) +# 20091018 2.7 handles empty tracks gracefully +# 20091015 2.6 grep() selects channels +# 20091010 2.5 merge_scores reassigns channels to avoid conflicts +# 20091010 2.4 fixed bug in to_millisecs which now only does opusses +# 20091010 2.3 score2stats returns channels & patch_changes, by_track & total +# 20091010 2.2 score2stats() returns also pitches and percussion dicts +# 20091010 2.1 bugs: >= not > in segment, to notice patch_change at time 0 +# 20091010 2.0 bugs: spurious pop(0) ( in _decode sysex +# 20091008 1.9 bugs: ISO decoding in sysex; str( not int( in note-off warning +# 20091008 1.8 add concatenate_scores() +# 20091006 1.7 score2stats() measures nticks and ticks_per_quarter +# 20091004 1.6 first mix_scores() and merge_scores() +# 20090424 1.5 timeshift() bugfix: earliest only sees events after from_time +# 20090330 1.4 timeshift() has also a from_time argument +# 20090322 1.3 timeshift() has also a start_time argument +# 20090319 1.2 add segment() and timeshift() +# 20090301 1.1 add to_millisecs() + +_previous_warning = '' # 5.4 +_previous_times = 0 # 5.4 +#------------------------------- Encoding stuff -------------------------- + +def opus2midi(opus=[]): + r'''The argument is a list: the first item in the list is the "ticks" +parameter, the others are the tracks. Each track is a list +of midi-events, and each event is itself a list; see above. +opus2midi() returns a bytestring of the MIDI, which can then be +written either to a file opened in binary mode (mode='wb'), +or to stdout by means of: sys.stdout.buffer.write() + +my_opus = [ + 96, + [ # track 0: + ['patch_change', 0, 1, 8], # and these are the events... + ['note_on', 5, 1, 25, 96], + ['note_off', 96, 1, 25, 0], + ['note_on', 0, 1, 29, 96], + ['note_off', 96, 1, 29, 0], + ], # end of track 0 +] +my_midi = opus2midi(my_opus) +sys.stdout.buffer.write(my_midi) +''' + if len(opus) < 2: + opus=[1000, [],] + tracks = copy.deepcopy(opus) + ticks = int(tracks.pop(0)) + ntracks = len(tracks) + if ntracks == 1: + format = 0 + else: + format = 1 + + my_midi = b"MThd\x00\x00\x00\x06"+struct.pack('>HHH',format,ntracks,ticks) + for track in tracks: + events = _encode(track) + my_midi += b'MTrk' + struct.pack('>I',len(events)) + events + _clean_up_warnings() + return my_midi + + +def score2opus(score=None): + r''' +The argument is a list: the first item in the list is the "ticks" +parameter, the others are the tracks. Each track is a list +of score-events, and each event is itself a list. A score-event +is similar to an opus-event (see above), except that in a score: + 1) the times are expressed as an absolute number of ticks + from the track's start time + 2) the pairs of 'note_on' and 'note_off' events in an "opus" + are abstracted into a single 'note' event in a "score": + ['note', start_time, duration, channel, pitch, velocity] +score2opus() returns a list specifying the equivalent "opus". + +my_score = [ + 96, + [ # track 0: + ['patch_change', 0, 1, 8], + ['note', 5, 96, 1, 25, 96], + ['note', 101, 96, 1, 29, 96] + ], # end of track 0 +] +my_opus = score2opus(my_score) +''' + if len(score) < 2: + score=[1000, [],] + tracks = copy.deepcopy(score) + ticks = int(tracks.pop(0)) + opus_tracks = [] + for scoretrack in tracks: + time2events = dict([]) + for scoreevent in scoretrack: + if scoreevent[0] == 'note': + note_on_event = ['note_on',scoreevent[1], + scoreevent[3],scoreevent[4],scoreevent[5]] + note_off_event = ['note_off',scoreevent[1]+scoreevent[2], + scoreevent[3],scoreevent[4],scoreevent[5]] + if time2events.get(note_on_event[1]): + time2events[note_on_event[1]].append(note_on_event) + else: + time2events[note_on_event[1]] = [note_on_event,] + if time2events.get(note_off_event[1]): + time2events[note_off_event[1]].append(note_off_event) + else: + time2events[note_off_event[1]] = [note_off_event,] + continue + if time2events.get(scoreevent[1]): + time2events[scoreevent[1]].append(scoreevent) + else: + time2events[scoreevent[1]] = [scoreevent,] + + sorted_times = [] # list of keys + for k in time2events.keys(): + sorted_times.append(k) + sorted_times.sort() + + sorted_events = [] # once-flattened list of values sorted by key + for time in sorted_times: + sorted_events.extend(time2events[time]) + + abs_time = 0 + for event in sorted_events: # convert abs times => delta times + delta_time = event[1] - abs_time + abs_time = event[1] + event[1] = delta_time + opus_tracks.append(sorted_events) + opus_tracks.insert(0,ticks) + _clean_up_warnings() + return opus_tracks + +def score2midi(score=None): + r''' +Translates a "score" into MIDI, using score2opus() then opus2midi() +''' + return opus2midi(score2opus(score)) + +#--------------------------- Decoding stuff ------------------------ + +def midi2opus(midi=b''): + r'''Translates MIDI into a "opus". For a description of the +"opus" format, see opus2midi() +''' + my_midi=bytearray(midi) + if len(my_midi) < 4: + _clean_up_warnings() + return [1000,[],] + id = bytes(my_midi[0:4]) + if id != b'MThd': + _warn("midi2opus: midi starts with "+str(id)+" instead of 'MThd'") + _clean_up_warnings() + return [1000,[],] + [length, format, tracks_expected, ticks] = struct.unpack( + '>IHHH', bytes(my_midi[4:14])) + if length != 6: + _warn("midi2opus: midi header length was "+str(length)+" instead of 6") + _clean_up_warnings() + return [1000,[],] + my_opus = [ticks,] + my_midi = my_midi[14:] + track_num = 1 # 5.1 + while len(my_midi) >= 8: + track_type = bytes(my_midi[0:4]) + if track_type != b'MTrk': + _warn('midi2opus: Warning: track #'+str(track_num)+' type is '+str(track_type)+" instead of b'MTrk'") + [track_length] = struct.unpack('>I', my_midi[4:8]) + my_midi = my_midi[8:] + if track_length > len(my_midi): + _warn('midi2opus: track #'+str(track_num)+' length '+str(track_length)+' is too large') + _clean_up_warnings() + return my_opus # 5.0 + my_midi_track = my_midi[0:track_length] + my_track = _decode(my_midi_track) + my_opus.append(my_track) + my_midi = my_midi[track_length:] + track_num += 1 # 5.1 + _clean_up_warnings() + return my_opus + +def opus2score(opus=[]): + r'''For a description of the "opus" and "score" formats, +see opus2midi() and score2opus(). +''' + if len(opus) < 2: + _clean_up_warnings() + return [1000,[],] + tracks = copy.deepcopy(opus) # couple of slices probably quicker... + ticks = int(tracks.pop(0)) + score = [ticks,] + for opus_track in tracks: + ticks_so_far = 0 + score_track = [] + chapitch2note_on_events = dict([]) # 4.0 + for opus_event in opus_track: + ticks_so_far += opus_event[1] + if opus_event[0] == 'note_off' or (opus_event[0] == 'note_on' and opus_event[4] == 0): # 4.8 + cha = opus_event[2] + pitch = opus_event[3] + key = cha*128 + pitch + if chapitch2note_on_events.get(key): + new_event = chapitch2note_on_events[key].pop(0) + new_event[2] = ticks_so_far - new_event[1] + score_track.append(new_event) + elif pitch > 127: + pass #_warn('opus2score: note_off with no note_on, bad pitch='+str(pitch)) + else: + pass #_warn('opus2score: note_off with no note_on cha='+str(cha)+' pitch='+str(pitch)) + elif opus_event[0] == 'note_on': + cha = opus_event[2] + pitch = opus_event[3] + key = cha*128 + pitch + new_event = ['note',ticks_so_far,0,cha,pitch, opus_event[4]] + if chapitch2note_on_events.get(key): + chapitch2note_on_events[key].append(new_event) + else: + chapitch2note_on_events[key] = [new_event,] + else: + opus_event[1] = ticks_so_far + score_track.append(opus_event) + # check for unterminated notes (Oisín) -- 5.2 + for chapitch in chapitch2note_on_events: + note_on_events = chapitch2note_on_events[chapitch] + for new_e in note_on_events: + new_e[2] = ticks_so_far - new_e[1] + score_track.append(new_e) + pass #_warn("opus2score: note_on with no note_off cha="+str(new_e[3])+' pitch='+str(new_e[4])+'; adding note_off at end') + score.append(score_track) + _clean_up_warnings() + return score + +def midi2score(midi=b''): + r''' +Translates MIDI into a "score", using midi2opus() then opus2score() +''' + return opus2score(midi2opus(midi)) + +def midi2ms_score(midi=b''): + r''' +Translates MIDI into a "score" with one beat per second and one +tick per millisecond, using midi2opus() then to_millisecs() +then opus2score() +''' + return opus2score(to_millisecs(midi2opus(midi))) + +#------------------------ Other Transformations --------------------- + +def to_millisecs(old_opus=None): + r'''Recallibrates all the times in an "opus" to use one beat +per second and one tick per millisecond. This makes it +hard to retrieve any information about beats or barlines, +but it does make it easy to mix different scores together. +''' + if old_opus == None: + return [1000,[],] + try: + old_tpq = int(old_opus[0]) + except IndexError: # 5.0 + _warn('to_millisecs: the opus '+str(type(old_opus))+' has no elements') + return [1000,[],] + new_opus = [1000,] + # 6.7 first go through building a table of set_tempos by absolute-tick + ticks2tempo = {} + itrack = 1 + while itrack < len(old_opus): + ticks_so_far = 0 + for old_event in old_opus[itrack]: + if old_event[0] == 'note': + raise TypeError('to_millisecs needs an opus, not a score') + ticks_so_far += old_event[1] + if old_event[0] == 'set_tempo': + ticks2tempo[ticks_so_far] = old_event[2] + itrack += 1 + # then get the sorted-array of their keys + tempo_ticks = [] # list of keys + for k in ticks2tempo.keys(): + tempo_ticks.append(k) + tempo_ticks.sort() + # then go through converting to millisec, testing if the next + # set_tempo lies before the next track-event, and using it if so. + itrack = 1 + while itrack < len(old_opus): + ms_per_old_tick = 500.0 / old_tpq # float: will round later 6.3 + i_tempo_ticks = 0 + ticks_so_far = 0 + ms_so_far = 0.0 + previous_ms_so_far = 0.0 + new_track = [['set_tempo',0,1000000],] # new "crochet" is 1 sec + for old_event in old_opus[itrack]: + # detect if ticks2tempo has something before this event + # 20160702 if ticks2tempo is at the same time, leave it + event_delta_ticks = old_event[1] + if (i_tempo_ticks < len(tempo_ticks) and + tempo_ticks[i_tempo_ticks] < (ticks_so_far + old_event[1])): + delta_ticks = tempo_ticks[i_tempo_ticks] - ticks_so_far + ms_so_far += (ms_per_old_tick * delta_ticks) + ticks_so_far = tempo_ticks[i_tempo_ticks] + ms_per_old_tick = ticks2tempo[ticks_so_far] / (1000.0*old_tpq) + i_tempo_ticks += 1 + event_delta_ticks -= delta_ticks + new_event = copy.deepcopy(old_event) # now handle the new event + ms_so_far += (ms_per_old_tick * old_event[1]) + new_event[1] = round(ms_so_far - previous_ms_so_far) + if old_event[0] != 'set_tempo': + previous_ms_so_far = ms_so_far + new_track.append(new_event) + ticks_so_far += event_delta_ticks + new_opus.append(new_track) + itrack += 1 + _clean_up_warnings() + return new_opus + +def event2alsaseq(event=None): # 5.5 + r'''Converts an event into the format needed by the alsaseq module, +http://pp.com.mx/python/alsaseq +The type of track (opus or score) is autodetected. +''' + pass + +def grep(score=None, channels=None): + r'''Returns a "score" containing only the channels specified +''' + if score == None: + return [1000,[],] + ticks = score[0] + new_score = [ticks,] + if channels == None: + return new_score + channels = set(channels) + global Event2channelindex + itrack = 1 + while itrack < len(score): + new_score.append([]) + for event in score[itrack]: + channel_index = Event2channelindex.get(event[0], False) + if channel_index: + if event[channel_index] in channels: + new_score[itrack].append(event) + else: + new_score[itrack].append(event) + itrack += 1 + return new_score + +def play_score(score=None): + r'''Converts the "score" to midi, and feeds it into 'aplaymidi -' +''' + if score == None: + return + import subprocess + pipe = subprocess.Popen(['aplaymidi','-'], stdin=subprocess.PIPE) + if score_type(score) == 'opus': + pipe.stdin.write(opus2midi(score)) + else: + pipe.stdin.write(score2midi(score)) + pipe.stdin.close() + +def timeshift(score=None, shift=None, start_time=None, from_time=0, tracks={0,1,2,3,4,5,6,7,8,10,12,13,14,15}): + r'''Returns a "score" shifted in time by "shift" ticks, or shifted +so that the first event starts at "start_time" ticks. + +If "from_time" is specified, only those events in the score +that begin after it are shifted. If "start_time" is less than +"from_time" (or "shift" is negative), then the intermediate +notes are deleted, though patch-change events are preserved. + +If "tracks" are specified, then only those tracks get shifted. +"tracks" can be a list, tuple or set; it gets converted to set +internally. + +It is deprecated to specify both "shift" and "start_time". +If this does happen, timeshift() will print a warning to +stderr and ignore the "shift" argument. + +If "shift" is negative and sufficiently large that it would +leave some event with a negative tick-value, then the score +is shifted so that the first event occurs at time 0. This +also occurs if "start_time" is negative, and is also the +default if neither "shift" nor "start_time" are specified. +''' + #_warn('tracks='+str(tracks)) + if score == None or len(score) < 2: + return [1000, [],] + new_score = [score[0],] + my_type = score_type(score) + if my_type == '': + return new_score + if my_type == 'opus': + _warn("timeshift: opus format is not supported\n") + # _clean_up_scores() 6.2; doesn't exist! what was it supposed to do? + return new_score + if not (shift == None) and not (start_time == None): + _warn("timeshift: shift and start_time specified: ignoring shift\n") + shift = None + if shift == None: + if (start_time == None) or (start_time < 0): + start_time = 0 + # shift = start_time - from_time + + i = 1 # ignore first element (ticks) + tracks = set(tracks) # defend against tuples and lists + earliest = 1000000000 + if not (start_time == None) or shift < 0: # first find the earliest event + while i < len(score): + if len(tracks) and not ((i-1) in tracks): + i += 1 + continue + for event in score[i]: + if event[1] < from_time: + continue # just inspect the to_be_shifted events + if event[1] < earliest: + earliest = event[1] + i += 1 + if earliest > 999999999: + earliest = 0 + if shift == None: + shift = start_time - earliest + elif (earliest + shift) < 0: + start_time = 0 + shift = 0 - earliest + + i = 1 # ignore first element (ticks) + while i < len(score): + if len(tracks) == 0 or not ((i-1) in tracks): # 3.8 + new_score.append(score[i]) + i += 1 + continue + new_track = [] + for event in score[i]: + new_event = list(event) + #if new_event[1] == 0 and shift > 0 and new_event[0] != 'note': + # pass + #elif new_event[1] >= from_time: + if new_event[1] >= from_time: + # 4.1 must not rightshift set_tempo + if new_event[0] != 'set_tempo' or shift<0: + new_event[1] += shift + elif (shift < 0) and (new_event[1] >= (from_time+shift)): + continue + new_track.append(new_event) + if len(new_track) > 0: + new_score.append(new_track) + i += 1 + _clean_up_warnings() + return new_score + +def segment(score=None, start_time=None, end_time=None, start=0, end=100000000, + tracks={0,1,2,3,4,5,6,7,8,10,11,12,13,14,15}): + r'''Returns a "score" which is a segment of the one supplied +as the argument, beginning at "start_time" ticks and ending +at "end_time" ticks (or at the end if "end_time" is not supplied). +If the set "tracks" is specified, only those tracks will +be returned. +''' + if score == None or len(score) < 2: + return [1000, [],] + if start_time == None: # as of 4.2 start_time is recommended + start_time = start # start is legacy usage + if end_time == None: # likewise + end_time = end + new_score = [score[0],] + my_type = score_type(score) + if my_type == '': + return new_score + if my_type == 'opus': + # more difficult (disconnecting note_on's from their note_off's)... + _warn("segment: opus format is not supported\n") + _clean_up_warnings() + return new_score + i = 1 # ignore first element (ticks); we count in ticks anyway + tracks = set(tracks) # defend against tuples and lists + while i < len(score): + if len(tracks) and not ((i-1) in tracks): + i += 1 + continue + new_track = [] + channel2cc_num = {} # most recent controller change before start + channel2cc_val = {} + channel2cc_time = {} + channel2patch_num = {} # keep most recent patch change before start + channel2patch_time = {} + set_tempo_num = 500000 # most recent tempo change before start 6.3 + set_tempo_time = 0 + earliest_note_time = end_time + for event in score[i]: + if event[0] == 'control_change': # 6.5 + cc_time = channel2cc_time.get(event[2]) or 0 + if (event[1] <= start_time) and (event[1] >= cc_time): + channel2cc_num[event[2]] = event[3] + channel2cc_val[event[2]] = event[4] + channel2cc_time[event[2]] = event[1] + elif event[0] == 'patch_change': + patch_time = channel2patch_time.get(event[2]) or 0 + if (event[1]<=start_time) and (event[1] >= patch_time): # 2.0 + channel2patch_num[event[2]] = event[3] + channel2patch_time[event[2]] = event[1] + elif event[0] == 'set_tempo': + if (event[1]<=start_time) and (event[1]>=set_tempo_time): #6.4 + set_tempo_num = event[2] + set_tempo_time = event[1] + if (event[1] >= start_time) and (event[1] <= end_time): + new_track.append(event) + if (event[0] == 'note') and (event[1] < earliest_note_time): + earliest_note_time = event[1] + if len(new_track) > 0: + new_track.append(['set_tempo', start_time, set_tempo_num]) + for c in channel2patch_num: + new_track.append(['patch_change',start_time,c,channel2patch_num[c]],) + for c in channel2cc_num: # 6.5 + new_track.append(['control_change',start_time,c,channel2cc_num[c],channel2cc_val[c]]) + new_score.append(new_track) + i += 1 + _clean_up_warnings() + return new_score + +def score_type(opus_or_score=None): + r'''Returns a string, either 'opus' or 'score' or '' +''' + if opus_or_score == None or str(type(opus_or_score)).find('list')<0 or len(opus_or_score) < 2: + return '' + i = 1 # ignore first element + while i < len(opus_or_score): + for event in opus_or_score[i]: + if event[0] == 'note': + return 'score' + elif event[0] == 'note_on': + return 'opus' + i += 1 + return '' + +def concatenate_scores(scores): + r'''Concatenates a list of scores into one score. +If the scores differ in their "ticks" parameter, +they will all get converted to millisecond-tick format. +''' + # the deepcopys are needed if the input_score's are refs to the same obj + # e.g. if invoked by midisox's repeat() + input_scores = _consistentise_ticks(scores) # 3.7 + output_score = copy.deepcopy(input_scores[0]) + for input_score in input_scores[1:]: + output_stats = score2stats(output_score) + delta_ticks = output_stats['nticks'] + itrack = 1 + while itrack < len(input_score): + if itrack >= len(output_score): # new output track if doesn't exist + output_score.append([]) + for event in input_score[itrack]: + output_score[itrack].append(copy.deepcopy(event)) + output_score[itrack][-1][1] += delta_ticks + itrack += 1 + return output_score + +def merge_scores(scores): + r'''Merges a list of scores into one score. A merged score comprises +all of the tracks from all of the input scores; un-merging is possible +by selecting just some of the tracks. If the scores differ in their +"ticks" parameter, they will all get converted to millisecond-tick +format. merge_scores attempts to resolve channel-conflicts, +but there are of course only 15 available channels... +''' + input_scores = _consistentise_ticks(scores) # 3.6 + output_score = [1000] + channels_so_far = set() + all_channels = {0,1,2,3,4,5,6,7,8,10,11,12,13,14,15} + global Event2channelindex + for input_score in input_scores: + new_channels = set(score2stats(input_score).get('channels_total', [])) + new_channels.discard(9) # 2.8 cha9 must remain cha9 (in GM) + for channel in channels_so_far & new_channels: + # consistently choose lowest avaiable, to ease testing + free_channels = list(all_channels - (channels_so_far|new_channels)) + if len(free_channels) > 0: + free_channels.sort() + free_channel = free_channels[0] + else: + free_channel = None + break + itrack = 1 + while itrack < len(input_score): + for input_event in input_score[itrack]: + channel_index=Event2channelindex.get(input_event[0],False) + if channel_index and input_event[channel_index]==channel: + input_event[channel_index] = free_channel + itrack += 1 + channels_so_far.add(free_channel) + + channels_so_far |= new_channels + output_score.extend(input_score[1:]) + return output_score + +def _ticks(event): + return event[1] +def mix_opus_tracks(input_tracks): # 5.5 + r'''Mixes an array of tracks into one track. A mixed track +cannot be un-mixed. It is assumed that the tracks share the same +ticks parameter and the same tempo. +Mixing score-tracks is trivial (just insert all events into one array). +Mixing opus-tracks is only slightly harder, but it's common enough +that a dedicated function is useful. +''' + output_score = [1000, []] + for input_track in input_tracks: # 5.8 + input_score = opus2score([1000, input_track]) + for event in input_score[1]: + output_score[1].append(event) + output_score[1].sort(key=_ticks) + output_opus = score2opus(output_score) + return output_opus[1] + +def mix_scores(scores): + r'''Mixes a list of scores into one one-track score. +A mixed score cannot be un-mixed. Hopefully the scores +have no undesirable channel-conflicts between them. +If the scores differ in their "ticks" parameter, +they will all get converted to millisecond-tick format. +''' + input_scores = _consistentise_ticks(scores) # 3.6 + output_score = [1000, []] + for input_score in input_scores: + for input_track in input_score[1:]: + output_score[1].extend(input_track) + return output_score + +def score2stats(opus_or_score=None): + r'''Returns a dict of some basic stats about the score, like +bank_select (list of tuples (msb,lsb)), +channels_by_track (list of lists), channels_total (set), +general_midi_mode (list), +ntracks, nticks, patch_changes_by_track (list of dicts), +num_notes_by_channel (list of numbers), +patch_changes_total (set), +percussion (dict histogram of channel 9 events), +pitches (dict histogram of pitches on channels other than 9), +pitch_range_by_track (list, by track, of two-member-tuples), +pitch_range_sum (sum over tracks of the pitch_ranges), +''' + bank_select_msb = -1 + bank_select_lsb = -1 + bank_select = [] + channels_by_track = [] + channels_total = set([]) + general_midi_mode = [] + num_notes_by_channel = dict([]) + patches_used_by_track = [] + patches_used_total = set([]) + patch_changes_by_track = [] + patch_changes_total = set([]) + percussion = dict([]) # histogram of channel 9 "pitches" + pitches = dict([]) # histogram of pitch-occurrences channels 0-8,10-15 + pitch_range_sum = 0 # u pitch-ranges of each track + pitch_range_by_track = [] + is_a_score = True + if opus_or_score == None: + return {'bank_select':[], 'channels_by_track':[], 'channels_total':[], + 'general_midi_mode':[], 'ntracks':0, 'nticks':0, + 'num_notes_by_channel':dict([]), + 'patch_changes_by_track':[], 'patch_changes_total':[], + 'percussion':{}, 'pitches':{}, 'pitch_range_by_track':[], + 'ticks_per_quarter':0, 'pitch_range_sum':0} + ticks_per_quarter = opus_or_score[0] + i = 1 # ignore first element, which is ticks + nticks = 0 + while i < len(opus_or_score): + highest_pitch = 0 + lowest_pitch = 128 + channels_this_track = set([]) + patch_changes_this_track = dict({}) + for event in opus_or_score[i]: + if event[0] == 'note': + num_notes_by_channel[event[3]] = num_notes_by_channel.get(event[3],0) + 1 + if event[3] == 9: + percussion[event[4]] = percussion.get(event[4],0) + 1 + else: + pitches[event[4]] = pitches.get(event[4],0) + 1 + if event[4] > highest_pitch: + highest_pitch = event[4] + if event[4] < lowest_pitch: + lowest_pitch = event[4] + channels_this_track.add(event[3]) + channels_total.add(event[3]) + finish_time = event[1] + event[2] + if finish_time > nticks: + nticks = finish_time + elif event[0] == 'note_off' or (event[0] == 'note_on' and event[4] == 0): # 4.8 + finish_time = event[1] + if finish_time > nticks: + nticks = finish_time + elif event[0] == 'note_on': + is_a_score = False + num_notes_by_channel[event[2]] = num_notes_by_channel.get(event[2],0) + 1 + if event[2] == 9: + percussion[event[3]] = percussion.get(event[3],0) + 1 + else: + pitches[event[3]] = pitches.get(event[3],0) + 1 + if event[3] > highest_pitch: + highest_pitch = event[3] + if event[3] < lowest_pitch: + lowest_pitch = event[3] + channels_this_track.add(event[2]) + channels_total.add(event[2]) + elif event[0] == 'patch_change': + patch_changes_this_track[event[2]] = event[3] + patch_changes_total.add(event[3]) + elif event[0] == 'control_change': + if event[3] == 0: # bank select MSB + bank_select_msb = event[4] + elif event[3] == 32: # bank select LSB + bank_select_lsb = event[4] + if bank_select_msb >= 0 and bank_select_lsb >= 0: + bank_select.append((bank_select_msb,bank_select_lsb)) + bank_select_msb = -1 + bank_select_lsb = -1 + elif event[0] == 'sysex_f0': + if _sysex2midimode.get(event[2], -1) >= 0: + general_midi_mode.append(_sysex2midimode.get(event[2])) + if is_a_score: + if event[1] > nticks: + nticks = event[1] + else: + nticks += event[1] + if lowest_pitch == 128: + lowest_pitch = 0 + channels_by_track.append(channels_this_track) + patch_changes_by_track.append(patch_changes_this_track) + pitch_range_by_track.append((lowest_pitch,highest_pitch)) + pitch_range_sum += (highest_pitch-lowest_pitch) + i += 1 + + return {'bank_select':bank_select, + 'channels_by_track':channels_by_track, + 'channels_total':channels_total, + 'general_midi_mode':general_midi_mode, + 'ntracks':len(opus_or_score)-1, + 'nticks':nticks, + 'num_notes_by_channel':num_notes_by_channel, + 'patch_changes_by_track':patch_changes_by_track, + 'patch_changes_total':patch_changes_total, + 'percussion':percussion, + 'pitches':pitches, + 'pitch_range_by_track':pitch_range_by_track, + 'pitch_range_sum':pitch_range_sum, + 'ticks_per_quarter':ticks_per_quarter} + +#----------------------------- Event stuff -------------------------- + +_sysex2midimode = { + "\x7E\x7F\x09\x01\xF7": 1, + "\x7E\x7F\x09\x02\xF7": 0, + "\x7E\x7F\x09\x03\xF7": 2, +} + +# Some public-access tuples: +MIDI_events = tuple('''note_off note_on key_after_touch +control_change patch_change channel_after_touch +pitch_wheel_change'''.split()) + +Text_events = tuple('''text_event copyright_text_event +track_name instrument_name lyric marker cue_point text_event_08 +text_event_09 text_event_0a text_event_0b text_event_0c +text_event_0d text_event_0e text_event_0f'''.split()) + +Nontext_meta_events = tuple('''end_track set_tempo +smpte_offset time_signature key_signature sequencer_specific +raw_meta_event sysex_f0 sysex_f7 song_position song_select +tune_request'''.split()) +# unsupported: raw_data + +# Actually, 'tune_request' is is F-series event, not strictly a meta-event... +Meta_events = Text_events + Nontext_meta_events +All_events = MIDI_events + Meta_events + +# And three dictionaries: +Number2patch = { # General MIDI patch numbers: +0:'Acoustic Grand', +1:'Bright Acoustic', +2:'Electric Grand', +3:'Honky-Tonk', +4:'Electric Piano 1', +5:'Electric Piano 2', +6:'Harpsichord', +7:'Clav', +8:'Celesta', +9:'Glockenspiel', +10:'Music Box', +11:'Vibraphone', +12:'Marimba', +13:'Xylophone', +14:'Tubular Bells', +15:'Dulcimer', +16:'Drawbar Organ', +17:'Percussive Organ', +18:'Rock Organ', +19:'Church Organ', +20:'Reed Organ', +21:'Accordion', +22:'Harmonica', +23:'Tango Accordion', +24:'Acoustic Guitar(nylon)', +25:'Acoustic Guitar(steel)', +26:'Electric Guitar(jazz)', +27:'Electric Guitar(clean)', +28:'Electric Guitar(muted)', +29:'Overdriven Guitar', +30:'Distortion Guitar', +31:'Guitar Harmonics', +32:'Acoustic Bass', +33:'Electric Bass(finger)', +34:'Electric Bass(pick)', +35:'Fretless Bass', +36:'Slap Bass 1', +37:'Slap Bass 2', +38:'Synth Bass 1', +39:'Synth Bass 2', +40:'Violin', +41:'Viola', +42:'Cello', +43:'Contrabass', +44:'Tremolo Strings', +45:'Pizzicato Strings', +46:'Orchestral Harp', +47:'Timpani', +48:'String Ensemble 1', +49:'String Ensemble 2', +50:'SynthStrings 1', +51:'SynthStrings 2', +52:'Choir Aahs', +53:'Voice Oohs', +54:'Synth Voice', +55:'Orchestra Hit', +56:'Trumpet', +57:'Trombone', +58:'Tuba', +59:'Muted Trumpet', +60:'French Horn', +61:'Brass Section', +62:'SynthBrass 1', +63:'SynthBrass 2', +64:'Soprano Sax', +65:'Alto Sax', +66:'Tenor Sax', +67:'Baritone Sax', +68:'Oboe', +69:'English Horn', +70:'Bassoon', +71:'Clarinet', +72:'Piccolo', +73:'Flute', +74:'Recorder', +75:'Pan Flute', +76:'Blown Bottle', +77:'Skakuhachi', +78:'Whistle', +79:'Ocarina', +80:'Lead 1 (square)', +81:'Lead 2 (sawtooth)', +82:'Lead 3 (calliope)', +83:'Lead 4 (chiff)', +84:'Lead 5 (charang)', +85:'Lead 6 (voice)', +86:'Lead 7 (fifths)', +87:'Lead 8 (bass+lead)', +88:'Pad 1 (new age)', +89:'Pad 2 (warm)', +90:'Pad 3 (polysynth)', +91:'Pad 4 (choir)', +92:'Pad 5 (bowed)', +93:'Pad 6 (metallic)', +94:'Pad 7 (halo)', +95:'Pad 8 (sweep)', +96:'FX 1 (rain)', +97:'FX 2 (soundtrack)', +98:'FX 3 (crystal)', +99:'FX 4 (atmosphere)', +100:'FX 5 (brightness)', +101:'FX 6 (goblins)', +102:'FX 7 (echoes)', +103:'FX 8 (sci-fi)', +104:'Sitar', +105:'Banjo', +106:'Shamisen', +107:'Koto', +108:'Kalimba', +109:'Bagpipe', +110:'Fiddle', +111:'Shanai', +112:'Tinkle Bell', +113:'Agogo', +114:'Steel Drums', +115:'Woodblock', +116:'Taiko Drum', +117:'Melodic Tom', +118:'Synth Drum', +119:'Reverse Cymbal', +120:'Guitar Fret Noise', +121:'Breath Noise', +122:'Seashore', +123:'Bird Tweet', +124:'Telephone Ring', +125:'Helicopter', +126:'Applause', +127:'Gunshot', +} +Notenum2percussion = { # General MIDI Percussion (on Channel 9): +35:'Acoustic Bass Drum', +36:'Bass Drum 1', +37:'Side Stick', +38:'Acoustic Snare', +39:'Hand Clap', +40:'Electric Snare', +41:'Low Floor Tom', +42:'Closed Hi-Hat', +43:'High Floor Tom', +44:'Pedal Hi-Hat', +45:'Low Tom', +46:'Open Hi-Hat', +47:'Low-Mid Tom', +48:'Hi-Mid Tom', +49:'Crash Cymbal 1', +50:'High Tom', +51:'Ride Cymbal 1', +52:'Chinese Cymbal', +53:'Ride Bell', +54:'Tambourine', +55:'Splash Cymbal', +56:'Cowbell', +57:'Crash Cymbal 2', +58:'Vibraslap', +59:'Ride Cymbal 2', +60:'Hi Bongo', +61:'Low Bongo', +62:'Mute Hi Conga', +63:'Open Hi Conga', +64:'Low Conga', +65:'High Timbale', +66:'Low Timbale', +67:'High Agogo', +68:'Low Agogo', +69:'Cabasa', +70:'Maracas', +71:'Short Whistle', +72:'Long Whistle', +73:'Short Guiro', +74:'Long Guiro', +75:'Claves', +76:'Hi Wood Block', +77:'Low Wood Block', +78:'Mute Cuica', +79:'Open Cuica', +80:'Mute Triangle', +81:'Open Triangle', +} + +Event2channelindex = { 'note':3, 'note_off':2, 'note_on':2, + 'key_after_touch':2, 'control_change':2, 'patch_change':2, + 'channel_after_touch':2, 'pitch_wheel_change':2 +} + +################################################################ +# The code below this line is full of frightening things, all to +# do with the actual encoding and decoding of binary MIDI data. + +def _twobytes2int(byte_a): + r'''decode a 16 bit quantity from two bytes,''' + return (byte_a[1] | (byte_a[0] << 8)) + +def _int2twobytes(int_16bit): + r'''encode a 16 bit quantity into two bytes,''' + return bytes([(int_16bit>>8) & 0xFF, int_16bit & 0xFF]) + +def _read_14_bit(byte_a): + r'''decode a 14 bit quantity from two bytes,''' + return (byte_a[0] | (byte_a[1] << 7)) + +def _write_14_bit(int_14bit): + r'''encode a 14 bit quantity into two bytes,''' + return bytes([int_14bit & 0x7F, (int_14bit>>7) & 0x7F]) + +def _ber_compressed_int(integer): + r'''BER compressed integer (not an ASN.1 BER, see perlpacktut for +details). Its bytes represent an unsigned integer in base 128, +most significant digit first, with as few digits as possible. +Bit eight (the high bit) is set on each byte except the last. +''' + ber = bytearray(b'') + seven_bits = 0x7F & integer + ber.insert(0, seven_bits) # XXX surely should convert to a char ? + integer >>= 7 + while integer > 0: + seven_bits = 0x7F & integer + ber.insert(0, 0x80|seven_bits) # XXX surely should convert to a char ? + integer >>= 7 + return ber + +def _unshift_ber_int(ba): + r'''Given a bytearray, returns a tuple of (the ber-integer at the +start, and the remainder of the bytearray). +''' + if not len(ba): # 6.7 + _warn('_unshift_ber_int: no integer found') + return ((0, b"")) + byte = ba.pop(0) + integer = 0 + while True: + integer += (byte & 0x7F) + if not (byte & 0x80): + return ((integer, ba)) + if not len(ba): + _warn('_unshift_ber_int: no end-of-integer found') + return ((0, ba)) + byte = ba.pop(0) + integer <<= 7 + +def _clean_up_warnings(): # 5.4 + # Call this before returning from any publicly callable function + # whenever there's a possibility that a warning might have been printed + # by the function, or by any private functions it might have called. + global _previous_times + global _previous_warning + if _previous_times > 1: + # E:1176, 0: invalid syntax (, line 1176) (syntax-error) ??? + # print(' previous message repeated '+str(_previous_times)+' times', file=sys.stderr) + # 6.7 + sys.stderr.write(' previous message repeated {0} times\n'.format(_previous_times)) + elif _previous_times > 0: + sys.stderr.write(' previous message repeated\n') + _previous_times = 0 + _previous_warning = '' + +def _warn(s=''): + global _previous_times + global _previous_warning + if s == _previous_warning: # 5.4 + _previous_times = _previous_times + 1 + else: + _clean_up_warnings() + sys.stderr.write(str(s)+"\n") + _previous_warning = s + +def _some_text_event(which_kind=0x01, text=b'some_text'): + if str(type(text)).find("'str'") >= 0: # 6.4 test for back-compatibility + data = bytes(text, encoding='ISO-8859-1') + else: + data = bytes(text) + return b'\xFF'+bytes((which_kind,))+_ber_compressed_int(len(data))+data + +def _consistentise_ticks(scores): # 3.6 + # used by mix_scores, merge_scores, concatenate_scores + if len(scores) == 1: + return copy.deepcopy(scores) + are_consistent = True + ticks = scores[0][0] + iscore = 1 + while iscore < len(scores): + if scores[iscore][0] != ticks: + are_consistent = False + break + iscore += 1 + if are_consistent: + return copy.deepcopy(scores) + new_scores = [] + iscore = 0 + while iscore < len(scores): + score = scores[iscore] + new_scores.append(opus2score(to_millisecs(score2opus(score)))) + iscore += 1 + return new_scores + + +########################################################################### + +def _decode(trackdata=b'', exclude=None, include=None, + event_callback=None, exclusive_event_callback=None, no_eot_magic=False): + r'''Decodes MIDI track data into an opus-style list of events. +The options: + 'exclude' is a list of event types which will be ignored SHOULD BE A SET + 'include' (and no exclude), makes exclude a list + of all possible events, /minus/ what include specifies + 'event_callback' is a coderef + 'exclusive_event_callback' is a coderef +''' + trackdata = bytearray(trackdata) + if exclude == None: + exclude = [] + if include == None: + include = [] + if include and not exclude: + exclude = All_events + include = set(include) + exclude = set(exclude) + + # Pointer = 0; not used here; we eat through the bytearray instead. + event_code = -1; # used for running status + event_count = 0; + events = [] + + while(len(trackdata)): + # loop while there's anything to analyze ... + eot = False # When True, the event registrar aborts this loop + event_count += 1 + + E = [] + # E for events - we'll feed it to the event registrar at the end. + + # Slice off the delta time code, and analyze it + [time, remainder] = _unshift_ber_int(trackdata) + + # Now let's see what we can make of the command + first_byte = trackdata.pop(0) & 0xFF + + if (first_byte < 0xF0): # It's a MIDI event + if (first_byte & 0x80): + event_code = first_byte + else: + # It wants running status; use last event_code value + trackdata.insert(0, first_byte) + if (event_code == -1): + _warn("Running status not set; Aborting track.") + return [] + + command = event_code & 0xF0 + channel = event_code & 0x0F + + if (command == 0xF6): # 0-byte argument + pass + elif (command == 0xC0 or command == 0xD0): # 1-byte argument + parameter = trackdata.pop(0) # could be B + else: # 2-byte argument could be BB or 14-bit + parameter = (trackdata.pop(0), trackdata.pop(0)) + + ################################################################# + # MIDI events + + if (command == 0x80): + if 'note_off' in exclude: + continue + E = ['note_off', time, channel, parameter[0], parameter[1]] + elif (command == 0x90): + if 'note_on' in exclude: + continue + E = ['note_on', time, channel, parameter[0], parameter[1]] + elif (command == 0xA0): + if 'key_after_touch' in exclude: + continue + E = ['key_after_touch',time,channel,parameter[0],parameter[1]] + elif (command == 0xB0): + if 'control_change' in exclude: + continue + E = ['control_change',time,channel,parameter[0],parameter[1]] + elif (command == 0xC0): + if 'patch_change' in exclude: + continue + E = ['patch_change', time, channel, parameter] + elif (command == 0xD0): + if 'channel_after_touch' in exclude: + continue + E = ['channel_after_touch', time, channel, parameter] + elif (command == 0xE0): + if 'pitch_wheel_change' in exclude: + continue + E = ['pitch_wheel_change', time, channel, + _read_14_bit(parameter)-0x2000] + else: + _warn("Shouldn't get here; command="+hex(command)) + + elif (first_byte == 0xFF): # It's a Meta-Event! ################## + #[command, length, remainder] = + # unpack("xCwa*", substr(trackdata, $Pointer, 6)); + #Pointer += 6 - len(remainder); + # # Move past JUST the length-encoded. + command = trackdata.pop(0) & 0xFF + [length, trackdata] = _unshift_ber_int(trackdata) + if (command == 0x00): + if (length == 2): + E = ['set_sequence_number',time,_twobytes2int(trackdata)] + else: + _warn('set_sequence_number: length must be 2, not '+str(length)) + E = ['set_sequence_number', time, 0] + + elif command >= 0x01 and command <= 0x0f: # Text events + # 6.2 take it in bytes; let the user get the right encoding. + # text_str = trackdata[0:length].decode('ascii','ignore') + # text_str = trackdata[0:length].decode('ISO-8859-1') + # 6.4 take it in bytes; let the user get the right encoding. + text_data = bytes(trackdata[0:length]) # 6.4 + # Defined text events + if (command == 0x01): + E = ['text_event', time, text_data] + elif (command == 0x02): + E = ['copyright_text_event', time, text_data] + elif (command == 0x03): + E = ['track_name', time, text_data] + elif (command == 0x04): + E = ['instrument_name', time, text_data] + elif (command == 0x05): + E = ['lyric', time, text_data] + elif (command == 0x06): + E = ['marker', time, text_data] + elif (command == 0x07): + E = ['cue_point', time, text_data] + # Reserved but apparently unassigned text events + elif (command == 0x08): + E = ['text_event_08', time, text_data] + elif (command == 0x09): + E = ['text_event_09', time, text_data] + elif (command == 0x0a): + E = ['text_event_0a', time, text_data] + elif (command == 0x0b): + E = ['text_event_0b', time, text_data] + elif (command == 0x0c): + E = ['text_event_0c', time, text_data] + elif (command == 0x0d): + E = ['text_event_0d', time, text_data] + elif (command == 0x0e): + E = ['text_event_0e', time, text_data] + elif (command == 0x0f): + E = ['text_event_0f', time, text_data] + + # Now the sticky events ------------------------------------- + elif (command == 0x2F): + E = ['end_track', time] + # The code for handling this, oddly, comes LATER, + # in the event registrar. + elif (command == 0x51): # DTime, Microseconds/Crochet + if length != 3: + _warn('set_tempo event, but length='+str(length)) + E = ['set_tempo', time, + struct.unpack(">I", b'\x00'+trackdata[0:3])[0]] + elif (command == 0x54): + if length != 5: # DTime, HR, MN, SE, FR, FF + _warn('smpte_offset event, but length='+str(length)) + E = ['smpte_offset',time] + list(struct.unpack(">BBBBB",trackdata[0:5])) + elif (command == 0x58): + if length != 4: # DTime, NN, DD, CC, BB + _warn('time_signature event, but length='+str(length)) + E = ['time_signature', time]+list(trackdata[0:4]) + elif (command == 0x59): + if length != 2: # DTime, SF(signed), MI + _warn('key_signature event, but length='+str(length)) + E = ['key_signature',time] + list(struct.unpack(">bB",trackdata[0:2])) + elif (command == 0x7F): # 6.4 + E = ['sequencer_specific',time, bytes(trackdata[0:length])] + else: + E = ['raw_meta_event', time, command, + bytes(trackdata[0:length])] # 6.0 + #"[uninterpretable meta-event command of length length]" + # DTime, Command, Binary Data + # It's uninterpretable; record it as raw_data. + + # Pointer += length; # Now move Pointer + trackdata = trackdata[length:] + + ###################################################################### + elif (first_byte == 0xF0 or first_byte == 0xF7): + # Note that sysexes in MIDI /files/ are different than sysexes + # in MIDI transmissions!! The vast majority of system exclusive + # messages will just use the F0 format. For instance, the + # transmitted message F0 43 12 00 07 F7 would be stored in a + # MIDI file as F0 05 43 12 00 07 F7. As mentioned above, it is + # required to include the F7 at the end so that the reader of the + # MIDI file knows that it has read the entire message. (But the F7 + # is omitted if this is a non-final block in a multiblock sysex; + # but the F7 (if there) is counted in the message's declared + # length, so we don't have to think about it anyway.) + #command = trackdata.pop(0) + [length, trackdata] = _unshift_ber_int(trackdata) + if first_byte == 0xF0: + # 20091008 added ISO-8859-1 to get an 8-bit str + # 6.4 return bytes instead + E = ['sysex_f0', time, bytes(trackdata[0:length])] + else: + E = ['sysex_f7', time, bytes(trackdata[0:length])] + trackdata = trackdata[length:] + + ###################################################################### + # Now, the MIDI file spec says: + # = + + # = + # = | | + # I know that, on the wire, can include note_on, + # note_off, and all the other 8x to Ex events, AND Fx events + # other than F0, F7, and FF -- namely, , + # , and . + # + # Whether these can occur in MIDI files is not clear specified + # from the MIDI file spec. So, I'm going to assume that + # they CAN, in practice, occur. I don't know whether it's + # proper for you to actually emit these into a MIDI file. + + elif (first_byte == 0xF2): # DTime, Beats + # ::= F2 + E = ['song_position', time, _read_14_bit(trackdata[:2])] + trackdata = trackdata[2:] + + elif (first_byte == 0xF3): # ::= F3 + # E = ['song_select', time, struct.unpack('>B',trackdata.pop(0))[0]] + E = ['song_select', time, trackdata[0]] + trackdata = trackdata[1:] + # DTime, Thing (what?! song number? whatever ...) + + elif (first_byte == 0xF6): # DTime + E = ['tune_request', time] + # What would a tune request be doing in a MIDI /file/? + + ######################################################### + # ADD MORE META-EVENTS HERE. TODO: + # f1 -- MTC Quarter Frame Message. One data byte follows + # the Status; it's the time code value, from 0 to 127. + # f8 -- MIDI clock. no data. + # fa -- MIDI start. no data. + # fb -- MIDI continue. no data. + # fc -- MIDI stop. no data. + # fe -- Active sense. no data. + # f4 f5 f9 fd -- unallocated + + r''' + elif (first_byte > 0xF0) { # Some unknown kinda F-series event #### + # Here we only produce a one-byte piece of raw data. + # But the encoder for 'raw_data' accepts any length of it. + E = [ 'raw_data', + time, substr(trackdata,Pointer,1) ] + # DTime and the Data (in this case, the one Event-byte) + ++Pointer; # itself + +''' + elif first_byte > 0xF0: # Some unknown F-series event + # Here we only produce a one-byte piece of raw data. + # E = ['raw_data', time, bytest(trackdata[0])] # 6.4 + E = ['raw_data', time, trackdata[0]] # 6.4 6.7 + trackdata = trackdata[1:] + else: # Fallthru. + _warn("Aborting track. Command-byte first_byte="+hex(first_byte)) + break + # End of the big if-group + + + ###################################################################### + # THE EVENT REGISTRAR... + if E and (E[0] == 'end_track'): + # This is the code for exceptional handling of the EOT event. + eot = True + if not no_eot_magic: + if E[1] > 0: # a null text-event to carry the delta-time + E = ['text_event', E[1], ''] + else: + E = [] # EOT with a delta-time of 0; ignore it. + + if E and not (E[0] in exclude): + #if ( $exclusive_event_callback ): + # &{ $exclusive_event_callback }( @E ); + #else: + # &{ $event_callback }( @E ) if $event_callback; + events.append(E) + if eot: + break + + # End of the big "Event" while-block + + return events + + +########################################################################### +def _encode(events_lol, unknown_callback=None, never_add_eot=False, + no_eot_magic=False, no_running_status=False): + # encode an event structure, presumably for writing to a file + # Calling format: + # $data_r = MIDI::Event::encode( \@event_lol, { options } ); + # Takes a REFERENCE to an event structure (a LoL) + # Returns an (unblessed) REFERENCE to track data. + + # If you want to use this to encode a /single/ event, + # you still have to do it as a reference to an event structure (a LoL) + # that just happens to have just one event. I.e., + # encode( [ $event ] ) or encode( [ [ 'note_on', 100, 5, 42, 64] ] ) + # If you're doing this, consider the never_add_eot track option, as in + # print MIDI ${ encode( [ $event], { 'never_add_eot' => 1} ) }; + + data = [] # what I'll store the chunks of byte-data in + + # This is so my end_track magic won't corrupt the original + events = copy.deepcopy(events_lol) + + if not never_add_eot: + # One way or another, tack on an 'end_track' + if events: + last = events[-1] + if not (last[0] == 'end_track'): # no end_track already + if (last[0] == 'text_event' and len(last[2]) == 0): + # 0-length text event at track-end. + if no_eot_magic: + # Exceptional case: don't mess with track-final + # 0-length text_events; just peg on an end_track + events.append(['end_track', 0]) + else: + # NORMAL CASE: replace with an end_track, leaving DTime + last[0] = 'end_track' + else: + # last event was neither 0-length text_event nor end_track + events.append(['end_track', 0]) + else: # an eventless track! + events = [['end_track', 0],] + + # maybe_running_status = not no_running_status # unused? 4.7 + last_status = -1 + + for event_r in (events): + E = copy.deepcopy(event_r) + # otherwise the shifting'd corrupt the original + if not E: + continue + + event = E.pop(0) + if not len(event): + continue + + dtime = int(E.pop(0)) + # print('event='+str(event)+' dtime='+str(dtime)) + + event_data = '' + + if ( # MIDI events -- eligible for running status + event == 'note_on' + or event == 'note_off' + or event == 'control_change' + or event == 'key_after_touch' + or event == 'patch_change' + or event == 'channel_after_touch' + or event == 'pitch_wheel_change' ): + + # This block is where we spend most of the time. Gotta be tight. + if (event == 'note_off'): + status = 0x80 | (int(E[0]) & 0x0F) + parameters = struct.pack('>BB', int(E[1])&0x7F, int(E[2])&0x7F) + elif (event == 'note_on'): + status = 0x90 | (int(E[0]) & 0x0F) + parameters = struct.pack('>BB', int(E[1])&0x7F, int(E[2])&0x7F) + elif (event == 'key_after_touch'): + status = 0xA0 | (int(E[0]) & 0x0F) + parameters = struct.pack('>BB', int(E[1])&0x7F, int(E[2])&0x7F) + elif (event == 'control_change'): + status = 0xB0 | (int(E[0]) & 0x0F) + parameters = struct.pack('>BB', int(E[1])&0xFF, int(E[2])&0xFF) + elif (event == 'patch_change'): + status = 0xC0 | (int(E[0]) & 0x0F) + parameters = struct.pack('>B', int(E[1]) & 0xFF) + elif (event == 'channel_after_touch'): + status = 0xD0 | (int(E[0]) & 0x0F) + parameters = struct.pack('>B', int(E[1]) & 0xFF) + elif (event == 'pitch_wheel_change'): + status = 0xE0 | (int(E[0]) & 0x0F) + parameters = _write_14_bit(int(E[1]) + 0x2000) + else: + _warn("BADASS FREAKOUT ERROR 31415!") + + # And now the encoding + # w = BER compressed integer (not ASN.1 BER, see perlpacktut for + # details). Its bytes represent an unsigned integer in base 128, + # most significant digit first, with as few digits as possible. + # Bit eight (the high bit) is set on each byte except the last. + + data.append(_ber_compressed_int(dtime)) + if (status != last_status) or no_running_status: + data.append(struct.pack('>B', status)) + data.append(parameters) + + last_status = status + continue + else: + # Not a MIDI event. + # All the code in this block could be more efficient, + # but this is not where the code needs to be tight. + # print "zaz $event\n"; + last_status = -1 + + if event == 'raw_meta_event': + event_data = _some_text_event(int(E[0]), E[1]) + elif (event == 'set_sequence_number'): # 3.9 + event_data = b'\xFF\x00\x02'+_int2twobytes(E[0]) + + # Text meta-events... + # a case for a dict, I think (pjb) ... + elif (event == 'text_event'): + event_data = _some_text_event(0x01, E[0]) + elif (event == 'copyright_text_event'): + event_data = _some_text_event(0x02, E[0]) + elif (event == 'track_name'): + event_data = _some_text_event(0x03, E[0]) + elif (event == 'instrument_name'): + event_data = _some_text_event(0x04, E[0]) + elif (event == 'lyric'): + event_data = _some_text_event(0x05, E[0]) + elif (event == 'marker'): + event_data = _some_text_event(0x06, E[0]) + elif (event == 'cue_point'): + event_data = _some_text_event(0x07, E[0]) + elif (event == 'text_event_08'): + event_data = _some_text_event(0x08, E[0]) + elif (event == 'text_event_09'): + event_data = _some_text_event(0x09, E[0]) + elif (event == 'text_event_0a'): + event_data = _some_text_event(0x0A, E[0]) + elif (event == 'text_event_0b'): + event_data = _some_text_event(0x0B, E[0]) + elif (event == 'text_event_0c'): + event_data = _some_text_event(0x0C, E[0]) + elif (event == 'text_event_0d'): + event_data = _some_text_event(0x0D, E[0]) + elif (event == 'text_event_0e'): + event_data = _some_text_event(0x0E, E[0]) + elif (event == 'text_event_0f'): + event_data = _some_text_event(0x0F, E[0]) + # End of text meta-events + + elif (event == 'end_track'): + event_data = b"\xFF\x2F\x00" + + elif (event == 'set_tempo'): + #event_data = struct.pack(">BBwa*", 0xFF, 0x51, 3, + # substr( struct.pack('>I', E[0]), 1, 3)) + event_data = b'\xFF\x51\x03'+struct.pack('>I',E[0])[1:] + elif (event == 'smpte_offset'): + # event_data = struct.pack(">BBwBBBBB", 0xFF, 0x54, 5, E[0:5] ) + event_data = struct.pack(">BBBbBBBB", 0xFF,0x54,0x05,E[0],E[1],E[2],E[3],E[4]) + elif (event == 'time_signature'): + # event_data = struct.pack(">BBwBBBB", 0xFF, 0x58, 4, E[0:4] ) + event_data = struct.pack(">BBBbBBB", 0xFF, 0x58, 0x04, E[0],E[1],E[2],E[3]) + elif (event == 'key_signature'): + event_data = struct.pack(">BBBbB", 0xFF, 0x59, 0x02, E[0],E[1]) + elif (event == 'sequencer_specific'): + # event_data = struct.pack(">BBwa*", 0xFF,0x7F, len(E[0]), E[0]) + event_data = _some_text_event(0x7F, E[0]) + # End of Meta-events + + # Other Things... + elif (event == 'sysex_f0'): + #event_data = struct.pack(">Bwa*", 0xF0, len(E[0]), E[0]) + #B=bitstring w=BER-compressed-integer a=null-padded-ascii-str + event_data = bytearray(b'\xF0')+_ber_compressed_int(len(E[0]))+bytearray(E[0]) + elif (event == 'sysex_f7'): + #event_data = struct.pack(">Bwa*", 0xF7, len(E[0]), E[0]) + event_data = bytearray(b'\xF7')+_ber_compressed_int(len(E[0]))+bytearray(E[0]) + + elif (event == 'song_position'): + event_data = b"\xF2" + _write_14_bit( E[0] ) + elif (event == 'song_select'): + event_data = struct.pack('>BB', 0xF3, E[0] ) + elif (event == 'tune_request'): + event_data = b"\xF6" + elif (event == 'raw_data'): + _warn("_encode: raw_data event not supported") + # event_data = E[0] + continue + # End of Other Stuff + + else: + # The Big Fallthru + if unknown_callback: + # push(@data, &{ $unknown_callback }( @$event_r )) + pass + else: + _warn("Unknown event: "+str(event)) + # To surpress complaint here, just set + # 'unknown_callback' => sub { return () } + continue + + #print "Event $event encoded part 2\n" + if str(type(event_data)).find("'str'") >= 0: + event_data = bytearray(event_data.encode('Latin1', 'ignore')) + if len(event_data): # how could $event_data be empty + # data.append(struct.pack('>wa*', dtime, event_data)) + # print(' event_data='+str(event_data)) + data.append(_ber_compressed_int(dtime)+event_data) + + return b''.join(data) + +#=============================================================================== + +""" +================================================================================ + + pyFluidSynth + + Python bindings for FluidSynth + + Copyright 2008, Nathan Whitehead + + + Released under the LGPL + + This module contains python bindings for FluidSynth. FluidSynth is a + software synthesizer for generating music. It works like a MIDI + synthesizer. You load patches, set parameters, then send NOTEON and + NOTEOFF events to play notes. Instruments are defined in SoundFonts, + generally files with the extension SF2. FluidSynth can either be used + to play audio itself, or you can call a function that returns chunks + of audio data and output the data to the soundcard yourself. + FluidSynth works on all major platforms, so pyFluidSynth should also. + +================================================================================ +""" + +from ctypes import * +from ctypes.util import find_library +import os + +# A short circuited or expression to find the FluidSynth library +# (mostly needed for Windows distributions of libfluidsynth supplied with QSynth) + +# DLL search method changed in Python 3.8 +# https://docs.python.org/3/library/os.html#os.add_dll_directory +if hasattr(os, 'add_dll_directory'): + os.add_dll_directory(os.getcwd()) + +lib = find_library('fluidsynth') or \ + find_library('libfluidsynth') or \ + find_library('libfluidsynth-3') or \ + find_library('libfluidsynth-2') or \ + find_library('libfluidsynth-1') + +if lib is None: + raise ImportError("Couldn't find the FluidSynth library.") + +# Dynamically link the FluidSynth library +# Architecture (32-/64-bit) must match your Python version +_fl = CDLL(lib) + +# Helper function for declaring function prototypes +def cfunc(name, result, *args): + """Build and apply a ctypes prototype complete with parameter flags""" + if hasattr(_fl, name): + atypes = [] + aflags = [] + for arg in args: + atypes.append(arg[1]) + aflags.append((arg[2], arg[0]) + arg[3:]) + return CFUNCTYPE(result, *atypes)((name, _fl), tuple(aflags)) + else: # Handle Fluidsynth 1.x, 2.x, etc. API differences + return None + +# Bump this up when changing the interface for users +api_version = '1.3.1' + +# Function prototypes for C versions of functions + +FLUID_OK = 0 +FLUID_FAILED = -1 + +fluid_version = cfunc('fluid_version', c_void_p, + ('major', POINTER(c_int), 1), + ('minor', POINTER(c_int), 1), + ('micro', POINTER(c_int), 1)) + +majver = c_int() +fluid_version(majver, c_int(), c_int()) +if majver.value > 1: + FLUIDSETTING_EXISTS = FLUID_OK +else: + FLUIDSETTING_EXISTS = 1 + +# fluid settings +new_fluid_settings = cfunc('new_fluid_settings', c_void_p) + +fluid_settings_setstr = cfunc('fluid_settings_setstr', c_int, + ('settings', c_void_p, 1), + ('name', c_char_p, 1), + ('str', c_char_p, 1)) + +fluid_settings_setnum = cfunc('fluid_settings_setnum', c_int, + ('settings', c_void_p, 1), + ('name', c_char_p, 1), + ('val', c_double, 1)) + +fluid_settings_setint = cfunc('fluid_settings_setint', c_int, + ('settings', c_void_p, 1), + ('name', c_char_p, 1), + ('val', c_int, 1)) + +fluid_settings_copystr = cfunc('fluid_settings_copystr', c_int, + ('settings', c_void_p, 1), + ('name', c_char_p, 1), + ('str', c_char_p, 1), + ('len', c_int, 1)) + +fluid_settings_getnum = cfunc('fluid_settings_getnum', c_int, + ('settings', c_void_p, 1), + ('name', c_char_p, 1), + ('val', POINTER(c_double), 1)) + +fluid_settings_getint = cfunc('fluid_settings_getint', c_int, + ('settings', c_void_p, 1), + ('name', c_char_p, 1), + ('val', POINTER(c_int), 1)) + +delete_fluid_settings = cfunc('delete_fluid_settings', None, + ('settings', c_void_p, 1)) + +fluid_synth_activate_key_tuning = cfunc('fluid_synth_activate_key_tuning', c_int, + ('synth', c_void_p, 1), + ('bank', c_int, 1), + ('prog', c_int, 1), + ('name', c_char_p, 1), + ('pitch', POINTER(c_double), 1), + ('apply', c_int, 1)) + +fluid_synth_activate_tuning = cfunc('fluid_synth_activate_tuning', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('bank', c_int, 1), + ('prog', c_int, 1), + ('apply', c_int, 1)) + +fluid_synth_deactivate_tuning = cfunc('fluid_synth_deactivate_tuning', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('apply', c_int, 1)) + +fluid_synth_tuning_dump = cfunc('fluid_synth_tuning_dump', c_int, + ('synth', c_void_p, 1), + ('bank', c_int, 1), + ('prog', c_int, 1), + ('name', c_char_p, 1), + ('length', c_int, 1), + ('pitch', POINTER(c_double), 1)) + +# fluid synth +new_fluid_synth = cfunc('new_fluid_synth', c_void_p, + ('settings', c_void_p, 1)) + +delete_fluid_synth = cfunc('delete_fluid_synth', None, + ('synth', c_void_p, 1)) + +fluid_synth_sfload = cfunc('fluid_synth_sfload', c_int, + ('synth', c_void_p, 1), + ('filename', c_char_p, 1), + ('update_midi_presets', c_int, 1)) + +fluid_synth_sfunload = cfunc('fluid_synth_sfunload', c_int, + ('synth', c_void_p, 1), + ('sfid', c_int, 1), + ('update_midi_presets', c_int, 1)) + +fluid_synth_program_select = cfunc('fluid_synth_program_select', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('sfid', c_int, 1), + ('bank', c_int, 1), + ('preset', c_int, 1)) + +fluid_synth_noteon = cfunc('fluid_synth_noteon', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('key', c_int, 1), + ('vel', c_int, 1)) + +fluid_synth_noteoff = cfunc('fluid_synth_noteoff', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('key', c_int, 1)) + +fluid_synth_pitch_bend = cfunc('fluid_synth_pitch_bend', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('val', c_int, 1)) + +fluid_synth_cc = cfunc('fluid_synth_cc', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('ctrl', c_int, 1), + ('val', c_int, 1)) + +fluid_synth_get_cc = cfunc('fluid_synth_get_cc', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('num', c_int, 1), + ('pval', POINTER(c_int), 1)) + +fluid_synth_program_change = cfunc('fluid_synth_program_change', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('prg', c_int, 1)) + +fluid_synth_unset_program = cfunc('fluid_synth_unset_program', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1)) + +fluid_synth_get_program = cfunc('fluid_synth_get_program', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('sfont_id', POINTER(c_int), 1), + ('bank_num', POINTER(c_int), 1), + ('preset_num', POINTER(c_int), 1)) + +fluid_synth_bank_select = cfunc('fluid_synth_bank_select', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('bank', c_int, 1)) + +fluid_synth_sfont_select = cfunc('fluid_synth_sfont_select', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('sfid', c_int, 1)) + +fluid_synth_program_reset = cfunc('fluid_synth_program_reset', c_int, + ('synth', c_void_p, 1)) + +fluid_synth_system_reset = cfunc('fluid_synth_system_reset', c_int, + ('synth', c_void_p, 1)) + +fluid_synth_write_s16 = cfunc('fluid_synth_write_s16', c_void_p, + ('synth', c_void_p, 1), + ('len', c_int, 1), + ('lbuf', c_void_p, 1), + ('loff', c_int, 1), + ('lincr', c_int, 1), + ('rbuf', c_void_p, 1), + ('roff', c_int, 1), + ('rincr', c_int, 1)) + +fluid_synth_all_notes_off = cfunc('fluid_synth_all_notes_off', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1)) + +fluid_synth_all_sounds_off = cfunc('fluid_synth_all_sounds_off', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1)) + + +class fluid_synth_channel_info_t(Structure): + _fields_ = [ + ('assigned', c_int), + ('sfont_id', c_int), + ('bank', c_int), + ('program', c_int), + ('name', c_char*32), + ('reserved', c_char*32)] + +fluid_synth_get_channel_info = cfunc('fluid_synth_get_channel_info', c_int, + ('synth', c_void_p, 1), + ('chan', c_int, 1), + ('info', POINTER(fluid_synth_channel_info_t), 1)) + +fluid_synth_set_reverb_full = cfunc('fluid_synth_set_reverb_full', c_int, + ('synth', c_void_p, 1), + ('set', c_int, 1), + ('roomsize', c_double, 1), + ('damping', c_double, 1), + ('width', c_double, 1), + ('level', c_double, 1)) + +fluid_synth_set_chorus_full = cfunc('fluid_synth_set_chorus_full', c_int, + ('synth', c_void_p, 1), + ('set', c_int, 1), + ('nr', c_int, 1), + ('level', c_double, 1), + ('speed', c_double, 1), + ('depth_ms', c_double, 1), + ('type', c_int, 1)) + +fluid_synth_set_reverb = cfunc('fluid_synth_set_reverb', c_int, + ('synth', c_void_p, 1), + ('roomsize', c_double, 1), + ('damping', c_double, 1), + ('width', c_double, 1), + ('level', c_double, 1)) + +fluid_synth_set_chorus = cfunc('fluid_synth_set_chorus', c_int, + ('synth', c_void_p, 1), + ('nr', c_int, 1), + ('level', c_double, 1), + ('speed', c_double, 1), + ('depth_ms', c_double, 1), + ('type', c_int, 1)) + +fluid_synth_set_reverb_roomsize = cfunc('fluid_synth_set_reverb_roomsize', c_int, + ('synth', c_void_p, 1), + ('roomsize', c_double, 1)) + +fluid_synth_set_reverb_damp = cfunc('fluid_synth_set_reverb_damp', c_int, + ('synth', c_void_p, 1), + ('damping', c_double, 1)) + +fluid_synth_set_reverb_level = cfunc('fluid_synth_set_reverb_level', c_int, + ('synth', c_void_p, 1), + ('level', c_double, 1)) + +fluid_synth_set_reverb_width = cfunc('fluid_synth_set_reverb_width', c_int, + ('synth', c_void_p, 1), + ('width', c_double, 1)) + +fluid_synth_set_chorus_nr = cfunc('fluid_synth_set_chorus_nr', c_int, + ('synth', c_void_p, 1), + ('nr', c_int, 1)) + +fluid_synth_set_chorus_level = cfunc('fluid_synth_set_chorus_level', c_int, + ('synth', c_void_p, 1), + ('level', c_double, 1)) + +fluid_synth_set_chorus_type = cfunc('fluid_synth_set_chorus_type', c_int, + ('synth', c_void_p, 1), + ('type', c_int, 1)) +fluid_synth_get_reverb_roomsize = cfunc('fluid_synth_get_reverb_roomsize', c_double, + ('synth', c_void_p, 1)) + +fluid_synth_get_reverb_damp = cfunc('fluid_synth_get_reverb_damp', c_double, + ('synth', c_void_p, 1)) + +fluid_synth_get_reverb_level = cfunc('fluid_synth_get_reverb_level', c_double, + ('synth', c_void_p, 1)) + +fluid_synth_get_reverb_width = cfunc('fluid_synth_get_reverb_width', c_double, + ('synth', c_void_p, 1)) + + +fluid_synth_get_chorus_nr = cfunc('fluid_synth_get_chorus_nr', c_int, + ('synth', c_void_p, 1)) + +fluid_synth_get_chorus_level = cfunc('fluid_synth_get_chorus_level', c_double, + ('synth', c_void_p, 1)) + +fluid_synth_get_chorus_speed_Hz = cfunc('fluid_synth_get_chorus_speed_Hz', c_double, + ('synth', c_void_p, 1)) + +fluid_synth_get_chorus_depth_ms = cfunc('fluid_synth_get_chorus_depth_ms', c_double, + ('synth', c_void_p, 1)) + +fluid_synth_get_chorus_type = cfunc('fluid_synth_get_chorus_type', c_int, + ('synth', c_void_p, 1)) + +fluid_synth_set_midi_router = cfunc('fluid_synth_set_midi_router', None, + ('synth', c_void_p, 1), + ('router', c_void_p, 1)) + +fluid_synth_handle_midi_event = cfunc('fluid_synth_handle_midi_event', c_int, + ('data', c_void_p, 1), + ('event', c_void_p, 1)) + +# fluid sequencer +new_fluid_sequencer2 = cfunc('new_fluid_sequencer2', c_void_p, + ('use_system_timer', c_int, 1)) + +fluid_sequencer_process = cfunc('fluid_sequencer_process', None, + ('seq', c_void_p, 1), + ('msec', c_uint, 1)) + +fluid_sequencer_register_fluidsynth = cfunc('fluid_sequencer_register_fluidsynth', c_short, + ('seq', c_void_p, 1), + ('synth', c_void_p, 1)) + +fluid_sequencer_register_client = cfunc('fluid_sequencer_register_client', c_short, + ('seq', c_void_p, 1), + ('name', c_char_p, 1), + ('callback', CFUNCTYPE(None, c_uint, c_void_p, c_void_p, c_void_p), 1), + ('data', c_void_p, 1)) + +fluid_sequencer_get_tick = cfunc('fluid_sequencer_get_tick', c_uint, + ('seq', c_void_p, 1)) + +fluid_sequencer_set_time_scale = cfunc('fluid_sequencer_set_time_scale', None, + ('seq', c_void_p, 1), + ('scale', c_double, 1)) + +fluid_sequencer_get_time_scale = cfunc('fluid_sequencer_get_time_scale', c_double, + ('seq', c_void_p, 1)) + +fluid_sequencer_send_at = cfunc('fluid_sequencer_send_at', c_int, + ('seq', c_void_p, 1), + ('evt', c_void_p, 1), + ('time', c_uint, 1), + ('absolute', c_int, 1)) + + +delete_fluid_sequencer = cfunc('delete_fluid_sequencer', None, + ('seq', c_void_p, 1)) + +# fluid event +new_fluid_event = cfunc('new_fluid_event', c_void_p) + +fluid_event_set_source = cfunc('fluid_event_set_source', None, + ('evt', c_void_p, 1), + ('src', c_void_p, 1)) + +fluid_event_set_dest = cfunc('fluid_event_set_dest', None, + ('evt', c_void_p, 1), + ('dest', c_void_p, 1)) + +fluid_event_timer = cfunc('fluid_event_timer', None, + ('evt', c_void_p, 1), + ('data', c_void_p, 1)) + +fluid_event_note = cfunc('fluid_event_note', None, + ('evt', c_void_p, 1), + ('channel', c_int, 1), + ('key', c_short, 1), + ('vel', c_short, 1), + ('duration', c_uint, 1)) + +fluid_event_noteon = cfunc('fluid_event_noteon', None, + ('evt', c_void_p, 1), + ('channel', c_int, 1), + ('key', c_short, 1), + ('vel', c_short, 1)) + +fluid_event_noteoff = cfunc('fluid_event_noteoff', None, + ('evt', c_void_p, 1), + ('channel', c_int, 1), + ('key', c_short, 1)) + +delete_fluid_event = cfunc('delete_fluid_event', None, + ('evt', c_void_p, 1)) + +fluid_midi_event_get_channel = cfunc('fluid_midi_event_get_channel', c_int, + ('evt', c_void_p, 1)) + +fluid_midi_event_get_control = cfunc('fluid_midi_event_get_control', c_int, + ('evt', c_void_p, 1)) + +fluid_midi_event_get_program = cfunc('fluid_midi_event_get_program', c_int, + ('evt', c_void_p, 1)) + +fluid_midi_event_get_key = cfunc('fluid_midi_event_get_key', c_int, + ('evt', c_void_p, 1)) + +fluid_midi_event_get_type = cfunc('fluid_midi_event_get_type', c_int, + ('evt', c_void_p, 1)) + +fluid_midi_event_get_value = cfunc('fluid_midi_event_get_value', c_int, + ('evt', c_void_p, 1)) + +fluid_midi_event_get_velocity = cfunc('fluid_midi_event_get_velocity', c_int, + ('evt', c_void_p, 1)) + +# fluid_player_status returned by fluid_player_get_status() +FLUID_PLAYER_READY = 0 +FLUID_PLAYER_PLAYING = 1 +FLUID_PLAYER_STOPPING = 2 +FLUID_PLAYER_DONE = 3 + +# tempo_type used by fluid_player_set_tempo() +FLUID_PLAYER_TEMPO_INTERNAL = 0 +FLUID_PLAYER_TEMPO_EXTERNAL_BPM = 1 +FLUID_PLAYER_TEMPO_EXTERNAL_MIDI = 2 + +new_fluid_player = cfunc('new_fluid_player', c_void_p, + ('synth', c_void_p, 1)) + +delete_fluid_player = cfunc('delete_fluid_player', None, + ('player', c_void_p, 1)) + +fluid_player_add = cfunc('fluid_player_add', c_int, + ('player', c_void_p, 1), + ('filename', c_char_p, 1)) + + +fluid_player_get_status = cfunc('fluid_player_get_status', c_int, + ('player', c_void_p, 1)) +fluid_player_join = cfunc('fluid_player_join', c_int, + ('player', c_void_p, 1)) + +fluid_player_play = cfunc('fluid_player_play', c_int, + ('player', c_void_p, 1)) + +fluid_player_set_playback_callback = cfunc('fluid_player_set_playback_callback', c_int, + ('player', c_void_p, 1), + ('handler', CFUNCTYPE(c_int, c_void_p, c_void_p), 1), + ('event_handler_data', c_void_p, 1)) + +fluid_player_set_tempo = cfunc('fluid_player_set_tempo', c_int, + ('player', c_void_p, 1), + ('tempo_type', c_int, 1), + ('tempo', c_double, 1)) + +fluid_player_seek = cfunc('fluid_player_seek', c_int, + ('player', c_void_p, 1), + ('ticks', c_int, 1)) + +fluid_player_stop = cfunc('fluid_player_stop', c_int, + ('player', c_void_p, 1)) + +# fluid audio driver +new_fluid_audio_driver = cfunc('new_fluid_audio_driver', c_void_p, + ('settings', c_void_p, 1), + ('synth', c_void_p, 1)) + +delete_fluid_audio_driver = cfunc('delete_fluid_audio_driver', None, + ('driver', c_void_p, 1)) + +# fluid midi driver +new_fluid_midi_driver = cfunc('new_fluid_midi_driver', c_void_p, + ('settings', c_void_p, 1), + ('handler', CFUNCTYPE(c_int, c_void_p, c_void_p), 1), + ('event_handler_data', c_void_p, 1)) + + +# fluid midi router rule +class fluid_midi_router_t(Structure): + _fields_ = [ + ('synth', c_void_p), + ('rules_mutex', c_void_p), + ('rules', c_void_p*6), + ('free_rules', c_void_p), + ('event_handler', c_void_p), + ('event_handler_data', c_void_p), + ('nr_midi_channels', c_int), + ('cmd_rule', c_void_p), + ('cmd_rule_type', POINTER(c_int))] + +delete_fluid_midi_router_rule = cfunc('delete_fluid_midi_router_rule', c_int, + ('rule', c_void_p, 1)) + +new_fluid_midi_router_rule = cfunc('new_fluid_midi_router_rule', c_void_p) + +fluid_midi_router_rule_set_chan = cfunc('fluid_midi_router_rule_set_chan', None, + ('rule', c_void_p, 1), + ('min', c_int, 1), + ('max', c_int, 1), + ('mul', c_float, 1), + ('add', c_int, 1)) + +fluid_midi_router_rule_set_param1 = cfunc('fluid_midi_router_rule_set_param1', None, + ('rule', c_void_p, 1), + ('min', c_int, 1), + ('max', c_int, 1), + ('mul', c_float, 1), + ('add', c_int, 1)) + +fluid_midi_router_rule_set_param2 = cfunc('fluid_midi_router_rule_set_param2', None, + ('rule', c_void_p, 1), + ('min', c_int, 1), + ('max', c_int, 1), + ('mul', c_float, 1), + ('add', c_int, 1)) + +# fluid midi router +new_fluid_midi_router = cfunc('new_fluid_midi_router', POINTER(fluid_midi_router_t), + ('settings', c_void_p, 1), + ('handler', CFUNCTYPE(c_int, c_void_p, c_void_p), 1), + ('event_handler_data', c_void_p, 1)) + +fluid_midi_router_handle_midi_event = cfunc('fluid_midi_router_handle_midi_event', c_int, + ('data', c_void_p, 1), + ('event', c_void_p, 1)) + +fluid_midi_router_clear_rules = cfunc('fluid_midi_router_clear_rules', c_int, + ('router', POINTER(fluid_midi_router_t), 1)) + +fluid_midi_router_set_default_rules = cfunc('fluid_midi_router_set_default_rules', c_int, + ('router', POINTER(fluid_midi_router_t), 1)) + +fluid_midi_router_add_rule = cfunc('fluid_midi_router_add_rule', c_int, + ('router', POINTER(fluid_midi_router_t), 1), + ('rule', c_void_p, 1), + ('type', c_int, 1)) + +# fluidsynth 2.x +new_fluid_cmd_handler=cfunc('new_fluid_cmd_handler', c_void_p, + ('synth', c_void_p, 1), + ('router', c_void_p, 1)) + +fluid_synth_get_sfont_by_id = cfunc('fluid_synth_get_sfont_by_id', c_void_p, + ('synth', c_void_p, 1), + ('id', c_int, 1)) + +fluid_sfont_get_preset = cfunc('fluid_sfont_get_preset', c_void_p, + ('sfont', c_void_p, 1), + ('banknum', c_int, 1), + ('prenum', c_int, 1)) + +fluid_preset_get_name = cfunc('fluid_preset_get_name', c_char_p, + ('preset', c_void_p, 1)) + +fluid_synth_set_reverb = cfunc('fluid_synth_set_reverb', c_int, + ('synth', c_void_p, 1), + ('roomsize', c_double, 1), + ('damping', c_double, 1), + ('width', c_double, 1), + ('level', c_double, 1)) + +fluid_synth_set_chorus = cfunc('fluid_synth_set_chorus', c_int, + ('synth', c_void_p, 1), + ('nr', c_int, 1), + ('level', c_double, 1), + ('speed', c_double, 1), + ('depth_ms', c_double, 1), + ('type', c_int, 1)) + +fluid_synth_get_chorus_speed = cfunc('fluid_synth_get_chorus_speed', c_double, + ('synth', c_void_p, 1)) + +fluid_synth_get_chorus_depth = cfunc('fluid_synth_get_chorus_depth', c_double, + ('synth', c_void_p, 1)) + + +def fluid_synth_write_s16_stereo(synth, len): + """Return generated samples in stereo 16-bit format + + Return value is a Numpy array of samples. + + """ + import numpy + buf = create_string_buffer(len * 4) + fluid_synth_write_s16(synth, len, buf, 0, 2, buf, 1, 2) + return numpy.frombuffer(buf[:], dtype=numpy.int16) + + +# Object-oriented interface, simplifies access to functions + +class Synth: + """Synth represents a FluidSynth synthesizer""" + def __init__(self, gain=0.2, samplerate=44100, channels=256, **kwargs): + """Create new synthesizer object to control sound generation + + Optional keyword arguments: + gain : scale factor for audio output, default is 0.2 + lower values are quieter, allow more simultaneous notes + samplerate : output samplerate in Hz, default is 44100 Hz + added capability for passing arbitrary fluid settings using args + """ + self.settings = new_fluid_settings() + self.setting('synth.gain', gain) + self.setting('synth.sample-rate', float(samplerate)) + self.setting('synth.midi-channels', channels) + for opt,val in kwargs.items(): + self.setting(opt, val) + self.synth = new_fluid_synth(self.settings) + self.audio_driver = None + self.midi_driver = None + self.router = None + def setting(self, opt, val): + """change an arbitrary synth setting, type-smart""" + if isinstance(val, (str, bytes)): + fluid_settings_setstr(self.settings, opt.encode(), val.encode()) + elif isinstance(val, int): + fluid_settings_setint(self.settings, opt.encode(), val) + elif isinstance(val, float): + fluid_settings_setnum(self.settings, opt.encode(), c_double(val)) + def get_setting(self, opt): + """get current value of an arbitrary synth setting""" + val = c_int() + if fluid_settings_getint(self.settings, opt.encode(), byref(val)) == FLUIDSETTING_EXISTS: + return val.value + strval = create_string_buffer(32) + if fluid_settings_copystr(self.settings, opt.encode(), strval, 32) == FLUIDSETTING_EXISTS: + return strval.value.decode() + num = c_double() + if fluid_settings_getnum(self.settings, opt.encode(), byref(num)) == FLUIDSETTING_EXISTS: + return round(num.value, 6) + return None + + def start(self, driver=None, device=None, midi_driver=None, midi_router=None): + """Start audio output driver in separate background thread + + Call this function any time after creating the Synth object. + If you don't call this function, use get_samples() to generate + samples. + + Optional keyword argument: + driver : which audio driver to use for output + device : the device to use for audio output + midi_driver : the midi driver to use for communicating with midi devices + see http://www.fluidsynth.org/api/fluidsettings.xml for allowed values and defaults by platform + """ + driver = driver or self.get_setting('audio.driver') + device = device or self.get_setting('audio.%s.device' % driver) + midi_driver = midi_driver or self.get_setting('midi.driver') + + self.setting('audio.driver', driver) + self.setting('audio.%s.device' % driver, device) + self.audio_driver = new_fluid_audio_driver(self.settings, self.synth) + self.setting('midi.driver', midi_driver) + self.router = new_fluid_midi_router(self.settings, fluid_synth_handle_midi_event, self.synth) + if new_fluid_cmd_handler: + new_fluid_cmd_handler(self.synth, self.router) + else: + fluid_synth_set_midi_router(self.synth, self.router) + if midi_router == None: ## Use fluidsynth to create a MIDI event handler + self.midi_driver = new_fluid_midi_driver(self.settings, fluid_midi_router_handle_midi_event, self.router) + self.custom_router_callback = None + else: ## Supply an external MIDI event handler + self.custom_router_callback = CFUNCTYPE(c_int, c_void_p, c_void_p)(midi_router) + self.midi_driver = new_fluid_midi_driver(self.settings, self.custom_router_callback, self.router) + return FLUID_OK + + def delete(self): + if self.audio_driver: + delete_fluid_audio_driver(self.audio_driver) + delete_fluid_synth(self.synth) + delete_fluid_settings(self.settings) + def sfload(self, filename, update_midi_preset=0): + """Load SoundFont and return its ID""" + return fluid_synth_sfload(self.synth, filename.encode(), update_midi_preset) + def sfunload(self, sfid, update_midi_preset=0): + """Unload a SoundFont and free memory it used""" + return fluid_synth_sfunload(self.synth, sfid, update_midi_preset) + def program_select(self, chan, sfid, bank, preset): + """Select a program""" + return fluid_synth_program_select(self.synth, chan, sfid, bank, preset) + def program_unset(self, chan): + """Set the preset of a MIDI channel to an unassigned state""" + return fluid_synth_unset_program(self.synth, chan) + def channel_info(self, chan): + """get soundfont, bank, prog, preset name of channel""" + if fluid_synth_get_channel_info is not None: + info=fluid_synth_channel_info_t() + fluid_synth_get_channel_info(self.synth, chan, byref(info)) + return (info.sfont_id, info.bank, info.program, info.name) + else: + (sfontid, banknum, presetnum) = self.program_info(chan) + presetname = self.sfpreset_name(sfontid, banknum, presetnum) + return (sfontid, banknum, presetnum, presetname) + def program_info(self, chan): + """get active soundfont, bank, prog on a channel""" + if fluid_synth_get_program is not None: + sfontid=c_int() + banknum=c_int() + presetnum=c_int() + fluid_synth_get_program(self.synth, chan, byref(sfontid), byref(banknum), byref(presetnum)) + return (sfontid.value, banknum.value, presetnum.value) + else: + (sfontid, banknum, prognum, presetname) = self.channel_info(chan) + return (sfontid, banknum, prognum) + def sfpreset_name(self, sfid, bank, prenum): + """Return name of a soundfont preset""" + if fluid_synth_get_sfont_by_id is not None: + sfont=fluid_synth_get_sfont_by_id(self.synth, sfid) + preset=fluid_sfont_get_preset(sfont, bank, prenum) + if not preset: + return None + return fluid_preset_get_name(preset).decode('ascii') + else: + (sfontid, banknum, presetnum, presetname) = self.channel_info(chan) + return presetname + def router_clear(self): + if self.router is not None: + fluid_midi_router_clear_rules(self.router) + def router_default(self): + if self.router is not None: + fluid_midi_router_set_default_rules(self.router) + def router_begin(self, type): + """types are [note|cc|prog|pbend|cpress|kpress]""" + if self.router is not None: + if type=='note': + self.router.cmd_rule_type=0 + elif type=='cc': + self.router.cmd_rule_type=1 + elif type=='prog': + self.router.cmd_rule_type=2 + elif type=='pbend': + self.router.cmd_rule_type=3 + elif type=='cpress': + self.router.cmd_rule_type=4 + elif type=='kpress': + self.router.cmd_rule_type=5 + if 'self.router.cmd_rule' in globals(): + delete_fluid_midi_router_rule(self.router.cmd_rule) + self.router.cmd_rule = new_fluid_midi_router_rule() + def router_end(self): + if self.router is not None: + if self.router.cmd_rule is None: + return + if fluid_midi_router_add_rule(self.router, self.router.cmd_rule, self.router.cmd_rule_type)<0: + delete_fluid_midi_router_rule(self.router.cmd_rule) + self.router.cmd_rule=None + def router_chan(self, min, max, mul, add): + if self.router is not None: + fluid_midi_router_rule_set_chan(self.router.cmd_rule, min, max, mul, add) + def router_par1(self, min, max, mul, add): + if self.router is not None: + fluid_midi_router_rule_set_param1(self.router.cmd_rule, min, max, mul, add) + def router_par2(self, min, max, mul, add): + if self.router is not None: + fluid_midi_router_rule_set_param2(self.router.cmd_rule, min, max, mul, add) + def set_reverb(self, roomsize=-1.0, damping=-1.0, width=-1.0, level=-1.0): + """ + roomsize Reverb room size value (0.0-1.0) + damping Reverb damping value (0.0-1.0) + width Reverb width value (0.0-100.0) + level Reverb level value (0.0-1.0) + """ + if fluid_synth_set_reverb is not None: + return fluid_synth_set_reverb(self.synth, roomsize, damping, width, level) + else: + set=0 + if roomsize>=0: + set+=0b0001 + if damping>=0: + set+=0b0010 + if width>=0: + set+=0b0100 + if level>=0: + set+=0b1000 + return fluid_synth_set_reverb_full(self.synth, set, roomsize, damping, width, level) + def set_chorus(self, nr=-1, level=-1.0, speed=-1.0, depth=-1.0, type=-1): + """ + nr Chorus voice count (0-99, CPU time consumption proportional to this value) + level Chorus level (0.0-10.0) + speed Chorus speed in Hz (0.29-5.0) + depth_ms Chorus depth (max value depends on synth sample rate, 0.0-21.0 is safe for sample rate values up to 96KHz) + type Chorus waveform type (0=sine, 1=triangle) + """ + if fluid_synth_set_chorus is not None: + return fluid_synth_set_chorus(self.synth, nr, level, speed, depth, type) + else: + set=0 + if nr>=0: + set+=0b00001 + if level>=0: + set+=0b00010 + if speed>=0: + set+=0b00100 + if depth>=0: + set+=0b01000 + if type>=0: + set+=0b10000 + return fluid_synth_set_chorus_full(self.synth, set, nr, level, speed, depth, type) + def set_reverb_roomsize(self, roomsize): + if fluid_synth_set_reverb_roomsize is not None: + return fluid_synth_set_reverb_roomsize(self.synth, roomsize) + else: + return self.set_reverb(roomsize=roomsize) + def set_reverb_damp(self, damping): + if fluid_synth_set_reverb_damp is not None: + return fluid_synth_set_reverb_damp(self.synth, damping) + else: + return self.set_reverb(damping=damping) + def set_reverb_level(self, level): + if fluid_synth_set_reverb_level is not None: + return fluid_synth_set_reverb_level(self.synth, level) + else: + return self.set_reverb(level=level) + def set_reverb_width(self, width): + if fluid_synth_set_reverb_width is not None: + return fluid_synth_set_reverb_width(self.synth, width) + else: + return self.set_reverb(width=width) + def set_chorus_nr(self, nr): + if fluid_synth_set_chorus_nr is not None: + return fluid_synth_set_chorus_nr(self.synth, nr) + else: + return self.set_chorus(nr=nr) + def set_chorus_level(self, level): + if fluid_synth_set_chorus_level is not None: + return fluid_synth_set_chorus_level(self.synth, level) + else: + return self.set_chorus(leve=level) + def set_chorus_speed(self, speed): + if fluid_synth_set_chorus_speed is not None: + return fluid_synth_set_chorus_speed(self.synth, speed) + else: + return self.set_chorus(speed=speed) + def set_chorus_depth(self, depth): + if fluid_synth_set_chorus_depth is not None: + return fluid_synth_set_chorus_depth(self.synth, depth) + else: + return self.set_chorus(depth=depth) + def set_chorus_type(self, type): + if fluid_synth_set_chorus_type is not None: + return fluid_synth_set_chorus_type(self.synth, type) + else: + return self.set_chorus(type=type) + def get_reverb_roomsize(self): + return fluid_synth_get_reverb_roomsize(self.synth) + def get_reverb_damp(self): + return fluid_synth_get_reverb_damp(self.synth) + def get_reverb_level(self): + return fluid_synth_get_reverb_level(self.synth) + def get_reverb_width(self): + return fluid_synth_get_reverb_width(self.synth) + def get_chorus_nr(self): + return fluid_synth_get_chorus_nr(self.synth) + def get_chorus_level(self): + return fluid_synth_get_reverb_level(self.synth) + def get_chorus_speed(self): + if fluid_synth_get_chorus_speed is not None: + return fluid_synth_get_chorus_speed(self.synth) + else: + return fluid_synth_get_chorus_speed_Hz(self.synth) + def get_chorus_depth(self): + if fluid_synth_get_chorus_depth is not None: + return fluid_synth_get_chorus_depth(self.synth) + else: + return fluid_synth_get_chorus_depth_ms(self.synth) + def get_chorus_type(self): + return fluid_synth_get_chorus_type(self.synth) + def noteon(self, chan, key, vel): + """Play a note""" + if key < 0 or key > 127: + return False + if chan < 0: + return False + if vel < 0 or vel > 127: + return False + return fluid_synth_noteon(self.synth, chan, key, vel) + def noteoff(self, chan, key): + """Stop a note""" + if key < 0 or key > 127: + return False + if chan < 0: + return False + return fluid_synth_noteoff(self.synth, chan, key) + def pitch_bend(self, chan, val): + """Adjust pitch of a playing channel by small amounts + + A pitch bend value of 0 is no pitch change from default. + A value of -2048 is 1 semitone down. + A value of 2048 is 1 semitone up. + Maximum values are -8192 to +8192 (transposing by 4 semitones). + + """ + return fluid_synth_pitch_bend(self.synth, chan, val + 8192) + def cc(self, chan, ctrl, val): + """Send control change value + + The controls that are recognized are dependent on the + SoundFont. Values are always 0 to 127. Typical controls + include: + 1 : vibrato + 7 : volume + 10 : pan (left to right) + 11 : expression (soft to loud) + 64 : sustain + 91 : reverb + 93 : chorus + """ + return fluid_synth_cc(self.synth, chan, ctrl, val) + def get_cc(self, chan, num): + i=c_int() + fluid_synth_get_cc(self.synth, chan, num, byref(i)) + return i.value + def program_change(self, chan, prg): + """Change the program""" + return fluid_synth_program_change(self.synth, chan, prg) + def bank_select(self, chan, bank): + """Choose a bank""" + return fluid_synth_bank_select(self.synth, chan, bank) + def all_notes_off(self, chan): + """Turn off all notes on a channel (release all keys)""" + return fluid_synth_all_notes_off(self.synth, chan) + def all_sounds_off(self, chan): + """Turn off all sounds on a channel (equivalent to mute)""" + return fluid_synth_all_sounds_off(self.synth, chan) + def sfont_select(self, chan, sfid): + """Choose a SoundFont""" + return fluid_synth_sfont_select(self.synth, chan, sfid) + def program_reset(self): + """Reset the programs on all channels""" + return fluid_synth_program_reset(self.synth) + def system_reset(self): + """Stop all notes and reset all programs""" + return fluid_synth_system_reset(self.synth) + def get_samples(self, len=1024): + """Generate audio samples + + The return value will be a NumPy array containing the given + length of audio samples. If the synth is set to stereo output + (the default) the array will be size 2 * len. + + """ + return fluid_synth_write_s16_stereo(self.synth, len) + def tuning_dump(self, bank, prog, pitch): + return fluid_synth_tuning_dump(self.synth, bank, prog, name.encode(), length(name), pitch) + + def midi_event_get_type(self, event): + return fluid_midi_event_get_type(event) + def midi_event_get_velocity(self, event): + return fluid_midi_event_get_velocity(event) + def midi_event_get_key(self, event): + return fluid_midi_event_get_key(event) + def midi_event_get_channel(self, event): + return fluid_midi_event_get_channel(event) + def midi_event_get_control(self, event): + return fluid_midi_event_get_control(event) + def midi_event_get_program(self, event): + return fluid_midi_event_get_program(event) + def midi_event_get_value(self, event): + return fluid_midi_event_get_value(event) + + def play_midi_file(self, filename): + self.player = new_fluid_player(self.synth) + if self.player == None: return FLUID_FAILED + if self.custom_router_callback != None: + fluid_player_set_playback_callback(self.player, self.custom_router_callback, self.synth) + status = fluid_player_add(self.player, filename.encode()) + if status == FLUID_FAILED: return status + status = fluid_player_play(self.player) + return status + + def play_midi_stop(self): + status = fluid_player_stop(self.player) + if status == FLUID_FAILED: return status + status = fluid_player_seek(self.player, 0) + delete_fluid_player(self.player) + return status + + def player_set_tempo(self, tempo_type, tempo): + return fluid_player_set_tempo(self.player, tempo_type, tempo) + + + +class Sequencer: + def __init__(self, time_scale=1000, use_system_timer=True): + """Create new sequencer object to control and schedule timing of midi events + + Optional keyword arguments: + time_scale: ticks per second, defaults to 1000 + use_system_timer: whether the sequencer should advance by itself + """ + self.client_callbacks = [] + self.sequencer = new_fluid_sequencer2(use_system_timer) + fluid_sequencer_set_time_scale(self.sequencer, time_scale) + + def register_fluidsynth(self, synth): + response = fluid_sequencer_register_fluidsynth(self.sequencer, synth.synth) + if response == FLUID_FAILED: + raise Error("Registering fluid synth failed") + return response + + def register_client(self, name, callback, data=None): + c_callback = CFUNCTYPE(None, c_uint, c_void_p, c_void_p, c_void_p)(callback) + response = fluid_sequencer_register_client(self.sequencer, name.encode(), c_callback, data) + if response == FLUID_FAILED: + raise Error("Registering client failed") + + # store in a list to prevent garbage collection + self.client_callbacks.append(c_callback) + + return response + + def note(self, time, channel, key, velocity, duration, source=-1, dest=-1, absolute=True): + evt = self._create_event(source, dest) + fluid_event_note(evt, channel, key, velocity, duration) + self._schedule_event(evt, time, absolute) + delete_fluid_event(evt) + + def note_on(self, time, channel, key, velocity=127, source=-1, dest=-1, absolute=True): + evt = self._create_event(source, dest) + fluid_event_noteon(evt, channel, key, velocity) + self._schedule_event(evt, time, absolute) + delete_fluid_event(evt) + + def note_off(self, time, channel, key, source=-1, dest=-1, absolute=True): + evt = self._create_event(source, dest) + fluid_event_noteoff(evt, channel, key) + self._schedule_event(evt, time, absolute) + delete_fluid_event(evt) + + def timer(self, time, data=None, source=-1, dest=-1, absolute=True): + evt = self._create_event(source, dest) + fluid_event_timer(evt, data) + self._schedule_event(evt, time, absolute) + delete_fluid_event(evt) + + def _create_event(self, source=-1, dest=-1): + evt = new_fluid_event() + fluid_event_set_source(evt, source) + fluid_event_set_dest(evt, dest) + return evt + + def _schedule_event(self, evt, time, absolute=True): + response = fluid_sequencer_send_at(self.sequencer, evt, time, absolute) + if response == FLUID_FAILED: + raise Error("Scheduling event failed") + + def get_tick(self): + return fluid_sequencer_get_tick(self.sequencer) + + def process(self, msec): + fluid_sequencer_process(self.sequencer, msec) + + def delete(self): + delete_fluid_sequencer(self.sequencer) + +def raw_audio_string(data): + """Return a string of bytes to send to soundcard + + Input is a numpy array of samples. Default output format + is 16-bit signed (other formats not currently supported). + + """ + import numpy + return (data.astype(numpy.int16)).tostring() + +#=============================================================================== + +import numpy as np +import wave + +def midi_opus_to_colab_audio(midi_opus, + soundfont_path='/usr/share/sounds/sf2/FluidR3_GM.sf2', + sample_rate=16000, # 44100 + volume_scale=10, + trim_silence=True, + silence_threshold=0.1, + output_for_gradio=False, + write_audio_to_WAV='' + ): + + def normalize_volume(matrix, factor=10): + norm = np.linalg.norm(matrix) + matrix = matrix/norm # normalized matrix + mult_matrix = matrix * factor + final_matrix = np.clip(mult_matrix, -1.0, 1.0) + return final_matrix + + if midi_opus[1]: + + ticks_per_beat = midi_opus[0] + event_list = [] + for track_idx, track in enumerate(midi_opus[1:]): + abs_t = 0 + for event in track: + abs_t += event[1] + event_new = [*event] + event_new[1] = abs_t + event_list.append(event_new) + event_list = sorted(event_list, key=lambda e: e[1]) + + tempo = int((60 / 120) * 10 ** 6) # default 120 bpm + ss = np.empty((0, 2), dtype=np.int16) + fl = Synth(samplerate=float(sample_rate)) + sfid = fl.sfload(soundfont_path) + last_t = 0 + for c in range(16): + fl.program_select(c, sfid, 128 if c == 9 else 0, 0) + for event in event_list: + name = event[0] + sample_len = int(((event[1] / ticks_per_beat) * tempo / (10 ** 6)) * sample_rate) + sample_len -= int(((last_t / ticks_per_beat) * tempo / (10 ** 6)) * sample_rate) + last_t = event[1] + if sample_len > 0: + sample = fl.get_samples(sample_len).reshape(sample_len, 2) + ss = np.concatenate([ss, sample]) + if name == "set_tempo": + tempo = event[2] + elif name == "patch_change": + c, p = event[2:4] + fl.program_select(c, sfid, 128 if c == 9 else 0, p) + elif name == "control_change": + c, cc, v = event[2:5] + fl.cc(c, cc, v) + elif name == "note_on" and event[3] > 0: + c, p, v = event[2:5] + fl.noteon(c, p, v) + elif name == "note_off" or (name == "note_on" and event[3] == 0): + c, p = event[2:4] + fl.noteoff(c, p) + + fl.delete() + if ss.shape[0] > 0: + max_val = np.abs(ss).max() + if max_val != 0: + ss = (ss / max_val) * np.iinfo(np.int16).max + ss = ss.astype(np.int16) + + if trim_silence: + threshold = np.std(np.abs(ss)) * silence_threshold + exceeded_thresh = np.abs(ss) > threshold + if np.any(exceeded_thresh): + last_idx = np.where(exceeded_thresh)[0][-1] + ss = ss[:last_idx+1] + + if output_for_gradio: + return ss + + ss = ss.swapaxes(1, 0) + + raw_audio = normalize_volume(ss, volume_scale) + + if write_audio_to_WAV != '': + + r_audio = raw_audio.T + + r_audio = np.int16(r_audio / np.max(np.abs(r_audio)) * 32767) + + with wave.open(write_audio_to_WAV, 'w') as wf: + wf.setframerate(sample_rate) + wf.setsampwidth(2) + wf.setnchannels(r_audio.shape[1]) + wf.writeframes(r_audio) + + return raw_audio + + else: + return None + +def midi_to_colab_audio(midi_file, + soundfont_path='/usr/share/sounds/sf2/FluidR3_GM.sf2', + sample_rate=16000, # 44100 + volume_scale=10, + trim_silence=True, + silence_threshold=0.1, + output_for_gradio=False, + write_audio_to_WAV=False + ): + + ''' + + Returns raw audio to pass to IPython.disaply.Audio func + + Example usage: + + from IPython.display import Audio + + display(Audio(raw_audio, rate=16000, normalize=False)) + + ''' + + def normalize_volume(matrix, factor=10): + norm = np.linalg.norm(matrix) + matrix = matrix/norm # normalized matrix + mult_matrix = matrix * factor + final_matrix = np.clip(mult_matrix, -1.0, 1.0) + return final_matrix + + midi_opus = midi2opus(open(midi_file, 'rb').read()) + + if midi_opus[1]: + + ticks_per_beat = midi_opus[0] + event_list = [] + for track_idx, track in enumerate(midi_opus[1:]): + abs_t = 0 + for event in track: + abs_t += event[1] + event_new = [*event] + event_new[1] = abs_t + event_list.append(event_new) + event_list = sorted(event_list, key=lambda e: e[1]) + + tempo = int((60 / 120) * 10 ** 6) # default 120 bpm + ss = np.empty((0, 2), dtype=np.int16) + fl = Synth(samplerate=float(sample_rate)) + sfid = fl.sfload(soundfont_path) + last_t = 0 + for c in range(16): + fl.program_select(c, sfid, 128 if c == 9 else 0, 0) + for event in event_list: + name = event[0] + sample_len = int(((event[1] / ticks_per_beat) * tempo / (10 ** 6)) * sample_rate) + sample_len -= int(((last_t / ticks_per_beat) * tempo / (10 ** 6)) * sample_rate) + last_t = event[1] + if sample_len > 0: + sample = fl.get_samples(sample_len).reshape(sample_len, 2) + ss = np.concatenate([ss, sample]) + if name == "set_tempo": + tempo = event[2] + elif name == "patch_change": + c, p = event[2:4] + fl.program_select(c, sfid, 128 if c == 9 else 0, p) + elif name == "control_change": + c, cc, v = event[2:5] + fl.cc(c, cc, v) + elif name == "note_on" and event[3] > 0: + c, p, v = event[2:5] + fl.noteon(c, p, v) + elif name == "note_off" or (name == "note_on" and event[3] == 0): + c, p = event[2:4] + fl.noteoff(c, p) + + fl.delete() + if ss.shape[0] > 0: + max_val = np.abs(ss).max() + if max_val != 0: + ss = (ss / max_val) * np.iinfo(np.int16).max + ss = ss.astype(np.int16) + + if trim_silence: + threshold = np.std(np.abs(ss)) * silence_threshold + exceeded_thresh = np.abs(ss) > threshold + if np.any(exceeded_thresh): + last_idx = np.where(exceeded_thresh)[0][-1] + ss = ss[:last_idx+1] + + if output_for_gradio: + return ss + + ss = ss.swapaxes(1, 0) + + raw_audio = normalize_volume(ss, volume_scale) + + if write_audio_to_WAV: + + filename = midi_file.split('.')[-2] + '.wav' + + r_audio = raw_audio.T + + r_audio = np.int16(r_audio / np.max(np.abs(r_audio)) * 32767) + + with wave.open(filename, 'w') as wf: + wf.setframerate(sample_rate) + wf.setsampwidth(2) + wf.setnchannels(r_audio.shape[1]) + wf.writeframes(r_audio) + + return raw_audio + + else: + return None + +#=================================================================================================================== \ No newline at end of file diff --git a/packages.txt b/packages.txt new file mode 100644 index 0000000000000000000000000000000000000000..286d57180fea3bf68b7fa31b853dc20e32f856d4 --- /dev/null +++ b/packages.txt @@ -0,0 +1 @@ +fluidsynth diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..270200c9fcd84324703515896af567649f8c1cad --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +torch +gradio +einops \ No newline at end of file diff --git a/x_transformer_1_23_2.py b/x_transformer_1_23_2.py new file mode 100644 index 0000000000000000000000000000000000000000..14ffdab2725fd97c5c15565d1933087d5bf931b8 --- /dev/null +++ b/x_transformer_1_23_2.py @@ -0,0 +1,2464 @@ +#=================================================================================================================== +# +# X Trasformer Module +# +# Partial x-transformers code With useful modifications +# +# Version 1.0 +# +# Original source code courtesy of lucidrains +# https://github.com/lucidrains/x-transformers +# +# Original source code retrieved on 10/10/2023 +# +# Project Los Angeles +# Tegridy Code 2023 + +#=================================================================================================================== + +# Critical dependencies +# +# !pip install torch +# !pip install einops + +#=================================================================================================================== + +from functools import partial +from typing import Optional, Tuple + +import torch +from torch import nn, einsum, Tensor +import torch.nn.functional as F +# from torch.nn.attention import SDPBackend, sdpa_kernel + +from collections import namedtuple +from functools import wraps +from packaging import version +from dataclasses import dataclass + +from einops import rearrange, repeat + +# constants + +EfficientAttentionConfig = namedtuple('EfficientAttentionConfig', ['enable_flash', 'enable_math', 'enable_mem_efficient']) + +@dataclass +class Intermediates: + qk_similarities: Optional[Tensor] = None + pre_softmax_attn: Optional[Tensor] = None + post_softmax_attn: Optional[Tensor] = None + cached_kv: Optional[Tuple[Tensor, Tensor]] = None + + def to_tuple(self): + return (self.qk_similarities, self.pre_softmax_attn, self.post_softmax_attn) + +# helpers + +def exists(val): + return val is not None + +def default(val, d): + return val if exists(val) else d + +def compact(arr): + return [*filter(exists, arr)] + +def once(fn): + called = False + @wraps(fn) + def inner(x): + nonlocal called + if called: + return + called = True + return fn(x) + return inner + +print_once = once(print) + +# functions for creating causal mask +# need a special one for onnx cpu (no support for .triu) + +def create_causal_mask(i, j, device): + return torch.ones((i, j), device = device, dtype = torch.bool).triu(j - i + 1) + +def onnx_create_causal_mask(i, j, device): + r = torch.arange(i, device = device) + causal_mask = rearrange(r, 'i -> i 1') < rearrange(r, 'j -> 1 j') + causal_mask = F.pad(causal_mask, (j - i, 0), value = False) + return causal_mask + +# main class + +class Attend(nn.Module): + def __init__( + self, + *, + dropout = 0., + causal = False, + heads = None, + talking_heads = False, + sparse_topk = None, + scale = None, + qk_norm = False, + flash = False, + add_zero_kv = False, + onnxable = False + ): + super().__init__() + self.scale = scale + self.qk_norm = qk_norm + + self.causal = causal + self.create_causal_mask = onnx_create_causal_mask if onnxable else create_causal_mask + + self.attn_fn = partial(F.softmax, dtype = torch.float32) if not qk_norm else F.softmax + + self.dropout = dropout + self.attn_dropout = nn.Dropout(dropout) + + # talking heads + + assert not (flash and talking_heads), 'talking heads not compatible with flash attention' + + self.talking_heads = talking_heads + if talking_heads: + self.pre_softmax_talking_heads = nn.Conv2d(heads, heads, 1, bias = False) + self.post_softmax_talking_heads = nn.Conv2d(heads, heads, 1, bias = False) + + # sparse topk + + assert not (flash and sparse_topk), 'sparse topk not compatible with flash attention' + self.sparse_topk = sparse_topk + + # add a key / value token composed of zeros + # in case this helps controlling outliers, proposed by https://www.evanmiller.org/attention-is-off-by-one.html + + self.add_zero_kv = add_zero_kv + + # flash attention + + self.flash = flash + assert not (flash and version.parse(torch.__version__) < version.parse('2.0.0')), 'in order to use flash attention, you must be using pytorch 2.0 or above' + + # determine efficient attention configs for cuda and cpu + + self.cpu_config = EfficientAttentionConfig(True, True, True) + self.cuda_config = None + + if not torch.cuda.is_available() or not flash: + return + + device_properties = torch.cuda.get_device_properties(torch.device('cuda')) + + major, minor = device_properties.major, device_properties.minor + + if (major, minor) == (8, 0): + print_once('A100 GPU detected, using flash attention if input tensor is on cuda') + self.cuda_config = EfficientAttentionConfig(True, False, False) + elif (major, minor) == (9, 0): + print_once('H100 GPU detected, using flash attention') + self.cuda_config = EfficientAttentionConfig(True, False, False) + else: + print_once('Non-A100 GPU detected, using math or mem efficient attention if input tensor is on cuda') + self.cuda_config = EfficientAttentionConfig(False, True, True) + + def flash_attn( + self, + q, k, v, + mask = None, + attn_bias = None + ): + batch, heads, q_len, _, k_len, is_cuda, device = *q.shape, k.shape[-2], q.is_cuda, q.device + + # Recommended for multi-query single-key-value attention by Tri Dao + # kv shape torch.Size([1, 512, 64]) -> torch.Size([1, 8, 512, 64]) + + if k.ndim == 3: + k = rearrange(k, 'b ... -> b 1 ...').expand_as(q) + + if v.ndim == 3: + v = rearrange(v, 'b ... -> b 1 ...').expand_as(q) + + # handle scale - by default they scale by dim_head ** -0.5, but need to take care if using cosine sim attention + + if self.qk_norm: + default_scale = q.shape[-1] ** -0.5 + q = q * (self.scale / default_scale) + + # Check if mask exists and expand to compatible shape + # The mask is B L, so it would have to be expanded to B H N L + + causal = self.causal + + # in the case of kv caching with one token (q_len == 1), just turn off causal masking + # in speculative decoding, this may go up to 5-6, so right aligned causal mask will be needed there + + if q_len == 1 and causal: + causal = False + + # expand key padding mask + + if exists(mask): + assert mask.ndim == 4 + mask = mask.expand(batch, heads, q_len, k_len) + + # handle kv cache - this should be bypassable in updated flash attention 2 + + if k_len > q_len and causal: + causal_mask = self.create_causal_mask(q_len, k_len, device = device) + if not exists(mask): + mask = ~causal_mask + else: + mask = mask & ~causal_mask + causal = False + + # manually handle causal mask, if another mask was given + + row_is_entirely_masked = None + + if exists(mask) and causal: + causal_mask = self.create_causal_mask(q_len, k_len, device = device) + mask = mask & ~causal_mask + + # protect against an entire row being masked out + + row_is_entirely_masked = ~mask.any(dim = -1) + mask[..., 0] = mask[..., 0] | row_is_entirely_masked + + causal = False + + # handle alibi positional bias + # convert from bool to float + + if exists(attn_bias): + attn_bias = rearrange(attn_bias, 'h i j -> 1 h i j').expand(batch, heads, -1, -1) + + # if mask given, the mask would already contain the causal mask from above logic + # otherwise, if no mask given but still causal, mask out alibi positional bias to a large negative number + + mask_value = -torch.finfo(q.dtype).max + + if exists(mask): + attn_bias = attn_bias.masked_fill(~mask, mask_value // 2) + elif causal: + causal_mask = self.create_causal_mask(q_len, k_len, device = device) + attn_bias = attn_bias.masked_fill(causal_mask, mask_value // 2) + causal = False + + # scaled_dot_product_attention handles attn_mask either as bool or additive bias + # make it an additive bias here + + mask = attn_bias + + # Check if there is a compatible device for flash attention + + config = self.cuda_config if is_cuda else self.cpu_config + + # pytorch 2.0 flash attn: q, k, v, mask, dropout, causal, softmax_scale + + # Legacy code... + with torch.backends.cuda.sdp_kernel(enable_math=True, enable_mem_efficient=True): + + # New SDP kernel code... + # with sdpa_kernel(SDPBackend.FLASH_ATTENTION): + + out = F.scaled_dot_product_attention( + q, k, v, + attn_mask = mask, + dropout_p = self.dropout if self.training else 0., + is_causal = causal + ) + + # for a row that is entirely masked out, should zero out the output of that row token + + if exists(row_is_entirely_masked): + out = out.masked_fill(row_is_entirely_masked[..., None], 0.) + + return out, Intermediates() + + def forward( + self, + q, k, v, + mask = None, + attn_bias = None, + prev_attn = None + ): + """ + einstein notation + b - batch + h - heads + n, i, j - sequence length (base sequence length, source, target) + d - feature dimension + """ + + n, heads, kv_heads, device = q.shape[-2], q.shape[1], k.shape[1], q.device + + scale = default(self.scale, q.shape[-1] ** -0.5) + + causal = self.causal + + # handle kv cached decoding + + if n == 1 and causal: + causal = False + + # handle grouped multi-query attention + + if kv_heads == 1: + k, v = map(lambda t: rearrange(t, 'b 1 n d -> b n d'), (k, v)) + elif kv_heads < heads: + k, v = map(lambda t: repeat(t, 'b kvh n d -> b (r kvh) n d', r = heads // kv_heads), (k, v)) + + # handle zero kv, as means for allowing network to attend to nothing + + if self.add_zero_kv: + k, v = map(lambda t: F.pad(t, (0, 0, 1, 0), value = 0.), (k, v)) + + if exists(mask): + mask = F.pad(mask, (1, 0), value = True) + + if exists(attn_bias): + attn_bias = F.pad(attn_bias, (1, 0), value = 0.) + + if self.flash: + assert not exists(prev_attn), 'residual attention not compatible with flash attention' + return self.flash_attn(q, k, v, mask = mask, attn_bias = attn_bias) + + kv_einsum_eq = 'b j d' if k.ndim == 3 else 'b h j d' + + dots = einsum(f'b h i d, {kv_einsum_eq} -> b h i j', q, k) * scale + + if exists(prev_attn): + dots = dots + prev_attn + + qk_similarities = dots.clone() + + if self.talking_heads: + dots = self.pre_softmax_talking_heads(dots) + + if exists(attn_bias): + dots = dots + attn_bias + + i, j, dtype = *dots.shape[-2:], dots.dtype + + mask_value = -torch.finfo(dots.dtype).max + + if exists(self.sparse_topk) and self.sparse_topk < j: + top_values, _ = dots.topk(self.sparse_topk, dim = -1) + sparse_topk_mask = dots < top_values[..., -1:] + mask = (mask & sparse_topk_mask) if exists(mask) else sparse_topk_mask + + if exists(mask): + dots = dots.masked_fill(~mask, mask_value) + + if causal: + causal_mask = self.create_causal_mask(i, j, device = device) + dots = dots.masked_fill(causal_mask, mask_value) + + pre_softmax_attn = dots.clone() + + attn = self.attn_fn(dots, dim = -1) + attn = attn.type(dtype) + + post_softmax_attn = attn.clone() + + attn = self.attn_dropout(attn) + + if self.talking_heads: + attn = self.post_softmax_talking_heads(attn) + + out = einsum(f'b h i j, {kv_einsum_eq} -> b h i d', attn, v) + + intermediates = Intermediates( + qk_similarities = qk_similarities, + pre_softmax_attn = pre_softmax_attn, + post_softmax_attn = post_softmax_attn + ) + + return out, intermediates + +#=================================================================================================================== + +from math import ceil, log +from typing import Optional, Union, Tuple, Callable + +import torch +from torch import nn, Tensor +from torch.nn import Module +import torch.nn.functional as F + +from einops import rearrange, pack, unpack + +def exists(val): + return val is not None + +def default(val, d): + return val if exists(val) else d + +def identity(t, *args, **kwargs): + return t + +def cast_tuple(t, length = 1): + return t if isinstance(t, tuple) else (t,) * length + +def eval_decorator(fn): + def inner(self, *args, **kwargs): + was_training = self.training + self.eval() + out = fn(self, *args, **kwargs) + self.train(was_training) + return out + return inner + +# for variable lengthed prefixes + +def align_right(t, lens, pad_id = 0): + batch, seq_len, device, dtype = *t.shape, t.device, t.dtype + + assert lens.ndim == 1 and lens.shape[0] == batch + assert lens.amax() <= seq_len + + pad_lens = seq_len - lens + max_pad_len = pad_lens.amax() + + batch_arange = torch.arange(batch, device = device, dtype = torch.long)[..., None] + prompt_len_arange = torch.arange(seq_len, device = device, dtype = torch.long) + + t = F.pad(t, (max_pad_len, 0), value = 0) + offset = max_pad_len - pad_lens + + aligned = t[batch_arange, prompt_len_arange + offset[..., None]] + return aligned + +# nucleus + +def top_p(logits, thres = 0.9): + sorted_logits, sorted_indices = torch.sort(logits, descending = True) + cum_probs = torch.cumsum(F.softmax(sorted_logits, dim = -1), dim = -1) + + sorted_indices_to_remove = cum_probs > thres + sorted_indices_to_remove = F.pad(sorted_indices_to_remove, (1, -1), value = False) + + sorted_logits[sorted_indices_to_remove] = float('-inf') + return sorted_logits.scatter(1, sorted_indices, sorted_logits) + +# topk + +def top_k(logits, frac_num_tokens = 0.1, k = None): + num_tokens = logits.shape[-1] + + k = default(k, ceil(frac_num_tokens * num_tokens)) + k = min(k, num_tokens) + + val, ind = torch.topk(logits, k) + probs = torch.full_like(logits, float('-inf')) + probs.scatter_(1, ind, val) + return probs + +# top_a + +def top_a(logits, min_p_pow = 2.0, min_p_ratio = 0.02): + probs = F.softmax(logits, dim = -1) + max_probs = torch.amax(probs, dim = -1, keepdim = True) + limit = torch.pow(max_probs, min_p_pow) * min_p_ratio + return torch.where(probs < limit, float('-inf'), logits) + +# contrastive decoding function + +def contrastive_decode_fn( + expert_logits, + amateur_logits, + alpha = 0.1, + beta = 0.5 +): + """ + Appendix A Algorithm 2 + https://arxiv.org/abs/2309.09117 + """ + + cutoff = log(alpha) + expert_logits.amax(dim = -1, keepdim = True) + diffs = (1 + beta) * expert_logits - beta * amateur_logits + contrastive_decode_logits = diffs.masked_fill(expert_logits < cutoff, -torch.finfo(expert_logits.dtype).max) + return contrastive_decode_logits + +# autoregressive wrapper class + +class AutoregressiveWrapper(Module): + def __init__( + self, + net, + ignore_index = -100, + pad_value = 0, + mask_prob = 0., + add_attn_z_loss = False + ): + super().__init__() + self.pad_value = pad_value + self.ignore_index = ignore_index + + self.net = net + self.max_seq_len = net.max_seq_len + + # paper shows masking (MLM) in conjunction with autoregressive decoder-only training leads to big improvements https://arxiv.org/abs/2210.13432 + assert mask_prob < 1. + self.mask_prob = mask_prob + + # whether to add router z-loss + self.add_attn_z_loss = add_attn_z_loss + + @torch.no_grad() + @eval_decorator + def generate( + self, + prompts, + seq_len, + eos_token = None, + temperature = 1., + prompt_lens: Optional[Tensor] = None, + filter_logits_fn: Callable = top_k, + restrict_to_max_seq_len = True, + amateur_model: Optional[Union[Module, Tuple[Module]]] = None, + filter_kwargs: dict = dict(), + contrastive_decode_kwargs: Union[dict, Tuple[dict]] = dict( + beta = 0.5, + alpha = 0.1 + ), + cache_kv = True, + verbose=True, + return_prime=False, + **kwargs + ): + max_seq_len, device = self.max_seq_len, prompts.device + + prompts, ps = pack([prompts], '* n') + + b, t = prompts.shape + + # handle variable lengthed prompts (prefixes) + + seq_start_pos = None + if exists(prompt_lens): + prompts = align_right(prompts, prompt_lens, pad_id = self.pad_value) + seq_start_pos = t - prompt_lens + + # output from which sampled tokens appended to + + out = prompts + + if verbose: + print("Generating sequence of max length:", seq_len) + + # kv caches + + cache = None + + # if doing contrastive decoding, turn off filter automatically + + if exists(amateur_model): + amateur_model = cast_tuple(amateur_model) + contrastive_decode_kwargs = cast_tuple(contrastive_decode_kwargs) + + assert len(amateur_model) == len(contrastive_decode_kwargs) + + amateur_caches = [None] * len(amateur_model) + filter_logits_fn = identity + + for i, module in enumerate(amateur_model): + if isinstance(module, AutoregressiveWrapper): + amateur_model[i] = module.net + + module.eval() + + # sampling up to seq_len + + for sl in range(seq_len): + + if restrict_to_max_seq_len: + x = out[:, -max_seq_len:] + + if exists(cache): + for inter in cache.attn_intermediates: + inter.cached_kv = [t[..., -(max_seq_len - 1):, :] for t in inter.cached_kv] + + logits, new_cache = self.net( + x, + return_intermediates = True, + cache = cache, + seq_start_pos = seq_start_pos, + **kwargs + ) + + if cache_kv and self.net.can_cache_kv: + cache = new_cache + + logits = logits[:, -1] + + # handle contrastive decoding, Li et al. + # https://arxiv.org/abs/2210.15097 + + if exists(amateur_model): + for i, (amateur, amateur_cache, amateur_contrastive_decode_kwargs) in enumerate(zip(amateur_model, amateur_caches, contrastive_decode_kwargs)): + amateur_logits, next_amateur_cache = amateur( + x, + return_intermediates = True, + cache = amateur_cache, + seq_start_pos = seq_start_pos, + **kwargs + ) + + amateur_logits = amateur_logits[:, -1] + + assert amateur_logits.shape == logits.shape, 'logits dimension are not the same between amateur and expert model' + logits = contrastive_decode_fn(logits, amateur_logits, **amateur_contrastive_decode_kwargs) + + if cache_kv and amateur.can_cache_kv: + amateur_caches[i] = next_amateur_cache + + # filter by top_k, top_p (nucleus), top_a, or custom + + filtered_logits = filter_logits_fn(logits, **filter_kwargs) + + probs = F.softmax(filtered_logits / temperature, dim=-1) + + sample = torch.multinomial(probs, 1) + + out = torch.cat((out, sample), dim=-1) + + if verbose: + if sl % 32 == 0: + print(sl, '/', seq_len) + + if exists(eos_token): + is_eos_tokens = (out == eos_token) + + if is_eos_tokens.any(dim = -1).all(): + # mask out everything after the eos tokens + shifted_is_eos_tokens = F.pad(is_eos_tokens, (1, -1)) + mask = shifted_is_eos_tokens.float().cumsum(dim = -1) >= 1 + out = out.masked_fill(mask, self.pad_value) + + if verbose: + print('Model called the end of sequence at:', sl, '/', seq_len) + + break + + if return_prime: + return out[:, :] + + else: + return out[:, t:] + + # out, = unpack(out, ps, '* n') + + # return out + + def compute_accuracy(self, logits, labels): + out = torch.argmax(logits, dim=-1) + out = out.flatten() + labels = labels.flatten() + + mask = (labels != self.ignore_index) # can also be self.pad_value (your choice) + out = out[mask] + labels = labels[mask] + + num_right = (out == labels) + num_right = torch.sum(num_right).type(torch.float32) + + acc = num_right / len(labels) + return acc + + def forward(self, x, **kwargs): + seq, ignore_index, add_attn_z_loss = x.shape[1], self.ignore_index, self.add_attn_z_loss + + inp, target = x[:, :-1], x[:, 1:] + inp = torch.where(inp == ignore_index, self.pad_value, inp) + + if self.mask_prob > 0.: + rand = torch.randn(inp.shape, device = x.device) + rand[:, 0] = -torch.finfo(rand.dtype).max # first token should not be masked out + num_mask = min(int(seq * self.mask_prob), seq - 1) + indices = rand.topk(num_mask, dim = -1).indices + mask = ~torch.zeros_like(inp).scatter(1, indices, 1.).bool() + kwargs.update(self_attn_kv_mask = mask) + + logits, cache = self.net( + inp, + return_intermediates = True, + return_attn_z_loss = add_attn_z_loss, + **kwargs + ) + + acc = self.compute_accuracy(logits, target) + + loss = F.cross_entropy( + rearrange(logits, 'b n c -> b c n'), + target, + ignore_index = ignore_index + ) + + if add_attn_z_loss: + loss = loss + cache.attn_z_loss + + return loss, acc + +#=============================================================================== + +import math +from random import random + +import torch +from torch import nn, einsum, Tensor +import torch.nn.functional as F + +from functools import partial, wraps +from inspect import isfunction +from collections import namedtuple +from dataclasses import dataclass +from typing import List, Callable, Optional + +from einops import rearrange, repeat, reduce, pack, unpack +from einops.layers.torch import Rearrange + +# constants + +DEFAULT_DIM_HEAD = 64 + +@dataclass +class LayerIntermediates: + hiddens: Optional[List[Tensor]] = None + attn_intermediates: Optional[List[Intermediates]] = None + layer_hiddens: Optional[List[Tensor]] = None + attn_z_loss: Optional[Tensor] = None + mems: Optional[Tensor] = None + +# helpers + +def exists(val): + return val is not None + +def default(val, d): + if exists(val): + return val + return d() if isfunction(d) else d + +def cast_tuple(val, depth): + return val if isinstance(val, tuple) else (val,) * depth + +def divisible_by(num, den): + return (num % den) == 0 + +def maybe(fn): + @wraps(fn) + def inner(x, *args, **kwargs): + if not exists(x): + return x + return fn(x, *args, **kwargs) + return inner + +class always(): + def __init__(self, val): + self.val = val + def __call__(self, *args, **kwargs): + return self.val + +class not_equals(): + def __init__(self, val): + self.val = val + def __call__(self, x, *args, **kwargs): + return x != self.val + +class equals(): + def __init__(self, val): + self.val = val + def __call__(self, x, *args, **kwargs): + return x == self.val + +def Sequential(*modules): + return nn.Sequential(*filter(exists, modules)) + +# tensor helpers + +def max_neg_value(tensor): + return -torch.finfo(tensor.dtype).max + +def l2norm(t, groups = 1): + t = rearrange(t, '... (g d) -> ... g d', g = groups) + t = F.normalize(t, p = 2, dim = -1) + return rearrange(t, '... g d -> ... (g d)') + +def pad_at_dim(t, pad, dim = -1, value = 0.): + dims_from_right = (- dim - 1) if dim < 0 else (t.ndim - dim - 1) + zeros = ((0, 0) * dims_from_right) + return F.pad(t, (*zeros, *pad), value = value) + +def or_reduce(masks): + head, *body = masks + for rest in body: + head = head | rest + return head + +# auxiliary loss helpers + +def calc_z_loss( + pre_softmax_attns: List[Tensor], + mask = None, + weight = 1. +): + # the same loss applied to the mixture of experts router logits in https://arxiv.org/abs/2202.08906 + # in the paper, in a tiny footnote, they mention using it on attention logits with stabilizing effects + # also used in PaLM as one of the measures + + lse = 0. + + for attn in pre_softmax_attns: + lse = lse + attn.logsumexp(dim = -1) + + loss = torch.square(lse) + loss = reduce(loss, 'b h n -> b n', 'sum') + + if not exists(mask): + return loss.mean() * weight + + loss = loss[mask].sum() / mask.sum().clamp(min = 1e-5) + return loss * weight + +# init helpers + +def init_zero_(layer): + nn.init.constant_(layer.weight, 0.) + if exists(layer.bias): + nn.init.constant_(layer.bias, 0.) + +# keyword argument helpers + +def pick_and_pop(keys, d): + values = list(map(lambda key: d.pop(key), keys)) + return dict(zip(keys, values)) + +def group_dict_by_key(cond, d): + return_val = [dict(),dict()] + for key in d.keys(): + match = bool(cond(key)) + ind = int(not match) + return_val[ind][key] = d[key] + return (*return_val,) + +def string_begins_with(prefix, str): + return str.startswith(prefix) + +def group_by_key_prefix(prefix, d): + return group_dict_by_key(partial(string_begins_with, prefix), d) + +def groupby_prefix_and_trim(prefix, d): + kwargs_with_prefix, kwargs = group_dict_by_key(partial(string_begins_with, prefix), d) + kwargs_without_prefix = dict(map(lambda x: (x[0][len(prefix):], x[1]), tuple(kwargs_with_prefix.items()))) + return kwargs_without_prefix, kwargs + +# structured dropout, more effective than traditional attention dropouts + +def dropout_seq(seq, mask, dropout): + b, n, *_, device = *seq.shape, seq.device + logits = torch.randn(b, n, device = device) + + if exists(mask): + mask_value = max_neg_value(logits) + logits = logits.masked_fill(~mask, mask_value) + + keep_prob = 1. - dropout + num_keep = max(1, int(keep_prob * n)) + keep_indices = logits.topk(num_keep, dim = 1).indices + + batch_indices = torch.arange(b, device = device) + batch_indices = rearrange(batch_indices, 'b -> b 1') + + seq = seq[batch_indices, keep_indices] + + if exists(mask): + seq_counts = mask.sum(dim = -1) + seq_keep_counts = torch.ceil(seq_counts * keep_prob).int() + keep_mask = torch.arange(num_keep, device = device) < rearrange(seq_keep_counts, 'b -> b 1') + + mask = mask[batch_indices, keep_indices] & keep_mask + + return seq, mask + +# activations + +class ReluSquared(nn.Module): + def forward(self, x): + return F.relu(x) ** 2 + +# embedding + +class TokenEmbedding(nn.Module): + def __init__(self, dim, num_tokens, l2norm_embed = False): + super().__init__() + self.l2norm_embed = l2norm_embed + self.emb = nn.Embedding(num_tokens, dim) + + def forward(self, x): + token_emb = self.emb(x) + return l2norm(token_emb) if self.l2norm_embed else token_emb + +# positional embeddings + +class AbsolutePositionalEmbedding(nn.Module): + def __init__(self, dim, max_seq_len, l2norm_embed = False): + super().__init__() + self.scale = dim ** -0.5 if not l2norm_embed else 1. + self.max_seq_len = max_seq_len + self.l2norm_embed = l2norm_embed + self.emb = nn.Embedding(max_seq_len, dim) + + def forward(self, x, pos = None, seq_start_pos = None): + seq_len, device = x.shape[1], x.device + assert seq_len <= self.max_seq_len, f'you are passing in a sequence length of {seq_len} but your absolute positional embedding has a max sequence length of {self.max_seq_len}' + + if not exists(pos): + pos = torch.arange(seq_len, device = device) + + if exists(seq_start_pos): + pos = (pos - seq_start_pos[..., None]).clamp(min = 0) + + pos_emb = self.emb(pos) + pos_emb = pos_emb * self.scale + return l2norm(pos_emb) if self.l2norm_embed else pos_emb + +class ScaledSinusoidalEmbedding(nn.Module): + def __init__(self, dim, theta = 10000): + super().__init__() + assert divisible_by(dim, 2) + self.scale = nn.Parameter(torch.ones(1) * dim ** -0.5) + + half_dim = dim // 2 + freq_seq = torch.arange(half_dim).float() / half_dim + inv_freq = theta ** -freq_seq + self.register_buffer('inv_freq', inv_freq, persistent = False) + + def forward(self, x, pos = None, seq_start_pos = None): + seq_len, device = x.shape[1], x.device + + if not exists(pos): + pos = torch.arange(seq_len, device = device) + + if exists(seq_start_pos): + pos = pos - seq_start_pos[..., None] + + emb = einsum('i, j -> i j', pos, self.inv_freq) + emb = torch.cat((emb.sin(), emb.cos()), dim = -1) + return emb * self.scale + +class RelativePositionBias(nn.Module): + def __init__(self, scale, causal = False, num_buckets = 32, max_distance = 128, heads = 8): + super().__init__() + self.scale = scale + self.causal = causal + self.num_buckets = num_buckets + self.max_distance = max_distance + self.relative_attention_bias = nn.Embedding(num_buckets, heads) + + @staticmethod + def _relative_position_bucket(relative_position, causal = True, num_buckets = 32, max_distance = 128): + ret = 0 + n = -relative_position + if not causal: + num_buckets //= 2 + ret += (n < 0).long() * num_buckets + n = torch.abs(n) + else: + n = torch.max(n, torch.zeros_like(n)) + + max_exact = num_buckets // 2 + is_small = n < max_exact + + val_if_large = max_exact + ( + torch.log(n.float() / max_exact) / math.log(max_distance / max_exact) * (num_buckets - max_exact) + ).long() + val_if_large = torch.min(val_if_large, torch.full_like(val_if_large, num_buckets - 1)) + + ret += torch.where(is_small, n, val_if_large) + return ret + + @property + def device(self): + return next(self.parameters()).device + + def forward(self, i, j): + device = self.device + q_pos = torch.arange(j - i, j, dtype = torch.long, device = device) + k_pos = torch.arange(j, dtype = torch.long, device = device) + rel_pos = k_pos[None, :] - q_pos[:, None] + rp_bucket = self._relative_position_bucket(rel_pos, causal = self.causal, num_buckets = self.num_buckets, max_distance = self.max_distance) + values = self.relative_attention_bias(rp_bucket) + bias = rearrange(values, 'i j h -> h i j') + return bias * self.scale + +class DynamicPositionBias(nn.Module): + def __init__(self, dim, *, heads, depth, log_distance = False, norm = False): + super().__init__() + assert depth >= 1, 'depth for dynamic position bias MLP must be greater or equal to 1' + self.log_distance = log_distance + + self.mlp = nn.ModuleList([]) + + self.mlp.append(Sequential( + nn.Linear(1, dim), + nn.LayerNorm(dim) if norm else None, + nn.SiLU() + )) + + for _ in range(depth - 1): + self.mlp.append(Sequential( + nn.Linear(dim, dim), + nn.LayerNorm(dim) if norm else None, + nn.SiLU() + )) + + self.mlp.append(nn.Linear(dim, heads)) + + @property + def device(self): + return next(self.parameters()).device + + def forward(self, i, j): + assert i == j + n, device = j, self.device + + # get the (n x n) matrix of distances + seq_arange = torch.arange(n, device = device) + context_arange = torch.arange(n, device = device) + indices = rearrange(seq_arange, 'i -> i 1') - rearrange(context_arange, 'j -> 1 j') + indices += (n - 1) + + # input to continuous positions MLP + pos = torch.arange(-n + 1, n, device = device).float() + pos = rearrange(pos, '... -> ... 1') + + if self.log_distance: + pos = torch.sign(pos) * torch.log(pos.abs() + 1) # log of distance is sign(rel_pos) * log(abs(rel_pos) + 1) + + for layer in self.mlp: + pos = layer(pos) + + # get position biases + bias = pos[indices] + bias = rearrange(bias, 'i j h -> h i j') + return bias + +class AlibiPositionalBias(nn.Module): + def __init__(self, heads, total_heads, **kwargs): + super().__init__() + self.heads = heads + self.total_heads = total_heads + + slopes = Tensor(self._get_slopes(heads)) + slopes = rearrange(slopes, 'h -> h 1 1') + self.register_buffer('slopes', slopes, persistent = False) + self.register_buffer('bias', None, persistent = False) + + def get_bias(self, i, j, device): + i_arange = torch.arange(j - i, j, device = device) + j_arange = torch.arange(j, device = device) + bias = -torch.abs(rearrange(j_arange, 'j -> 1 1 j') - rearrange(i_arange, 'i -> 1 i 1')) + return bias + + @staticmethod + def _get_slopes(heads): + def get_slopes_power_of_2(n): + start = (2**(-2**-(math.log2(n)-3))) + ratio = start + return [start*ratio**i for i in range(n)] + + if math.log2(heads).is_integer(): + return get_slopes_power_of_2(heads) + + closest_power_of_2 = 2 ** math.floor(math.log2(heads)) + return get_slopes_power_of_2(closest_power_of_2) + get_slopes_power_of_2(2 * closest_power_of_2)[0::2][:heads-closest_power_of_2] + + @property + def device(self): + return next(self.buffers()).device + + def forward(self, i, j): + h, device = self.total_heads, self.device + + if exists(self.bias) and self.bias.shape[-1] >= j and self.bias.shape[-2] >= i: + return self.bias[..., -i:, -j:] + + bias = self.get_bias(i, j, device) + bias = bias * self.slopes + + num_heads_unalibied = h - bias.shape[0] + bias = pad_at_dim(bias, (0, num_heads_unalibied), dim = 0) + self.register_buffer('bias', bias, persistent = False) + + return self.bias + +class RotaryEmbedding(nn.Module): + def __init__( + self, + dim, + use_xpos = False, + scale_base = 512, + interpolation_factor = 1., + base = 10000, + base_rescale_factor = 1. + ): + super().__init__() + # proposed by reddit user bloc97, to rescale rotary embeddings to longer sequence length without fine-tuning + # has some connection to NTK literature + # https://www.reddit.com/r/LocalLLaMA/comments/14lz7j5/ntkaware_scaled_rope_allows_llama_models_to_have/ + base *= base_rescale_factor ** (dim / (dim - 2)) + + inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim)) + self.register_buffer('inv_freq', inv_freq) + + assert interpolation_factor >= 1. + self.interpolation_factor = interpolation_factor + + if not use_xpos: + self.register_buffer('scale', None) + return + + scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) + + self.scale_base = scale_base + self.register_buffer('scale', scale) + + def forward(self, seq_len): + device = self.inv_freq.device + t = torch.arange(seq_len, device = device).type_as(self.inv_freq) + + t = t / self.interpolation_factor + + freqs = torch.einsum('i , j -> i j', t, self.inv_freq) + freqs = torch.cat((freqs, freqs), dim = -1) + + if not exists(self.scale): + return freqs, 1. + + power = (torch.arange(seq_len, device = device) - (seq_len // 2)) / self.scale_base + scale = self.scale ** rearrange(power, 'n -> n 1') + scale = torch.cat((scale, scale), dim = -1) + + return freqs, scale + + +def rotate_half(x): + x = rearrange(x, '... (j d) -> ... j d', j = 2) + x1, x2 = x.unbind(dim = -2) + return torch.cat((-x2, x1), dim = -1) + +def apply_rotary_pos_emb(t, freqs, scale = 1): + rot_dim, seq_len = freqs.shape[-1], t.shape[-2] + freqs = freqs[-seq_len:, :] + + if t.ndim == 4 and freqs.ndim == 3: + freqs = rearrange(freqs, 'b n d -> b 1 n d') + + # partial rotary embeddings, Wang et al. GPT-J + t, t_unrotated = t[..., :rot_dim], t[..., rot_dim:] + t = (t * freqs.cos() * scale) + (rotate_half(t) * freqs.sin() * scale) + return torch.cat((t, t_unrotated), dim = -1) + +# norms + +class Scale(nn.Module): + def __init__(self, value, fn): + super().__init__() + self.value = value + self.fn = fn + + def forward(self, x, **kwargs): + out = self.fn(x, **kwargs) + scale_fn = lambda t: t * self.value + + if not isinstance(out, tuple): + return scale_fn(out) + + return (scale_fn(out[0]), *out[1:]) + +class ScaleNorm(nn.Module): + def __init__(self, dim, eps = 1e-5): + super().__init__() + self.eps = eps + self.g = nn.Parameter(torch.ones(1) * (dim ** -0.5)) + + def forward(self, x): + norm = torch.norm(x, dim = -1, keepdim = True) + return x / norm.clamp(min = self.eps) * self.g + +class RMSNorm(nn.Module): + def __init__(self, dim): + super().__init__() + self.scale = dim ** 0.5 + self.g = nn.Parameter(torch.ones(dim)) + + def forward(self, x): + return F.normalize(x, dim = -1) * self.scale * self.g + +class SimpleRMSNorm(nn.Module): + def __init__(self, dim): + super().__init__() + self.scale = dim ** 0.5 + + def forward(self, x): + return F.normalize(x, dim = -1) * self.scale + +# residual and residual gates + +class Residual(nn.Module): + def __init__(self, dim, scale_residual = False, scale_residual_constant = 1.): + super().__init__() + self.residual_scale = nn.Parameter(torch.ones(dim)) if scale_residual else None + self.scale_residual_constant = scale_residual_constant + + def forward(self, x, residual): + if exists(self.residual_scale): + residual = residual * self.residual_scale + + if self.scale_residual_constant != 1: + residual = residual * self.scale_residual_constant + + return x + residual + +class GRUGating(nn.Module): + def __init__(self, dim, scale_residual = False, **kwargs): + super().__init__() + self.gru = nn.GRUCell(dim, dim) + self.residual_scale = nn.Parameter(torch.ones(dim)) if scale_residual else None + + def forward(self, x, residual): + if exists(self.residual_scale): + residual = residual * self.residual_scale + + gated_output = self.gru( + rearrange(x, 'b n d -> (b n) d'), + rearrange(residual, 'b n d -> (b n) d') + ) + + return gated_output.reshape_as(x) + +# token shifting + +def shift(t, amount, mask = None): + if amount == 0: + return t + else: + amount = min(amount, t.shape[1]) + + if exists(mask): + t = t.masked_fill(~mask[..., None], 0.) + + return pad_at_dim(t, (amount, -amount), dim = - 2, value = 0.) + +class ShiftTokens(nn.Module): + def __init__(self, shifts, fn): + super().__init__() + self.fn = fn + self.shifts = tuple(shifts) + + def forward(self, x, **kwargs): + mask = kwargs.get('mask', None) + shifts = self.shifts + segments = len(shifts) + feats_per_shift = x.shape[-1] // segments + splitted = x.split(feats_per_shift, dim = -1) + segments_to_shift, rest = splitted[:segments], splitted[segments:] + segments_to_shift = list(map(lambda args: shift(*args, mask = mask), zip(segments_to_shift, shifts))) + x = torch.cat((*segments_to_shift, *rest), dim = -1) + return self.fn(x, **kwargs) + +# feedforward + +class GLU(nn.Module): + def __init__( + self, + dim_in, + dim_out, + activation: Callable, + mult_bias = False + ): + super().__init__() + self.act = activation + self.proj = nn.Linear(dim_in, dim_out * 2) + self.mult_bias = nn.Parameter(torch.ones(dim_out)) if mult_bias else 1. + + def forward(self, x): + x, gate = self.proj(x).chunk(2, dim = -1) + return x * self.act(gate) * self.mult_bias + +class FeedForward(nn.Module): + def __init__( + self, + dim, + dim_out = None, + mult = 4, + glu = False, + glu_mult_bias = False, + swish = False, + relu_squared = False, + post_act_ln = False, + dropout = 0., + no_bias = False, + zero_init_output = False + ): + super().__init__() + inner_dim = int(dim * mult) + dim_out = default(dim_out, dim) + + if relu_squared: + activation = ReluSquared() + elif swish: + activation = nn.SiLU() + else: + activation = nn.GELU() + + if glu: + project_in = GLU(dim, inner_dim, activation, mult_bias = glu_mult_bias) + else: + project_in = nn.Sequential( + nn.Linear(dim, inner_dim, bias = not no_bias), + activation + ) + + self.ff = Sequential( + project_in, + nn.LayerNorm(inner_dim) if post_act_ln else None, + nn.Dropout(dropout), + nn.Linear(inner_dim, dim_out, bias = not no_bias) + ) + + # init last linear layer to 0 + if zero_init_output: + init_zero_(self.ff[-1]) + + def forward(self, x): + return self.ff(x) + +# attention. it is all we need + +class Attention(nn.Module): + def __init__( + self, + dim, + dim_head = DEFAULT_DIM_HEAD, + heads = 8, + causal = False, + flash = False, + talking_heads = False, + head_scale = False, + sparse_topk = None, + num_mem_kv = 0, + dropout = 0., + on_attn = False, + gate_value_heads = False, + gate_values = False, + zero_init_output = False, + max_attend_past = None, + qk_norm = False, + qk_norm_groups = 1, + qk_norm_scale = 10, + qk_norm_dim_scale = False, + one_kv_head = False, + kv_heads = None, + shared_kv = False, + value_dim_head = None, + tensor_product = False, # https://arxiv.org/abs/2208.06061 + add_zero_kv = False, # same as add_zero_attn in pytorch + rotary_embed_values = False, + onnxable = False + ): + super().__init__() + self.scale = dim_head ** -0.5 + + self.heads = heads + self.causal = causal + self.max_attend_past = max_attend_past + + assert not (exists(kv_heads) and one_kv_head), 'either attn_one_kv_head is set to True (in which case kv_heads is set to 1), or attn_kv_heads is set, but not both' + + value_dim_head = default(value_dim_head, dim_head) + kv_heads = default(kv_heads, heads) + + kv_heads = 1 if one_kv_head else kv_heads + assert divisible_by(heads, kv_heads) + + self.kv_heads = kv_heads + + q_dim = dim_head * heads + k_dim = dim_head * kv_heads + v_dim = value_dim_head * kv_heads + out_dim = value_dim_head * heads + + self.to_q = nn.Linear(dim, q_dim, bias = False) + self.to_k = nn.Linear(dim, k_dim, bias = False) + + # shared key / values, for further memory savings during inference + assert not (shared_kv and value_dim_head != dim_head), 'key and value head dimensions must be equal for shared key / values' + self.to_v = nn.Linear(dim, v_dim, bias = False) if not shared_kv else None + + # relations projection from tp-attention + self.to_r = nn.Linear(dim, v_dim, bias = False) if tensor_product else None + + # add GLU gating for aggregated values, from alphafold2 + self.to_v_gate = None + if gate_values: + self.to_v_gate = nn.Linear(dim, out_dim) + nn.init.constant_(self.to_v_gate.weight, 0) + nn.init.constant_(self.to_v_gate.bias, 10) + + # add per head gating of the output values, from 'Attend to nothing' paper + self.to_v_head_gate = None + if gate_value_heads: + self.to_v_head_gate = nn.Linear(dim, heads) + nn.init.constant_(self.to_v_head_gate.weight, 0) + nn.init.constant_(self.to_v_head_gate.bias, 10) + + # cosine sim attention + self.qk_norm = qk_norm + self.qk_norm_groups = qk_norm_groups + self.qk_norm_scale = qk_norm_scale + + # whether to use the rmsnorm (equivalent to cosine sim attention when scale is equal to 1) - https://arxiv.org/abs/2302.05442 + self.qk_norm_dim_scale = qk_norm_dim_scale + + self.qk_norm_q_scale = self.qk_norm_k_scale = 1 + if qk_norm and qk_norm_dim_scale: + self.qk_norm_q_scale = nn.Parameter(torch.ones(heads, 1, dim_head)) + self.qk_norm_k_scale = nn.Parameter(torch.ones(heads, 1, dim_head)) + + assert (not qk_norm) or divisible_by(dim_head, qk_norm_groups), 'dimension per attention head must be divisible by the qk norm groups' + assert not (qk_norm and (dim_head // qk_norm_groups) <= 2), 'the group dimension may be too small (2 was too small in my tests, but 4 still works, surprisingly)' + + # attend class - includes core attention algorithm + talking heads + + self.attend = Attend( + heads = heads, + causal = causal, + talking_heads = talking_heads, + dropout = dropout, + sparse_topk = sparse_topk, + qk_norm = qk_norm, + scale = qk_norm_scale if qk_norm else self.scale, + add_zero_kv = add_zero_kv, + flash = flash, + onnxable = onnxable + ) + + # head scaling + self.head_scale = head_scale + if head_scale: + self.head_scale_params = nn.Parameter(torch.ones(1, heads, 1, 1)) + + # explicit topk sparse attention + self.sparse_topk = sparse_topk + + # add memory key / values + self.num_mem_kv = num_mem_kv + if num_mem_kv > 0: + self.mem_k = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) + self.mem_v = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) + + # attention on attention + self.attn_on_attn = on_attn + self.to_out = nn.Sequential(nn.Linear(out_dim, dim * 2, bias = False), nn.GLU()) if on_attn else nn.Linear(out_dim, dim, bias = False) + + # whether to rotate positions into values, for absolute positions in addition to relative + self.rotary_embed_values = rotary_embed_values + + # init output projection 0 + if zero_init_output: + init_zero_(self.to_out) + + def forward( + self, + x, + context = None, + mask = None, + context_mask = None, + attn_mask = None, + rel_pos = None, + rotary_pos_emb = None, + prev_attn = None, + mem = None, + return_intermediates = False, + cache: Optional[Intermediates] = None, + ): + b, n, _, h, kv_h, head_scale, device, has_context = *x.shape, self.heads, self.kv_heads, self.head_scale, x.device, exists(context) + kv_input = default(context, x) + + q_input = x + k_input = kv_input + v_input = kv_input + r_input = x + + if exists(mem): + k_input, mem_packed_shape = pack([mem, k_input], 'b * d') + v_input, _ = pack([mem, v_input], 'b * d') + + q = self.to_q(q_input) + k = self.to_k(k_input) + v = self.to_v(v_input) if exists(self.to_v) else k + r = self.to_r(r_input) if exists(self.to_r) else None + + q = rearrange(q, 'b n (h d) -> b h n d', h = h) + + k, v, r = map(lambda t: maybe(rearrange)(t, 'b n (h d) -> b h n d', h = kv_h), (k, v, r)) + + if exists(cache) and not has_context: + ck, cv = cache.cached_kv + + if exists(mem): + mk, k = unpack(k, mem_packed_shape, 'b h * d') + mv, v = unpack(v, mem_packed_shape, 'b h * d') + + k = torch.cat((ck, k), dim = -2) + v = torch.cat((cv, v), dim = -2) + + if exists(mem): + k = torch.cat((mk, k), dim = -2) + v = torch.cat((mv, v), dim = -2) + + if return_intermediates: + mem_len = mem.shape[-2] if exists(mem) else 0 + cached_kv = (k[..., mem_len:, :], v[..., mem_len:, :]) + + if self.qk_norm: + qk_l2norm = partial(l2norm, groups = self.qk_norm_groups) + q, k = map(qk_l2norm, (q, k)) + scale = self.qk_norm_scale + + q = q * self.qk_norm_q_scale + k = k * self.qk_norm_k_scale + + if exists(rotary_pos_emb) and not has_context: + freqs, xpos_scale = rotary_pos_emb + q_xpos_scale, k_xpos_scale = (xpos_scale, xpos_scale ** -1.) if exists(xpos_scale) else (1., 1.) + + q = apply_rotary_pos_emb(q, freqs, q_xpos_scale) + k = apply_rotary_pos_emb(k, freqs, k_xpos_scale) + + if self.rotary_embed_values: + v = apply_rotary_pos_emb(v, freqs, k_xpos_scale) + + input_mask = context_mask + + if not exists(input_mask) and not has_context: + input_mask = mask + + if self.num_mem_kv > 0: + mem_k, mem_v = map(lambda t: repeat(t, 'h n d -> b h n d', b = b), (self.mem_k, self.mem_v)) + + if self.qk_norm: + mem_k = l2norm(mem_k) + mem_k = mem_k * self.qk_norm_k_scale + + k = torch.cat((mem_k, k), dim = -2) + v = torch.cat((mem_v, v), dim = -2) + + if exists(input_mask): + input_mask = pad_at_dim(input_mask, (self.num_mem_kv, 0), dim = -1, value = True) + + i, j = map(lambda t: t.shape[-2], (q, k)) + + # determine masking + + mask_value = max_neg_value(q) + masks = [] + final_attn_mask = None + + if exists(input_mask): + input_mask = rearrange(input_mask, 'b j -> b 1 1 j') + masks.append(~input_mask) + + if exists(attn_mask): + assert 2 <= attn_mask.ndim <= 4, 'attention mask must have greater than 2 dimensions but less than or equal to 4' + if attn_mask.ndim == 2: + attn_mask = rearrange(attn_mask, 'i j -> 1 1 i j') + elif attn_mask.ndim == 3: + attn_mask = rearrange(attn_mask, 'h i j -> 1 h i j') + masks.append(~attn_mask) + + if exists(self.max_attend_past): + range_q = torch.arange(j - i, j, device = device) + range_k = torch.arange(j, device = device) + dist = rearrange(range_q, 'i -> 1 1 i 1') - rearrange(range_k, 'j -> 1 1 1 j') + max_attend_past_mask = dist > self.max_attend_past + masks.append(max_attend_past_mask) + + if len(masks) > 0: + final_attn_mask = ~or_reduce(masks) + + # prepare relative positional bias, if needed + + attn_bias = None + if exists(rel_pos): + attn_bias = rel_pos(i, j) + + # attention is all we need + + out, intermediates = self.attend( + q, k, v, + mask = final_attn_mask, + attn_bias = attn_bias, + prev_attn = prev_attn + ) + + # https://arxiv.org/abs/2208.06061 proposes to add a residual for better gradients + + if exists(r): + out = out * r + out + + # normformer scaling of heads + + if head_scale: + out = out * self.head_scale_params + + # per head gating, from https://arxiv.org/abs/2306.12929 + + if exists(self.to_v_head_gate): + head_gate = self.to_v_head_gate(x) + out = out * rearrange(head_gate, 'b n h -> b h n 1').sigmoid() + + # merge heads + + out = rearrange(out, 'b h n d -> b n (h d)') + + # alphafold2 styled gating of the values + + if exists(self.to_v_gate): + gates = self.to_v_gate(x) + out = out * gates.sigmoid() + + # combine the heads + + out = self.to_out(out) + + if exists(mask): + mask = rearrange(mask, 'b n -> b n 1') + out = out.masked_fill(~mask, 0.) + + if not return_intermediates: + return out + + intermediates.cached_kv = cached_kv + + return out, intermediates + +class AttentionLayers(nn.Module): + def __init__( + self, + dim, + depth, + heads = 8, + causal = False, + cross_attend = False, + only_cross = False, + use_scalenorm = False, + use_rmsnorm = False, + use_simple_rmsnorm = False, + alibi_pos_bias = False, + alibi_num_heads = None, + rel_pos_bias = False, + rel_pos_num_buckets = 32, + rel_pos_max_distance = 128, + dynamic_pos_bias = False, + dynamic_pos_bias_log_distance = False, + dynamic_pos_bias_mlp_depth = 2, + dynamic_pos_bias_norm = False, + rotary_pos_emb = False, + rotary_emb_dim = None, + rotary_xpos = False, + rotary_interpolation_factor = 1., + rotary_xpos_scale_base = 512, + rotary_base_rescale_factor = 1., + custom_layers = None, + sandwich_coef = None, + par_ratio = None, + weight_tie_layers = False, # Albert - https://arxiv.org/abs/1909.11942 + layers_execute_order = None, # generalizes weight tying, can do arbitrary layer execution orders + residual_attn = False, + cross_residual_attn = False, + macaron = False, + pre_norm = True, + pre_norm_has_final_norm = True, + gate_residual = False, + scale_residual = False, + scale_residual_constant = 1., + shift_tokens = 0, + sandwich_norm = False, + resi_dual = False, + resi_dual_scale = 1., + zero_init_branch_output = False, + layer_dropout = 0., + cross_attn_tokens_dropout = 0., + **kwargs + ): + super().__init__() + rotary_pos_emb = rotary_pos_emb or rotary_xpos + + ff_kwargs, kwargs = groupby_prefix_and_trim('ff_', kwargs) + attn_kwargs, kwargs = groupby_prefix_and_trim('attn_', kwargs) + + dim_head = attn_kwargs.get('dim_head', DEFAULT_DIM_HEAD) + + self.dim = dim + self.depth = depth + self.causal = causal + self.layers = nn.ModuleList([]) + + self.has_pos_emb = rel_pos_bias or rotary_pos_emb + + rotary_emb_dim = max(default(rotary_emb_dim, dim_head // 2), 32) + + assert not (rotary_xpos and not causal), 'rotary xpos is not compatible with bidirectional attention' + self.rotary_pos_emb = RotaryEmbedding(rotary_emb_dim, use_xpos = rotary_xpos, scale_base = rotary_xpos_scale_base, interpolation_factor = rotary_interpolation_factor, base_rescale_factor = rotary_base_rescale_factor) if rotary_pos_emb else None + + assert not (alibi_pos_bias and rel_pos_bias), 'you can only choose Alibi positional bias or T5 relative positional bias, not both' + assert rel_pos_num_buckets <= rel_pos_max_distance, 'number of relative position buckets must be less than the relative position max distance' + + # relative positional bias + + flash_attn = attn_kwargs.get('flash', False) + assert (int(rel_pos_bias) + int(dynamic_pos_bias) + int(alibi_pos_bias)) <= 1, 'you can only choose up to one of t5, alibi, or dynamic positional bias' + + self.rel_pos = None + if rel_pos_bias: + assert not flash_attn, 'flash attention not compatible with t5 relative positional bias' + self.rel_pos = RelativePositionBias(scale = dim_head ** 0.5, causal = causal, heads = heads, num_buckets = rel_pos_num_buckets, max_distance = rel_pos_max_distance) + elif dynamic_pos_bias: + assert not flash_attn, 'flash attention not compatible with dynamic positional bias' + self.rel_pos = DynamicPositionBias(dim = dim // 4, heads = heads, log_distance = dynamic_pos_bias_log_distance, depth = dynamic_pos_bias_mlp_depth, norm = dynamic_pos_bias_norm) + elif alibi_pos_bias: + alibi_num_heads = default(alibi_num_heads, heads) + assert alibi_num_heads <= heads, 'number of ALiBi heads must be less than the total number of heads' + self.rel_pos = AlibiPositionalBias(heads = alibi_num_heads, total_heads = heads) + + assert (int(sandwich_norm) + int(resi_dual)) <= 1, 'either sandwich norm or resiDual is selected, but not both' + assert not (not pre_norm and sandwich_norm), 'sandwich norm cannot be used when not using prenorm' + + if resi_dual: + pre_norm = False + + self.pre_norm = pre_norm + self.sandwich_norm = sandwich_norm + + self.resi_dual = resi_dual + assert 0 < resi_dual_scale <= 1., 'resiDual prenorm residual must be scaled by a factor greater than 0 and less than or equal to 1.' + self.resi_dual_scale = resi_dual_scale + + self.residual_attn = residual_attn + self.cross_residual_attn = cross_residual_attn + assert not (flash_attn and (residual_attn or cross_residual_attn)), 'flash attention is not compatible with residual attention' + + self.cross_attend = cross_attend + + assert (int(use_scalenorm) + int(use_rmsnorm) + int(use_simple_rmsnorm)) <= 1, 'you can only use either scalenorm, rmsnorm, or simple rmsnorm' + + if use_scalenorm: + norm_class = ScaleNorm + elif use_rmsnorm: + norm_class = RMSNorm + elif use_simple_rmsnorm: + norm_class = SimpleRMSNorm + else: + norm_class = nn.LayerNorm + + norm_fn = partial(norm_class, dim) + + if cross_attend and not only_cross: + default_block = ('a', 'c', 'f') + elif cross_attend and only_cross: + default_block = ('c', 'f') + else: + default_block = ('a', 'f') + + if macaron: + default_block = ('f',) + default_block + + # zero init + + if zero_init_branch_output: + attn_kwargs = {**attn_kwargs, 'zero_init_output': True} + ff_kwargs = {**ff_kwargs, 'zero_init_output': True} + + # setup weight tying, which is a special case of `layer_execute_order` + + assert not (weight_tie_layers and any([*map(exists, (custom_layers, par_ratio, sandwich_coef))])) + + if weight_tie_layers: + assert not exists(layers_execute_order) + layers_execute_order = tuple(range(len(default_block))) * depth + depth = 1 + + # calculate layer block order + + if exists(custom_layers): + layer_types = custom_layers + elif exists(par_ratio): + par_depth = depth * len(default_block) + assert 1 < par_ratio <= par_depth, 'par ratio out of range' + default_block = tuple(filter(not_equals('f'), default_block)) + par_attn = par_depth // par_ratio + depth_cut = par_depth * 2 // 3 # 2 / 3 attention layer cutoff suggested by PAR paper + par_width = (depth_cut + depth_cut // par_attn) // par_attn + assert len(default_block) <= par_width, 'default block is too large for par_ratio' + par_block = default_block + ('f',) * (par_width - len(default_block)) + par_head = par_block * par_attn + layer_types = par_head + ('f',) * (par_depth - len(par_head)) + elif exists(sandwich_coef): + assert sandwich_coef > 0 and sandwich_coef <= depth, 'sandwich coefficient should be less than the depth' + layer_types = ('a',) * sandwich_coef + default_block * (depth - sandwich_coef) + ('f',) * sandwich_coef + else: + layer_types = default_block * depth + + self.layer_types = layer_types + self.layers_execute_order = default(layers_execute_order, tuple(range(len(layer_types)))) + + assert all([i < len(self.layer_types) for i in self.layers_execute_order]) + + self.num_attn_layers = len(list(filter(equals('a'), layer_types))) + + # stochastic depth + + self.layer_dropouts = cast_tuple(layer_dropout, len(layer_types)) + + # structured dropout for cross attending + + self.cross_attn_tokens_dropout = cross_attn_tokens_dropout + + # calculate token shifting + + shift_tokens = cast_tuple(shift_tokens, len(layer_types)) + + # whether it has post norm + + self.final_norm = norm_fn() if pre_norm or resi_dual else nn.Identity() + + # iterate and construct layers + + for ind, (layer_type, layer_shift_tokens) in enumerate(zip(self.layer_types, shift_tokens)): + is_last_layer = ind == (len(self.layer_types) - 1) + + if layer_type == 'a': + layer = Attention(dim, heads = heads, causal = causal, **attn_kwargs) + elif layer_type == 'c': + layer = Attention(dim, heads = heads, **attn_kwargs) + elif layer_type == 'f': + layer = FeedForward(dim, **ff_kwargs) + layer = layer if not macaron else Scale(0.5, layer) + else: + raise Exception(f'invalid layer type {layer_type}') + + if layer_shift_tokens > 0: + shift_range_upper = layer_shift_tokens + 1 + shift_range_lower = -layer_shift_tokens if not causal else 0 + layer = ShiftTokens(range(shift_range_lower, shift_range_upper), layer) + + residual_fn = GRUGating if gate_residual else Residual + residual = residual_fn(dim, scale_residual = scale_residual, scale_residual_constant = scale_residual_constant) + + pre_branch_norm = norm_fn() if pre_norm else None + post_branch_norm = norm_fn() if sandwich_norm else None + post_main_norm = norm_fn() if not pre_norm else None + + norms = nn.ModuleList([ + pre_branch_norm, + post_branch_norm, + post_main_norm + ]) + + self.layers.append(nn.ModuleList([ + norms, + layer, + residual + ])) + + def forward( + self, + x, + context = None, + mask = None, + context_mask = None, + attn_mask = None, + self_attn_kv_mask = None, + mems = None, + seq_start_pos: Optional[Tensor] = None, + cache: Optional[LayerIntermediates] = None, + cache_age = 1, + return_hiddens = False + ): + assert not (self.cross_attend ^ exists(context)), 'context must be passed in if cross_attend is set to True' + + # initialize accums + + hiddens = [] + layer_hiddens = [] + intermediates = [] + + prev_attn = None + prev_cross_attn = None + + mems = mems.copy() if exists(mems) else [None] * self.num_attn_layers + + # handle left padded sequences + + if exists(seq_start_pos): + seq_arange = torch.arange(x.shape[-2], device = x.device, dtype = torch.long) + left_pad_mask = seq_arange >= seq_start_pos[..., None] + + if exists(self_attn_kv_mask): + self_attn_kv_mask = self_attn_kv_mask & left_pad_mask + else: + self_attn_kv_mask = left_pad_mask + + # rotary positions + + rotary_pos_emb = None + + if exists(self.rotary_pos_emb): + max_rotary_emb_length = max(list(map(lambda m: (m.shape[1] if exists(m) else 0) + x.shape[1], mems))) + rotary_pos_emb = self.rotary_pos_emb(max_rotary_emb_length) + + # assume cached key / values + + attn_cache = [] + + if exists(cache): + assert not self.training and self.causal and not any([*map(exists, (mask, attn_mask))]) + + if cache_age > 0: + x = x[:, -cache_age:] # for spec decoding, may be greater than 1 + + attn_cache = cache.attn_intermediates + + iter_attn_cache = iter(attn_cache) + + # outer residual - for resiDual paper + + outer_residual = x * self.resi_dual_scale + + # get layers to be executed + + layer_variables = ( + self.layer_types, + self.layers, + self.layer_dropouts + ) + + layer_variables = tuple(tuple(layer_variable[i] for i in self.layers_execute_order) for layer_variable in layer_variables) + + # go through the attention and feedforward layers + + for ind, (layer_type, (norm, block, residual_fn), layer_dropout) in enumerate(zip(*layer_variables)): + is_last = ind == (len(self.layers) - 1) + + if self.training and layer_dropout > 0. and random() < layer_dropout: + continue + + if layer_type == 'a': + if return_hiddens: + hiddens.append(x) + layer_mem = mems.pop(0) if mems else None + + if layer_type == 'c': + if self.training and self.cross_attn_tokens_dropout > 0.: + context, context_mask = dropout_seq(context, context_mask, self.cross_attn_tokens_dropout) + + inner_residual = x + + if return_hiddens: + layer_hiddens.append(x) + + pre_norm, post_branch_norm, post_main_norm = norm + + if exists(pre_norm): + x = pre_norm(x) + + if layer_type == 'a': + out, inter = block(x, mask = mask, context_mask = self_attn_kv_mask, attn_mask = attn_mask, rel_pos = self.rel_pos, rotary_pos_emb = rotary_pos_emb, prev_attn = prev_attn, cache = next(iter_attn_cache, None), mem = layer_mem, return_intermediates = True) + elif layer_type == 'c': + out, inter = block(x, context = context, mask = mask, context_mask = context_mask, prev_attn = prev_cross_attn, cache = next(iter_attn_cache, None), return_intermediates = True) + elif layer_type == 'f': + out = block(x) + + if self.resi_dual: + outer_residual = outer_residual + out * self.resi_dual_scale + + if exists(post_branch_norm): + out = post_branch_norm(out) + + x = residual_fn(out, inner_residual) + + if layer_type in ('a', 'c') and return_hiddens: + intermediates.append(inter) + + if layer_type == 'a' and self.residual_attn: + prev_attn = inter.pre_softmax_attn + elif layer_type == 'c' and self.cross_residual_attn: + prev_cross_attn = inter.pre_softmax_attn + + if exists(post_main_norm): + x = post_main_norm(x) + + if return_hiddens: + layer_hiddens.append(x) + + if self.resi_dual: + x = x + self.final_norm(outer_residual) + else: + x = self.final_norm(x) + + if not return_hiddens: + return x + + intermediates = LayerIntermediates( + hiddens = hiddens, + attn_intermediates = intermediates, + layer_hiddens = layer_hiddens + ) + + return x, intermediates + +class Encoder(AttentionLayers): + def __init__(self, **kwargs): + assert 'causal' not in kwargs, 'cannot set causality on encoder' + super().__init__(causal = False, **kwargs) + +class Decoder(AttentionLayers): + def __init__(self, **kwargs): + assert 'causal' not in kwargs, 'cannot set causality on decoder' + super().__init__(causal = True, **kwargs) + +class CrossAttender(AttentionLayers): + def __init__(self, **kwargs): + super().__init__(cross_attend = True, only_cross = True, **kwargs) + +class ViTransformerWrapper(nn.Module): + def __init__( + self, + *, + image_size, + patch_size, + attn_layers, + channels = 3, + num_classes = None, + post_emb_norm = False, + num_register_tokens = 0, + emb_dropout = 0. + ): + super().__init__() + assert isinstance(attn_layers, Encoder), 'attention layers must be an Encoder' + assert divisible_by(image_size, patch_size), 'image dimensions must be divisible by the patch size' + dim = attn_layers.dim + num_patches = (image_size // patch_size) ** 2 + patch_dim = channels * patch_size ** 2 + + self.patch_size = patch_size + + self.pos_embedding = nn.Parameter(torch.randn(1, num_patches, dim)) + + has_register_tokens = num_register_tokens > 0 + self.has_register_tokens = has_register_tokens + + if has_register_tokens: + self.register_tokens = nn.Parameter(torch.randn(num_register_tokens, dim)) + + self.patch_to_embedding = nn.Sequential( + nn.LayerNorm(patch_dim), + nn.Linear(patch_dim, dim), + nn.LayerNorm(dim) + ) + + self.post_emb_norm = nn.LayerNorm(dim) if post_emb_norm else nn.Identity() + self.dropout = nn.Dropout(emb_dropout) + + self.attn_layers = attn_layers + + self.mlp_head = nn.Linear(dim, num_classes) if exists(num_classes) else nn.Identity() + + def forward( + self, + img, + return_embeddings = False + ): + b, p = img.shape[0], self.patch_size + + x = rearrange(img, 'b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1 = p, p2 = p) + x = self.patch_to_embedding(x) + n = x.shape[1] + + x = x + self.pos_embedding[:, :n] + + x = self.post_emb_norm(x) + x = self.dropout(x) + + if self.has_register_tokens: + r = repeat(self.register_tokens, 'n d -> b n d', b = b) + x, ps = pack((x, r), 'b * d') + + x = self.attn_layers(x) + + if self.has_register_tokens: + x, _ = unpack(x, ps, 'b * d') + + if not exists(self.mlp_head) or return_embeddings: + return x + + x = x.mean(dim = -2) + return self.mlp_head(x) + +class TransformerWrapper(nn.Module): + def __init__( + self, + *, + num_tokens, + max_seq_len, + attn_layers, + emb_dim = None, + max_mem_len = 0, + shift_mem_down = 0, + emb_dropout = 0., + post_emb_norm = False, + num_memory_tokens = None, + memory_tokens_interspersed_every = None, + tie_embedding = False, + logits_dim = None, + use_abs_pos_emb = True, + scaled_sinu_pos_emb = False, + l2norm_embed = False, + emb_frac_gradient = 1., # GLM-130B and Cogview successfully used this, set at 0.1 + attn_z_loss_weight = 1e-4, + ): + super().__init__() + assert isinstance(attn_layers, AttentionLayers), 'attention layers must be one of Encoder or Decoder' + + dim = attn_layers.dim + emb_dim = default(emb_dim, dim) + self.emb_dim = emb_dim + self.num_tokens = num_tokens + + self.max_seq_len = max_seq_len + self.max_mem_len = max_mem_len + self.shift_mem_down = shift_mem_down + + self.l2norm_embed = l2norm_embed + self.token_emb = TokenEmbedding(emb_dim, num_tokens, l2norm_embed = l2norm_embed) + + if not (use_abs_pos_emb and not attn_layers.has_pos_emb): + self.pos_emb = always(0) + elif scaled_sinu_pos_emb: + self.pos_emb = ScaledSinusoidalEmbedding(emb_dim) + else: + self.pos_emb = AbsolutePositionalEmbedding(emb_dim, max_seq_len, l2norm_embed = l2norm_embed) + + self.emb_frac_gradient = emb_frac_gradient # fraction of the gradient that should go to the embedding, https://arxiv.org/abs/2105.13290 + + self.post_emb_norm = nn.LayerNorm(emb_dim) if post_emb_norm else nn.Identity() + self.emb_dropout = nn.Dropout(emb_dropout) + + self.project_emb = nn.Linear(emb_dim, dim) if emb_dim != dim else nn.Identity() + self.attn_layers = attn_layers + + self.init_() + + logits_dim = default(logits_dim, num_tokens) + self.to_logits = nn.Linear(dim, logits_dim) if not tie_embedding else lambda t: t @ self.token_emb.emb.weight.t() + + # memory tokens (like [cls]) from Memory Transformers paper + + num_memory_tokens = default(num_memory_tokens, 0) + self.num_memory_tokens = num_memory_tokens + if num_memory_tokens > 0: + self.memory_tokens = nn.Parameter(torch.randn(num_memory_tokens, dim)) + + self.memory_tokens_interspersed_every = memory_tokens_interspersed_every + + # whether can do cached kv decoding + + self.can_cache_kv = self.num_memory_tokens == 0 + + def init_(self): + if self.l2norm_embed: + nn.init.normal_(self.token_emb.emb.weight, std = 1e-5) + if not isinstance(self.pos_emb, always): + nn.init.normal_(self.pos_emb.emb.weight, std = 1e-5) + return + + nn.init.kaiming_normal_(self.token_emb.emb.weight) + + def forward( + self, + x, + return_embeddings = False, + return_logits_and_embeddings = False, + return_intermediates = False, + mask = None, + return_mems = False, + return_attn = False, + mems = None, + pos = None, + prepend_embeds = None, + sum_embeds = None, + return_attn_z_loss = False, + attn_z_loss_weight = 1e-4, + seq_start_pos = None, + cache: Optional[LayerIntermediates] = None, + **kwargs + ): + b, n, device, num_mems, has_memory_tokens, emb_frac_gradient = *x.shape, x.device, self.num_memory_tokens, self.num_memory_tokens > 0, self.emb_frac_gradient + return_hiddens = return_mems | return_attn | return_intermediates | return_attn_z_loss + + # absolute positional embedding + + external_pos_emb = exists(pos) and pos.dtype != torch.long + pos_emb = self.pos_emb(x, pos = pos, seq_start_pos = seq_start_pos) if not external_pos_emb else pos + x = self.token_emb(x) + pos_emb + + # for summing embeddings passed externally - needs this for self-conditioning in non-autoregressive training + + if exists(sum_embeds): + x = x + sum_embeds + + # post embedding norm, purportedly leads to greater stabilization + + x = self.post_emb_norm(x) + + # whether to append embeds, as in PaLI, for image embeddings + + if exists(prepend_embeds): + prepend_seq, prepend_dim = prepend_embeds.shape[1:] + assert prepend_dim == x.shape[-1], 'prepended embeddings need to have same dimensions as text model dimensions' + + x = torch.cat((prepend_embeds, x), dim = -2) + + # whether to reduce the gradient going to the embedding, from cogview paper, corroborated by GLM-130B model + + if emb_frac_gradient < 1: + assert emb_frac_gradient > 0 + x = x * emb_frac_gradient + x.detach() * (1 - emb_frac_gradient) + + # embedding dropout + + x = self.emb_dropout(x) + + x = self.project_emb(x) + + if has_memory_tokens: + mem_every = self.memory_tokens_interspersed_every + + if exists(mem_every): + assert mem_every > 0 + assert isinstance(self.attn_layers, Decoder), 'only for decoder' + next_seq_len = math.ceil(n / mem_every) * mem_every + + x = pad_at_dim(x, (0, next_seq_len - n), dim = -2, value = 0.) + x = rearrange(x, 'b (n m) d -> (b n) m d', m = mem_every) + + mem = repeat(self.memory_tokens, 'n d -> b n d', b = x.shape[0]) + x, mem_packed_shape = pack((mem, x), 'b * d') + + # auto-handle masking after appending memory tokens + if not exists(mem_every) and exists(mask): + mask = pad_at_dim(mask, (num_mems, 0), dim = -1, value = True) + + if exists(mem_every): + x = rearrange(x, '(b n) m d -> b (n m) d', b = b) + + if self.shift_mem_down and exists(mems): + mems_l, mems_r = mems[:self.shift_mem_down], mems[self.shift_mem_down:] + mems = [*mems_r, *mems_l] + + x, intermediates = self.attn_layers(x, mask = mask, mems = mems, cache = cache, return_hiddens = True, seq_start_pos = seq_start_pos, **kwargs) + + if has_memory_tokens: + if exists(mem_every): + x = rearrange(x, 'b (n m) d -> (b n) m d', m = (mem_every + num_mems)) + + mem, x = unpack(x, mem_packed_shape, 'b * d') + + if exists(mem_every): + x = rearrange(x, '(b n) m d -> b (n m) d', b = b) + + x = x[:, :n] + + if return_logits_and_embeddings: + out = (self.to_logits(x), x) + elif return_embeddings: + out = x + else: + out = self.to_logits(x) + + if return_attn_z_loss: + pre_softmax_attns = list(map(lambda t: t.pre_softmax_attn, intermediates.attn_intermediates)) + intermediates.attn_z_loss = calc_z_loss(pre_softmax_attns, weight = attn_z_loss_weight) + return_intermediates = True + + if return_mems: + hiddens = intermediates.hiddens + new_mems = list(map(lambda pair: torch.cat(pair, dim = -2), zip(mems, hiddens))) if exists(mems) else hiddens + new_mems = list(map(lambda t: t[..., -self.max_mem_len:, :].detach(), new_mems)) + + if not return_intermediates: + return out, new_mems + + intermediates.mems = new_mems + + if return_intermediates: + return out, intermediates + + if return_attn: + attn_maps = list(map(lambda t: t.post_softmax_attn, intermediates.attn_intermediates)) + return out, attn_maps + + return out + +class ContinuousTransformerWrapper(nn.Module): + def __init__( + self, + *, + max_seq_len, + attn_layers, + dim_in = None, + dim_out = None, + emb_dim = None, + max_mem_len = 0, + post_emb_norm = False, + emb_dropout = 0., + use_abs_pos_emb = True, + scaled_sinu_pos_emb = False + ): + super().__init__() + assert isinstance(attn_layers, AttentionLayers), 'attention layers must be one of Encoder or Decoder' + + dim = attn_layers.dim + + self.max_seq_len = max_seq_len + + self.max_mem_len = max_mem_len + + if not (use_abs_pos_emb and not attn_layers.has_pos_emb): + self.pos_emb = always(0) + elif scaled_sinu_pos_emb: + self.pos_emb = ScaledSinusoidalEmbedding(dim) + else: + self.pos_emb = AbsolutePositionalEmbedding(dim, max_seq_len) + + self.post_emb_norm = nn.LayerNorm(dim) if post_emb_norm else nn.Identity() + self.emb_dropout = nn.Dropout(emb_dropout) + + self.project_in = nn.Linear(dim_in, dim) if exists(dim_in) else nn.Identity() + + self.attn_layers = attn_layers + + self.project_out = nn.Linear(dim, dim_out) if exists(dim_out) else nn.Identity() + + def forward( + self, + x, + return_embeddings = False, + return_intermediates = False, + return_mems = False, + mask = None, + return_attn = False, + mems = None, + pos = None, + prepend_embeds = None, + **kwargs + ): + x = self.project_in(x) + x = x + self.pos_emb(x, pos = pos) + + x = self.post_emb_norm(x) + + # whether to append embeds, as in PaLI, for image embeddings + + if exists(prepend_embeds): + _, prepend_dim = prepend_embeds.shape[1:] + assert prepend_dim == x.shape[-1], 'prepended embeddings need to have same dimensions as model dimensions' + + x = torch.cat((prepend_embeds, x), dim = -2) + + x = self.emb_dropout(x) + + x, intermediates = self.attn_layers(x, mask = mask, mems = mems, return_hiddens = True, **kwargs) + + out = self.project_out(x) if not return_embeddings else x + + if return_intermediates: + return out, intermediates + + if return_mems: + hiddens = intermediates.hiddens + new_mems = list(map(lambda t: t[..., -self.max_mem_len:, :].detach(), hiddens)) + return out, new_mems + + if return_attn: + attn_maps = list(map(lambda t: t.post_softmax_attn, intermediates.attn_intermediates)) + return out, attn_maps + + return out + +class XTransformer(nn.Module): + def __init__( + self, + *, + dim, + tie_token_emb = False, + ignore_index = -100, + pad_value = 0, + cross_attn_tokens_dropout = 0., + **kwargs + ): + super().__init__() + enc_kwargs, kwargs = groupby_prefix_and_trim('enc_', kwargs) + dec_kwargs, kwargs = groupby_prefix_and_trim('dec_', kwargs) + + assert 'dim' not in enc_kwargs and 'dim' not in dec_kwargs, 'dimension of either encoder or decoder must be set with `dim` keyword' + enc_transformer_kwargs = pick_and_pop(['num_tokens', 'max_seq_len'], enc_kwargs) + enc_transformer_kwargs['emb_dropout'] = enc_kwargs.pop('emb_dropout', 0) + enc_transformer_kwargs['num_memory_tokens'] = enc_kwargs.pop('num_memory_tokens', None) + enc_transformer_kwargs['scaled_sinu_pos_emb'] = enc_kwargs.pop('scaled_sinu_pos_emb', False) + enc_transformer_kwargs['use_abs_pos_emb'] = enc_kwargs.pop('use_abs_pos_emb', True) + + dec_transformer_kwargs = pick_and_pop(['num_tokens', 'max_seq_len'], dec_kwargs) + dec_transformer_kwargs['emb_dropout'] = dec_kwargs.pop('emb_dropout', 0) + dec_transformer_kwargs['scaled_sinu_pos_emb'] = dec_kwargs.pop('scaled_sinu_pos_emb', False) + dec_transformer_kwargs['use_abs_pos_emb'] = dec_kwargs.pop('use_abs_pos_emb', True) + + self.cross_attn_tokens_dropout = cross_attn_tokens_dropout # how many tokens from the encoder to dropout when cross attending from decoder - seen in a couple papers, including Perceiver AR - this will also be very effective regularization when cross attending to very long memories + + self.encoder = TransformerWrapper( + **enc_transformer_kwargs, + attn_layers = Encoder(dim = dim, **enc_kwargs) + ) + + self.decoder = TransformerWrapper( + **dec_transformer_kwargs, + attn_layers = Decoder(dim = dim, cross_attend = True, **dec_kwargs) + ) + + if tie_token_emb: + self.decoder.token_emb = self.encoder.token_emb + + self.decoder = AutoregressiveWrapper(self.decoder, ignore_index=ignore_index, pad_value=pad_value) + + @torch.no_grad() + def generate(self, seq_in, seq_out_start, seq_len, mask = None, attn_mask = None, **kwargs): + encodings = self.encoder(seq_in, mask = mask, attn_mask = attn_mask, return_embeddings = True) + return self.decoder.generate(seq_out_start, seq_len, context = encodings, context_mask = mask, **kwargs) + + def forward(self, src, tgt, mask = None, attn_mask = None, src_prepend_embeds = None): + + if exists(src_prepend_embeds) and exists(mask): + mask = pad_at_dim(mask, (src_prepend_embeds.shape[-2], 0), dim = -1, value = True) + + enc = self.encoder(src, mask = mask, attn_mask = attn_mask, prepend_embeds = src_prepend_embeds, return_embeddings = True) + + if self.training and self.cross_attn_tokens_dropout > 0: + enc, mask = dropout_seq(enc, mask, self.cross_attn_tokens_dropout) + + out = self.decoder(tgt, context = enc, context_mask = mask) + return out \ No newline at end of file