def modify_input_wav(wav, noise, room_dim, max_order, snr_vals, mic_pos): ''' for mono ''' fs_s, audio_anechoic = wavfile.read(wav) fs_n, noise_anechoic = wavfile.read(noise) #Create a room for the signal room_signal = pra.ShoeBox(room_dim, absorption=0.2, fs=fs_s, max_order=max_order) #rCeate a room for the noise room_noise = pra.ShoeBox(room_dim, absorption=0.2, fs=fs_n, max_order=max_order) #source of the signal and of the noise in their respectiv boxes room_signal.add_source([2, 3.1, 2], signal=audio_anechoic) room_noise.add_source([4, 2, 1.5], signal=noise_anechoic) #we add a microphone at the same position in both of the boxes room_signal.add_microphone_array( pra.MicrophoneArray(mic_pos.T, room_signal.fs)) room_noise.add_microphone_array( pra.MicrophoneArray(mic_pos.T, room_noise.fs)) #simulate both rooms room_signal.simulate() room_noise.simulate() #take the mic_array.signals from each room audio_reverb = room_signal.mic_array.signals noise_reverb = room_noise.mic_array.signals #verify the size of the two arrays such that we can continue working on the signal if (len(noise_reverb[0]) < len(audio_reverb[0])): raise ValueError( 'the length of the noise signal is inferior to the one of the audio signal !!' ) #normalize the noise_reverb noise_reverb = noise_reverb[0, :len(audio_reverb[0])] noise_normalized = noise_reverb / np.linalg.norm(noise_reverb) #initialize the noiy_signal noisy_signal = {} #for each snr values create a noisy_signal for the labelling function for snr in snr_vals: noise_std = np.linalg.norm(audio_reverb[0]) / (10**(snr / 20.)) final_noise = noise_normalized * noise_std noisy_signal[snr] = audio_reverb[0] + final_noise return noisy_signal
def simulateSound(room_dim, R_loc, source_locations, source_audios, rt60, materials=None, max_order=None): # source_audios: array of numpy array # L: max of all audios. Zero padding at the end # return (all_channel_data (C, L), groundtruth_with_reverb (N, C, L), groundtruth_data (N, C, L), angles (N) if materials is not None: (ceiling, east, west, north, south, floor) = materials room = pra.ShoeBox( room_dim, fs=fs, materials=pra.make_materials( ceiling=ceiling, floor=floor, east=east, west=west, north=north, south=south, ), max_order=max_order ) else: try: e_absorption, max_order_rt60 = pra.inverse_sabine(rt60, room_dim) except ValueError: e_absorption, max_order_rt60 = pra.inverse_sabine(1, room_dim) room = pra.ShoeBox(room_dim, fs=fs, materials=pra.Material(e_absorption), max_order=max_order_rt60) R = generate_mic_array(R_MIC, N_MIC, R_loc) room.add_microphone_array(pra.MicrophoneArray(R, room.fs)) length = max([len(source_audios[i]) for i in range(len(source_audios))]) for i in range(len(source_audios)): source_audios[i] = np.pad(source_audios[i], (0, length - len(source_audios[i])), 'constant') for i in range(len(source_locations)): room.add_source(source_locations[i], signal=source_audios[i], delay=0) room.image_source_model() premix_w_reverb = room.simulate(return_premix=True) mixed = room.mic_array.signals # groundtruth room_gt = pra.ShoeBox(room_dim, fs=fs, materials=pra.Material(1.0), max_order=0) # R_gt=generate_mic_array(R_MIC, N_MIC, R_loc) R_gt = generate_mic_array(0, 1, R_loc) room_gt.add_microphone_array(pra.MicrophoneArray(R_gt, room.fs)) for i in range(len(source_locations)): room_gt.add_source(source_locations[i], signal=source_audios[i], delay=0) room_gt.compute_rir() room_gt.image_source_model() premix = room_gt.simulate(return_premix=True) return (mixed, premix_w_reverb, premix, R)
def get_random_access_input(self, res, record_name="TIMIT.tfrecords"): writer = tf.python_io.TFRecordWriter(record_name) for counter, base_signal in enumerate(res): print("write in to record, {}".format(counter)) label_signal = base_signal self.room = pra.ShoeBox([20, 20], absorption=1.) R = pra.beamforming.circular_2D_array(center=[10, 10], M=6, radius=self.r, phi0=0 ) R = np.concatenate([R, np.expand_dims(np.asarray([10, 10]), axis=1)], axis=1) self.room.add_microphone_array(pra.MicrophoneArray(R, self.room.fs)) dist = 3 seta = np.random.randint(0, 360) pos_x = dist*np.cos(seta/180*np.pi) pos_y = dist*np.sin(seta/180*np.pi) self.room.add_source(position=[10+pos_x, 10+pos_y], signal=label_signal) self.room.compute_rir() self.room.simulate(snr=None, reference_mic=-1) recv_sig = self.room.mic_array.signals diff_L = int((recv_sig.shape[1] - len(label_signal))/2) recv_sig = tf.cast(recv_sig[:, diff_L:-diff_L], tf.float32).numpy() label_signal = tf.cast(label_signal, tf.float32).numpy() sf.write("haha.wav", recv_sig[:, 0], samplerate=16000) example = tf.train.Example(features=tf.train.Features( feature={ "recv_sig_stft": self._bytes_feature(recv_sig.tostring()), "label_signal_stft": self._bytes_feature(label_signal.tostring()), "local_choice": self._int64_feature(seta), } )) writer.write(example.SerializeToString()) writer.close()
def main_doa(self,az,di,S,c1,f,n,fbm,fbM,r_x,r_y,room_temp,room_humidity,is_airAbsorption,num, angle, raduis, algos,save): # We define a meaningful distance measure on the circle # Location of original source azimuth = az / 180.0 * np.pi distance = di SNR = S # signal-to-noise ratio c = c1 # speed of sound fs = f # sampling frequency nfft = n # FFT size freq_bins = np.arange(fbm, fbM) # FFT bins to use for estimation # compute the noise variance sigma2 = 10 ** (-SNR / 10) / (4.0 * np.pi * distance) ** 2 # Create an anechoic room room_dim = np.r_[r_x, r_y] aroom = pra.ShoeBox(room_dim, fs=fs, max_order=0, sigma2_awgn=sigma2,temperature=room_temp,humidity=room_humidity,air_absorption=is_airAbsorption) # add the source source_location = room_dim / 2 + distance * np.r_[np.cos(azimuth), np.sin(azimuth)] source_signal = np.random.randn((nfft // 2 + 1) * nfft) aroom.add_source(source_location, signal=source_signal) # We use a circular array R = pra.circular_2D_array(room_dim / 2, num, angle,raduis) aroom.add_microphone_array(pra.MicrophoneArray(R, fs=aroom.fs)) # run the simulation aroom.simulate() # Compute the STFT frames needed X = np.array( [ pra.transform.stft.analysis(signal, nfft, nfft // 2).T for signal in aroom.mic_array.signals ] ) for algo_name in algos: # Construct the new DOA object doa = pra.doa.algorithms[algo_name](R, fs, nfft, c=c) # this call here perform localization on the frames in X doa.locate_sources(X, freq_bins=freq_bins) doa.polar_plt_dirac() plt.title(algo_name) # doa.azimuth_recon contains the reconstructed location of the source if algo_name == "CSSM": self.label_62.setText('{:.2f}'.format(float(doa.azimuth_recon / np.pi * 180.0))) self.label_68.setText('{:.2f}'.format(float(circ_dist(azimuth, doa.azimuth_recon) / np.pi * 180.0))) if algo_name == "MUSIC": self.label_56.setText('{:.2f}'.format(float(doa.azimuth_recon / np.pi * 180.0))) self.label_66.setText('{:.2f}'.format(float(circ_dist(azimuth, doa.azimuth_recon) / np.pi * 180.0))) if algo_name == "SRP": self.label_61.setText('{:.2f}'.format(float(doa.azimuth_recon / np.pi * 180.0))) self.label_67.setText('{:.2f}'.format(float(circ_dist(azimuth, doa.azimuth_recon) / np.pi * 180.0))) if algo_name == "TOPS": self.label_64.setText('{:.2f}'.format(float(doa.azimuth_recon / np.pi * 180.0))) self.label_70.setText('{:.2f}'.format(float(circ_dist(azimuth, doa.azimuth_recon) / np.pi * 180.0))) if algo_name == "WAVES": self.label_63.setText('{:.2f}'.format(float(doa.azimuth_recon / np.pi * 180.0))) self.label_69.setText('{:.2f}'.format(float(circ_dist(azimuth, doa.azimuth_recon) / np.pi * 180.0))) if save: plt.savefig("./{}.png".format(algo_name)) if not save: plt.show()
def save_main_beam(self, rs_x, rs_y, sp_x, sp_y, is_circular, mic_num, c_x, c_y, i_m_d, angle, room_frequency, freques, room_temp=None, room_humidity=None, is_airAbsorption=None): # Create a rs_x by rs_y metres shoe box room room = pra.ShoeBox([rs_x, rs_y], fs=room_frequency, temperature=room_temp, humidity=room_humidity, air_absorption=is_airAbsorption) # Add a source somewhere in the room room.add_source([sp_x, sp_y]) # Create a linear array beamformer with 4 microphones # with angle 0 degrees and inter mic distance 10 cm if (is_circular): R = pra.circular_2D_array([c_x, c_y], mic_num, angle, i_m_d) else: R = pra.linear_2D_array([c_x, c_y], mic_num, angle, i_m_d) room.add_microphone_array(pra.Beamformer(R, room.fs)) # Now compute the delay and sum weights for the beamformer room.mic_array.rake_delay_and_sum_weights(room.sources[0][:1]) # plot the room and resulting beamformer room.plot(freq=freques, img_order=0) # plt.show() plt.savefig('./fig.png')
def compute_room_proc(self): #room = rg.generate_from_dict(self.db_setup) min_abs, max_abs = self.db_setup['absorption'] abs_vec = np.random.rand(6) * (max_abs - min_abs) + min_abs keys = ['west', 'north', 'east', 'south', 'ceiling', 'floor'] absorption = dict(zip(keys, abs_vec.T)) room_dim = [5, 4, 6] thisdict = { 'west': 0.1, 'north': 0.4, 'east': 0.2, 'south': 0.1, 'ceiling': 0.5, 'floor': 0.1 } room = pra.ShoeBox( room_dim, absorption=absorption, fs=16000, max_order=15, ) room.add_source([2, 3.1, 2]) room.add_microphone_array( pra.MicrophoneArray(np.array([[2, 1.5, 2]]).T, room.fs)) room.compute_rir() peaks = self.compute_peaks(room) room.peaks = peaks self.output.put(room)
def get_rir(audio_signal, fs, rt60=0.2, room_dim=[60, 60, 10], room_source=[30, 30, 4.5], mic_pos=[30, 10, 7], T=19, D=0.01, S=35): import pyroomacoustics as pra import numpy as np c = 1449.2 + 4.6 * T - 0.055 * T**2 + 0.0029 * T**3 + (1.34 - 0.01 * T) * ( S - 35) + 0.016 * D e_absorption, max_order = pra.inverse_sabine(rt60, room_dim, c=c) room = pra.ShoeBox(room_dim, fs=fs, materials=pra.Material(e_absorption), ray_tracing=False, max_order=3, air_absorption=False) room.add_source(room_source, signal=audio_signal, delay=1.0) mic_locs = np.c_[mic_pos, # mic 1 ] room.add_microphone_array(mic_locs) room.compute_rir() rir = room.rir[0][0] return rir
def main(): # define room room_material = pra.Material(energy_absorption=0.96, scattering=None) room_dim = [10, 10, 5] room = pra.ShoeBox(room_dim, fs=16000, materials=room_material, max_order=4, ray_tracing=True, air_absorption=True) # define obstacle obstacle_faces = make_polygon([3, 3, 2.5], 1, 4, 4, [0, 0, 0.78]) obstacle_material = pra.Material(energy_absorption=0.2, scattering=0.1) add_obstacle(room, obstacle_faces, obstacle_material) obstacle_faces = make_cylinder([6, 4, 2.5], 1.4, 5, [1.57, 0, 0]) add_obstacle(room, obstacle_faces, obstacle_material) # define mics and sources room.add_source([1.7, 3, 1.]) room.add_microphone([1.7, 3, 1.1]) room.image_source_model() room.plot(img_order=1) plt.gca().set_xlim3d(left=-2, right=11) plt.gca().set_ylim3d(bottom=-2, top=11) plt.gca().set_zlim3d(bottom=-2, top=11) plt.show() room.compute_rir() room.plot_rir() plt.show()
def simulate_k_room(clean, noise, cfg): n_mics = cfg['num_mics'] room_size = [cfg['room_size']] * 3 room = pra.ShoeBox(room_size, fs=cfg['fs'], absorption=0.35, max_order=10) speech_loc = np.array([cfg['room_size'] - 1] * 3) noise_loc = np.array([1] * 3) room.add_source(speech_loc, signal=clean, delay=0) room.add_source(noise_loc, signal=noise, delay=0) mic_locs = np.zeros((3, n_mics)) mic_locs[:, :cfg['n_noise_mics']] = noise_loc[:, None] + \ np.random.normal(size=(3, cfg['n_noise_mics'])) mic_locs[:, cfg['n_noise_mics']:] = speech_loc[:, None] + \ np.random.normal(size=(3, cfg['n_speech_mics'])) mic_locs[mic_locs > cfg['room_size']] = cfg['room_size'] mic_locs[mic_locs < 0] = 0 mic_array = pra.MicrophoneArray(mic_locs, room.fs) room.add_microphone_array(mic_array) res = room.simulate(return_premix=True) clean_ref = res[0, 0] noise_ref = res[1, 0] return clean_ref.T, noise_ref.T, res.sum(0).T
def test_add_source_mic_obj(): room = pra.ShoeBox(room_size) source0 = pra.SoundSource(source_loc0, signal=sig) source1 = pra.SoundSource(source_loc1, signal=sig) mic_array0 = pra.MicrophoneArray(np.c_[mic0], fs=room.fs) mic_array1 = pra.MicrophoneArray(np.c_[mic1], fs=room.fs) room.add(source0).add(mic_array0) assert len(room.sources) == 1 assert np.allclose(room.sources[0].position, source_loc0) assert len(room.mic_array) == 1 assert room.mic_array.R.shape == (3, 1) assert np.allclose(room.mic_array.R[:, 0], mic0) room.add(mic_array1).add(source1) assert len(room.sources) == 2 assert np.allclose(room.sources[1].position, source_loc1) assert len(room.mic_array) == 2 assert np.allclose(room.mic_array.R[:, 0], mic0) assert np.allclose(room.mic_array.R[:, 1], mic1) assert room.mic_array.R.shape == (3, 2)
def generate_multiband_rirs(x, y, z, mic_pos, source_pos, fs, max_order, abs_coeffs): warnings.filterwarnings("ignore", category=FutureWarning) mic_array = pra.MicrophoneArray(mic_pos.T, fs=fs) multiband_rir_batch = np.zeros([len(mic_pos), fs // 2]) center_freqs = [125, 250, 500, 1000, 2000, 4000, 4000 * np.sqrt(2)] for i in range(7): coeffs = get_absorption_by_index(abs_coeffs, i) room = pra.ShoeBox([x, y, z], fs=fs, max_order=max_order, absorption=coeffs) room.add_source(source_pos) room.add_microphone_array(mic_array) room.compute_rir() rir_batch = [] for j, rir in enumerate(room.rir): rir = rir[0] if (i < 6): rir = ac.signal.octavepass(rir, center_freqs[i], fs, 1, order=32) else: rir = ac.signal.highpass(rir, center_freqs[i], fs, order=32) rir_batch.append(rir[:fs // 2]) multiband_rir_batch += np.array(rir_batch) return multiband_rir_batch
def main(): # define room room_material = pra.Material(energy_absorption=0.1, scattering=None) room_dim = [17, 6, 6] room = pra.ShoeBox(room_dim, fs=16000, materials=room_material) # define pipe pipe_center = np.array(room_dim) / 2 pipe_faces = pra_utils.make_polygon(pipe_center, 1, 15, 4, [0, 1.57, 0]) pipe_material = pra.Material(energy_absorption=0.2, scattering=0.1) pra_utils.add_obstacle(room, pipe_faces, pipe_material) # define mics and sources room.add_source([5, 3, 3]) room.add_microphone([5.05, 3, 3.]) room.image_source_model() room.plot(img_order=1) plt.gca().set_xlim3d(left=-1, right=18) plt.gca().set_ylim3d(bottom=-1, top=18) plt.gca().set_zlim3d(bottom=-1, top=18) plt.show() room.compute_rir() room.plot_rir() plt.show()
def test_issue_115_ism_breaking(): """ When a source was too close to the microphone, the time-of-flight might be smaller than the delay due to the fractionnal delay filter used to create the impulse response. It is then necessary to add this delay to the rir filter to ensure no runtime error. """ print("Test with source close to microphone.") shoebox = pra.ShoeBox( [9.29447785567344, 6.529510207957697, 4.4677460263160995], materials=pra.Material(energy_absorption=0.1675976883006225), fs=16000, max_order=17, ) source_loc = [5.167674641605016, 4.379726875714017, 2.9190423403507504] shoebox.add_source(source_loc) noise_loc = [8.47420884677372, 5.675261722911696, 1.2040578622058364] shoebox.add_source(noise_loc) R = np.array([[8.571318246865648], [5.799718630723678], [1.3702254938278977]]) print( "mic - source distance : {} m".format( np.sqrt(sum((np.array(source_loc) - np.squeeze(R)) ** 2)) ) ) print( "mic - noise distance : {} m".format( np.sqrt(sum((np.array(noise_loc) - np.squeeze(R)) ** 2)) ) ) shoebox.add_microphone_array(pra.MicrophoneArray(R, shoebox.fs)) shoebox.compute_rir()
def gen_args(parameters): import pyroomacoustics as pra room = pra.ShoeBox(parameters['room_dim']) device_locations = np.array(parameters['device_locations']).T n_dim = device_locations.shape[0] n_sources = parameters['n_sources'] assert n_dim == 2, 'Simulation only supports 2D for now' gain_range = np.array(parameters['source_gain_range']) def map_range(x, I): return x * (I[1] - I[0]) + I[0] args = [] for epoch in range(parameters['n_loops']): source_gains_db = map_range(np.random.rand(n_sources), gain_range) source_locations = np.zeros((n_dim, n_sources)) for i in range(n_sources): success = False while not success: theta = np.random.rand() * 2 * np.pi p = np.array([np.cos(theta), np.sin(theta)]) candidate = device_locations[:, i] + p + np.random.randn(2) * 0.2 success = room.is_inside(candidate) source_locations[:, i] = candidate args.append([source_locations.T.tolist(), source_gains_db.tolist()]) return args
def test_issue_22(): np.random.seed(0) n_mics = 1 n_src = 1 n_times = 25000 dim = 3 mic_pos = np.random.rand(dim, n_mics) abs_coeff = 0.1 fs = 16000 wall_max_len = 15 room_dim = np.random.rand(dim) * wall_max_len shoebox = pyroomacoustics.ShoeBox(room_dim, absorption=abs_coeff, fs=fs, max_order=0) src_pos = np.random.rand(dim, n_src) * room_dim[:, None] for src in src_pos.T: shoebox.add_source(src) shoebox.add_microphone_array(pyroomacoustics.MicrophoneArray(mic_pos, fs)) for i in range(n_times): shoebox.image_source_model() if i != 0 and i % 1000 == 0: print(i)
def test_z(self): # create room mic_loc = [12, 5, 5] source_loc = [2, 5, 5] room = ( pra.ShoeBox(p=[14, 10, 10], max_order=1) .add_source(source_loc) .add_microphone([12, 5, 5]) ) # compute image sources room.image_source_model() # compute azimuth_s and colatitude_s pair for images along z-axis source_angle_array = pyroomacoustics.directivities.source_angle_shoebox( image_source_loc=room.sources[0].images, wall_flips=abs(room.sources[0].orders_xyz), mic_loc=mic_loc, ) source_angle_array = np.array(source_angle_array) z1_idx = np.where( room.sources[0].images[2] == source_loc[2] - 2 * source_loc[2] )[0][0] z2_idx = np.where( room.sources[0].images[2] == source_loc[2] + 2 * source_loc[2] )[0][0] np.testing.assert_almost_equal( source_angle_array[:, z1_idx], [0, 3 * np.pi / 4] ) np.testing.assert_almost_equal(source_angle_array[:, z2_idx], [0, np.pi / 4])
def get_rir(echoic=False): """Get the room impulse response (RIR). Args: echoic (bool): if use an echoic environment. Returns: ret (numpy.ndarray): A 3-dimensional array that ret[m][s] is the RIR from the s-th source to the m-th microphone. """ absorption = 0.2 if echoic else 0.66 room = pra.ShoeBox([6, 5, 3], fs=16000, absorption=absorption, max_order=32) mic_coordinates = np.c_[[2.0, 1.95, 1.5], [2.0, 2.05, 1.5]] mic_array = pra.MicrophoneArray(mic_coordinates, room.fs) room.add_microphone_array(mic_array) room.add_source([2.69, 2.40, 1.75]) room.add_source([2.61, 1.57, 1.75]) room.compute_rir() ret = room.rir min_len = min(len(ret[0][0]), len(ret[0][1]), len(ret[1][0]), len(ret[1][1])) for m, s in product(range(2), range(2)): ret[m][s] = ret[m][s][:min_len] return ret
def test_issue_115_rt_breaking(): """ As background, only ray tracing only starts to be active for rays that run longer than the maximum ISM order. The problem happen when the ISM order is very high (here 17), then, in some circumstances, it is possible that no ray travels longer than that. Then the histogram is empty and an error happen. """ print("Test with high order ISM") shoebox = pra.ShoeBox( [4.232053263716528, 3.9244954007318853, 5.563810437305445], materials=pra.Material(energy_absorption=0.6965517438548237), fs=16000, max_order=17, ray_tracing=True, ) source_loc = [1.2028020579854695, 2.2980760894630676, 2.0654520390433984] shoebox.add_source(source_loc) R = np.array([[1.8062807887952617], [2.7793113278109454], [1.42966428606882]]) print( "mic - source distance : {} m".format( np.sqrt(sum((np.array(source_loc) - np.squeeze(R)) ** 2)) ) ) shoebox.add_microphone_array(pra.MicrophoneArray(R, shoebox.fs)) shoebox.compute_rir()
def simroom(room_dim, src_loc, mic_locs): parser = argparse.ArgumentParser( description= "Simulates and adds reverberation to a dry sound sample. Saves it into `./examples/samples`." ) parser.add_argument( "--method", "-m", choices=methods, default=methods[0], help="Simulation method to use", ) args = parser.parse_args() # The desired reverberation time and dimensions of the room rt60_tgt = 0.3 # seconds # meters # import a mono wavfile as the source signal # the sampling frequency should match that of the room fs, audio = wavfile.read("examples/samples/guitar_16k.wav") # We invert Sabine's formula to obtain the parameters for the ISM simulator e_absorption, max_order = pra.inverse_sabine(rt60_tgt, room_dim) # Create the room room = pra.ShoeBox(room_dim, fs=fs, materials=pra.Material(e_absorption), max_order=max_order) room.add_source(src_loc, signal=audio, delay=0.5) # finally place the array in the room room.add_microphone_array(mic_locs) # Run the simulation (this will also build the RIR automatically) room.simulate() room.mic_array.to_wav( "examples/samples/guitar_16k_reverb_{}.wav".format(args.method), norm=True, bitdepth=np.int16, ) """ detect_peaks(room.mic_array.signals[0, :], mph=0, mpd=1000, threshold=10, show=True) detect_peaks(room.mic_array.signals[1, :], mph=0, mpd=1000, threshold=10, show=True) detect_peaks(room.mic_array.signals[2, :], mph=0, mpd=1000, threshold=10, show=True) print(max(room.mic_array.signals[0, :])) print(max(room.mic_array.signals[1, :])) print(max(room.mic_array.signals[2, :])) """ return np.array([ max(room.mic_array.signals[0, :]), max(room.mic_array.signals[1, :]), max(room.mic_array.signals[2, :]) ])
def __init__(self, room_dimensions, fs, absorption_factor, mic_location_array): self.room = pra.ShoeBox(room_dimensions, fs=fs, absorption=absorption_factor, max_order=3) self.add_microphones(mic_location_array, fs)
def test_rt60_theory_multi_band(): # Create the room room = pra.ShoeBox(room_dim, fs=fs, materials=pra.Material("curtains_cotton_0.5"),) # run the different rt60 functions room.rt60_theory(formula="sabine") room.rt60_theory(formula="eyring")
def compute_rir(order): fromPos = np.zeros((3)) toPos = np.ones((3, 1)) roomSize = np.array([3, 3, 3]) room = pra.ShoeBox(roomSize, fs=1000, absorption=0.95, max_order=order) room.add_source(fromPos) mics = pra.MicrophoneArray(toPos, room.fs) room.add_microphone_array(mics) room.compute_rir()
def test_trinicon(): ''' This test do not check the correctness of the output of Trinicon. Only that it runs without errors. ''' # STFT frame length L = 1024 # Room 8m by 9m room_dim = [8, 9] # source location source = np.array([1, 4.5]) # create the room with sources and mics room = pra.ShoeBox(room_dim, fs=16000, max_order=0, sigma2_awgn=1e-8) # get signals signals = [ np.concatenate( [wavfile.read(f)[1].astype(np.float32) for f in source_files]) for source_files in wav_files ] delays = [1., 0.] locations = [[2.5, 3], [2.5, 6]] # add mic and good source to room # Add silent signals to all sources for sig, d, loc in zip(signals, delays, locations): room.add_source(loc, signal=np.zeros_like(sig), delay=d) # add microphone array room.add_microphone_array( pra.MicrophoneArray(np.c_[[6.5, 4.39], [6.5, 4.51]], fs=room.fs)) # compute RIRs room.compute_rir() # Record each source separately separate_recordings = [] for source, signal in zip(room.sources, signals): source.signal[:] = signal room.simulate() separate_recordings.append(room.mic_array.signals) source.signal[:] = 0. separate_recordings = np.array(separate_recordings) # Mix down the recorded signals mics_signals = np.sum(separate_recordings, axis=0) # run Trinicon y, w = pra.bss.trinicon(mics_signals, filter_length=L, return_filters=True)
def empty_room(outpath): room = pra.ShoeBox([2.5, 3, 4], materials=pra.Material(0.2, 0.34), air_absorption=True) # pretty print room dict pprint(create_room_dict(room), sort_dicts=False) # dump room into a yaml dump_room(room, outpath)
def create_mix(filename=None): # Room 4m by 6m room_dim = [8, 9] # create an anechoic room with sources and mics room = pra.ShoeBox(room_dim, fs=16000, max_order=0) # get signals signals = [ np.concatenate( [wavfile.read(f)[1].astype(np.float32) for f in source_files]) for source_files in wav_files ] delays = [1.0, 0.0] locations = [[2.5, 3], [2.5, 6]] # add mic and good source to room # Add silent signals to all sources for sig, d, loc in zip(signals, delays, locations): room.add_source(loc, signal=sig, delay=d) # add microphone array room.add_microphone_array( pra.MicrophoneArray(np.c_[[6.5, 4.47], [6.5, 4.53], [6.5, 4.59]], fs=room.fs)) # compute RIRs room.compute_rir() def callback_mix(premix): sigma_s = np.std(premix[:, premix.shape[1] // 2, :], axis=1) premix /= sigma_s[:, None, None] premix[1:] *= 0.01 # make secondary sources quieter mix = np.sum(premix, axis=0) scale = np.maximum(np.max(np.abs(premix)), np.max(np.abs(mix))) mix *= 0.95 / scale premix *= 0.95 / scale return mix # Run the simulation separate_recordings = room.simulate(callback_mix=callback_mix, return_premix=True) mics_signals = room.mic_array.signals if filename is not None: wavfile.write(filename, room.fs, (mics_signals.T * (2**15)).astype(np.int16)) return mics_signals, separate_recordings, len(locations)
def DAB_generate(source_audio, out_folder, name): shoebox = pra.ShoeBox( room_dimensions, absorption=wall_absorption, fs=fs, max_order=15, ) # number of microphones M = 4 source_position = np.array([ random.uniform(0, room_dimensions[0]), random.uniform(0, room_dimensions[1]) ]) distances = np.random.randint(1, 20, M) mic_pos = [] for m in range(M): mic_distance = distances[m] mic_m = guess_microphone( source_position, mic_distance ) # random way: guess microphone position until it's in the room: very long time for small rooms mic_pos.append(mic_m) out_mic_file = os.path.join(out_folder, 'log_%s.txt' % name) if os.path.exists(out_mic_file): os.remove(out_mic_file) f1 = open(out_mic_file, 'w') for l in range(M): f1.write("%s, %f\n" % (str(mic_pos[l]), distances[l])) Lg_t = 0.100 # filter size in seconds Lg = np.ceil(Lg_t * fs) # in samples fft_len = 512 mics = pra.Beamformer(np.asarray(mic_pos).T, shoebox.fs, N=fft_len, Lg=Lg) shoebox.add_source(source_position, signal=source_audio) shoebox.add_microphone_array(mics) shoebox.compute_rir() shoebox.simulate() # ADDING NOISE AND SAVING for n in range(M): signal = np.asarray(shoebox.mic_array.signals[n, :], dtype=float) signal = pra.utilities.normalize(signal, bits=16) mixed_signal = add_noise(source_audio, signal) mixed_signal = np.array(mixed_signal, dtype=np.int16) mixed_file = os.path.join(out_folder, 'mix%d_%s' % (n, name)) pp.write_audio(mixed_file, mixed_signal, fs)
def room_simulate(num_mic, mic_array, room_type): room_list = { 'star_3': [8.3, 3.4, 2.5], 'room_819': [7.9, 7.0, 2.7], 'room_409': [7.0, 4.2, 2.7] } room = room_list[room_type] dim_x, dim_y, dim_z = room[0], room[1], room[2] sr = 16000 rt60 = 0.3 e_absorption, max_order = pra.inverse_sabine(rt60, [dim_x, dim_y, dim_z]) print(e_absorption, max_order) num_direction = 12 mic_radius = 0.04 #0.03231 testing #mic_radius = np.random.uniform(low=0.025,high=0.035) mic_x_radius = 0.0637 mic_y_radius = 0.0484 mic_lin = 0.04 room = pra.ShoeBox(room, fs=sr, materials=pra.Material(e_absorption), max_order=max_order) mic_center = np.array([dim_x / 2, dim_y / 2, 0.69]) thetas = np.arange(num_mic) / num_mic * 2 * np.pi theta_source = np.arange(num_direction) / num_direction * 2 * np.pi if mic_array == 'circle': center_to_mic = np.stack( [np.cos(thetas), np.sin(thetas), np.zeros_like(thetas)], 0) * mic_radius elif mic_array == 'ellipse': center_to_mic = np.stack([ mic_x_radius * np.cos(thetas), mic_y_radius * np.sin(thetas), np.zeros_like(thetas) ], 0) elif mic_array == 'linear': linear = np.arange(num_mic) * mic_lin linear = linear - np.max(linear) / 2 center_to_mic = np.stack( [linear, np.zeros_like(linear), np.zeros_like(linear)], 0) mic_positions = mic_center[:, None] + center_to_mic room.add_microphone_array(mic_positions) far_field_distance = 1 thetas = np.arange(num_direction) / num_direction * 2 * np.pi center_to_source = np.stack([ np.cos(theta_source), np.sin(theta_source), np.zeros_like(theta_source) ], -1) * far_field_distance source_positions = mic_center[None, :] + center_to_source return room, source_positions
def get_room_add_method(): shoebox = pra.ShoeBox( room_dim, materials=pra.Material(e_abs), fs=fs, max_order=max_order ) shoebox.add_source(source_position) mics = pra.MicrophoneArray(np.array([mic_position]).T, fs) shoebox.add_microphone_array(mics) shoebox.image_source_model() shoebox.compute_rir() return shoebox
def beamformed_das(comb, people_num, sr=16000): f1 = comb[0] f2 = comb[1] # def beamformed_das(f1, f2, people_num, sr=16000): f1_data = f1['data'] f2_data = f2['data'] signal_len = len(f1['data']) distance = 1.5 # azimuth = np.array([math.atan2(1.5, 0.5), math.atan2(1.5, -0.5)]) azimuth = np.array([ 90., 270., ]) * np.pi / 180 # centre = [2, 1.5] room_dim = np.r_[4, 6] room = pra.ShoeBox(room_dim, fs=sr) echo = pra.linear_2D_array(center=(room_dim / 2), M=5, phi=0, d=0.5) echo = np.concatenate((echo, np.array((room_dim / 2), ndmin=2).T), axis=1) mics = pra.Beamformer(echo, room.fs) room.add_microphone_array(mics) # room.add_source(np.array([1.5, 4.5]), delay=0., signal=f1_data) # room.add_source(np.array([2.5, 4.5]), delay=0., signal=f2_data[:len(f1_data)]) signals = [f1_data, f2_data] for i, ang in enumerate(azimuth): source_location = room_dim / 2 + distance * np.r_[np.cos(ang), np.sin(ang)] source_signal = signals[i] room.add_source(source_location, signal=source_signal[:signal_len], delay=0) mics.rake_delay_and_sum_weights(room.sources[0][:1]) # room.plot(freq=[300, 400, 500, 1000, 2000, 4000], img_order=0) # plt.show() # ax.legend(['300', '400', '500', '1000', '2000', '4000']) # fig.set_size_inches(20, 8) room.compute_rir() room.simulate() filename = 'beamformeded_%05d-%05d' % (f1['filename'], f2['filename']) + '.wav' with open(TXT_PATH + 'build_beamformeded.txt', 'a') as f: f.write(filename) f.write('\n') for i in range(5): wavfile.write(MICS_PATH + 'mic%d/' % (i + 1) + filename, sr, room.mic_array.signals[i, :])
def mic_rever_generator(room_size, target_location, target, fs, microphone_array, amplifier, absorption_value): ''' This function is used to implement single source microphone array reverberation speech generator. Usage: mic_rever_generator(room_size, target_location, target, fs, microphone_array, amplifier, absorption_value) room_size - the size of room [length, width, high] target_location - the location of target speech [x, y, z] target - the array of target speech file fs - sampling frequency microphone_array - the location of microphone array amplifier - the multiple of microphone's built-in amplifier absorption_value - absorption value of room wall Example call: clean_rever = mic_rever_generator(room_size, target_location, target, fs, microphone_array, amplifier, absorption_value) References: mircophone array speech generator release 0.1 Author: Rui Cheng ''' # create the room room = pra.ShoeBox(room_size, fs=fs, absorption=absorption_value, max_order=17) room.add_source(target_location, signal=target, delay=0) #room.add_source([3.5, 3.0, 1.76], signal=interf[:len(target)], delay=0) # add microphone array R = microphone_array fft_len = 512 Lg_t = 0.100 Lg = np.ceil(Lg_t * room.fs) mic_array = pra.Beamformer(R, room.fs, N=fft_len, Lg=Lg) room.add_microphone_array(mic_array) # create the room impulse response # compute image sources room.image_source_model(use_libroom=True) # microphone speech room.simulate() # clean speech in each channel clean_rever = amplifier * room.mic_array.signals.astype("int16") # return return clean_rever