def genBass(main_passage, low = -4, high = 7): prev_note = 4 main_cells = main_passage.cells bass_cells = [] for main_cell in main_cells: if main_cell.chord == [] or main_cell.chord == None: bass_cells.append(gc.genBlankCell(2.0)) prev_note = 4 else: main_pits = main_cell.beat_pits pits = [] pits.append(pth.getClosestPCDegree(prev_note, main_pits[0][0], low = low, high = high)) if len(main_pits) == 2: if hm.inChord(main_pits[1][0], main_cell.chord): pits.append(sc.closestNoteDegreeInChord(note=pits[-1], chord=main_cell.chord, same=False, low = low, high = high)) durs = [1.0,1.0] bass_cells.append(Chunk(pits = pits, durs=durs, chord=main_cell.chord, key = main_cell.key)) else: durs = [2.0] bass_cells.append(Chunk(pits = pits, durs = durs, chord=main_cell.chord, key = main_cell.key)) else: #if it's a 1.5, 0.5 or 1.5, 0.25,0.25 durs = [2.0] bass_cells.append(Chunk(pits = pits, durs = durs, chord=main_cell.chord, key = main_cell.key)) prev_note = pits[-1] return bass_cells
def genQuarters(main_cells): quarter_cells = [] prev_note = -3 pre_prev_note = -2 for main_cell in main_cells: main_pits = [i[0] for i in main_cell.beat_pits] if len(main_pits) == 1: main_pits.append(None) #we have a "Free" note if main_cell.chord == None or main_cell.chord == []: quarter_cells.append(gc.genBlankCell(2.0)) else: pits = [] for main_pit in main_pits: if main_pit == None: if pre_prev_note == prev_note: pits.append(sc.closestNoteDegreeInChord(prev_note + random.choice([-2,2]), main_cell.chord)) else: pits.append(sc.closestNoteDegreeInChord(prev_note, main_cell.chord)) else: closest_chord_notes = sc.closestNotesInChord(prev_note + random.choice([-2,2]), main_cell.chord) closest_chord_notes = filter(lambda i: i > -7, closest_chord_notes) for chord_note in closest_chord_notes: if hm.matches(chord_note, main_pit): pits.append(chord_note) break pre_prev_note = prev_note prev_note = pits[-1] durs = [1.0,1.0] quarter_cells.append(Chunk(pits=pits, durs=durs, chord=main_cell.chord, key = main_cell.key)) prev_note = quarter_cells[-1].pits[-1] return quarter_cells
def insertCallResponse(instr_cells, main_passage, group1, group2): main_cells = main_passage.cells main1 = [[] for instr in group1] main2 = [[] for instr in group2] empty = [gc.genBlankCell(2.0) for i in main_cells] for i in range(0, len(main_cells)): if i % 2 == 0: for j in range(0, len(main1)): main1[j].append(main_cells[i]) which_to_sound = random.randint(0, len(main2) - 1) for j in range(0, len(main2)): if j != which_to_sound: main2[j].append(gc.genBlankCell(2.0)) else: if main_cells[i].chord == None or main_cells[i].chord == []: main2[j].append(gc.genBlankCell(2.0)) else: main2[j].append(Chunk(pits = [random.choice(main_cells[i].chord)], durs=[2.0], key=main_cells[i].key)) else: for j in range(0, len(main2)): main2[j].append(main_cells[i]) which_to_sound = random.randint(0, len(main1) - 1) for j in range(0, len(main1)): if j != which_to_sound: main1[j].append(gc.genBlankCell(2.0)) else: if main_cells[i].chord == None or main_cells[i].chord == []: main1[j].append(gc.genBlankCell(2.0)) else: main1[j].append(Chunk(pits = [random.choice(main_cells[i].chord)], durs=[2.0], key=main_cells[i].key)) for i in range(0, len(group1)): instr_cells[group1[i]].extend(main1[i]) for i in range(0, len(group2)): instr_cells[group2[i]].extend(main2[i]) for instr in instrs: if instr not in group1 and instr not in group2: instr_cells[instr].extend(empty) return main_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])
__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 transformFirstCellTwice(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 = tf.transformCell(transform_motif[0], cell1, chords[1]) return [cell1, cell2]
def switchFirstSecond(transform_motif, prev_cell = gc.genBlankCell(2.0), chords = [[0,2,4],[0,2,4]]): cell1 = tf.transformCell(transform_motif[1], prev_cell, chords[0]) cell2 = tf.transformCell(transform_motif[0], cell1, chords[1]) return [cell1, cell2]
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]
import counterpoint as counter import genbass as gb from chunk import * #define instruments instr_cells = OrderedDict() instr_cells['Flute'] = [] instr_cells['Piano 1'] = [] instr_cells['Piano 2'] = [] instrs = ['Flute', 'Piano 1', 'Piano 2'] octaves = {'Flute':5, 'Piano 1':5, 'Piano 2':4} #generate content period1phrase1 = (gp.genPhrase(authentic_cadence=False)) instr_cells['Flute'].extend([gc.genBlankCell(2.0) for cell in period1phrase1.cells]) instr_cells['Piano 1'].extend(period1phrase1.cells) instr_cells['Piano 2'].extend(acc.genAlbertiEighths(period1phrase1)) period1phrase2 = (gp.genPhrase(basicIdea=period1phrase1.sub_chunks[0], authentic_cadence=True)) instr_cells['Flute'].extend(period1phrase2.cells) instr_cells['Piano 1'].extend(acc.genAlbertiEighths(period1phrase2)) instr_cells['Piano 2'].extend(acc.genQuarters(period1phrase2)) period2 = (gper.genPeriod()) period2phrase1 = period2.sub_chunks[0] instr_cells['Flute'].extend(counter.genCounter(period2phrase1)) instr_cells['Piano 1'].extend(period2phrase1.cells) instr_cells['Piano 2'].extend(acc.genAlbertiEighths(period2phrase1)) period2phrase2 = period2.sub_chunks[1]