def generate_output(args): c = patterns.fetch_classifier() segmentation = False all_keys = False if args.mid and args.start is not None and args.end is not None: musicpiece = Piece(args.mid) musicpiece = musicpiece.segment_by_bars(args.start, args.end) mm = piece_to_markov_model(musicpiece, c, segmentation) song, gen, a = generate_song(mm, musicpiece.meta, musicpiece.bar, segmentation) else: pieces = glob.glob('./mid/Bach/*') #pieces = ["mid/Bach/bwv804.mid", "mid/Bach/bwv802.mid"] mm = Markov() # generate a model _mm for each piece then add them together for p in pieces: musicpiece = Piece(p) _mm = piece_to_markov_model(musicpiece, c, segmentation, all_keys) mm = mm.add_model(_mm) song, gen, a = generate_song(mm, musicpiece.meta, musicpiece.bar, segmentation) midi.write('output.mid', song)
def addMidiSong(songmidi): def savetreamtomidi(filename, stream): filepath = os.path.join(segments_folder, filename) mf = midi.translate.streamToMidiFile(stream) mf.open(filepath, 'wb') mf.write() mf.close() transitions = _loadtransitiontable() musicpiece = data.piece(songmidi) segmented = experiments.analysis(musicpiece, patterns.fetch_classifier()) chosenscore, chosen, labelled_sections = segmented.chosenscore, segmented.chosen, segmented.labelled_sections musicstream = converter.parse(songmidi) filename = os.path.splitext(os.path.basename(songmidi))[0] for i, segment_score in enumerate(chosenscore): # cant process last segment if i >= len(chosenscore) - 1: continue start, duration = segment_score[0] first = musicstream.measures(start, start + duration) first_seg_str = "{}_{}_{}.mid".format(filename, start, start+duration) savetreamtomidi(first_seg_str, first) start, duration = chosenscore[i + 1][0] second = musicstream.measures(start, start + duration) second_seg_str = "{}_{}_{}.mid".format(filename, start, start+duration) savetreamtomidi(second_seg_str, second) #update transitions if (first_seg_str in transitions): transitions[first_seg_str] += [second_seg_str] else: transitions[first_seg_str] = [second_seg_str] _savetransitiontable(transitions)
def __init__(self, type, pieces): ''' type is a str describing the category that the training pieces are in pieces is a set of the pieces in that category ''' self.type = type self.pieces = set(pieces) mm = cmm.Markov() reverse_mm = cmm.Markov() c = patterns.fetch_classifier() segmentation = False all_keys = False for p in self.pieces: print(p) musicpiece = data.Piece(p) # segmentation off by default, all_keys off by default # this should automatically transpose everything to C major _mm = cmm.piece_to_markov_model(musicpiece, c, segmentation, all_keys) mm = mm.add_model(_mm) # now reverse the state chains to get the reverse mm b = _mm.state_chains rev_chains = [chain[::-1] for chain in _mm.state_chains] _mm.state_chains = rev_chains reverse_mm = reverse_mm.add_model(_mm) self.mm = mm self.rev_mm = reverse_mm
def add_piece(self, piece): ''' Add a new piece to the model. :param piece: location of midi :return: None ''' if piece in self.pieces: print(piece + " already in model.") return c = patterns.fetch_classifier() segmentation = False all_keys = True musicpiece = data.Piece(piece) _mm = cmm.piece_to_markov_model(musicpiece, c, segmentation, all_keys) self.mm = self.mm.add_model(_mm) b = _mm.state_chains rev_chains = [chain[::-1] for chain in _mm.state_chains] _mm.state_chains = rev_chains self.rev_mm = self.rev_mm.add_model(_mm)
def generate_output(): classifier = patterns.fetch_classifier() segmentation = False all_keys = False if len(sys.argv) == 4: # positional arguments: <midi-file> <start-bar> <end-bar> musicpiece = data.piece(sys.argv[1]) musicpiece = musicpiece.segment_by_bars(int(sys.argv[2]), int(sys.argv[3])) mm = piece_to_markov_model(musicpiece, classifier, segmentation) else: pieces = ["mid/hilarity.mid", "mid/froglegs.mid", "mid/easywinners.mid"] mm = Markov() # initialize an empty model # generate a model _mm for each piece then add them together for p in pieces: musicpiece = data.piece(p) _mm = piece_to_markov_model(musicpiece, classifier, segmentation, all_keys) mm = mm.add_model(_mm) song, gen, notes = generate_song(mm, musicpiece.meta, musicpiece.bar, segmentation) midi.write('output.mid', song)
def mixture(chord_transitions, sc2): sc2 = cmm.NoteState.piece_to_state_chain(piece2, use_chords=True) x = np.zeros((len(sc2),), dtype=np.float) for i in range(len(sc2)-1): x[i] = 0 if (sc2[i].chord, sc2[i+1].chord) in chord_transitions else 1 fig = figure() x.shape = 1, len(x) axprops = dict(xticks=[], yticks=[]) barprops = dict(aspect='auto', cmap=cm.binary, interpolation='bicubic') ax = fig.add_axes([0.1, 0.1, 0.8, 0.1], **axprops) ax.imshow(x, **barprops) show() if __name__ == '__main__': c = patterns.fetch_classifier() chord_transitions = set() for i in range(1, len(sys.argv)-1): piece1 = data.piece(sys.argv[i]) sc_ = cmm.NoteState.piece_to_state_chain(piece1, use_chords=True) schords = [s.chord for s in sc_ ] schords2 = [] for i in range(1, 6): schords2 += [ chords.translate(chords.untranslate(s.chord.split('m')[0])+i) + ('m' if 'm' in s.chord else '') for s in sc_ ] for i in range(1, 7): schords2 += [ chords.translate(chords.untranslate(s.chord.split('m')[0])-i) + ('m' if 'm' in s.chord else '') for s in sc_ ] schords += schords2 # assume chain length is 1 chord_transitions = chord_transitions.union({(schords[i], schords[i+1]) for i in range(len(schords)-1)})
plot = np.zeros((piece1.num_bars, piece2.num_bars), dtype=np.float) segment1 = [ piece1.segment_by_bars(i, i+1) for i in range(piece1.num_bars) ] segment2 = [ piece2.segment_by_bars(j, j+1) for j in range(piece2.num_bars) ] print "done 1/2" for i in range(piece1.num_bars): for j in range(piece2.num_bars): features = [segment1[i].compare_with(segment2[j])] score = c.predict_proba(features)[0][1] plot[i][j] = 0 if score >= 0.5 else 1 - score #plot[i][j] = 1 - score print "done 2/2" return np.fliplr(plot) if __name__ == '__main__': c = patterns.fetch_classifier() if len(sys.argv) == 3: piece1 = data.piece(sys.argv[1]) piece2 = data.piece(sys.argv[2]) if len(sys.argv) == 2: piece1 = data.piece(sys.argv[1]) piece2 = data.piece(sys.argv[1]) plot = recurrence(c, piece1, piece2) io.imshow(plot) io.show()