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 delete_doc(): params = request.json if not param_rule.delete_validator.validate(params): return jsonify({ "message": f'エラー,無効なパラメータ "{param_rule.delete_validator.errors}"' }), 400 table = params.get("table", None) index = params.get("index", None) db = utils.select_table(table) doc = utils.get_at(db.all(), index, None) if doc != None: db.remove(doc_ids=[doc.doc_id]) return jsonify({"message": "成功"}) else: return jsonify({"message": "指定されたindexにマッチするものが見つかりませんでした"})
def get_doc(): params = request.json if not param_rule.get_validator.validate(params): return jsonify( {"message": f'エラー,無効なパラメータ "{param_rule.get_validator.errors}"'}), 400 table = params.get("table", None) index = params.get("index", None) from_i = params.get("from_i", None) to_i = params.get("to_i", None) db = utils.select_table(table) data = db.all() if index != None: return jsonify({"data": utils.get_at(data, index, None)}) if from_i != None or to_i != None: from_i = from_i or 0 to_i = to_i or len(data) return jsonify({"data": data[from_i:to_i]}) return jsonify({"data": data})
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