def handle_slerp_z(model, stdin, stdout, state): slerp_z_msg = read_msg(stdin, protocol.slerp_z_struct.size) z0, z1, amount = protocol.from_slerp_z_msg(slerp_z_msg) z = gu.slerp(z0, z1, amount) stdout.write(protocol.to_tag_msg(protocol.OUT_TAG_Z)) stdout.write(protocol.to_count_msg(1)) stdout.write(protocol.to_z_msg(z)) stdout.flush()
def handle_gen_audio(model, stdin, stdout, state): count_msg = read_msg(stdin, protocol.count_struct.size) count = protocol.from_count_msg(count_msg) pitches = [] zs = [] for i in range(count): gen_msg = read_msg(stdin, protocol.gen_audio_struct.size) pitch, z = protocol.from_gen_msg(gen_msg) pitches.append(pitch) zs.append(z) layer_offsets = {} if 'ganspace_component_amplitudes' in state: components = state['ganspace_components']['comp'] std_devs = state['ganspace_components']['stdev'] edits = state['ganspace_component_amplitudes'] amounts = np.zeros(components.shape[:1], dtype=np.float32) amounts[:len(list(map(float, edits)))] = edits * std_devs scaled_directions = amounts.reshape(-1, 1, 1, 1) * components linear_combination = np.sum(scaled_directions, axis=0) linear_combination_batch = np.repeat(linear_combination.reshape( 1, *linear_combination.shape), 8, axis=0) layer_offsets[state['ganspace_components'] ['layer']] = linear_combination_batch z_arr = np.array(zs) try: audios = model.generate_samples_from_z(z_arr, pitches, layer_offsets=layer_offsets) except KeyError as e: print_err( "can't synthesize - model was not trained on pitch {}".format( e.args[0])) audios = [] stdout.write(protocol.to_tag_msg(protocol.OUT_TAG_AUDIO)) stdout.write(protocol.to_count_msg(len(audios))) for audio in audios: stdout.write(protocol.to_audio_size_msg(audio.size * audio.itemsize)) stdout.write(protocol.to_audio_msg(audio)) stdout.flush()
def handle_hallucinate_noz(model, stdin, stdout, state): max_note_length = model.config['audio_length'] sample_rate = model.config['sample_rate'] pca = state["ganspace_components"] stdevs = pca["stdev"] layer_dtype = stdevs.dtype hallucinate_msg = read_msg(stdin, protocol.hallucinate_struct.size) step_count, interpolation_steps, spacing, start_trim, attack, sustain, release = protocol.from_hallucinate_msg( hallucinate_msg) edit_count_msg = read_msg(stdin, protocol.count_struct.size) edit_count = protocol.from_count_msg(edit_count_msg) pitch = min(model.pitch_counts.keys()) steps = [] for i in range(step_count): edits = [] for j in range(edit_count): edit_msg = read_msg(stdin, protocol.f64_struct.size) edit = protocol.from_f64_msg(edit_msg) edits.append(edit) steps.append(np.array(edits, dtype=layer_dtype)) steps = list(interpolate_edits(steps, interpolation_steps)) layer_steps = np.array(list( map(lambda edits: make_layer(pca, edits), steps)), dtype=layer_dtype) pitch_steps = np.repeat([pitch], len(steps)) audios = model.generate_samples_from_layers({pca["layer"]: layer_steps}, pitch_steps) final_audio = combine_notes(audios, spacing=spacing, start_trim=start_trim, attack=attack, sustain=sustain, release=release, max_note_length=max_note_length, sr=sample_rate) final_audio = final_audio.astype("float32") stdout.write(protocol.to_tag_msg(protocol.OUT_TAG_AUDIO)) stdout.write( protocol.to_audio_size_msg(final_audio.size * final_audio.itemsize)) stdout.write(protocol.to_audio_msg(final_audio)) stdout.flush()
def handle_load_ganspace_components(model, stdin, stdout, state): size_msg = read_msg(stdin, protocol.int_struct.size) size = protocol.from_int_msg(size_msg) msg = read_msg(stdin, size) file = msg.decode('utf-8') print_err("Opening components file '{}'".format(file)) with open(file, "rb") as fp: state['ganspace_components'] = pickle.load(fp) print_err("Components file loaded.") component_count = len(state['ganspace_components']["comp"]) state['ganspace_component_count'] = component_count stdout.write(protocol.to_tag_msg(protocol.OUT_TAG_LOAD_COMPONENTS)) stdout.write(protocol.to_count_msg(component_count)) stdout.flush()
def handle_rand_z(model, stdin, stdout, state): """ Generates a given number of new Z coordinates. """ count_msg = read_msg(stdin, protocol.count_struct.size) count = protocol.from_count_msg(count_msg) zs = model.generate_z(count) stdout.write(protocol.to_tag_msg(protocol.OUT_TAG_Z)) stdout.write(protocol.to_count_msg(len(zs))) for z in zs: stdout.write(protocol.to_z_msg(z)) stdout.flush()
def handle_synthesize_noz(model, stdin, stdout, state): print_err("handle_synthesize_noz") count_msg = read_msg(stdin, protocol.count_struct.size) count = protocol.from_count_msg(count_msg) pitches = [] for i in range(count): gen_msg = read_msg(stdin, protocol.synthesize_noz_struct.size) pitch = protocol.from_synthesize_noz_msg(gen_msg) pitches.append(pitch) pca = state["ganspace_components"] stdevs = pca["stdev"] layer_dtype = stdevs.dtype edits = np.array(state["ganspace_component_amplitudes"], dtype=layer_dtype) layer = make_layer(pca, edits) layers = np.repeat([layer], len(pitches), axis=0) try: audios = model.generate_samples_from_layers({pca["layer"]: layers}, pitches) except KeyError as e: print_err( "can't synthesize - model was not trained on pitch {}".format( e.args[0])) audios = [] stdout.write(protocol.to_tag_msg(protocol.OUT_TAG_AUDIO)) stdout.write(protocol.to_count_msg(len(audios))) for audio in audios: stdout.write(protocol.to_audio_size_msg(audio.size * audio.itemsize)) stdout.write(protocol.to_audio_msg(audio)) stdout.flush()
def handle_hallucinate(model, stdin, stdout, state): max_note_length = model.config['audio_length'] sample_rate = model.config['sample_rate'] hallucinate_msg = read_msg(stdin, protocol.hallucinate_struct.size) args = protocol.from_hallucinate_msg(hallucinate_msg) note_count, interpolation_steps, spacing, start_trim, attack, sustain, release = args print_err( "note_count = {} interpolation_steps = {}, spacing = {}s, start_trim = {}s, attack = {}s, sustain = {}s, release = {}s" .format(*args)) initial_notes = model.generate_z(note_count) initial_piches = np.array( [32] * len(initial_notes) ) # np.floor(30 + np.random.rand(len(initial_notes)) * 30) final_notes, final_pitches = interpolate_notes(initial_notes, initial_piches, interpolation_steps) audios = synthesize(model, final_notes, final_pitches) final_audio = combine_notes(audios, spacing=spacing, start_trim=start_trim, attack=attack, sustain=sustain, release=release, max_note_length=max_note_length, sr=sample_rate) final_audio = final_audio.astype('float32') stdout.write(protocol.to_tag_msg(protocol.OUT_TAG_AUDIO)) stdout.write( protocol.to_audio_size_msg(final_audio.size * final_audio.itemsize)) stdout.write(protocol.to_audio_msg(final_audio)) stdout.flush()
def _write_msg(self, tag, *msgs): tag_msg = protocol.to_tag_msg(tag) self._proc.stdin.write(tag_msg) for msg in msgs: self._proc.stdin.write(msg) self._proc.stdin.flush()
from handlers import handlers try: ckpt_dir = sys.argv[1] batch_size = int(sys.argv[2]) except IndexError: print_err("usage: {} checkpoint_dir batch_size".format( os.path.basename(__file__))) sys.exit(1) flags = lib_flags.Flags({"batch_size_schedule": [batch_size]}) model = lib_model.Model.load_from_path(ckpt_dir, flags) stdin = os.fdopen(sys.stdin.fileno(), "rb", 0) stdout = os.fdopen(sys.stdout.fileno(), "wb", 0) stdout.write(gss.to_tag_msg(gss.OUT_TAG_INIT)) audio_length = model.config['audio_length'] sample_rate = model.config['sample_rate'] info_msg = gss.to_info_msg(audio_length=audio_length, sample_rate=sample_rate) stdout.write(info_msg) stdout.flush() state = {} while True: in_tag_msg = read_msg(stdin, gss.tag_struct.size) in_tag = gss.from_tag_msg(in_tag_msg) if in_tag not in handlers: raise ValueError("unknown input message tag: {}".format(in_tag))