spaudiopy.io

Input Output (IO) helpers.

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['axes.grid'] = True

import spaudiopy as spa

Functions

get_default_hrirs([grid_azi, grid_zen, ...])

Creates the default HRIRs loaded by load_hrirs() by inverse SHT.

load_audio(filenames[, fs])

Load mono and multichannel audio from files.

load_hrirs(fs[, filename, jobs_count])

Convenience function to load 'HRIRs.mat'.

load_layout(filename[, listener_position, ...])

Load loudspeaker layout from json configuration file.

load_sdm(filename[, init_nan])

Convenience function to load 'SDM.mat'.

load_sofa_data(filename)

Load .sofa file into python dictionary that contains the data in numpy arrays.

load_sofa_hrirs(filename)

Load SOFA file containing HRIRs.

save_audio(signal, filename[, fs, subtype])

Save signal to audio file.

save_layout(filename, ls_layout[, name, ...])

Save loudspeaker layout to json configuration file.

sofa_to_sh(filename, N_sph[, sh_type])

Load and transform SOFA IRs to the Spherical Harmonic Domain.

write_ssr_brirs_loudspeaker(filename, ...[, ...])

Write binaural room impulse responses (BRIRs) and save as wav file.

write_ssr_brirs_sdm(filename, sdm_p, ...[, ...])

Write binaural room impulse responses (BRIRs) and save as wav file.

spaudiopy.io.load_audio(filenames, fs=None)[source]

Load mono and multichannel audio from files.

Parameters:

filenames (string or list of strings) – Audio files.

Returns:

sig (sig.MonoSignal or sig.MultiSignal) – Audio signal.

spaudiopy.io.save_audio(signal, filename, fs=None, subtype='FLOAT')[source]

Save signal to audio file.

Parameters:
  • signal (sig. MonoSignal, sig.MultiSignal or np.ndarray) – Audio Signal, forwarded to sf.write(); (frames x channels).

  • filename (string) – Audio file name.

  • fs (int) – fs(t).

  • subtype (optional)

spaudiopy.io.load_hrirs(fs, filename=None, jobs_count=None)[source]

Convenience function to load ‘HRIRs.mat’. The file contains [‘hrir_l’, ‘hrir_r’, ‘fs’, ‘azi’, ‘zen’].

Parameters:
  • fs (int) – fs(t).

  • filename (string, optional) – HRTF.mat file or default set, or ‘dummy’ for debugging.

  • jobs_count (int or None, optional) – Number of parallel jobs for resample_hrirs() in get_default_hrirs(), ‘None’ employs ‘cpu_count’.

Returns:

HRIRs (sig.HRIRs instance) –

left(g, h) numpy.ndarray

h(t) for grid position g.

right(g, h) numpy.ndarray

h(t) for grid position g.

azi(g,) array_like

grid azimuth.

zen(g,) array_like

grid zenith / colatitude.

fsint

fs(t).

spaudiopy.io.get_default_hrirs(grid_azi=None, grid_zen=None, jobs_count=None)[source]

Creates the default HRIRs loaded by load_hrirs() by inverse SHT. By default it renders onto a gauss grid of order N=35, and additionally resamples fs to 48kHz.

Parameters:
  • grid_azi (array_like, optional)

  • grid_zen (array_like, optional)

  • jobs_count (int or None, optional) – Number of parallel jobs for resample_hrirs(), ‘None’ employs ‘cpu_count’.

Notes

HRTFs in SH domain obtained from http://dx.doi.org/10.14279/depositonce-5718.5

spaudiopy.io.load_sofa_data(filename)[source]

Load .sofa file into python dictionary that contains the data in numpy arrays.

spaudiopy.io.load_sofa_hrirs(filename)[source]

Load SOFA file containing HRIRs.

Parameters:

filename (string) – SOFA filepath.

Returns:

HRIRs (sig.HRIRs instance) –

left(g, h) numpy.ndarray

h(t) for grid position g.

right(g, h) numpy.ndarray

h(t) for grid position g.

azi(g,) array_like

grid azimuth.

zen(g,) array_like

grid zenith / colatitude.

fsint

fs(t).

spaudiopy.io.sofa_to_sh(filename, N_sph, sh_type='real')[source]

Load and transform SOFA IRs to the Spherical Harmonic Domain.

Parameters:
  • filename (string) – SOFA file name.

  • N_sph (int) – Spherical Harmonic Transform order.

  • sh_type (‘real’ (default) or ‘complex’ spherical harmonics.)

Returns:

  • IRs_nm ((2, (N_sph+1)**2, S) numpy.ndarray) – Left and right (stacked) SH coefficients.

  • fs (int)

spaudiopy.io.load_sdm(filename, init_nan=True)[source]

Convenience function to load ‘SDM.mat’. The file contains [‘h_ref’ or ‘p’, ‘sdm_azi’ or ‘sdm_phi’, ‘sdm_zen’ or ‘sdm_theta’, ‘fs’].

Parameters:
  • filename (string) – SDM.mat file

  • init_nan (bool, optional) – Initialize nan to [0, pi/2].

Returns:

  • h ((n,) array_like) – p(t).

  • sdm_azi ((n,) array_like) – Azimuth angle.

  • sdm_zen ((n,) array_like) – Colatitude angle.

  • fs (int) – fs(t).

spaudiopy.io.write_ssr_brirs_loudspeaker(filename, ls_irs, hull, fs, subtype='FLOAT', hrirs=None, jobs_count=1)[source]

Write binaural room impulse responses (BRIRs) and save as wav file.

The azimuth resolution is one degree. The channels are interleaved and directly compatible to the SoundScape Renderer (SSR) ssr-brs.

Parameters:
  • filename (string)

  • ls_irs ((L, S) np.ndarray) – Impulse responses of L loudspeakers, e.g. by hull.loudspeaker_signals().

  • hull (decoder.LoudspeakerSetup)

  • fs (int)

  • subtype (forwarded to sf.write(), optional)

  • hrirs (sig.HRIRs, optional)

  • jobs_count (int, optional) – [CPU Cores], Number of Processes, switches implementation for n > 1.

spaudiopy.io.write_ssr_brirs_sdm(filename, sdm_p, sdm_phi, sdm_theta, fs, subtype='FLOAT', hrirs=None)[source]

Write binaural room impulse responses (BRIRs) and save as wav file.

The azimuth resolution is one degree. The channels are interleaved and directly compatible to the SoundScape Renderer (SSR) ssr-brs.

Parameters:
  • filename (string)

  • sdm_p ((n,) array_like) – Pressure p(t).

  • sdm_phi ((n,) array_like) – Azimuth phi(t).

  • sdm_theta ((n,) array_like) – Colatitude theta(t).

  • fs (int)

  • subtype (forwarded to sf.write(), optional)

  • hrirs (sig.HRIRs, optional)

spaudiopy.io.load_layout(filename, listener_position=None, N_kernel=50)[source]

Load loudspeaker layout from json configuration file.

spaudiopy.io.save_layout(filename, ls_layout, name='unknown', description='unknown')[source]

Save loudspeaker layout to json configuration file.