spatial¶
Two ears, heads and rooms: spatialization through HRIRs, binaural cues and reverberation.
sonore.spatial.spatialization¶
Head-related spatialization.
Coordinates¶
Cartesian positions are in meters, head-centered: x = right, y = front,
z = up. Head-centered “hcc” coordinates are (dist [cm], elev [deg],
azim [deg]) with azimuth measured clockwise from the front (90° = right),
as in the PKU-IOA database. (SOFA files use counter-clockwise azimuth;
HRIRSet.from_sofa() converts.)
Moving sources¶
A path is where the source is as a function of the time it emits, in
seconds. A sample emitted at time t_e reaches each ear after that ear’s
delay d(t_e), the onset of the HRIR at the source’s position. Measured
HRIRs such as PKU-IOA’s keep the travel time from the source, so this
delay is the propagation delay plus the interaural delay. move_sound()
solves t = t_e + d(t_e) for every output sample t and reads the sound at
t_e between samples, so the delay changes smoothly, and the Doppler shift
of a source that changes distance comes out of the same read. The rest of
the HRIR, its shape with the onset removed, is switched every hop.
Beyond the measured distances, distance acts only through travel time and
level: the HRIR of the nearest measured distance in the same direction,
delayed by the extra distance over the speed of sound and scaled by the
ratio of distances (1/r).
- rect_to_sph(x: ArrayLike, y: ArrayLike, z: ArrayLike) tuple[Any, Any, Any][source]¶
Cartesian -> (rho, theta=elevation, phi=angle from +x), radians.
- linear_trajectory(start: ArrayLike, end: ArrayLike, n: int) ndarray[source]¶
Straight line between two Cartesian points; shape
(n, 3).
- circular_trajectory(start_hcc: ArrayLike, end_hcc: ArrayLike, n: int) ndarray[source]¶
Interpolate linearly in
(dist, elev, azim)and return Cartesian points, shape(n, 3). E.g.(100, 0, 90) -> (100, 0, -90)sweeps from right to left through the front at 1 m.
- hcc_trajectory(dist: float | tuple[ArrayLike, ArrayLike] | Callable[[ndarray], ArrayLike], elev: float | tuple[ArrayLike, ArrayLike] | Callable[[ndarray], ArrayLike], azim: float | tuple[ArrayLike, ArrayLike] | Callable[[ndarray], ArrayLike]) Callable[[ndarray], ndarray][source]¶
A path in head-centered coordinates, as a function of time.
Each coordinate is a number, a
(times, values)pair (interpolated linearly, held before the first and after the last time), or a function of time in seconds.distis in cm,elevandazimin degrees (azimuth clockwise from the front). Interpolating in these coordinates, rather than between Cartesian points, keeps an arc an arc. Azimuth is interpolated as a number of degrees, so 350 to 10 sweeps 340 degrees the long way round; write 350 to 370 for the short way.Returns a function
t -> positions, Cartesian [m], shape(n, 3), whichmove_sound()takes as a trajectory. For example, the azimuth swing of Cho & Kidd (2022), 30 degrees to either side at 2 Hz, 1 m away:so.hcc_trajectory(100, 0, lambda t: 30 * np.sin(2 * np.pi * 2 * t))
- distance_gain_db(distance: ArrayLike, *, ref: float = 1.0) ndarray[source]¶
Inverse-square-law level change [dB] for a point source re
refmeters:-20*log10(d/ref), i.e. -6 dB per doubling of distance.
- class HRIRSet(irs: ndarray, positions: ndarray, fs: float, align: bool = True, fit_onsets: bool = True, onset_threshold_db: float = -20.0, _pre: int = 4)[source]¶
Bases:
objectA set of measured head-related impulse responses.
- Parameters:
irs (numpy.ndarray) – Shape
(n_positions, 2, n_taps)(left, right).positions (numpy.ndarray) – Cartesian source positions [m], shape
(n_positions, 3).fs (float) – Sampling rate of the IRs.
align (bool) – Interpolate onset-aligned IRs and interpolate the onset delays separately. Avoids the comb filtering you get from averaging HRIRs with different delays. Used by
at()andspatialize();move_sound()always interpolates aligned shapes and delays.fit_onsets (bool) – With
alignand three or more measured distances, delay each IR’s two ears together so that their mean onset follows a fit across distances rather than the measured one: for each direction, the travel timer/cplusa + b/r, fitted to the measured mean onsets with any more than 50 µs off the first fit left out. Theb/rterm follows the ears sitting off the head’s center, which matters up close. Each ITD stays as measured. This keeps a distance measured with an offset (PKU-IOA’s horizontal ring at 1.3 m arrives about 110 µs early) from bending the delay, and so the Doppler shift, of a source moving through it.
- irs: ndarray¶
- positions: ndarray¶
- fs: float¶
- align: bool = True¶
- fit_onsets: bool = True¶
- onset_threshold_db: float = -20.0¶
- classmethod from_pku_ioa(directory: str | PathLike, *, fs: float = 65536, **kwargs) HRIRSet[source]¶
Load the PKU-IOA database from its original
.datfiles (azi{A}_elev{E}_dist{D}.dat, float64, left then right), found indirectoryor any folder below it, such as the distributeddist{D}/elev{E}/layout. Empty files are skipped with a warning.load_hrirs()downloads the SOFA copy instead.
- classmethod from_sofa(path: str | PathLike, **kwargs) HRIRSet[source]¶
Load a SOFA
SimpleFreeFieldHRIRfile (needsh5py). A nonzeroData.Delay(samples, per measurement and ear or one for all) is added to the IRs, which grow by the largest delay.
- classmethod concat(sets: Iterable[HRIRSet], **kwargs) HRIRSet[source]¶
Merge sets measured at the same sampling rate and length, e.g. one per distance, into a single set.
kwargsgo toHRIRSet.
- at(positions: ArrayLike, *, fs: float | None = None) ndarray[source]¶
Interpolated HRIRs at Cartesian
positions(N, 3), resampled tofs. Returns shape(N, 2, n): withalign,nis the number of taps plus the latest onset in the set, so a shape placed at any onset fits; without,nis the number of taps (counted at the set’s rate; resampling scales it).
- spatialize(sound: Sound, position: ArrayLike, hrirs: HRIRSet, *, speed_of_sound: float = 343.0) Sound[source]¶
Render a mono sound at a fixed Cartesian position [m].
Beyond the measured distances (or off the one distance of a single-distance set) the HRIR of the nearest measured distance in the same direction is delayed by the extra distance over
speed_of_soundand scaled by the ratio of distances (1/r). With several distances, a position nearer than the nearest measured one is refused.
- move_sound(sound: Sound, trajectory: Callable[[ndarray], ndarray] | tuple[TypeAliasForwardRef('ArrayLike'), TypeAliasForwardRef('ArrayLike')] | TypeAliasForwardRef('ArrayLike'), hrirs: HRIRSet, *, room: Sound | None = None, drr_db: float = 0.0, hop: float = 0.005, speed_of_sound: float = 343.0) Sound[source]¶
Render a mono sound moving along a path, through measured HRIRs.
- Parameters:
trajectory –
Where the source is, as a function of the time it emits:
a function
t -> positions, Cartesian [m], shape(n, 3), such ashcc_trajectory()returns;a
(times, points)pair:(N,)times [s] and(N, 3)Cartesian points, interpolated linearly in between and held before and after;an
(N, 3)array of points, passed at a constant rate over the sound’s duration (linear_trajectory(),circular_trajectory()).
hrirs – The measured HRIRs. Between measured distances they are interpolated; beyond them, distance acts through travel time and 1/r only (see the module notes). A set with one distance renders every distance that way; with several, a path nearer than the nearest measured distance is refused.
room – A reverberant tail, one or two channels, such as
so.synth_ir(rt60, fs, n_channels=2)(withoutdrr_db, so it has no direct sound). It is driven by the source as it arrives, without the 1/r, so its level stays put while the direct sound falls with distance, and each ear hears it through the HRIRs averaged over directions, since reverberation arrives from everywhere.drr_db – Direct-to-reverberant energy ratio at 1 m, averaged over directions.
hop – Spacing [s] at which the HRIR shapes are interpolated and switched with raised-cosine cross-fades.
speed_of_sound – In m/s, for distances beyond the measured ones.
delay (Each ear reads the sound through its own)
the (the HRIR onset at)
position (source's)
sample (which changes smoothly from sample to)
a (so)
being (source changing distance glides in pitch (Doppler) instead of)
delays (cross-faded between fixed)
output (which would comb-filter. The)
later. (starts when the source starts emitting; each ear hears it a delay)
sonore.spatial.hrir_data¶
Public HRIR databases, downloaded on demand and cached.
sonore bundles no HRIRs. load_hrirs() downloads the files a database
needs the first time they are asked for, checks each against a pinned SHA-256,
and keeps them in a cache directory: $SONORE_DATA_DIR if set, else the
platform’s user cache directory.
- data_dir() Path[source]¶
Where downloaded data is cached:
$SONORE_DATA_DIR, else the user cache directory.
- load_hrirs(name: str = 'pku-ioa', distances: float | Sequence[float] | Literal['all'] | None = None, cache_dir: str | PathLike | None = None, **kwargs) HRIRSet[source]¶
Load a public HRIR database, downloading its files on first use.
- Parameters:
name – Database name, a key of
HRIR_DATABASES."pku-ioa": KEMAR at 20, 30, 40, 50, 75, 100, 130 and 160 cm, about 13 MB per distance. Cite Qu et al. (2009) when you use it.distances – Source distances [cm] to load. Several distances give a set that interpolates across distance as well as direction.
Noneloads the database’s default (1 m for PKU-IOA),"all"every distance.cache_dir – Overrides
data_dir().**kwargs – Passed to
HRIRSet, e.g.align.
sonore.spatial.binaural¶
Binaural manipulation and analysis.
Sign convention throughout: positive ITD = right ear leads and positive ILD = right ear louder, i.e. positive values point to the right.
- apply_itd_ild(sound: Sound, *, itd: float = 0.0, ild: float = 0.0) Sound[source]¶
Impose an ITD [s] and ILD [dB] on a mono or two-channel sound (a two-channel sound keeps whatever differences it already has).
The lagging ear is delayed by
|itd|(fractional delays are exact up to band-limiting); the ILD is split symmetrically,±ild/2per ear.
- simple_bir(fs: float, *, itd: float = 0.0, ild: float = 0.0, half_width: int = 32) Sound[source]¶
A binaural impulse response with only an ITD [s] and ILD [dB].
Each ear is a (possibly fractionally delayed) unit impulse scaled by
±ild/2dB. Fractional delays use a windowed sinc, so the IR startshalf_widthsamples late whenever the ITD isn’t a whole number of samples. With the defaulthalf_width, the ears differ by the ITD and ILD within 0.005 dB and 1e-3 rad below 0.4 fs; above about 0.47 fs the lagging ear rolls off (3 dB down at 0.488 fs).
- class InterauralCues(t: ndarray, itd: ndarray, ild: ndarray, iac: ndarray, corr0: ndarray, cfs: ndarray | None = None)[source]¶
Bases:
ViewShort-time interaural cues. Arrays are
(n_windows,)for broadband analysis or(n_windows, n_bands)per band. Silent time windows are NaN. A view: it keeps a few numbers per time window and drops the sounds.- discards = 'InterauralCues keeps only the time and level differences and the coherence between the ears in each time window: it discards the sounds themselves.'¶
- t: ndarray¶
- itd: ndarray¶
- ild: ndarray¶
- iac: ndarray¶
- corr0: ndarray¶
- cfs: ndarray | None = None¶
- plot(ax: Axes | None = None, **kwargs) Axes[source]¶
ITD and ILD on twin axes, with IAC underneath (see
plot_interaural_cues()).
- interaural_cues(sound: Sound, *, win_dur: float = 0.02, hop_dur: float | None = None, max_itd: float = 0.001, filterbank: Filterbank | None = None, silence_db: float = -40.0) InterauralCues[source]¶
Windowed ITD, ILD and interaural coherence.
The ears are cut into Hann windows of
win_dureveryhop_dur(defaultwin_dur / 2);tgives each window’s center. ITD is the lag (within±max_itd) of the peak of the normalized cross-correlation, with the window’s own taper divided out, refined by parabolic interpolation.iacis that peak’s height (interaural coherence);corr0is the zero-lag correlation, which can be negative (use it for Oscor/Phasewarp). Time windows more thansilence_dbbelow the loudest one are NaN. A window where only one ear is silent has no ITD, IAC orcorr0(NaN) and an ILD of ±inf. If afilterbankis given, cues are computed per band, each band against its own loudest window.
- oscor(duration: float, fs: float, f_mod: float, *, rng: int | Generator | None = None, **noise_kwargs) Sound[source]¶
Oscillating-correlation noise (Siveke et al., 2008): the interaural correlation follows
sin(2*pi*f_mod*t). Normalized to an RMS of 1 over both ears.
- phasewarp(duration: float, fs: float, f_mod: float, *, rng: int | Generator | None = None, **noise_kwargs) Sound[source]¶
Phasewarp noise (Siveke et al., 2008): every component’s interaural phase difference rotates at
f_modHz, so the IPD sweeps through 360° and the zero-lag interaural correlation followscos(2*pi*f_mod*t).Implemented as a single-sideband frequency shift of the left-ear noise, so the right ear’s spectrum is the left’s moved up by
f_modHz (a 500-1000 Hz noise covers 500 + f_mod to 1000 + f_mod Hz in the right ear). Normalized to an RMS of 1 over both ears.
sonore.spatial.reverb¶
Synthetic room impulse responses from the statistics of natural reverberation (Traer & McDermott, 2016).
The regression coefficients in sonore/data/fit_*.npy are Traer &
McDermott’s fits to their survey of 271 real-world rooms: for each of 35
frequencies from 20 Hz to 16 kHz (their 33 subbands and the two edge
filters), a slope and an intercept of the band’s RT60 (Eq. S11) and onset
level (Eq. S12) against the room’s broadband RT60, the median RT60 across the
33 subbands (the paper’s T, Eq. S9). James Traer supplied them in 2017; the
slopes, intercepts and RT60 profiles match the paper’s Fig. S3 by eye. The
rooms were fitted from a T of 0.2 s up (shorter ones were left out), so a
shorter RT60 extrapolates.
- synth_ir(rt60: float | tuple[ArrayLike, ArrayLike], fs: float, *, drr_db: float | None = None, decay_db: float = 60.0, n_bands: int = 32, f_lo: float = 20.0, f_hi: float = 16000.0, n_channels: int = 1, decay_shape: Literal['exponential', 'time_reversed', 'linear_matched_start', 'linear_matched_end'] = 'exponential', rt60_profile: Literal['ecological', 'inverted', 'exaggerated', 'reduced'] = 'ecological', drr_profile: Literal['ecological', 'constant'] = 'ecological', rng: int | Generator | None = None) Sound[source]¶
Synthesize a room impulse response (Traer & McDermott, 2016).
Gaussian noise is split into ERB bands, each band is multiplied by a decay envelope, and the bands are re-filtered and summed. With the defaults the decay is exponential with frequency-dependent RT60s and onset levels taken from the paper’s regressions on 271 real-world rooms, scaled to the requested broadband
rt60. In the paper, listeners could not tell IRs synthesized this way from real ones; sonore’s have not been tested with listeners.The paper’s “atypical” IRs, which its listeners readily heard as unnatural, are available through
decay_shape,rt60_profileanddrr_profile. Note that in the paper each atypical IR was further adjusted to distort sounds as much as the ecological one (equating cochleagram error); that psychophysical control isn’t done here, so variants can differ somewhat in loudness and audible length.- Parameters:
rt60 – Broadband reverberation time [s] (the median of the band RT60s comes within about 6% of it; see
band_rt60s()), or a curve(freqs [Hz], RT60s [s])that each band reads at its center frequency (seeband_rt60s()); a curve keepsrt60_profileat"ecological".drr_db – Direct-to-reverberant energy ratio [dB]. If given, a unit impulse is placed at t=0 and the tail is scaled to this DRR. If None, only the reverberant tail is returned, with an RMS of 1 over all channels.
decay_db – For exponential decays, truncate once the slowest band has decayed this many dB (positive).
n_channels – Independent tails per channel (e.g. 2 for a decorrelated binaural tail); the direct impulse is identical in all channels.
decay_shape –
"exponential"(natural),"time_reversed","linear_matched_start"(linear from the natural starting level, same energy per band), or"linear_matched_end"(linear, reaching zero where the natural decay is 60 dB down, same energy per band).rt60_profile – Frequency dependence of decay; see
band_rt60s().drr_profile –
"ecological"onset levels per band, or"constant"(their mean).
- band_rt60s(rt60: float | tuple[ArrayLike, ArrayLike], freqs: ndarray, *, profile: Literal['ecological', 'inverted', 'exaggerated', 'reduced'] = 'ecological') ndarray[source]¶
Per-band RT60s [s] at
freqsfor a room’s broadband RT60, from the regression fits of Traer & McDermott (2016, Eq. S11), or one of the paper’s atypical variants (Eq. S15 and following). Onsynth_ir’s bands, the median of the ecological RT60s comes within about 6% of the broadband RT60 (0.95 to 1.06 times it for 0.3 to 3 s):"ecological": mid frequencies decay slowest, as in real rooms."inverted":max + min - ecological; slow where real rooms are fast."exaggerated": the profile of a room twice as reverberant, scaled down to this length (more sharply peaked than real rooms)."reduced": the profile of a room half as reverberant, scaled up (flatter).rt60may instead be a curve(freqs [Hz], RT60s [s]), such as one drawn by hand. Each frequency then takes the curve’s value there, interpolated linearly in log frequency and log RT60 and held flat beyond the curve’s ends. A curve is already a profile, soprofilemust stay"ecological".
- measure_rt60(ir: Sound, *, n_bands: int = 30, f_lo: float = 50.0, f_hi: float = 8000.0, fit_range_db: tuple[float, float] = (-5.0, -25.0)) tuple[ndarray, ndarray][source]¶
Per-band RT60s of an impulse response:
(center freqs [Hz], RT60s [s]).The channels are averaged first. Each ERB band’s energy decay curve (Schroeder backward integration) is fit with a line over
fit_range_dband extrapolated to -60 dB (so the default measures T20 x 3). The direct sound, if any, should be removed first; bands that never decay through the fit range give NaN.