def choose_melody_pitches(self, notes, register, harmonies): print 'Choosing pitches' for h in harmonies: h['pitch_classes'] = [p % 12 for p in h['pitch']] set_start_end(notes) set_start_end(harmonies) previous_note_index = random.choice(range(int(len(register) * .4))) prev = register[previous_note_index] for note in notes: beats = list( frange(note['start'], note['start'] + note['duration'], .25)) note_harmonies = [] for b in beats: h = get_at(b, harmonies) h = h['pitch_classes'] if h not in note_harmonies: note_harmonies.append(h) print 'note_harmonies', note_harmonies if len(note_harmonies) == 1: pitch_options = self.get_pitch_options(note_harmonies[0], prev) else: pitch_options = [] c = Counter() for h in note_harmonies: for p in h: c[p] += 1 common = [] for p, count in c.most_common(): if count == len(note_harmonies): common.append(p) if len(common) > 0: pitch_options = self.get_pitch_options(common, prev) if len(pitch_options) == 0: ranked = [p for p, _ in c.most_common() if p not in common] for p in ranked: pitch_options = self.get_pitch_options([p], prev) if len(pitch_options) > 0: break if len(pitch_options) == 0: pitch_options = [prev - 2, prev - 1, prev + 1, prev + 2] note['pitch'] = random.choice(pitch_options) print note['pitch'] % 12 print prev = note['pitch'] self.add_ornament(note, prev, note_harmonies)
def choose_melody_pitches(self, notes, register, harmonies): print 'Choosing pitches' for h in harmonies: h['pitch_classes'] = [p % 12 for p in h['pitch']] set_start_end(notes) set_start_end(harmonies) previous_note_index = random.choice(range(int(len(register) * .4))) prev = register[previous_note_index] for note in notes: beats = list(frange(note['start'], note['start'] + note['duration'], .25)) note_harmonies = [] for b in beats: h = get_at(b, harmonies) h = h['pitch_classes'] if h not in note_harmonies: note_harmonies.append(h) print 'note_harmonies', note_harmonies if len(note_harmonies) == 1: pitch_options = self.get_pitch_options(note_harmonies[0], prev) else: pitch_options = [] c = Counter() for h in note_harmonies: for p in h: c[p] += 1 common = [] for p, count in c.most_common(): if count == len(note_harmonies): common.append(p) if len(common) > 0: pitch_options = self.get_pitch_options(common, prev) if len(pitch_options) == 0: ranked = [p for p, _ in c.most_common() if p not in common] for p in ranked: pitch_options = self.get_pitch_options([p], prev) if len(pitch_options) > 0: break if len(pitch_options) == 0: pitch_options = [prev - 2, prev - 1, prev + 1, prev + 2] note['pitch'] = random.choice(pitch_options) print note['pitch'] % 12 print prev = note['pitch'] self.add_ornament(note, prev, note_harmonies)
def choose_melody_pitches(self, notes, register, harmonies, start_with_rest): # print 'Choosing pitches' for h in harmonies: h['pitch_classes'] = [p % 12 for p in h['pitch']] set_start_end(notes) set_start_end(harmonies) # Pick a random pitch from the instrument's register on which to start previous_note_index = random.choice(range(int(len(register)))) prev = register[previous_note_index] previous_note = {'duration': 1.0, 'pitch': prev} pitch_history = [] first = True # print '-'*10 for note in notes: if first and start_with_rest: note['pitch'] = 'rest' first = False continue beats = list(frange(note['start'], note['start'] + note['duration'], .25)) note_harmonies = [] for b in beats: h = get_at(b, harmonies) h = h['pitch_classes'] if h not in note_harmonies: note_harmonies.append(h) if len(note_harmonies) == 1: pitch_options = self.get_pitch_options(note_harmonies[0], prev) else: pitch_options = [] c = Counter() for h in note_harmonies: for p in h: c[p] += 1 common = [] for p, count in c.most_common(): if count == len(note_harmonies): common.append(p) if len(common) > 0: pitch_options = self.get_pitch_options(common, prev) if len(pitch_options) == 0: ranked = [p for p, _ in c.most_common() if p not in common] for p in ranked: pitch_options = self.get_pitch_options([p], prev) if len(pitch_options) > 0: break if len(pitch_options) == 0: pitch_options = [prev - 2, prev - 1, prev + 1, prev + 2] note['pitch'] = random.choice(pitch_options) self.add_ornament(note, previous_note, note_harmonies, first) # print note prev = note['pitch'] previous_note = note pitch_history.append(note['pitch']) first = False