def rhythmLine(baseDuration = QuarterNote(), minLength = 8.0, maxProbability = 0.5): newStream = stream.Stream() while newStream.duration.quarterLength < minLength: currentProbability = (newStream.duration.quarterLength / minLength) * maxProbability newNote = Note() newNote.duration = baseDuration.clone() x = random.random() while x < currentProbability: print(x, currentProbability) newNote.duration = alterRhythm(newNote.duration) x = random.random() newStream.append(newNote) #newStream.getNoteTimeInfo() return newStream
def generate(self, seq_len, a_par=0): pattern = self.model_inp[self.start] prediction_output = [] for note_index in range(seq_len): prediction_input = pattern.reshape(1, seq_len, 2, len(self.sorted_notes)) prediction_input = prediction_input / float(len(self.sorted_notes)) predictions = self.model.predict(prediction_input, verbose=0)[0] for prediction in predictions: index = np.argmax(prediction[0]) duration_i = np.argmax(prediction[1]) for name, value in self.sorted_notes.items(): if value == index: result = name break else: result = None for name, value in self.sorted_durations.items(): if value == duration_i: duration = name break else: duration = None prediction_output.append((result, Duration(duration))) result = np.zeros_like(prediction) result[0][index] = 1 result[1][duration_i] = 1 pattern = np.concatenate([pattern, [result]]) pattern = pattern[len(pattern) - seq_len:len(pattern)] offset = 0 output_notes = [] for pattern, duration in prediction_output: if pattern.isdigit() or ('.' in pattern): notes_in_chord = pattern.split('.') notes = [] for current_note in notes_in_chord: new_note = Note(int(current_note)) new_note.duration = duration new_note.storedInstrument = instrument.PanFlute() notes.append(new_note) new_chord = Chord(notes) new_chord.offset = offset output_notes.append(new_chord) else: new_note = Note(pattern) new_note.offset = offset new_note.storedInstrument = instrument.Flute() output_notes.append(new_note) offset += 0.6 midi_stream = stream.Stream(output_notes) midi_stream.write('midi', fp=f'my_music/{self.model.name}_{self.start}.mid')
def write_notation_cell(music, path, event_index): score = Score() metadata = Metadata() metadata.title = '' metadata.composer = '' score.insert(0, metadata) layout = ScoreLayout() layout.scalingMillimeters = 1.25 layout.scalingTenths = 40 score.insert(0, layout) for musician in music: instrument_name = musician['instrument'] instrument = get_instrument(instrument_name) instrument.partName = instrument.instrumentName if instrument.instrumentName is 'Violoncello': instrument.partName = 'Cello' instrument.partAbbreviation = instrument.instrumentAbbreviation parts = [] part = Part() parts.append(part) part.insert(0, instrument) score.insert(0, part) # score.insert(0, StaffGroup(parts)) for event in musician['music']: pitches = event['pitches'] dur = event['duration'] # if not pitches or pitches == 'stop': # note = Rest() if len(pitches) == 1: pitch = Pitch(pitches[0] + 60) note = Note(pitch) else: note = Chord(notes=[Pitch(p + 60) for p in pitches]) duration = Duration() duration.fill([dur]) note.duration = duration part.append(note) file_path = os.path.join(path, str(event_index).zfill(2)) musicxml_file_path = file_path + '.xml' png_output_file_path = file_path + '.png' score.write('musicxml', musicxml_file_path) write_png_with_musescore(musicxml_file_path, png_output_file_path, dpi=600)
def generate(tune, clef=m21.clef.TrebleClef()): # Load in the score score = m21utils.loadScoreForTune(tune) # Some basic preprocessing / cleanup score.parts[0].removeByClass('Instrument') score.parts[0].partName = None m21.harmony.realizeChordSymbolDurations(score) ## Argh!!! # Go measure by measure and build the study measures = score.parts[0].getElementsByClass("Measure") for measure in measures: # print measure # Remove any existing notes in the measure (we're about to add our own) measure.removeByClass('Note') # Grab the list of chord symbols in this measure chordSymbols = measure.getElementsByClass( 'ChordSymbol').matchingElements() # Remove the chord symbols so we can add them back interleaved with the notes # we're about to add measure.removeByClass('ChordSymbol') # Add notes for each chord symbol for symbol in chordSymbols: measure.append(symbol) # print symbol.duration.quarterLength n3 = Note(symbol.third) n3.duration = Duration(symbol.duration.quarterLength * 0.50) n3.octave = 5 n3.lyric = '3' measure.append(n3) if (symbol.containsSeventh()): n7 = m21.note.Note(symbol.seventh) n7.duration = Duration(symbol.duration.quarterLength * 0.50) n7.lyric = '7' n7.octave = 5 measure.append(n7) else: n5 = m21.note.Note(symbol.root()) n5.duration = Duration(symbol.duration.quarterLength * 0.50) n5.lyric = 'R' n5.octave = 5 measure.append(n5) # TODO: don't think this is needed # if measure.number % 4 == 0: # measure.append(m21.layout.SystemLayout(isNew=True)) # for x in range(len(c)): # MyMeasure = Measure() ## Make a measure and put everything inside it # MyMeasure.number = m.number ## give the measure a number # MyMeasure.rightBarline = m.rightBarline # MyMeasure.leftBarline = m.leftBarline # # print("_____________________") # # print("In measure "+ str(m.number) + "(" + str(m.id) + ")" +" of " + str(len(s)) ) #debug monitoring # c = m.getElementsByClass(ChordSymbol) # for x in range(len(c)): # # print(c[x].duration) # # print(c[x].beat) # # print (c[x].figure) # # print("--------------------") # TheFigure = c[x].figure # MyChord = Chord(c[x].pitches) # MySymbol = ChordSymbol() # ######## Fix XML chord symbols ############ # if (TheFigure.find(" alter b9") != -1): # MySymbol = m21utils.writeFlatNine(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find(" add b9") != -1): # MySymbol = m21utils.writeFlatNine(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find(" add #9") != -1): # MySymbol = m21utils.writeSharpNine(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find(" add #7") != -1): # MySymbol = m21utils.writeSharpSeven(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find(" add #11") != -1): # MySymbol = m21utils.writeSharpEleven(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find(" add b13") != -1): # MySymbol = m21utils.writeFlatThirteen(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find(" add b6") != -1): # MySymbol = m21utils.writeFlatSix(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find(" alter b5") != -1): # MySymbol = m21utils.writeHalfDim(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find(" alter #5") != -1): # MySymbol = m21utils.writeSharpFive(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # elif (TheFigure.find("pedal") != -1): # MySymbol = m21utils.writePedal(MyMeasure, c[x].pitches[0].name,c[x].duration,c[x].chordKind) # else: # if (c[x].duration.type != "zero"): # if (c[x].root().name != c[x].bass().name): # # print (c[x].root().name, c[x].bass().name) # MySymbol = ChordSymbol(root=c[x].root(), bass=c[x].bass(), kind=c[x].chordKind) # else: # MySymbol = ChordSymbol(root=c[x].root(), bass=c[x].root(), kind=c[x].chordKind) # MySymbol.duration = c[x].duration # MyMeasure.append(MySymbol) # # print("Wrote chord " + str(MySymbol.figure) + "...") # n3 = Note(MySymbol.third) # n3.duration = Duration(c[x].duration.quarterLength * 0.50) # # n3.lyric = '3rd' # n3.octave = 5 # MyMeasure.append(n3) # if (MySymbol.containsSeventh()): # n7 = m21.note.Note(MySymbol.seventh) # n7.duration = Duration(c[x].duration.quarterLength * 0.50) # # n7.lyric = '7th' # n7.octave = 5 # MyMeasure.append(n7) # else: # n5 = m21.note.Note(MySymbol.root()) # n5.duration = Duration(c[x].duration.quarterLength * 0.50) # # n5.lyric = 'R' # n5.octave = 5 # MyMeasure.append(n5) # if ((m.number)%4 == 0): # sl = m21.layout.SystemLayout(isNew=True) # MyMeasure.append(sl) # MyScore.append(MyMeasure) # Set metadata title = tune + ' - Guide Tone Study' score.metadata = m21.metadata.Metadata() score.metadata.title = title score.metadata.movementName = ' ' # For some reason this works, None and '' don't... score.metadata.composer = 'Greg Pascale' return score
from music21.instrument import fromString as get_instrument from music21.clef import BassClef timestamp = datetime.datetime.utcnow() metadata = Metadata() metadata.title = 'The Title' metadata.composer = 'Jonathan Marmor' metadata.date = timestamp.strftime('%Y/%m/%d') score = Score() score.insert(0, metadata) part = Part() parts = [part] oboe = get_instrument('oboe') part.insert(0, oboe) score.insert(0, part) score.insert(0, StaffGroup(parts)) for dur in [[1, .5], [.25], [.25, 2]]: pitch = Pitch(60) note = Note(pitch) duration = Duration() duration.fill(dur) note.duration = duration part.append(note) score.show('musicxml', '/Applications/Sibelius 7.5.app')
def generate(tune, clef=m21.clef.TrebleClef()): score = m21utils.loadScoreForTune(tune) # print 'score', score s = score.parts[0].getElementsByClass("Measure") m21.harmony.realizeChordSymbolDurations(s) ## Needed to make this work! MyScore = m21.stream.Score() MyScore.append(s[0].keySignature) ## get key from document MyScore.append(clef) #add clef for m in s: MyMeasure = Measure() ## Make a measure and put everything inside it MyMeasure.number = m.number ## give the measure a number MyMeasure.rightBarline = m.rightBarline MyMeasure.leftBarline = m.leftBarline # print("_____________________") # print("In measure "+ str(m.number) + "(" + str(m.id) + ")" +" of " + str(len(s)) ) #debug monitoring c = m.getElementsByClass(ChordSymbol) for x in range(len(c)): # print(c[x].duration) # print(c[x].beat) # print (c[x].figure) # print("--------------------") TheFigure = c[x].figure MyChord = Chord(c[x].pitches) MySymbol = ChordSymbol() ######## Fix XML chord symbols ############ if (TheFigure.find(" alter b9") != -1): MySymbol = m21utils.writeFlatNine(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find(" add b9") != -1): MySymbol = m21utils.writeFlatNine(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find(" add #9") != -1): MySymbol = m21utils.writeSharpNine(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find(" add #7") != -1): MySymbol = m21utils.writeSharpSeven(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find(" add #11") != -1): MySymbol = m21utils.writeSharpEleven(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find(" add b13") != -1): MySymbol = m21utils.writeFlatThirteen(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find(" add b6") != -1): MySymbol = m21utils.writeFlatSix(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find(" alter b5") != -1): MySymbol = m21utils.writeHalfDim(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find(" alter #5") != -1): MySymbol = m21utils.writeSharpFive(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) elif (TheFigure.find("pedal") != -1): MySymbol = m21utils.writePedal(MyMeasure, c[x].pitches[0].name, c[x].duration, c[x].chordKind) else: if (c[x].duration.type != "zero"): if (c[x].root().name != c[x].bass().name): # print (c[x].root().name, c[x].bass().name) MySymbol = ChordSymbol(root=c[x].root(), bass=c[x].bass(), kind=c[x].chordKind) else: MySymbol = ChordSymbol(root=c[x].root(), bass=c[x].root(), kind=c[x].chordKind) MySymbol.duration = c[x].duration MyMeasure.append(MySymbol) # print("Wrote chord " + str(MySymbol.figure) + "...") n3 = Note(MySymbol.third) n3.duration = Duration(c[x].duration.quarterLength * 0.50) # n3.lyric = '3rd' n3.octave = 5 MyMeasure.append(n3) if (MySymbol.containsSeventh()): n7 = m21.note.Note(MySymbol.seventh) n7.duration = Duration(c[x].duration.quarterLength * 0.50) # n7.lyric = '7th' n7.octave = 5 MyMeasure.append(n7) else: n5 = m21.note.Note(MySymbol.root()) n5.duration = Duration(c[x].duration.quarterLength * 0.50) # n5.lyric = 'R' n5.octave = 5 MyMeasure.append(n5) if ((m.number) % 4 == 0): sl = m21.layout.SystemLayout(isNew=True) MyMeasure.append(sl) MyScore.append(MyMeasure) # Set metadata title = tune + ' - Guide Tone Study' MyScore.metadata = m21.metadata.Metadata() MyScore.metadata.title = title MyScore.metadata.movementName = ' ' # For some reason this works, None and '' don't... MyScore.metadata.composer = 'Greg Pascale' return MyScore