def test_freeze_and_thaw(): """Test that messages are hashable.""" assert not is_frozen(thaw_message(freeze_message(Message('note_on'))))
def _parse(self): default_tempo = 500000 default_ticks_per_beat = 240 current_tempo = 500000 remembered_notes = {} # dictionary with single notes waiting to be offed {key : (time of waiting, message)} waiting = {} # seed to generate next midi-messages (of type (frozenset(notes), duration) ) prev = (frozenset({60}), 20) piece_of_music_prev = (frozenset({60}), 20) # set of notes to be treat as one (noted on but not yet off) piece_of_music = set() # set of (note_on message, note_off message) notes waited in from piece_of_music current_set = set() time = 0 # counts current duration of note with respect to current tempo and ticks per beat # if generator not care about note duration returns 0 def duration(t): if self.with_duration: return int( (t * current_tempo / self.midi.ticks_per_beat) / 25000) return 0 def update_waiting(t): for i in waiting: waiting[i] = (waiting[i][0] + t, waiting[i][1]) # update time in given message with respect to current tempo and default setting of generating file def update_tempo(mes): mes.time = mes.time * current_tempo / self.midi.ticks_per_beat mes.time = mes.time * default_ticks_per_beat / default_tempo mes.time = int(mes.time) return mes for track in self.midi.tracks: for message in track: message = update_tempo(message) if message.type == "set tempo": time += message.time update_waiting(message.time) current_tempo = message.tempo if message.type == "note_on": time += message.time update_waiting(message.time) if message.time == 0 or message.note not in piece_of_music \ or len(piece_of_music)+len(current_set) < self.max_piece_len: if not piece_of_music: time = 0 piece_of_music_prev = prev piece_of_music.add(message.note) remembered_notes[message.note] = freeze_message( message) else: waiting[message.note] = (0, freeze_message(message)) if message.type == "note_off": time += message.time update_waiting(message.time) if message.note in piece_of_music: # one note from chord ends current_set.add((remembered_notes[message.note], freeze_message(message))) piece_of_music.remove(message.note) if not piece_of_music: # if so it was last note from note notes_set = self._add_to_messages_dictionary( current_set, duration(time)) next_note = (notes_set, duration(time)) self._add_to_matrix(piece_of_music_prev, next_note) prev = next_note current_set.clear() time = 0 elif message.note in waiting: note_on = waiting[message.note] f = frozenset({message.note}) self.messages_dictionary[(f, duration( note_on[0]))] = ([note_on[1]], [freeze_message(message)]) self._add_to_matrix(prev, (f, duration(note_on[0]))) prev = (f, duration(note_on[0])) waiting.pop(message.note)
def create_markov_model(self): note_map_on = NoteMap() note_map_off = NoteMap() trigger_sequence = TriggerMap() note_map_complete = NoteMap() if self.input_file == '': #print('error, no file supplied for channel ' + str(self.channel)) return note_maps mid = MidiFile(self.input_file) frozen_messages = [] for i, track in enumerate(mid.tracks): ##print('Track {}: {}'.format(i, track.name)) frozen_messages.append([]) for msg in track: frozen_messages[i].append(freeze_message(msg)) ##print(frozen_messages) for track in frozen_messages: ##print(len(frozen_messages)) for i, msg in enumerate(track): if msg.type == 'note_on' or msg.type == 'note_off': ##print(msg) gram = track[i:i+self.order] nth = None nth_time = 0 nth_onoff = '' #NONE if i+self.order < len(track): nth = track[i+self.order].bytes() nth_time = track[i+self.order].time nth_onoff = track[i+self.order].type else: #FIXME nth = track[i].bytes() nth_time = track[i].time nth_onoff = track[i].type bytes = [i.bytes() for i in gram if len(gram) >= self.order] time = [i.time for i in gram if len(gram) >= self.order] triggers = [i.type for i in gram if len(gram) >= self.order] ##Byte_Notes byte_notes = [b[1] for b in bytes] byte_notes = tuple(byte_notes) trigger_key = tuple(triggers) if nth_onoff == 'note_on': note_map_on.add_note(outer_key=byte_notes, nth_note=nth[1], nth_onoff=nth_onoff, nth_time=nth_time) note_map_complete.add_note(outer_key=byte_notes, nth_note=nth[1], nth_onoff=nth_onoff, nth_time=nth_time) elif nth_onoff == 'note_off': note_map_off.add_note(outer_key=byte_notes, nth_note=nth[1], nth_onoff=nth_onoff, nth_time=nth_time) note_map_complete.add_note(outer_key=byte_notes, nth_note=nth[1], nth_onoff=nth_onoff, nth_time=nth_time) trigger_sequence.add_note(outer_key=trigger_key,nth_onoff=nth_onoff) return (note_map_on, note_map_off, trigger_sequence, note_map_complete)
from mido.frozen import FrozenMessage, freeze_message, thaw_message msg = FrozenMessage('note_on') msgDict = {msg: 'interesting'} print(msgDict) #msg.note = 2 thawedMsg = thaw_message(msg) thawedMsg.note = 2 msg2 = freeze_message(thawedMsg) msgDict[msg2] = 'fun' print(msgDict)
frozen_messages = [] #ngram_messages = {} note_map = {} vel_map = {} for i, track in enumerate(mid.tracks): #print('Track {}: {}'.format(i, track.name)) frozen_messages.append([]) for msg in track: if msg.type == msg.type: #'note_on': # if not msg.is_meta: print(msg) #if msg.type == 'set_tempo': # msg = msg.copy(tempo=150000) frozen = freeze_message(msg) frozen_messages[i].append(frozen) ################ ###ANALYZE FOR VELOCITY for track in frozen_messages: for i, msg in enumerate(track): if msg.type == 'note_on' or msg.type == 'note_off': gram = track[i:i + ORDER] nth = None nth_time = 0 nth_onoff = '' #NONE if i + ORDER < len(track): nth = track[i + ORDER].bytes()