music.localize_hrtf¶
- music.localize_hrtf(sonic_vector, left_hrir, right_hrir, sample_rate=None)[source]¶
Place a mono sound with a measured pair of impulse responses.
A head-related transfer function is the filtering a particular head, pinnae and torso apply to a sound arriving from a particular direction, and it is the thing that carries the cues
localize()andlocalize2()do not: the height of a source, and whether it is in front of the listener or behind. Applying one is a convolution, which is what the article says of it and what this does – once per ear, with the impulse response measured for that ear from that direction.This routine is not an HRTF; it is the step that applies one. The pair can be Gardner and Martin’s KEMAR measurements, made at 44.1 kHz, which
setup_hrtf()fetches once andhrir()reads for the measured direction nearest the one asked for. Or it can come from anywhere else: a database such as CIPIC, a set interpolated for a direction between measured ones, or one synthesised for a generic head. Choosing or making a response is research rather than signal processing, and none of it happens here.- Parameters:
- sonic_vectorarray_like
The mono sound to place.
- left_hrir, right_hrirarray_like
The head-related impulse responses for the two ears, from the direction the source is to come from. They need not be the same length as each other; the difference between them across the two ears is what carries the direction.
- sample_rate
integerorNone The sample rate. It has to be the rate the responses were measured at, and nothing here can check that, so it is taken on trust. It is accepted for symmetry with the other routines; a convolution needs no rate. None, the default, is the rate
music.get_sample_rate()returns.
- Returns:
ndarrayA stereo array of shape
(2, len(sonic_vector) + len(hrir) - 1), longer than the input by the tail of the response, as a convolution is. Trim it if a fixed length matters.
- Raises:
ValueErrorIf the sound is not one-dimensional, or either response is empty.
- Parameters:
sonic_vector (ArrayLike)
left_hrir (ArrayLike)
right_hrir (ArrayLike)
sample_rate (int | None)
- Return type:
NDArray[float64]
See also
hrirthe KEMAR responses measured nearest to a direction.
setup_hrtffetches the KEMAR measurements, once.
localizethe geometric model, from interaural time and intensity difference alone.
localize2the same cues per frequency band.
firconvolution with an impulse response, for one channel.
Notes
An HRTF is measured per listener, and one measured on another head is an approximation whose error is itself a research subject. A response measured at one sample rate and applied at another places the source somewhere else, because the interaural delay it encodes is a number of samples.
Cite the following article whenever you use this function.
References
[1]Fabbri, Renato, et al. “Musical elements in the discrete-time representation of sound.” arXiv preprint arXiv:abs/1412.6853 (2017)
Examples
>>> import numpy as np >>> left, right = np.array([1.0, 0.3]), np.array([0.0, 0.6, 0.2]) >>> placed = localize_hrtf(note(duration=0.1), left, right) >>> placed.shape[0] 2
With measured ears, from above and behind, at the default rate, which is the one the KEMAR responses were measured at:
>>> setup_hrtf() >>> _, _, left, right = hrir(elevation=30, azimuth=270) >>> placed = localize_hrtf(note(duration=2), left, right)