batch_ipt = [] batch_tgt = [] for step in range(timesteps - channels - 1): batch_ipt.append(signals[i, step:step + channels]) batch_tgt.append([signals[i, step + channels + 1]]) batch_ipt = np.array(batch_ipt) batch_tgt = np.array(batch_tgt) batch_ipt = np.expand_dims(batch_ipt, 0) batch_tgt = np.expand_dims(batch_tgt, 0) batch_loss = model.train_on_batch(batch_ipt, batch_tgt) ### TRAINNING loss.append([time.time() - initial_time, batch_loss]) print('LOSS:', loss[-1][1], 'Time:', loss[-1][0]) save_model(model, path, 'model') np.savetxt(path + 'loss.csv', np.array(loss), fmt='%.18e', delimiter=',') P_model.set_weights(model.get_weights()) save_model(P_model, path, 'P_model') for i in range(samples): seed = np.array(signals[i][0:channels]) save_wav(max_amp * seed, name=path + 'seed_' + names[i] + '.wav') seed = np.expand_dims(seed, 0) seed = np.expand_dims(seed, 0) pred_wave = np.zeros(timesteps) for j in range(channels, timesteps): predicted = P_model.predict(seed) pred_wave[j] = predicted[0, 0, 0] seed = np.roll(seed, -1) seed[0, 0, channels - 1] = predicted[0, 0, 0] save_wav(max_amp * pred_wave, name=path + 'wave_' + names[i] + '.wav')
plt.axis([0, L, -1.1 * amplitude, 1.1 * amplitude]) plt.axvline(L * pickup_position) plt.savefig(path + 'plots/' + str(ctr) + '.png') plt.close() ctr += 1 for j in range(1, N): print('step ', ctr,' of ', N) for i in range(1, M): y[i] = 2 * y_1[i] - y_2[i] + C**2 * (y_1[i+1] - 2*y_1[i] + y_1[i-1]) y[0] = y[0] * 0.5 y[M] = y[M] * 0.5 y = y * sustain w.append(y[pickup]) y_2[:] = y_1 y_1[:] = y if plot: loop = 0 plt.plot(x, y, 'k') plt.axis([0, L, -1.1 * amplitude, 1.1 * amplitude]) plt.axvline(L * pickup_position) plt.savefig(path + 'plots/' + str(ctr) + '.png') plt.close() ctr += 1 w = np.array(w) * 4000000 print('time =', time.time() - initial_time) save_wav( w, path + str(frequency) + '_Pluck=' + str(pluck_position) + '_Pick=' + str(pickup_position) + '.wav' )
model.add(K.layers.Dense(tgt.shape[1], activation=act, input_shape=(ipt.shape[1],))) else: model.add(K.layers.Dense(hlayers[0], activation=act, input_shape=(ipt.shape[1],))) # model.add(K.layers.Dropout(0.01)) for hn in hlayers[1:]: model.add(K.layers.Dense(hn, activation=act)) # model.add(K.layers.Dropout(0.01)) model.add(K.layers.Dense(tgt.shape[1], activation=act)) # model.add(K.layers.Dropout(0.01)) model.compile(loss='mean_squared_error', optimizer=opt) tb = K.callbacks.TensorBoard(logpath) K.utils.print_summary(model) history = model.fit(ipt, tgt, batch_size=batch, epochs=epchs, verbose=0, callbacks=[tb], shuffle=True) save_model(model, logpath, 'model') return model path = './Log/' m = train([75,75,75], path, batch = 100, epchs = 10) for i, input in enumerate(ipt): input = np.expand_dims(input, 0) pred = m.predict(input) save_wav(pred * max_amp, name = path + 'wave-' + labels[i] + '.wav') # %USERPROFILE%/.keras/keras.json
model = K.models.Model(input, output) K.utils.print_summary(model) model.compile(loss='mean_squared_error', optimizer=opt) tb = K.callbacks.TensorBoard(path) history = model.fit(ipt, tgt, batch_size=tgt.shape[0], epochs=epcs, verbose=1, callbacks=[tb], validation_split=0.0, shuffle=True) save_model(model, path, 'model') predictions = model.predict(xipt) * M # predictions = np.power(predictions, 3) N = predictions.shape[1] // 2 # d = (- np.log(0.1)) / original_len # X = np.arange(0, original_len, 1) # D = np.exp(-d * X) for i, p in enumerate(predictions): real = p[:N] imag = p[N:] w = irfft(real + imag * 1j, original_len) / 2 * original_len # w = np.multiply(D, w) save_wav(w, path + xnames[i] + '.wav')
net = K.models.load_model('03_train/' + piece_name + '/#model.h5', {'scaled_tanh': scaled_tanh}) K = np.genfromtxt("01_tuning_stretch/" + piece_name + "/coefs.csv") st = time.time() # <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> # key = int(key_name) f0 = np.power( 2, (key - 49) / 12) * 440 * (K[0] * key**K[1] * key**2 + K[2] * key + K[3]) key = (key - 1) / 87 ipt = np.array([[key, p] for p in np.linspace(0, 1, partials)]) [F, A, D] = net.predict(ipt) F = (np.ndarray.flatten(F) * (Mf - mf) + mf) * P * f0 A = np.ndarray.flatten(A) * ((Ma - ma) + ma) * 5000 D = np.ndarray.flatten(D) * ((Md - md) + md) # print(A.shape,D.shape,F.shape, P.shape) w = np.zeros(n) for i in range(partials): # print('partial:', i) ph = np.random.uniform(0, 2 * np.pi) w += create_signal(n, fps, F[i], ph, A[i], D[i]) print(time.time() - st) # <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> # save_wav(w, key_name + ".wav")
steps = a.shape[0] X = np.linspace(0 , 1 , steps) Y = 3 * X ** 2 - 2 * X** 3 return a * (1 - Y) + b * Y extended_ipt = [] extended_names = [] for i in np.arange(1, 5 + 0.1, 0.5): for j in np.arange(1, 5 + 0.1, 0.5): for k in np.arange(1, 4 + 0.1, 0.5): name = str(i) + '_' + str(j) + '_' + str(k) extended_names.append(name) extended_ipt.append([i, j, k]) extended_ipt = np.array(extended_ipt, dt) extended_ipt, maxs_ipt, mins_ipt = normalize_cols(extended_ipt) predictions = model.predict(extended_ipt) if overlap == 0: predictions = np.reshape(predictions, (extended_ipt.shape[0],-1)) for i, w in enumerate(predictions): save_wav(w * amp, path + extended_names[i] + '.wav') else: for i, p in enumerate(predictions): w = p[0] for j in range(1, timesteps): x = p[j] b = blend(w[-overlap:], x[:overlap]) w = np.hstack([w[:-overlap], b ,x[overlap:]]) save_wav(w[:-overlap] * amp, path + extended_names[i] + '.wav')
# names = [] # for filename in filenames: # names.append(filename.replace('.wav', '')) # names = np.sort(np.array(names, dtype=np.int)).astype(np.str) names = [] for i in range(1, 88 + 1): names.append(str(i)) A = np.genfromtxt('03_train/' + piece_name + '/amps_over_max_amp.csv', delimiter=',') D = np.genfromtxt('03_train/' + piece_name + '/decays.csv', delimiter=',') I = np.genfromtxt('03_train/' + piece_name + '/Mfreqs_over_Tfreqs.csv', delimiter=',') K = np.genfromtxt("01_tuning_stretch/" + piece_name + "/coefs.csv") for i, name in enumerate(names): k = int(name) print(name) w = np.zeros(n) f0 = np.power( 2, (k - 49) / 12) * 440 * (K[0] * k**K[1] * k**2 + K[2] * k + K[3]) for j in range(partials): p = np.random.uniform(0, 2 * np.pi) f = f0 * (j + 1) * I[i, j] d = D[i, j] a = A[i, j] * 5000 w += create_signal(n, fps, f, p, a, d) save_wav(w, save_path + name + '.wav')