if __name__ == "__main__": if len(sys.argv) < 5: print( "Usage: python3 gen_tone.py <type> <frequency> <volume> <duration> <samp_rate>" ) print("type:\t\tsine, square, sawtooth;") print("frequency:\tnumber;") print("volume:\t\tnumber from 0 to 100;") print("duration:\tnumber in seconds;") print("samp_rate:\tsampling rate in Hertz.") sys.exit() if sys.argv[0].lower() not in func_map.keys(): print("type must be sine, square, or sawtooth") # Give arguments names signal_func = func_map[sys.argv[0].lower()] freq = sys.argv[1] vol = sys.argv[2] duration = sys.argv[3] samp_rate = sys.argv[4] # Generate signal sig = signal_func(freq=freq, samp_rate=samp_rate, duration=duration) data = sig.data wav.write(f"audio/{sig}", data, samp_rate=samp_rate, amp_perc=vol / 100) print(f"File written to 'audio/{sig}.wav'")
# # Computation # if not os.path.exists(audio_out_folder): os.mkdir(audio_out_folder) # Load stereo audios source_root_abs = os.path.abspath(source_root) source_file_dict = af.id_filename_dict(source_root_abs, ids=ids_to_test, regexes=[f"{i}-stereo.wav" for i in ids_to_test], sort_function=lambda f: f) # Load truth files for idx in ids_to_test: print("working on", idx) # Load stereo recording stereo_signal = librosa.load(f"{source_root}/{source_file_dict[idx][0]}", sr=samp_rate, mono=False)[0] # Decompose with ICA transformer = FastICA(n_components=2, random_state=0) est_signals = transformer.fit_transform(stereo_signal.T).T # Write to file for i in range(est_signals.shape[0]): awav.write(f"ica_audio/{idx}-ica-{i}", est_signals[i], samp_rate=samp_rate)
recon_fig = FigData(xs=trec, ys=xrec, title="Reconstructed after filtering:\n" f"{scale_name} minor triads w/ inversions", plot_type="plot", xlabel="Time (s)", ylabel="Amplitude") # # Display results # aplot.single_subplots( grid_size=(2, 2), fig_data={ (0, 1): stft_fig, (1, 1): stftf_fig, (0, 0): signal_fig, (1, 0): recon_fig }, individual_figsize=(6, 4), savefig_path= f"ISTFT_{window_name}_{window_length}_{int(overlap_percent*100)}%_Pure_{scale_name}_minor_chord_sampr={samp_rate}" ) wav.write( f"audio/ISTFT_{window_name}_{window_length}_{int(overlap_percent * 100)}%_Pure_{scale_name}m", np.concatenate((nst.data, xrec)), samp_rate=samp_rate)
f"Separated source '{sep_names[i]}'\nat d={ds[i]} with width H={Hs[i]}", yscale="linear", ylim=ylim) fig_data[(2, i + 1)] = recon_stft_figs[i] # Plot null/peaks, src & source STFTs aplot.single_subplots( grid_size=(3, 4), fig_data=fig_data, individual_figsize=(6, 4), # savefig_path=f"ADRess_{channel}_{tech_name}.png" ) # # Output audio # wav.write(f"audio/{name}_stereo", src.T, samp_rate=samp_rate) wav.write(f"audio/{name}_left", left_signal, samp_rate=samp_rate) wav.write(f"audio/{name}_right", right_signal, samp_rate=samp_rate) for i, left_recon in enumerate(left_recons): wav.write(f"audio/{tech_name}_{short_sep_names[i]}", left_recon, samp_rate=samp_rate) for i, right_recon in enumerate(right_recons): wav.write(f"audio/{tech_name}_{short_sep_names[i + len(left_recons)]}", right_recon, samp_rate=samp_rate)
# Out of range or index not in whitelist, move on if n == -1 or idx not in whitelist: continue # Load the mono file mono = librosa.load(f"{dir_name}/{mono_filename}", sr=samp_rate, mono=True)[0] # Decompose the spectrogram with NMF S = np.abs(librosa.stft(mono))**2 comps, acts = librosa.decompose.decompose(S, n_components=n) # Reconstruct using each basis for i in range(n): ind_comp = np.zeros(comps.shape) ind_comp[i, :] = comps[i, :] ind_act = np.zeros(acts.shape) ind_act[:, i] = ind_act[:, i] recon_spectrogram = comps @ acts recon = librosa.istft(recon_spectrogram) # Write file wav.write( f"nmf_audio/{idx}-{i}.wav", recon, samp_rate=samp_rate, # auto_timestamp=True )
# Plot the frequency-azimuth spectrogram at the specified time frame aplot.single_subplots(grid_size=(2, 2), fig_data={(0, 0): l_azi_null_fig, (1, 0): l_azi_peak_fig, (0, 1): l_stft_src_fig, (1, 1): l_stft_rec_fig, }, individual_figsize=(6, 4), savefig_path=f"ADRess_left_{tech_name}.png") aplot.single_subplots(grid_size=(2, 2), fig_data={(0, 0): r_azi_null_fig, (1, 0): r_azi_peak_fig, (0, 1): r_stft_src_fig, (1, 1): r_stft_rec_fig, }, individual_figsize=(6, 4), savefig_path=f"ADRess_right_{tech_name}.png") # Output audio file for i, chord in enumerate(chord_names): wav.write(f"audio/true_{chord}M", true_sigs[i].data, samp_rate=samp_rate) wav.write(f"audio/{name}_stereo", src.T, samp_rate=samp_rate) wav.write(f"audio/{name}_left", lsig.data, samp_rate=samp_rate) wav.write(f"audio/{name}_right", rsig.data, samp_rate=samp_rate) wav.write(f"audio/{est_method}_{name}_{window}_{nperseg}_{noverlap}_sep1", left_recon, samp_rate=samp_rate) wav.write(f"audio/{est_method}_{name}_{window}_{nperseg}_{noverlap}_sep2", right_recon, samp_rate=samp_rate)
# # Generate data # signals = [ ss.SineSignal(freq=freqs[i], duration=durations[i], chop_range=chop_ranges[i]) for i in range(len(freqs)) ] nst = nsts.NonStationarySignal(signals) # # Encapsulate data in FigData # fig = FigData(xs=nst.samp_nums, ys=nst.data, title="Non stationary signal", plot_type="plot", xlabel="Time (s)", ylabel="Amplitude") # # Plot # aplot.single_plot(fig) # # Output audio # wav.write("audio/nonst8", nst, dtype=np.uint8)
aplot.single_subplots(grid_size=(4, 2), fig_data={(0, 0): fig_data[(3, 0)], (0, 1): fig_data[(4, 0)], (1, 0): fig_data[(3, 1)], (1, 1): fig_data[(4, 1)], (2, 0): fig_data[(3, 2)], (2, 1): fig_data[(4, 2)]}, individual_figsize=(4.8, 3), savefig_path=f"ADRess_{tech_name}_RECON.png", show=False ) # # Output audio # wav.write(f"audio/{name}_stereo", src.T, samp_rate=samp_rate) wav.write(f"audio/{name}_left", left_signal, samp_rate=samp_rate) wav.write(f"audio/{name}_right", right_signal, samp_rate=samp_rate) # True audio at each stereo position for pos, audio in true_signals.items(): wav.write(f"audio/{name}_pos={pos}", audio, samp_rate=samp_rate) # Reconstructed audio for i, recon in enumerate(recons): wav.write(f"audio/{tech_name}_{short_sep_names[i]}", recon, samp_rate=samp_rate, auto_timestamp=True )
_, recons, extra = adress.adress_stereo(left_signal=left_signal, right_signal=right_signal, samp_rate=samp_rate, ds=ds, Hs=Hs, beta=beta, window=window, nperseg=nperseg, noverlap=noverlap, # print_options=["progress"], more_outputs=["stfts", "stft_f", "stft_t"]) # Write audio: # Pan-mixed audio if write_pan_mix: wav.write(f"audio/{idx}-stereo", src.T, samp_rate=samp_rate) # Reconstructed audio for i, recon in enumerate(recons): wav.write(f"audio/{idx}-d={ds[i]}-H={Hs[i]}", recon, samp_rate=samp_rate, # auto_timestamp=True ) # Grab the other specified quantities t = extra["stft_t"] f = extra["stft_f"] left_stft = extra["stfts"]["left"] right_stft = extra["stfts"]["right"] # Compute the null/peak spectrogram for each specified tau nulls = []