Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_pseudo_intensity(test_jobs):
fs = 44100
n_samples = 10000
ambi_b = spa.sig.AmbiBSignal([np.random.randn(n_samples),
np.random.randn(n_samples),
np.random.randn(n_samples),
np.random.randn(n_samples)], fs=fs)
azi_r, colat_r, r_r = spa.sdm.pseudo_intensity(ambi_b, jobs_count=1)
azi_t, colat_t, r_t = spa.sdm.pseudo_intensity(ambi_b,
jobs_count=test_jobs)
assert_allclose([azi_t, colat_t, r_t], [azi_r, colat_r, r_r])
return azi_r, colat_r, r_r
axs[3].set_title("binaural ALLRAP2")
axs[4].plot(l_nls_ir)
axs[4].plot(r_nls_ir)
axs[4].set_title("binaural NLS")
for ax in axs:
ax.grid(True)
plt.tight_layout()
# Listen to some
s_in = sig.MonoSignal.from_file('../data/piano_mono.flac', fs)
s_in.trim(2.6, 6)
s_out_vbap = sig.MultiSignal(2*[s_in.signal], fs=fs)
s_out_vbap = s_out_vbap.conv([l_vbap_ir, r_vbap_ir])
s_out_allrap = sig.MultiSignal(2*[s_in.signal], fs=fs)
s_out_allrap = s_out_allrap.conv([l_allrap_ir, r_allrap_ir])
s_out_allrap2 = sig.MultiSignal(2*[s_in.signal], fs=fs)
s_out_allrap2 = s_out_allrap2.conv([l_allrap2_ir, r_allrap2_ir])
s_out_hrir = sig.MultiSignal(2*[s_in.signal], fs=fs)
s_out_hrir = s_out_hrir.conv([hrirs.nearest_hrirs(src_azi, src_colat)[0],
hrirs.nearest_hrirs(src_azi, src_colat)[1]])
if LISTEN:
print("input")
s_in.play()
print("hrir")
s_out_hrir.play()
print("vbap")
from spaudiopy import IO, sig, sdm, plots, utils, decoder
LISTEN = True
# 5.0+4 Setup
ls_dirs = np.array([[0, -30, 30, 110, -110, -30, 30, 110, -110],
[0, 0, 0, 0, 0, 45, 45, 45, 45]])
ls_x, ls_y, ls_z = utils.sph2cart(utils.deg2rad(ls_dirs[0, :]),
utils.deg2rad(90 - ls_dirs[1, :]))
ls_setup = decoder.LoudspeakerSetup(ls_x, ls_y, ls_z)
ls_setup.show()
# Load SH impulse response
ambi_ir = sig.MultiSignal.from_file('../data/IR_Gewandhaus_SH1.wav')
# convert to B-format
ambi_ir = sig.AmbiBSignal.sh_to_b(ambi_ir)
fs = ambi_ir.fs
# - SDM Encoding:
sdm_p = ambi_ir.W
sdm_azi, sdm_colat, _ = sdm.pseudo_intensity(ambi_ir, f_bp=(100, 5000))
# Show first 10000 samples DOA
plots.doa(sdm_azi[:10000], sdm_colat[:10000], fs, p=sdm_p[:10000])
# - SDM Decoding:
# very quick stereo SDM decoding. This is only for testing!
ir_st_l, ir_st_r = sdm.render_stereo_sdm(sdm_p, sdm_azi, sdm_colat)
s_pos = np.array(utils.sph2cart(sdm_azi, sdm_colat)).T
ls_gains = decoder.nearest_loudspeaker(s_pos, ls_setup)
assert len(ls_gains) == len(sdm_p)
ir_ls_l, ir_ls_r = sdm.render_binaural_loudspeaker_sdm(sdm_p, ls_gains,
ls_setup, fs)
# Render some examples
s_in = sig.MonoSignal.from_file('../data/piano_mono.flac', fs)
s_in.trim(2.6, 6)
# Convolve with the omnidirectional IR
s_out_p = s_in.copy()
s_out_p.conv(sdm_p)
# Convolve with the stereo SDM IR
s_out_SDM_stereo = sig.MultiSignal([s_in.signal, s_in.signal], fs=fs)
s_out_SDM_stereo.conv([ir_st_l, ir_st_r])
# Convolve with the loudspeaker SDM IR
s_out_SDM_ls = sig.MultiSignal([s_in.signal, s_in.signal], fs=fs)
s_out_SDM_ls.conv([ir_ls_l, ir_ls_r])
if LISTEN:
print("input")
s_in.play()
print("output: Omni")
s_out_p.play(gain=0.5/np.max(sdm_p))
print("output: Stereo SDM")
s_out_SDM_stereo.play(gain=0.5/np.max(sdm_p))
print("output: Binaural Loudspeaker SDM")
s_out_SDM_ls.play(gain=0.5/np.max(sdm_p))
axs[1].set_title("binaural VBAP")
axs[2].plot(l_allrap_ir)
axs[2].plot(r_allrap_ir)
axs[2].set_title("binaural ALLRAP")
axs[3].plot(l_allrap2_ir)
axs[3].plot(r_allrap2_ir)
axs[3].set_title("binaural ALLRAP2")
axs[4].plot(l_nls_ir)
axs[4].plot(r_nls_ir)
axs[4].set_title("binaural NLS")
for ax in axs:
ax.grid(True)
plt.tight_layout()
# Listen to some
s_in = sig.MonoSignal.from_file('../data/piano_mono.flac', fs)
s_in.trim(2.6, 6)
s_out_vbap = sig.MultiSignal(2*[s_in.signal], fs=fs)
s_out_vbap = s_out_vbap.conv([l_vbap_ir, r_vbap_ir])
s_out_allrap = sig.MultiSignal(2*[s_in.signal], fs=fs)
s_out_allrap = s_out_allrap.conv([l_allrap_ir, r_allrap_ir])
s_out_allrap2 = sig.MultiSignal(2*[s_in.signal], fs=fs)
s_out_allrap2 = s_out_allrap2.conv([l_allrap2_ir, r_allrap2_ir])
s_out_hrir = sig.MultiSignal(2*[s_in.signal], fs=fs)
s_out_hrir = s_out_hrir.conv([hrirs.nearest_hrirs(src_azi, src_colat)[0],
hrirs.nearest_hrirs(src_azi, src_colat)[1]])
"""Convert A 'MultiSignal' (type I: FLU, FRD, BLD, BRU) to B AmbiBSignal.
Parameters
----------
Ambi_A : sig.MultiSignal
Input signal.
filter_coeffs : string
Picklable file that contains b0_d, a0_d, b1_d, a1_d.
Returns
-------
Ambi_B : sig.AmbiBSignal
B-format output signal.
"""
_B = sph.soundfield_to_b(Ambi_A.get_signals())
Ambi_B = sig.AmbiBSignal([_B[0, :], _B[1, :], _B[2, :], _B[3, :]],
fs=Ambi_A.fs)
if filter_coeffs is not None:
b0_d, a0_d, b1_d, a1_d = pickle.load(open(filter_coeffs, "rb"))
Ambi_B.W = signal.lfilter(b0_d, a0_d, Ambi_B.W)
Ambi_B.X = signal.lfilter(b1_d, a1_d, Ambi_B.X)
Ambi_B.Y = signal.lfilter(b1_d, a1_d, Ambi_B.Y)
Ambi_B.Z = signal.lfilter(b1_d, a1_d, Ambi_B.Z)
return Ambi_B
for c in data.T:
loaded_data.append(c)
else:
loaded_data.append(data)
loaded_fs.append(fs_file)
# Assert same sample rate for all channels
assert all(x == loaded_fs[0] for x in loaded_fs)
# Check against provided samplerate
if fs is not None:
if fs != loaded_fs[0]:
raise ValueError("File: Found different fs:" + str(loaded_fs[0]))
else:
fs = loaded_fs[0]
# MonoSignal or MultiSignal
if len(loaded_data) == 1:
return sig.MonoSignal(loaded_data, fs=fs)
else:
return sig.MultiSignal([*loaded_data], fs=fs)
else:
mat = loadmat(filename)
if not filename == 'dummy':
hrir_l = np.array(np.squeeze(mat['hrir_l']), dtype=float)
hrir_r = np.array(np.squeeze(mat['hrir_r']), dtype=float)
try:
hrir_fs = int(mat['fs'])
except KeyError:
hrir_fs = int(mat['SamplingRate'])
azi = np.array(np.squeeze(mat['azi']), dtype=float)
colat = np.array(np.squeeze(mat['colat']), dtype=float)
grid = pd.DataFrame({'azi': azi, 'colat': colat})
HRIRs = sig.HRIRs(hrir_l, hrir_r, grid, hrir_fs)
assert HRIRs.fs == fs
return HRIRs
def save_audio(signal, filename, fs=None, subtype='FLOAT'):
"""Save signal to audio file.
Parameters
----------
signal : sig. MonoSignal, sig.MultiSignal or np.ndarray
Audio Signal, forwarded to sf.write()/
filename : string
Audio file name.
fs : int
fs(t).
subtype : optional
"""
# assert(isinstance(signal, (sig.MonoSignal, sig.MultiSignal)))
if isinstance(sig, sig.MonoSignal):
if fs is not None:
assert(signal.fs == fs)
if type(signal) == sig.MonoSignal:
data = signal.signal
data_fs = signal.fs
elif type(signal) in (sig.MultiSignal, sig.AmbiBSignal):
data = signal.get_signals().T
data_fs = signal.fs
elif isinstance(signal, (np.ndarray, np.generic)):
data = signal
data_fs = fs
else:
raise NotImplementedError('Data type not supported.')
sf.write(filename, data, data_fs, subtype=subtype)