def augment(): """ Pulls in note events from javascript, and interpolates the missing notes via note_interpolater methods. Inputs: No direct arguments, but ... Pulls in JSON note positions via flask Outputs: New JSON of notes. """ data = flask.request.json new_notes = "" note_stack, to_fill = notei.make_note_stack(data["notes"]) for i in to_fill: if i < max_length: new_note = notei.get_note_to_append( melody_marks, note_stack[0 : i + max_length + 1], melody_weights, max_length ) else: new_note = notei.get_note_to_append( melody_marks, note_stack[i - max_length : i + max_length + 1], melody_weights, max_length ) new_notes += "," + str(i) + "," + str(72 - new_note) note_stack = note_stack[:i] + [(i, [new_note])] + note_stack[i + 1 :] data["notes"] += new_notes return flask.jsonify(data)
def play_notes(): """ Pulls in note positions from javascript, creates a random filename, and writes .wav file representing those notes. Inputs: No direct arguments, but ... Pulls in JSON of note positions via flask Outputs: filename prefix (sends to javascript) """ data = flask.request.json if data["notes"] == "": return flask.jsonify(data) rand_id = str(uuid.uuid4().hex)[:6] note_data, _ = notei.make_note_stack(data["notes"]) s = [] fs = fluidsynth.Synth() sfid = fs.sfload(absolute_path + "FluidR3_GM.sf2") fs.program_select(0, sfid, 0, 0) for tick, notes in note_data: if notes != ["x"]: for val in notes: fs.noteon(0, int(val), 100) s = np.append(s, fs.get_samples(int(44100 * 0.2))) for val in notes: fs.noteoff(0, int(val)) else: s = np.append(s, fs.get_samples(int(44100 * 0.2))) fs.delete() samps = fluidsynth.raw_audio_string(s) wf = wave.open(absolute_path + "static/" + rand_id + ".wav", "wb") wf.setnchannels(2) wf.setframerate(44100) wf.setsampwidth(2) wf.writeframes(b"".join(samps)) wf.close() return flask.jsonify({"id": rand_id})