A module for generating sounds in python. |
sndlib
– Sound Synthesis Library¶A module for generating sounds in python.
Generate an amplitude modulated tone.
Carrier frequency in hertz.
Amplitude modulation frequency in Hz.
Amplitude modulation depth (a value of 1 corresponds to 100% modulation).
Starting phase in radians.
Starting AM phase in radians.
Average tone level in dB SPL. See notes.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Notes
For a fixed base amplitude, the average power of an AM tone (as defined in this function) increases proportionally with AM depth by a factor of 1+AMDepth^2/2 (Viemeister, 1979, Yost et al., 1989, Hartmann, 2004). This function compensates for this average increase in power. You can use the AMToneVarLev function if you want to generate AM tones varying in average power with AM depth.
References
Hartmann, W. M. (2004). Signals, Sound, and Sensation. Springer Science & Business Media
Viemeister, N. F. (1979). Temporal modulation transfer functions based upon modulation thresholds. The Journal of the Acoustical Society of America, 66(5), 1364–1380. https://doi.org/10.1121/1.383531
Yost, W., Sheft, S., & Opie, J. (1989). Modulation interference in detection and discrimination of amplitude modulation. The Journal of the Acoustical Society of America, 86(December 1989), 2138–2147. https://doi.org/10.1121/1.398474
Examples
>>> snd = AMTone(frequency=1000, AMFreq=20, AMDepth=1, phase=0,
... AMPhase=1.5*pi, level=65, duration=180, ramp=10, channel='Both',
... fs=48000, maxLevel=100)
Generate an amplitude modulated tone with an interaural phase difference (IPD) in the carrier and/or modulation phase.
Carrier frequency in hertz.
Amplitude modulation frequency in Hz.
Amplitude modulation depth (a value of 1 corresponds to 100% modulation).
Starting phase in radians.
Starting AM phase in radians.
IPD to apply to the carrier phase.
IPD to apply to the modulation phase.
Average tone level in dB SPL. See notes.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the phase will be shifted.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Notes
For a fixed base amplitude, the average power of an AM tone (as defined in this function) increases proportionally with AM depth by a factor of 1+AMDepth^2/2 (Viemeister, 1979, Yost et al., 1989, Hartmann, 2004). This function does not compensate for this average increase in power. You can use the AMTone function if you want to generate AM tones matched in average power irrespective of AM depth.
References
Hartmann, W. M. (2004). Signals, Sound, and Sensation. Springer Science & Business Media
Viemeister, N. F. (1979). Temporal modulation transfer functions based upon modulation thresholds. The Journal of the Acoustical Society of America, 66(5), 1364–1380. https://doi.org/10.1121/1.383531
Yost, W., Sheft, S., & Opie, J. (1989). Modulation interference in detection and discrimination of amplitude modulation. The Journal of the Acoustical Society of America, 86(December 1989), 2138–2147. https://doi.org/10.1121/1.398474
Examples
>>> snd = AMToneIPD(frequency=1000, AMFreq=20, AMDepth=1, phase=0, AMPhase=1.5*pi,
... phaseIPD=0, AMPhaseIPD=pi, level=65,
... duration=180, ramp=10, channel='Right', fs=48000, maxLevel=100)
Generate an amplitude modulated (AM) tone.
Carrier frequency in hertz.
Amplitude modulation frequency in Hz.
Amplitude modulation depth (a value of 1 corresponds to 100% modulation).
Starting phase in radians.
Starting AM phase in radians.
Average level of the tone in dB SPL when the AMDepth is zero. The level of the tone will be higher when AMDepth is > zero. See notes.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Notes
For a fixed base amplitude, the average power of an AM tone (as defined in this function) increases proportionally with AM depth by a factor of 1+AMDepth^2/2 (Viemeister, 1979, Yost et al., 1989, Hartmann, 2004). This function does not compensate for this average increase in power. You can use the AMTone function if you want to generate AM tones matched in average power irrespective of AM depth.
References
Hartmann, W. M. (2004). Signals, Sound, and Sensation. Springer Science & Business Media
Yost, W., Sheft, S., & Opie, J. (1989). Modulation interference in detection and discrimination of amplitude modulation. The Journal of the Acoustical Society of America, 86(December 1989), 2138–2147. https://doi.org/10.1121/1.398474
Examples
>>> snd = AMToneVarLev(frequency=1000, AMFreq=20, AMDepth=1, phase=0,
... AMPhase=1.5*pi, level=65, duration=180, ramp=10, channel='Both',
... fs=48000, maxLevel=100)
Compute the distance in equivalent rectangular bandwiths (ERBs) between f1 and f2.
frequency 1 in Hz
frequency 2 in Hz
distance between f1 and f2 in ERBs
References
Glasberg, B. R., & Moore, B. C. J. (1990). Derivation of auditory filter shapes from notched-noise data. Hear. Res., 47(1-2), 103–38.
Examples
>>> ERBDistance(1000, 1200)
Generate a frequency modulated tone.
Carrier frequency in hertz. This is the frequency of the tone at fm zero crossing.
Modulation frequency in Hz.
Modulation index, also called beta and is equal to deltaF/fm, where deltaF is the maximum deviation of the instantaneous frequency from the carrier frequency.
Starting phase in radians.
Tone level in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> snd = FMTone(fc=1000, fm=40, mi=1, phase=0, level=55, duration=180,
... ramp=10, channel='Both', fs=48000, maxLevel=100)
Set the ITD of a sound within the frequency region bounded by ‘f1’ and ‘f2’
Input signal.
The start point of the frequency region to be phase-shifted in hertz.
The end point of the frequency region to be phase-shifted in hertz.
The amount of ITD shift in microseconds
The channel in which to apply the shift.
The sampling frequency of the sound.
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> hp = ITDShift(sig=noise, f1=500, f2=600, ITD=5,
channel='Left', fs=48000) #this generates a Dichotic Pitch
Add or concatenate two sounds.
First sound.
Second sound.
Delay in milliseconds between the onset of ‘snd1’ and the onset of ‘snd2’
Sampling frequency in hertz of the two sounds.
Examples
>>> snd1 = pureTone(frequency=440, phase=0, level=65, duration=180,
... ramp=10, channel='Right', fs=48000, maxLevel=100)
>>> snd2 = pureTone(frequency=880, phase=0, level=65, duration=180,
... ramp=10, channel='Right', fs=48000, maxLevel=100)
>>> snd = addSounds(snd1=snd1, snd2=snd2, delay=100, fs=48000)
Generate a pure tone with an optional interaural time or level difference.
Tone frequency in hertz.
Starting phase in radians.
Tone level in dB SPL. If ‘ild’ is different than zero, this will be the level of the tone in the reference channel.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Interaural time difference, in microseconds.
The reference channel for the ‘itd’. The interaural time difference will be applied to the other channel with respect to the reference channel.
Interaural level difference in dB SPL.
The reference channel for the ‘ild’. The level of the other channel will be icreased of attenuated by ‘ild’ dB SPL with respect to the reference channel.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> itdTone = binauralPureTone(frequency=440, phase=0, level=65, duration=180,
... ramp=10, channel='Both', itd=480, itdRef='Right', ild=0, ildRef=None,
... fs=48000, maxLevel=100)
>>> ildTone = binauralPureTone(frequency=440, phase=0, level=65, duration=180,
... ramp=10, channel='Both', itd=0, itdRef=None, ild=-20, ildRef='Right',
... fs=48000, maxLevel=100)
Synthetise a broadband noise.
Intensity spectrum level of the noise in dB SPL.
Noise duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the noise will be generated. If ‘Both’ the same noise will be generated in both channels. If ‘Dichotic’ the noise will be independent at the two ears.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel="Both", fs=48000, maxLevel=100)
Generate a complex tone frequency modulated with an exponential sinusoid.
Fundamental aarrier frequency in hertz.
Lowest harmonic number.
Highest harmonic number.
Harmonic phase relationship. One of ‘Sine’, ‘Cosine’, or ‘Alternating’.
Modulation frequency in Hz.
Frequency excursion in cam units (ERBn number scale).
Starting fmPhase in radians.
Tone level in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> snd_peak = camSinFMComplex(F0=150, lowHarm=1, highHarm=10, harmPhase="Sine", fm=5, deltaCams=1, fmPhase=pi, level=60,
... duration=180, ramp=10, channel="Both", fs=48000, maxLevel=100)
>>> snd_trough = camSinFMComplex(F0=150, lowHarm=1, highHarm=10, harmPhase="Sine", fm=5, deltaCams=1, fmPhase=0, level=60,
... duration=180, ramp=10, channel="Both", fs=48000, maxLevel=100)
Generate a tone frequency modulated with an exponential sinusoid.
Carrier frequency in hertz.
Modulation frequency in Hz.
Frequency excursion in cam units (ERBn number scale).
Starting fmPhase in radians.
Tone level in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> tone_peak = camSinFMTone(fc=450, fm=5, deltaCams=1, fmPhase=pi, startPhase=0, level=60,
... duration=180, ramp=10, channel="Both", fs=48000, maxLevel=100)
>>> tone_trough = camSinFMTone(fc=450, fm=5, deltaCams=1, fmPhase=0, startPhase=0, level=60,
... duration=180, ramp=10, channel="Both", fs=48000, maxLevel=100)
Synthetize a chirp, that is a tone with frequency changing linearly or exponentially over time with a give rate.
Starting frequency in hertz.
If ‘linear’, the frequency will change linearly on a Hz scale. If ‘exponential’, the frequency will change exponentially on a cents scale.
Rate of frequency change, Hz/s if ftype is ‘linear’, and cents/s if ftype is ‘exponential’.
Level of the tone in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> gl = chirp(freqStart=440, ftype='linear', rate=500, level=55,
duration=980, phase=0, ramp=10, channel='Both',
fs=48000, maxLevel=100)
Synthetise a complex tone.
Tone fundamental frequency in hertz.
Phase relationship between the partials of the complex tone.
Lowest harmonic component number.
Highest harmonic component number.
Harmonic stretch in %F0. Increase each harmonic frequency by a fixed value that is equal to (F0*stretch)/100. If ‘stretch’ is different than zero, an inhanmonic complex tone will be generated.
The level of each partial in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated. If ‘channel’ if ‘Odd Right’, odd numbered harmonics will be presented to the right channel and even number harmonics to the left channel. The opposite is true if ‘channel’ is ‘Odd Left’.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> ct = complexTone(F0=440, harmPhase='Sine', lowHarm=3, highHarm=10,
... stretch=0, level=55, duration=180, ramp=10, channel='Both',
... fs=48000, maxLevel=100)
Synthetise a complex tone with an interaural phase difference (IPD).
Tone fundamental frequency in hertz.
Phase relationship between the partials of the complex tone.
Lowest harmonic component number.
Highest harmonic component number.
Harmonic stretch in %F0. Increase each harmonic frequency by a fixed value that is equal to (F0*stretch)/100. If ‘stretch’ is different than zero, an inhanmonic complex tone will be generated.
The level of each partial in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Interaural phase difference, in radians.
The ear in which the phase will be shifted.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> ct = complexToneIPD(F0=440, harmPhase='Sine', lowHarm=3, highHarm=10,
... stretch=0, level=55, duration=180, ramp=10, IPD=3.14, targetEar="Right",
... fs=48000, maxLevel=100)
Synthetise a complex tone.
This function produces the same results of complexTone. The only difference is that it uses the multiprocessing Python module to exploit multicore processors and compute the partials in a parallel fashion. Notice that there is a substantial overhead in setting up the parallel computations. This means that for relatively short sounds (in the order of seconds), this function will actually be slower than complexTone.
Tone fundamental frequency in hertz.
Phase relationship between the partials of the complex tone.
Lowest harmonic component number.
Highest harmonic component number.
Harmonic stretch in %F0. Increase each harmonic frequency by a fixed value that is equal to (F0*stretch)/100. If ‘stretch’ is different than zero, an inhanmonic complex tone will be generated.
The level of each partial in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated. If ‘channel’ if ‘Odd Right’, odd numbered harmonics will be presented to the right channel and even number harmonics to the left channel. The opposite is true if ‘channel’ is ‘Odd Left’.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> ct = complexToneParallel(F0=440, harmPhase='Sine', lowHarm=3, highHarm=10,
... stretch=0, level=55, duration=180, ramp=10, channel='Both',
... fs=48000, maxLevel=100)
Delay and add algorithm for the generation of iterated rippled noise.
The signal to manipulate
delay in seconds
The gain to apply to the delayed signal
The number of iterations of the delay-add cycle
If ‘Add Same’, the output of iteration N-1 is added to delayed signal of the current iteration. If ‘Add Original’, the original signal is added to delayed signal of the current iteration.
Sampling frequency in Hz.
The array has dimensions (nSamples, 2).
References
Yost, W. A., Patterson, R., & Sheft, S. (1996). A time domain description for the pitch strength of iterated rippled noise. J. Acoust. Soc. Am., 99(2), 1066–78.
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> irn = delayAdd(sig=noise, delay=1/440, gain=1, iterations=6, configuration='Add Same', fs=48000)
Generate Huggins pitch or narrow-band noise from random-phase sinusoids.
This function generates first noise by adding closely spaced sinusoids in a wide frequency range. Then, it can apply an interaural time difference (ITD), an interaural phase difference (IPD) or a level increase to harmonically related narrow frequency bands within the noise. In the first two cases (ITD and IPD) the result is a dichotic pitch. In the last case the pitch can also be heard monaurally; adjusting the level increase, its salience can be closely matched to that of a dichotic pitch.
Centre frequency of the fundamental in hertz.
Lowest harmonic component number.
Highest harmonic component number.
Level of each sinusoidal frequency component of the noise.
Lowest frequency in hertz of the noise.
Highest frequency in hertz of the noise.
Spacing between the sinusoidal components used to generate the noise.
Width of each harmonically related frequency band.
The unit of measure used for ‘compSpacing’ and ‘sigBandwidth’
If NoSpi, the phase of the regions within each frequency band will be shifted. If NpiSo, the phase of the regions between each frequency band will be shifted.
Selects whether the decorrelation in the target regions will be achieved by applying a costant interaural phase shift (IPD), a random IPD shift, a costant interaural time difference (ITD), or a constant interaural level difference achieved by a level change in the right (‘ILD Right’) or the left (‘ILD Left’) ear.
For ‘IPD Stepped’, this is the phase offset, in radians, between the correlated and the uncorrelated regions. For ‘ITD’ this is the ITD in the transition region, in micro seconds. For ‘Random Phase’, the range of phase shift randomization in the uncorrelated regions. For ‘ILD Left’ or ‘ILD Right’ this is the level difference between the left and right ear in the uncorrelated regions.
Level of the sinusoidal components in the frequency bands. If the ‘narrowBandCompLevel’ is greater than the level of the background noise (‘compLevel’), a complex tone consisting of narrowband noises in noise will be generated.
Sound duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> s1 = dichoticNoiseFromSin(F0=300, lowHarm=1, highHarm=3,
compLevel=30, narrowBandCompLevel=30,
lowFreq=40, highFreq=2000, compSpacing=10,
sigBandwidth=100, distanceUnit='Cent',
phaseRelationship='NoSpi', dichoticDifference='IPD Stepped',
dichoticDifferenceValue=pi, duration=380, ramp=10,
fs=48000, maxLevel=101)
Generate a sinusoidally amplitude-modulated noise with an exponentially modulated AM frequency.
Carrier AM frequency in hertz.
Modulation of the AM frequency in Hz.
AM frequency excursion in cents. The instataneous AM frequency of the noise will vary from fc**(-deltaCents/1200) to fc**(deltaCents/1200).
Starting phase of the AM modulation in radians.
Amplitude modulation depth.
Noise spectrum level in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> snd = expAMNoise(fc=150, fm=2.5, deltaCents=1200, fmPhase=3.14,
AMDepth = 1, spectrumLevel=24, duration=480, ramp=10, channel='Both',
fs=48000, maxLevel=100)
Generate a frequency-modulated complex tone with an exponential sinusoid.
Carrier frequency in hertz.
Modulation frequency in Hz.
will vary from fc**(-deltaCents/1200) to fc**(+deltaCents/1200).
Starting fmPhase in radians.
Tone level in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> tone_peak = expSinFMComplex(F0=150, lowHarm=1, highHarm=10, harmPhase="Sine", fm=5, deltaCents=300, fmPhase=pi, level=60,
... duration=180, ramp=10, channel="Both", fs=48000, maxLevel=101)
>>> tone_trough = expSinFMComplex(F0=150, lowHarm=1, highHarm=10, harmPhase="Sine", fm=5, deltaCents=300, fmPhase=0, level=60,
... duration=180, ramp=10, channel="Both", fs=48000, maxLevel=101)
Generate a frequency-modulated tone with an exponential sinusoid.
Carrier frequency in hertz.
Modulation frequency in Hz.
will vary from fc**(-deltaCents/1200) to fc**(+deltaCents/1200).
Starting fmPhase in radians.
Tone level in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> tone_peak = expSinFMTone(fc=450, fm=5, deltaCents=300, fmPhase=pi, level=60,
... duration=180, ramp=10, channel="Both", fs=48000, maxLevel=101)
>>> tone_trough = expSinFMTone(fc=450, fm=5, deltaCents=300, fmPhase=0, level=60,
... duration=180, ramp=10, channel="Both", fs=48000, maxLevel=101)
Filter signal with a fir2 filter.
This function designs and applies a fir2 filter to a sound. The frequency response of the ideal filter will transition from 0 to 1 between ‘f1’ and ‘f2’, and from 1 to zero between ‘f3’ and ‘f4’. The frequencies must be given in increasing order.
Frequency in hertz of the point at which the transition for the low-frequency cutoff ends.
Frequency in hertz of the point at which the transition for the low-frequency cutoff starts.
Frequency in hertz of the point at which the transition for the high-frequency cutoff starts.
Frequency in hertz of the point at which the transition for the high-frequency cutoff ends.
The sound to be filtered.
Sampling frequency of ‘snd’.
Notes
If ‘f1’ and ‘f2’ are zero the filter will be lowpass. If ‘f3’ and ‘f4’ are equal to or greater than the nyquist frequency (fs/2) the filter will be highpass. In the other cases the filter will be bandpass.
The order of the filter (number of taps) is fixed at 256. This function uses internally ‘scipy.signal.firwin2’.
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> lpNoise = fir2Filt(f1=0, f2=0, f3=1000, f4=1200,
... snd=noise, fs=48000) #lowpass filter
>>> hpNoise = fir2Filt(f1=5000, f2=6000, f3=24000, f4=26000,
... snd=noise, fs=48000) #highpass filter
>>> bpNoise = fir2Filt(f1=400, f2=600, f3=4000, f4=4400,
... snd=noise, fs=48000) #bandpass filter
Synthetise a complex tone with an embedded FM starting and stopping at a chosen time after the tone onset.
F0 at the FM zero crossing
Phase relationship between the partials of the complex tone.
Lowest harmonic component number.
Highest harmonic component number.
The level of each partial in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
FM frequency in Hz.
FM depth in %
Starting phase of FM
Start of FM in ms after start of tone
Duration of FM, in ms
If True, scale the harmonic level so that for a complex tone within a bandpass filter the overall level does not change with F0 modulations.
Channel in which the tone will be generated. If ‘channel’ if ‘Odd Right’, odd numbered harmonics will be presented to the right channel and even number harmonics to the left channel. The opposite is true if ‘channel’ is ‘Odd Left’.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> tone_up = fm_complex1(midF0=140, harmPhase="Sine", lowHarm=1, highHarm=10, level=60, duration=430, ramp=10, fmFreq=1.25, fmDepth=40, fmStartPhase=1.5*pi, fmStartTime=25, fmDuration=400, levelAdj=True, channel="Both", fs=48000, maxLevel=101)
>>> tone_down = fm_complex1(midF0=140, harmPhase="Sine", lowHarm=1, highHarm=10, level=60, duration=430, ramp=10, fmFreq=1.25, fmDepth=40, fmStartPhase=0.5*pi, fmStartTime=25, fmDuration=400, levelAdj=True, channel="Both", fs=48000, maxLevel=101)
Synthetise a complex tone with an embedded FM starting and stopping at a chosen time after the tone onset.
F0 at the FM zero crossing
Phase relationship between the partials of the complex tone.
Lowest harmonic component number.
Highest harmonic component number.
The level of each partial in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
FM frequency in Hz.
FM depth in %
Starting phase of FM
Start of FM in ms after start of tone
Duration of FM, in ms
If True, scale the harmonic level so that for a complex tone within a bandpass filter the overall level does not change with F0 modulations.
Channel in which the tone will be generated. If ‘channel’ if ‘Odd Right’, odd numbered harmonics will be presented to the right channel and even number harmonics to the left channel. The opposite is true if ‘channel’ is ‘Odd Left’.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> tone_up = fm_complex2(midF0=140, harmPhase="Sine", lowHarm=1, highHarm=10, level=60, duration=430, ramp=10, fmFreq=1.25, fmDepth=40, fmStartPhase=1.5*pi, fmStartTime=25, fmDuration=400, levelAdj=True, channel="Both", fs=48000, maxLevel=101)
>>> tone_down = fm_complex2(midF0=140, harmPhase="Sine", lowHarm=1, highHarm=10, level=60, duration=430, ramp=10, fmFreq=1.25, fmDepth=40, fmStartPhase=0.5*pi, fmStartTime=25, fmDuration=400, levelAdj=True, channel="Both", fs=48000, maxLevel=101)
Compute the frequency, in Hz, corresponding to a distance, in equivalent rectangular bandwidths (ERBs), of ‘deltaERB’ from f1.
frequency at one end of the interval in Hz
distance in ERBs
frequency at the other end of the interval in Hz
References
Glasberg, B. R., & Moore, B. C. J. (1990). Derivation of auditory filter shapes from notched-noise data. Hear. Res., 47(1-2), 103–38.
Examples
>>> freqFromERBInterval(100, 1.5)
>>> freqFromERBInterval(100, -1.5)
>>> #for several frequencies
>>> freqFromERBInterval([100, 200, 300], 1.5)
>>> # for several distances
>>> freqFromERBInterval(100, [1, 1.5, 2])
Impose onset and offset ramps to a sound.
The duration of the ramps.
The signal on which the ramps should be imposed.
The sampling frequency os ‘sig’
The ramped signal.
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=200, ramp=0,
... channel='Both', fs=48000, maxLevel=100)
>>> gate(ramps=10, sig=noise, fs=48000)
Compute the root mean square (RMS) value of the signal.
The signal for which the RMS needs to be computed.
Either an integer indicating the channel number, ‘each’ for a list of the RMS values in each channel, or ‘all’ for the RMS across all channels.
The RMS of ‘sig’.
Examples
>>> pt = pureTone(frequency=440, phase=0, level=65, duration=180,
... ramp=10, channel="Right", fs=48000, maxLevel=100)
>>> getRMS(pt, channel="each")
Synthetize a rising or falling tone glide with frequency changing linearly or exponentially.
Starting frequency in hertz.
If ‘linear’, the frequency will change linearly on a Hz scale. If ‘exponential’, the frequency will change exponentially on a cents scale.
If ftype is ‘linear’, excursion is the total frequency change in Hz. The final frequency will be freqStart + excursion. If ftype is ‘exponential’, excursion is the total frequency change in cents. The final frequency in Hz will be freqStart*2**(excursion/1200).
Level of the tone in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> gl = glide(freqStart=440, ftype='exponential', excursion=500,
level=55, duration=180, phase=0, ramp=10, channel='Both',
fs=48000, maxLevel=100)
Generate an harmonic complex tone from narrow noise bands.
Fundamental frequency of the complex.
Lowest harmonic component number. The first component is #1.
Highest harmonic component number.
The spectrum level of the noise bands in dB SPL.
The width of each noise band.
Defines whether the bandwith of the noise bands is expressed in hertz (Hz), cents (Cent), or equivalent rectangular bandwidths (ERB).
Harmonic stretch in %F0. Increase each harmonic frequency by a fixed value that is equal to (F0*stretch)/100. If ‘stretch’ is different than zero, an inhanmonic complex tone will be generated.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated. If ‘channel’ if ‘Odd Right’, odd numbered harmonics will be presented to the right channel and even number harmonics to the left channel. The opposite is true if ‘channel’ is ‘Odd Left’.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> c1 = harmComplFromNarrowbandNoise(F0=440, lowHarm=3, highHarm=8,
level=40, bandwidth=80, bandwidthUnit="Hz", stretch=0, duration=180, ramp=10, channel='Both',
fs=48000, maxLevel=100)
Impose a glide in level to a sound.
This function changes the level of a sound with a smooth transition (an amplitude ramp) between ‘startTime’ and ‘endTime’. If the signal input to the function has a level L, the signal output by the function will have a level L between time 0 and ‘startTime’, and a level L+deltaL between endTime and the end of the sound.
Sound on which to impose the level change.
Magnitude of the level change in dB SPL.
Start of the level transition in milliseconds.
End of the level transition in milliseconds.
Channel to which apply the level transition.
Samplig frequency of the sound in Hz.
Examples
>>> pt = pureTone(frequency=440, phase=0, level=65, duration=180,
... ramp=10, channel='Right', fs=48000, maxLevel=100)
>>> pt2 = imposeLevelGlide(sig=pt, deltaL=10, startTime=100,
endTime=120, channel='Both', fs=48000)
Compute the frequency closest to ‘freq’ that has an integer number of cycles for the given sound duration.
Frequency in hertz.
Duration of the sound, in milliseconds.
Examples
>>> intNCyclesFreq(freq=2.1, duration=1000)
2.0
>>> intNCyclesFreq(freq=2, duration=1000)
2.0
Convert an interaural time difference to an equivalent interaural phase difference for a given frequency.
Interaural time difference in seconds.
Frequency in hertz.
Examples
>>> itd = 300 #microseconds
>>> itd = 300/1000000 #convert to seconds
>>> itdtoipd(itd=itd, freq=1000)
Join a list of sounds with given interstimulus intervals
The sounds to be joined.
The interstimulus intervals between the sounds in milliseconds. This list should have one element less than the sndList.
Sampling frequency of the sounds in Hz.
Examples
>>> pt1 = pureTone(frequency=440, phase=0, level=65, duration=180,
... ramp=10, channel='Right', fs=48000, maxLevel=100)
>>> pt2 = pureTone(frequency=440, phase=0, level=65, duration=180,
... ramp=10, channel='Right', fs=48000, maxLevel=100)
>>> tone_seq = joinSndISI([pt1, pt2], [500], 48000)
Generate an asynchronous chord.
This function will add a set of pure tones with a given stimulus onset asynchrony (SOA). The temporal order of the successive tones is random.
Frequencies of the chord components in hertz.
Level of each chord component in dB SPL.
Starting phase of each chord component.
Duration of the tones composing the chord in milliseconds. All tones have the same duration.
Duration of the onset and offset ramps in milliseconds. The total duration of the tones will be tonesDuration+ramp*2.
Channel in which the tones will be generated.
Onset asynchrony between the chord components.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Examples
>>> freqs = [250, 500, 1000]
>>> levels = [50, 50, 50]
>>> phases = [0, 0, 0]
>>> c1 = makeAsynchChord(freqs=freqs, levels=levels, phases=phases,
tonesDuration=180, tonesRamps=10, tonesChannel='Both',
SOA=60, fs=48000, maxLevel=100)
Convert a white noise into a blue noise.
The spectrum level of the blue noise at the frequency ‘refHz’ will be equal to the spectrum level of the white noise input to the function.
The white noise to be turned into a blue noise.
Sampling frequency of the sound.
Reference frequency in Hz. The amplitude of the other frequencies will be scaled with respect to the amplitude of this frequency.
The array has dimensions (nSamples, 2).
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> noise = makeBlueRef(sig=noise, fs=48000, refHz=1000)
Synthetise a complex Huggings Pitch.
The centre frequency of the F0 of the complex in hertz.
Lowest harmonic component number.
Highest harmonic component number.
The spectrum level of the noise from which the Huggins pitch is derived in dB SPL. If ‘noiseType’ is ‘Pink’, the spectrum level will be equal to ‘spectrumLevel’ at 1 kHz.
Bandwidth of the frequency regions in which the phase transitions occurr.
Defines whether the bandwith of the decorrelated bands is expressed in hertz (Hz), cents (Cent), or equivalent rectangular bandwidths (ERB).
Selects whether the decorrelation in the target regions will be achieved by applying a costant interaural phase shift (IPD), an costant interaural time difference (ITD), or a random IPD shift.
For ‘IPD Linear’ this is the phase difference between the start and the end of each transition region, in radians. For ‘IPD Stepped’, this is the phase offset, in radians, between the correlated and the uncorrelated regions. For ‘ITD’ this is the ITD in the transition region, in micro seconds. For ‘Random Phase’, the range of phase shift randomization in the uncorrelated regions.
If NoSpi, the phase of the regions within each frequency band will be shifted. If NpiSo, the phase of the regions between each frequency band will be shifted.
Harmonic stretch in %F0. Increase each harmonic frequency by a fixed value that is equal to (F0*stretch)/100. If ‘stretch’ is different than zero, an inhanmonic complex tone will be generated.
The type of noise used to derive the Huggins Pitch.
Complex duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
References
Cramer, E. M., & Huggins, W. H. (1958). Creation of Pitch through Binaural Interaction. J. Acoust. Soc. Am., 30(5), 413.
Akeroyd, M. A., & Summerfield, a Q. (2000). The lateralization of simple dichotic pitches. J. Acoust. Soc. Am., 108(1), 316–334.
Zhang, P. X., & Hartmann, W. M. (2008). Lateralization of Huggins pitch. J. Acoust. Soc. Am., 124(6), 3873–87.
Examples
>>> hp = makeHugginsPitch(F0=300, lowHarm=1, highHarm=3, spectrumLevel=45,
bandwidth=100, bandwidthUnit='Hz', dichoticDifference='IPD Stepped',
dichoticDifferenceValue=pi, phaseRelationship='NoSpi', stretch=0,
noiseType='White', duration=380, ramp=10, fs=48000, maxLevel=101)
Synthetise a iterated rippled noise
delay in seconds
The gain to apply to the delayed signal
The number of iterations of the delay-add cycle
If ‘Add Same’, the output of iteration N-1 is added to delayed signal of the current iteration. If ‘Add Original’, the original signal is added to delayed signal of the current iteration.
Intensity spectrum level of the noise in dB SPL.
Noise duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the noise will be generated.
Sampling frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> irn = makeIRN(delay=1/440, gain=1, iterations=6, configuration='Add Same',
spectrumLevel=25, duration=280, ramp=10, channel='Both', fs=48000,
maxLevel=101)
Convert a white noise into a pink noise.
The spectrum level of the pink noise at 1000 Hz will be equal to the spectrum level of the white noise input to the function.
The white noise to be turned into a pink noise.
Sampling frequency of the sound.
The array has dimensions (nSamples, 2).
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> noise = makePink(sig=noise, fs=48000)
Convert a white noise into a pink noise.
The spectrum level of the pink noise at the frequency ‘refHz’ will be equal to the spectrum level of the white noise input to the function.
The white noise to be turned into a pink noise.
Sampling frequency of the sound.
Reference frequency in Hz. The amplitude of the other frequencies will be scaled with respect to the amplitude of this frequency.
The array has dimensions (nSamples, 2).
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> noise = makePinkRef(sig=noise, fs=48000, refHz=1000)
Convert a white noise into a red noise.
The spectrum level of the red noise at the frequency ‘refHz’ will be equal to the spectrum level of the white noise input to the function.
The white noise to be turned into a red noise.
Sampling frequency of the sound.
Reference frequency in Hz. The amplitude of the other frequencies will be scaled with respect to the amplitude of this frequency.
The array has dimensions (nSamples, 2).
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> noise = makeRedRef(sig=noise, fs=48000, refHz=1000)
Generate a silence.
This function just fills an array with zeros for the desired duration.
Duration of the silence in milliseconds.
Samplig frequency in Hz.
The array has dimensions (nSamples, 2).
Examples
>>> sil = makeSilence(duration=200, fs=48000)
Convert a white noise into a violet noise.
The spectrum level of the violet noise at the frequency ‘refHz’ will be equal to the spectrum level of the white noise input to the function.
The white noise to be turned into a violet noise.
Sampling frequency of the sound.
Reference frequency in Hz. The amplitude of the other frequencies will be scaled with respect to the amplitude of this frequency.
The array has dimensions (nSamples, 2).
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> noise = makeVioletRef(sig=noise, fs=48000, refHz=1000)
Next power of two.
Base number.
The power to which 2 should be raised.
Examples
>>> nextpow2(511)
9
>>> 2**9
512
Shift the interaural phases of a sound within a given frequency region.
Input signal.
The start point of the frequency region to be phase-shifted in hertz.
The end point of the frequency region to be phase-shifted in hertz.
The amount of phase shift in radians.
If ‘Linear’ the phase changes progressively on a linear Hz scale from X to X+’phaseShift’ from f1 to f2. If ‘Stepped’ ‘phaseShift’ is added as a constant to the phases from f1 to f2. If ‘Random’ a random phase shift from 0 to ‘phaseShift’ is added to each frequency component from f1 to f2.
The channel in which to apply the phase shift.
The sampling frequency of the sound.
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> hp = phaseShift(sig=noise, f1=500, f2=600, phaseShift=3.14,
phaseShiftType='Linear', channel='Left', fs=48000) #this generates a Dichotic Pitch
Generate a pink noise by adding sinusoids spaced by a fixed interval in cents.
Level of each sinusoidal component in dB SPL.
Frequency of the lowest noise component in hertz.
Frequency of the highest noise component in hertz.
Spacing between the frequencies of the sinusoidal components in hertz.
Noise duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the noise will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> noise = pinkNoiseFromSin(compLevel=23, lowCmp=100, highCmp=1000,
spacing=20, duration=180, ramp=10, channel='Both',
fs=48000, maxLevel=100)
Generate a pink noise by adding sinusoids spaced by a fixed interval in cents.
This function should produce the same output of pinkNoiseFromSin, it simply uses a different algorithm that uses matrix operations instead of a for loop. It doesn’t seem to be much faster though.
Level of each sinusoidal component in dB SPL.
Frequency of the lowest noise component in hertz.
Frequency of the highest noise component in hertz.
Spacing between the frequencies of the sinusoidal components in hertz.
Noise duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the noise will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> noise = pinkNoiseFromSin2(compLevel=23, lowCmp=100, highCmp=1000,
spacing=20, duration=180, ramp=10, channel='Both',
fs=48000, maxLevel=100)
Synthetise a pure tone.
Tone frequency in hertz.
Starting phase in radians.
Tone level in dB SPL.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> pt = pureTone(frequency=440, phase=0, level=65, duration=180,
... ramp=10, channel='Right', fs=48000, maxLevel=100)
>>> pt.shape
(9600, 2)
Increase or decrease the amplitude of a sound signal.
Desired increment or decrement in dB SPL.
Signal to scale.
Examples
>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
... channel='Both', fs=48000, maxLevel=100)
>>> noise = scale(level=-10, sig=noise) #reduce level by 10 dB
Set the RMS level of a sound signal to a given value.
The desired RMS level of the signal in dB SPL.
Signal whose level is to be set.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
Channel in which the level will be set.
Examples
>>> import copy
>>> pt = pureTone(frequency=1000, phase=0, level=60, duration=100,
... ramp=0, channel="Both", fs=48000, maxLevel=100)
>>> pt2 = copy.copy(pt) #this function modifies the argument so make a copy!
>>> pt2 = setLevel_(level=40, snd=pt2, maxLevel=100, channel="Both") #set spectrum level to 20 dB SPL
>>> levDiff = 20*log10(getRMS(pt)[1]/getRMS(pt2)[1])
Generate a broadband noise with a modulated spectral envelope. The modulation is sinuisoidal on a log2 frequency, and logarithmic amplitude (dB) scale.
Intensity spectrum level of the noise in dB SPL (before modulation). Note that the shaped noise is scaled after shaping so as to have the same overall RMS level than the noise before shaping.
Noise duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Modulation amplitude (peak-to-trough difference) in dB.
Modulation frequency in cycles-per-octave
Starting modulation phase
Channel in which the noise will be generated. If ‘Both’ the same noise will be generated in both channels. If ‘Dichotic’ the noise will be independent at the two ears.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
References
Andéol, G., Macpherson, E. A., & Sabin, A. T. (2013). Sound localization in noise and sensitivity to spectral shape. Hearing Research, 304, 20–27. https://doi.org/10.1016/j.heares.2013.06.001
Eddins, D. A., & Bero, E. M. (2007). Spectral modulation detection as a function of modulation frequency, carrier bandwidth, and carrier frequency region. The Journal of the Acoustical Society of America, 121(1), 363–372. https://doi.org/10.1121/1.2382347
Litvak, L. M., Spahr, A. J., Saoji, A. A., & Fridman, G. Y. (2007). Relationship between perception of spectral ripple and speech recognition in cochlear implant and vocoder listeners. The Journal of the Acoustical Society of America, 122(2), 982–991. https://doi.org/10.1121/1.2749413
Examples
>>> noise = spectralModNoise(spectrumLevel=40, duration=180, ramp=10,
... modAmp=10, modFreq=2, phase="Random",
... channel='Both', fs=48000, maxLevel=100)
Synthetise band-limited noise from the addition of random-phase sinusoids.
Start frequency of the noise.
End frequency of the noise.
Noise spectrum level.
Tone duration (excluding ramps) in milliseconds.
Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.
Channel in which the tone will be generated.
Samplig frequency in Hz.
Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.
The array has dimensions (nSamples, 2).
Examples
>>> nbNoise = steepNoise(frequency1=440, frequency2=660, level=65,
... duration=180, ramp=10, channel='Right', fs=48000, maxLevel=100)