def genMotif(prev_note = random.choice([0,2,4,7]), chords = [[0,2,4],[0,2,4]], cell1 = None): #generate first cell start_chord = chords[0] if cell1 == None: cell1 = gc.genCell(2.0, prev_note, first_note = None, chord=start_chord, durs=[], cell_type=None) #now generate second cell from first cell end_chord = chords[1] if random.uniform(0, 1) < 0.8 and len(set(cell1.durs)) > 1 and 0.33333333 not in cell1.durs: return [cell1, tf.transformCell(cell1, cell1, end_chord)] else: if end_chord == None: end_chord = hm.getHighestProbChord(start_chord, None) cell2 = gc.genCell(2.0, cell1.pits[-1], None, end_chord, [], None) return [cell1, cell2]
def keepRhythm(old_cell, prev_cell, first_notes, chord=[0,2,4]): first_notes = ([old_cell.pits[-1] + i for i in [0,-1,1]]) first_notes.extend([prev_cell.pits[-1] + i for i in range(-4,4)]) new_cells = [] for first_note in first_notes: for i in range(0,4): durs = rhy.alterRhythm(old_cell) #print(durs) new_cells.append(gc.genCell(length=old_cell.length, first_note=first_note, chord=chord, durs=durs, cell_type = old_cell.ctype)) return new_cells
def genContinuationCadential(prev_note = random.choice([2,4,7]), end_chords = [[0,2,4], [3,5,7], [4,6,8], [0,2,4]]): cords = [[0,2,4], [1,3,5],[4,6,8], [-2,0,2],[1,3,5],[0,2,4]] cells = [] for i in range(0, 3): for j in range(0,2): cell_type = CHORDAL if random.uniform(0,1) < 0.5 else SCALEWISE chord = cords[i*2 + j] cell = gc.genCell(prev_note = prev_note, length=2.0, chord=chord, durs=[0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25], cell_type=cell_type) for k in range(0,4): if (cell.pits[0] == cell.pits[2] and cell.pits[1] == cell.pits[3] or cell.pits[4] == cell.pits[6] and cell.pits[5] == cell.pits[7]): cell = gc.genCell(prev_note = prev_note, length=2.0, chord=chord, durs=[0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25], cell_type=cell_type) cells.append(cell) prev_note = cells[-1].pits[-1] end_chord_durs = [[0.75,0.25,0.75,0.25],[0.75,0.25,0.75,0.25],[0.5,0.5,0.5,0.5],[1.0]] for i in range(0, len(end_chords) - 1): cells.append(gc.genCell(prev_note = prev_note, length=2.0, chord=end_chords[i], durs=end_chord_durs[i], cell_type=CHORDAL)) prev_note = cells[-1].pits[-1] if prev_note > 14: prev_note -= 3 cells.append(genending.genEnding(prev_note, chord=[0,2,4], authentic=True)) return Chunk(sub_chunks=cells)
def transformCell(transformed_cell, prev_cell = gc.genBlankCell(2.0), chord_choices = [[0,2,4],[4,6,8]], best_chord = [0,2,4]): transform_cell_function_names = ['transpose', 'retrograde', 'toDouble', 'fromDouble', 'switchBeats'] random.shuffle(transform_cell_function_names) transform_cell_function_names = ['addOrnamentation'] + transform_cell_function_names transform_cell_function_names.append('keepRhythm') function_index = -1 while True: function_index += 1 if function_index >= len(transform_cell_function_names): #print('dead') return gc.genCell(length=2.0, chord=best_chord, durs=rhy.alterRhythm(transformed_cell))#(gc.genCell(length=2.0, chord=best_chord, durs=rhy.alterRhythm(transformed_cell), cell_type=transformed_cell.ctype), 'dead') else: #print(transform_cell_function_names[function_index]) attempting_cells = transform_cell_functions[transform_cell_function_names[function_index]](transformed_cell, prev_cell, ct.getFirstNotes(prev_cell, transformed_cell), best_chord) for attempting_cell in attempting_cells: random.shuffle(chord_choices) for chord in chord_choices: if hm.chunkInChord(attempting_cell, chord) and pref.goodCells([prev_cell, attempting_cell]): attempting_cell.chord = chord return attempting_cell#(attempting_cell, transform_cell_function_names[function_index])
def genPhrase( prev_note=random.randint(0, 4), chords=[[0, 2, 4], [0, 2, 4], [4, 6, 8], [0, 2, 4], [0, 2, 4], [3, 5, 7], [4, 6, 8], [0, 2, 4]], basicIdea=None, authentic_cadence=False, ): phrase_cells = [] if basicIdea == None: basicIdea = gb.genBasicIdea(prev_note=prev_note, chords=chords[:4], motif1=None) phrase_cells = basicIdea.cells # choose first or second motif of basic idea to transform for third motif which_motif_to_transform = random.choice([phrase_cells[:2], phrase_cells[2:]]) third_motif = tf.transformMotif(which_motif_to_transform, phrase_cells[-1], chords[4:6]) motif4cell1 = gc.genCell( 2.0, third_motif[-1].pits[-1], first_note=None, chord=chords[6], durs=rhy.getDefiningRhythm(phrase_cells + third_motif), ) motif4cell2 = genending.genEnding(motif4cell1.pits[-1], chords[-1], authentic_cadence) second_half = Chunk(sub_chunks=third_motif + [motif4cell1] + [motif4cell2], ctype="half") return Chunk(sub_chunks=[basicIdea, second_half], ctype="phrase")
__author__ = 'halley' import gencell as gc import music21helpers as mh import harmony as hm from music21 import * part1_cells = [] part2_cells = [] beginning_chords = [[0,2,4],[0,2,4],[4,6,8],[0,2,4]] #beginning chords prev_note = 4 for i in range(0, len(beginning_chords)): part1_cells.append(gc.genCell(length = 2.0, chord=beginning_chords[i])) part2_cells.append(gc.genBlankCell(2.0)) for i in range(0,10): prev_part = part1_cells[-4:] for j in range(0, len(prev_part)): new_cell = gc.genCell(prev_note = part1_cells[-1].pits[-1], length = 2.0, chord=prev_part[j].chord) while not (hm.chunkMatches(new_cell, prev_part[j])): new_cell = gc.genCell(prev_note = part1_cells[-1].pits[-1], length = 2.0, chord=prev_part[j].chord) part1_cells.append(new_cell) part2_cells.append(prev_part[j]) print('i = ' + str(i) + ' j = ' + str(j)) part1 = mh.cellsToPart(part1_cells) part2 = mh.cellsToPart(part2_cells, octave=3) s = stream.Stream()
def transformFirstChangeSecond(transform_motif, prev_cell = gc.genBlankCell(2.0), chords = [[0,2,4],[0,2,4]]): cell1 = tf.transformCell(transform_motif[0], prev_cell, chords[0]) cell2 = gc.genCell(length=2.0, prev_note=cell1.pits[-1], chord=chords[1]) return [cell1, cell2]
def sameCtypeSameRhythmDifferentChunk(cell1, prog, prev_note = 0, up_down = random.choice([-1,1])): return [gc.genCell(length=2.0, prev_note=prev_note, first_note = None, chord = prog, durs = cell1.durs, cell_type = cell1.ctype) for i in range(0,2)]
def subRhythm(chunk, chord, prev_note = 0, up_down = random.choice([-1,1])): new_rhythm = rhy.alterRhythm(chunk.durs, 0.3) return gc.genCell(length=sum(new_rhythm),prev_note=prev_note, first_note = None, chord=chord, durs=new_rhythm, cell_type=chunk.ctype)
elif i > 0.75: return 'f' else: return 'fff' f = open(sys.argv[1] + '.json', 'r+') data = json.load(f) sentiment = [i['sentiment'] for i in data] similarities = [i['similarity'] for i in data] loudness = [i['loudness'] for i in data] loudness = [i + (1.0-max(loudness)) for i in loudness] loudness = [getLoudness(i) for i in loudness] hashTagFreq = [i['hashtags'] for i in data] melody_cells = [gc.genCell(2.0,0)] melody_cells.append(gc.genCell(2.0, melody_cells[-1].pits[-1])) bass_cells = [] def getScale(sentiment): if sentiment == 0: return [0,2,4,6,8,10] elif sentiment <= 0: return [0,2,3,5,7,8,10] else: return [0,2,4,5,7,9,11] for i in range(0,len(data)): sims = similarities[i][:i]