sndlib A module for generating sounds in python.

sndlib – Sound Synthesis Library

A module for generating sounds in python.

sndlib.AMTone(frequency=1000, AMFreq=20, AMDepth=1, phase=0, AMPhase=0, level=60, duration=980, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate an amplitude modulated tone.

Parameters:
frequency : float

Carrier frequency in hertz.

AMFreq : float

Amplitude modulation frequency in Hz.

AMDepth : float

Amplitude modulation depth (a value of 1 corresponds to 100% modulation).

phase : float

Starting phase in radians.

AMPhase : float

Starting AM phase in radians.

level : float

Average tone level in dB SPL. See notes.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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

[H]Hartmann, W. M. (2004). Signals, Sound, and Sensation. Springer Science & Business Media
[V79]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
[YSO]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)
sndlib.AMToneIPD(frequency=1000, AMFreq=20, AMDepth=1, phase=0, AMPhase=0, phaseIPD=0, AMPhaseIPD=0, level=60, duration=980, ramp=10, channel=u'Right', fs=48000, maxLevel=101)[source]

Generate an amplitude modulated tone with an interaural phase difference (IPD) in the carrier and/or modulation phase.

Parameters:
frequency : float

Carrier frequency in hertz.

AMFreq : float

Amplitude modulation frequency in Hz.

AMDepth : float

Amplitude modulation depth (a value of 1 corresponds to 100% modulation).

phase : float

Starting phase in radians.

AMPhase : float

Starting AM phase in radians.

phaseIPD : float

IPD to apply to the carrier phase.

AMPhaseIPD : float

IPD to apply to the modulation phase.

level : float

Average tone level in dB SPL. See notes.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’)

Channel in which the phase will be shifted.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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

[H]Hartmann, W. M. (2004). Signals, Sound, and Sensation. Springer Science & Business Media
[V79]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
[YSO]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)
sndlib.AMToneVarLev(frequency=1000, AMFreq=20, AMDepth=1, phase=0, AMPhase=0, level=60, duration=980, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate an amplitude modulated (AM) tone.

Parameters:
frequency : float

Carrier frequency in hertz.

AMFreq : float

Amplitude modulation frequency in Hz.

AMDepth : float

Amplitude modulation depth (a value of 1 corresponds to 100% modulation).

phase : float

Starting phase in radians.

AMPhase : float

Starting AM phase in radians.

level : float

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.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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

[H]Hartmann, W. M. (2004). Signals, Sound, and Sensation. Springer Science & Business Media
[YSO]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)
sndlib.ERBDistance(f1, f2)[source]

Compute the distance in equivalent rectangular bandwiths (ERBs) between f1 and f2.

Parameters:
f1 : float

frequency 1 in Hz

f2 : float

frequency 2 in Hz

Returns:
deltaERB : float

distance between f1 and f2 in ERBs

References

[GM]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)
sndlib.FMTone(fc=1000, fm=40, mi=1, phase=0, level=60, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate a frequency modulated tone.

Parameters:
fc : float

Carrier frequency in hertz. This is the frequency of the tone at fm zero crossing.

fm : float

Modulation frequency in Hz.

mi : float

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.

phase : float

Starting phase in radians.

level : float

Tone level in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’ or ‘Both’

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

Examples

>>> snd = FMTone(fc=1000, fm=40, mi=1, phase=0, level=55, duration=180,
...     ramp=10, channel='Both', fs=48000, maxLevel=100)
sndlib.ITDShift(sig, f1, f2, ITD, channel, fs)[source]

Set the ITD of a sound within the frequency region bounded by ‘f1’ and ‘f2’

Parameters:
sig : array of floats

Input signal.

f1 : float

The start point of the frequency region to be phase-shifted in hertz.

f2 : float

The end point of the frequency region to be phase-shifted in hertz.

ITD : float

The amount of ITD shift in microseconds

channel : string (‘Right’ or ‘Left’)

The channel in which to apply the shift.

fs : float

The sampling frequency of the sound.

Returns:
out : 2-dimensional array of floats

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
sndlib.addSounds(snd1, snd2, delay, fs)[source]

Add or concatenate two sounds.

Parameters:
snd1 : array of floats

First sound.

snd2 : array of floats

Second sound.

delay : float

Delay in milliseconds between the onset of ‘snd1’ and the onset of ‘snd2’

fs : float

Sampling frequency in hertz of the two sounds.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.binauralPureTone(frequency=1000, phase=0, level=60, duration=980, ramp=10, channel=u'Both', itd=0, itdRef=u'Right', ild=10, ildRef=u'Right', fs=48000, maxLevel=101)[source]

Generate a pure tone with an optional interaural time or level difference.

Parameters:
frequency : float

Tone frequency in hertz.

phase : float

Starting phase in radians.

level : float

Tone level in dB SPL. If ‘ild’ is different than zero, this will be the level of the tone in the reference channel.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the tone will be generated.

itd : float

Interaural time difference, in microseconds.

itdRef : ‘Right’, ‘Left’ or None

The reference channel for the ‘itd’. The interaural time difference will be applied to the other channel with respect to the reference channel.

ild : float

Interaural level difference in dB SPL.

ildRef : ‘Right’, ‘Left’ or None

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.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.broadbandNoise(spectrumLevel=25, duration=980, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetise a broadband noise.

Parameters:
spectrumLevel : float

Intensity spectrum level of the noise in dB SPL.

duration : float

Noise duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (“Right”, “Left”, “Both”, or “Dichotic”)

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.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

The array has dimensions (nSamples, 2).

Examples

>>> noise = broadbandNoise(spectrumLevel=40, duration=180, ramp=10,
...     channel="Both", fs=48000, maxLevel=100)
sndlib.camSinFMComplex(F0=150, lowHarm=1, highHarm=10, harmPhase=u'Sine', fm=5, deltaCams=1, fmPhase=3.141592653589793, level=60, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate a complex tone frequency modulated with an exponential sinusoid.

Parameters:
F0 : float

Fundamental aarrier frequency in hertz.

lowHarm: int

Lowest harmonic number.

highHarm: int

Highest harmonic number.

harmPhase: string

Harmonic phase relationship. One of ‘Sine’, ‘Cosine’, or ‘Alternating’.

fm : float

Modulation frequency in Hz.

deltaCams : float

Frequency excursion in cam units (ERBn number scale).

fmPhase : float

Starting fmPhase in radians.

level : float

Tone level in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’ or ‘Both’

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.camSinFMTone(fc=450, fm=5, deltaCams=1, fmPhase=3.141592653589793, startPhase=0, level=60, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=100)[source]

Generate a tone frequency modulated with an exponential sinusoid.

Parameters:
fc : float

Carrier frequency in hertz.

fm : float

Modulation frequency in Hz.

deltaCams : float

Frequency excursion in cam units (ERBn number scale).

fmPhase : float

Starting fmPhase in radians.

level : float

Tone level in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’ or ‘Both’

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.chirp(freqStart=440, ftype=u'linear', rate=500, level=60, duration=980, phase=0, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetize a chirp, that is a tone with frequency changing linearly or exponentially over time with a give rate.

Parameters:
freqStart : float

Starting frequency in hertz.

ftype : string

If ‘linear’, the frequency will change linearly on a Hz scale. If ‘exponential’, the frequency will change exponentially on a cents scale.

rate : float

Rate of frequency change, Hz/s if ftype is ‘linear’, and cents/s if ftype is ‘exponential’.

level : float

Level of the tone in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.complexTone(F0=220, harmPhase=u'Sine', lowHarm=1, highHarm=10, stretch=0, level=60, duration=980, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetise a complex tone.

Parameters:
F0 : float

Tone fundamental frequency in hertz.

harmPhase : one of ‘Sine’, ‘Cosine’, ‘Alternating’, ‘Random’, ‘Schroeder-’, ‘Schroeder+’

Phase relationship between the partials of the complex tone.

lowHarm : int

Lowest harmonic component number.

highHarm : int

Highest harmonic component number.

stretch : float

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.

level : float

The level of each partial in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’, ‘Both’, ‘Odd Right’ or ‘Odd Left’

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’.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.complexToneIPD(F0=220, harmPhase=u'Sine', lowHarm=1, highHarm=10, stretch=0, level=60, duration=980, ramp=10, IPD=3.14, targetEar=u'Right', fs=48000, maxLevel=101)[source]

Synthetise a complex tone with an interaural phase difference (IPD).

Parameters:
F0 : float

Tone fundamental frequency in hertz.

harmPhase : one of ‘Sine’, ‘Cosine’, ‘Alternating’, ‘Random’, ‘Schroeder-’, ‘Schroeder+’

Phase relationship between the partials of the complex tone.

lowHarm : int

Lowest harmonic component number.

highHarm : int

Highest harmonic component number.

stretch : float

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.

level : float

The level of each partial in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

IPD : float

Interaural phase difference, in radians.

targetEar : string

The ear in which the phase will be shifted.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.complexToneParallel(F0=220, harmPhase=u'Sine', lowHarm=1, highHarm=10, stretch=0, level=0, duration=980, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

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.

Parameters:
F0 : float

Tone fundamental frequency in hertz.

harmPhase : one of ‘Sine’, ‘Cosine’, ‘Alternating’, ‘Random’, ‘Schroeder-’, ‘Schroeder+’

Phase relationship between the partials of the complex tone.

lowHarm : int

Lowest harmonic component number.

highHarm : int

Highest harmonic component number.

stretch : float

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.

level : float

The level of each partial in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’, ‘Both’, ‘Odd Right’ or ‘Odd Left’

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’.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.delayAdd(sig, delay, gain, iterations, configuration, fs)[source]

Delay and add algorithm for the generation of iterated rippled noise.

Parameters:
sig : array of floats

The signal to manipulate

delay : float

delay in seconds

gain : float

The gain to apply to the delayed signal

iterations : int

The number of iterations of the delay-add cycle

configuration : string

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.

fs : int

Sampling frequency in Hz.

Returns:
snd : 2-dimensional array of floats

The array has dimensions (nSamples, 2).

References

[YPS1996]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)
sndlib.dichoticNoiseFromSin(F0=300, lowHarm=1, highHarm=3, compLevel=30, narrowBandCompLevel=30, lowFreq=40, highFreq=2000, compSpacing=10, sigBandwidth=100, distanceUnit=u'Cent', phaseRelationship=u'NoSpi', dichoticDifference=u'IPD Stepped', dichoticDifferenceValue=3.141592653589793, duration=380, ramp=10, fs=48000, maxLevel=101)[source]

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.

Parameters:
F0 : float

Centre frequency of the fundamental in hertz.

lowHarm : int

Lowest harmonic component number.

highHarm : int

Highest harmonic component number.

compLevel : float

Level of each sinusoidal frequency component of the noise.

lowFreq : float

Lowest frequency in hertz of the noise.

highFreq : float

Highest frequency in hertz of the noise.

compSpacing : float

Spacing between the sinusoidal components used to generate the noise.

sigBandwidth : float

Width of each harmonically related frequency band.

distanceUnit : string (one of ‘Hz’, ‘Cent’, ‘ERB’)

The unit of measure used for ‘compSpacing’ and ‘sigBandwidth’

phaseRelationship : string (‘NoSpi’ or ‘NpiSo’)

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.

dichoticDifference : string (‘IPD Stepped’, ‘IPD Random’, ‘ITD’, ‘ILD Right’, ‘ILD Left’)

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.

dichoticDifferenceValue : float

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.

narrowBandCompLevel : float

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.

duration : float

Sound duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.expAMNoise(fc=150, fm=2.5, deltaCents=1200, fmPhase=3.141592653589793, AMDepth=1, spectrumLevel=24, duration=480, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate a sinusoidally amplitude-modulated noise with an exponentially modulated AM frequency.

Parameters:
fc : float

Carrier AM frequency in hertz.

fm : float

Modulation of the AM frequency in Hz.

deltaCents : float

AM frequency excursion in cents. The instataneous AM frequency of the noise will vary from fc**(-deltaCents/1200) to fc**(deltaCents/1200).

fmPhase : float

Starting phase of the AM modulation in radians.

AMDepth : float

Amplitude modulation depth.

spectrumLevel : float

Noise spectrum level in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’ or ‘Both’

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.expSinFMComplex(F0=150, lowHarm=1, highHarm=10, harmPhase=u'Sine', fm=40, deltaCents=1200, fmPhase=0, level=60, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate a frequency-modulated complex tone with an exponential sinusoid.

Parameters:
fc : float

Carrier frequency in hertz.

fm : float

Modulation frequency in Hz.

deltaCents : float
Frequency excursion in cents. The instataneous frequency of the tone

will vary from fc**(-deltaCents/1200) to fc**(+deltaCents/1200).

fmPhase : float

Starting fmPhase in radians.

level : float

Tone level in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’ or ‘Both’

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.expSinFMTone(fc=450, fm=5, deltaCents=300, fmPhase=3.141592653589793, startPhase=0, level=60, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate a frequency-modulated tone with an exponential sinusoid.

Parameters:
fc : float

Carrier frequency in hertz.

fm : float

Modulation frequency in Hz.

deltaCents : float
Frequency excursion in cents. The instataneous frequency of the tone

will vary from fc**(-deltaCents/1200) to fc**(+deltaCents/1200).

fmPhase : float

Starting fmPhase in radians.

level : float

Tone level in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’ or ‘Both’

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.fir2Filt(f1, f2, f3, f4, snd, fs)[source]

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.

Parameters:
f1 : float

Frequency in hertz of the point at which the transition for the low-frequency cutoff ends.

f2 : float

Frequency in hertz of the point at which the transition for the low-frequency cutoff starts.

f3 : float

Frequency in hertz of the point at which the transition for the high-frequency cutoff starts.

f4 : float

Frequency in hertz of the point at which the transition for the high-frequency cutoff ends.

snd : array of floats

The sound to be filtered.

fs : int

Sampling frequency of ‘snd’.

Returns:
snd : 2-dimensional array of floats

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
sndlib.fm_complex1(midF0=140, harmPhase=u'Sine', lowHarm=1, highHarm=10, level=60, duration=430, ramp=10, fmFreq=1.25, fmDepth=40, fmStartPhase=4.71238898038469, fmStartTime=25, fmDuration=400, levelAdj=True, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetise a complex tone with an embedded FM starting and stopping at a chosen time after the tone onset.

Parameters:
midF0 : float

F0 at the FM zero crossing

harmPhase : one of ‘Sine’, ‘Cosine’, ‘Alternating’, ‘Random’, ‘Schroeder-’, or ‘Schroeder+’

Phase relationship between the partials of the complex tone.

lowHarm : int

Lowest harmonic component number.

highHarm : int

Highest harmonic component number.

level : float

The level of each partial in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

fmFreq : float

FM frequency in Hz.

fmDepth : float

FM depth in %

fmStartPhase : float

Starting phase of FM

fmStartTime : float

Start of FM in ms after start of tone

fmDuration : float

Duration of FM, in ms

levelAdj : logical

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: ‘Right’, ‘Left’, ‘Both’, ‘Odd Right’ or ‘Odd Left’

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’.

fs : int

Samplig frequency in Hz.

maxLevel : float

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)
sndlib.fm_complex2(midF0=140, harmPhase=u'Sine', lowHarm=1, highHarm=10, level=60, duration=430, ramp=10, fmFreq=1.25, fmDepth=40, fmStartPhase=4.71238898038469, fmStartTime=25, fmDuration=400, levelAdj=True, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetise a complex tone with an embedded FM starting and stopping at a chosen time after the tone onset.

Parameters:
midF0 : float

F0 at the FM zero crossing

harmPhase : one of ‘Sine’, ‘Cosine’, ‘Alternating’, ‘Random’, ‘Schroeder-’, ‘Schroeder+’

Phase relationship between the partials of the complex tone.

lowHarm : int

Lowest harmonic component number.

highHarm : int

Highest harmonic component number.

level : float

The level of each partial in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

fmFreq : float

FM frequency in Hz.

fmDepth : float

FM depth in %

fmStartPhase : float

Starting phase of FM

fmStartTime : float

Start of FM in ms after start of tone

fmDuration : float

Duration of FM, in ms

levelAdj : logical

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: ‘Right’, ‘Left’, ‘Both’, ‘Odd Right’ or ‘Odd Left’

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’.

fs : int

Samplig frequency in Hz.

maxLevel : float

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)
sndlib.freqFromERBInterval(f1, deltaERB)[source]

Compute the frequency, in Hz, corresponding to a distance, in equivalent rectangular bandwidths (ERBs), of ‘deltaERB’ from f1.

Parameters:
f1 : float

frequency at one end of the interval in Hz

deltaERB : float

distance in ERBs

Returns:
f2 : float

frequency at the other end of the interval in Hz

References

[GM]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])
sndlib.gate(ramps, sig, fs)[source]

Impose onset and offset ramps to a sound.

Parameters:
ramps : float

The duration of the ramps.

sig : array of floats

The signal on which the ramps should be imposed.

fs : int

The sampling frequency os ‘sig’

Returns:
sig : array of floats

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)
sndlib.getRMS(sig, channel=u'each')[source]

Compute the root mean square (RMS) value of the signal.

Parameters:
sig : array of floats

The signal for which the RMS needs to be computed.

channel : string or int

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.

Returns:
rms : float

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")
sndlib.glide(freqStart=440, ftype=u'exponential', excursion=500, level=60, duration=180, phase=0, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetize a rising or falling tone glide with frequency changing linearly or exponentially.

Parameters:
freqStart : float

Starting frequency in hertz.

ftype : string

If ‘linear’, the frequency will change linearly on a Hz scale. If ‘exponential’, the frequency will change exponentially on a cents scale.

excursion : float

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 : float

Level of the tone in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.harmComplFromNarrowbandNoise(F0=440, lowHarm=1, highHarm=8, level=40, bandwidth=80, bandwidthUnit=u'Hz', stretch=0, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate an harmonic complex tone from narrow noise bands.

Parameters:
F0 : float

Fundamental frequency of the complex.

lowHarm : int

Lowest harmonic component number. The first component is #1.

highHarm : int

Highest harmonic component number.

level : float

The spectrum level of the noise bands in dB SPL.

bandwidth : float

The width of each noise band.

bandwidthUnit : string (‘Hz’, ‘Cent’, ‘ERB’)

Defines whether the bandwith of the noise bands is expressed in hertz (Hz), cents (Cent), or equivalent rectangular bandwidths (ERB).

stretch : float

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.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : ‘Right’, ‘Left’, ‘Both’, ‘Odd Right’ or ‘Odd Left’

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’.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : array of floats

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)
sndlib.imposeLevelGlide(sig, deltaL, startTime, endTime, channel, fs)[source]

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.

Parameters:
sig : float

Sound on which to impose the level change.

deltaL : float

Magnitude of the level change in dB SPL.

startTime : float

Start of the level transition in milliseconds.

endTime : float

End of the level transition in milliseconds.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel to which apply the level transition.

fs : int

Samplig frequency of the sound in Hz.

Returns:
snd : array of floats

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)
sndlib.intNCyclesFreq(freq, duration)[source]

Compute the frequency closest to ‘freq’ that has an integer number of cycles for the given sound duration.

Parameters:
frequency : float

Frequency in hertz.

duration : float

Duration of the sound, in milliseconds.

Returns:
adjFreq : float

Examples

>>> intNCyclesFreq(freq=2.1, duration=1000)
2.0
>>> intNCyclesFreq(freq=2, duration=1000)
2.0
sndlib.itdtoipd(itd, freq)[source]

Convert an interaural time difference to an equivalent interaural phase difference for a given frequency.

Parameters:
itd : float

Interaural time difference in seconds.

freq : float

Frequency in hertz.

Returns:
ipd : float

Examples

>>> itd = 300 #microseconds
>>> itd = 300/1000000 #convert to seconds
>>> itdtoipd(itd=itd, freq=1000)
sndlib.joinSndISI(sndList, ISIList, fs)[source]

Join a list of sounds with given interstimulus intervals

Parameters:
sndList : list of arrays

The sounds to be joined.

ISIList : list of floats

The interstimulus intervals between the sounds in milliseconds. This list should have one element less than the sndList.

fs : int

Sampling frequency of the sounds in Hz.

Returns:
snd : array of floats

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)
sndlib.makeAsynchChord(freqs, levels, phases, tonesDuration, tonesRamps, tonesChannel, SOA, fs, maxLevel)[source]

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.

Parameters:
freqs : array or list of floats.

Frequencies of the chord components in hertz.

levels : array or list of floats.

Level of each chord component in dB SPL.

phases : array or list of floats.

Starting phase of each chord component.

tonesDuration : float

Duration of the tones composing the chord in milliseconds. All tones have the same duration.

tonesRamps : float

Duration of the onset and offset ramps in milliseconds. The total duration of the tones will be tonesDuration+ramp*2.

tonesChannel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the tones will be generated.

SOA : float

Onset asynchrony between the chord components.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.makeBlueRef(sig, fs, refHz)[source]

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.

Parameters:
sig : array of floats

The white noise to be turned into a blue noise.

fs : int

Sampling frequency of the sound.

refHz : int

Reference frequency in Hz. The amplitude of the other frequencies will be scaled with respect to the amplitude of this frequency.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.makeHugginsPitch(F0=300, lowHarm=1, highHarm=3, spectrumLevel=45, bandwidth=100, bandwidthUnit=u'Hz', dichoticDifference=u'IPD Stepped', dichoticDifferenceValue=3.141592653589793, phaseRelationship=u'NoSpi', stretch=0, noiseType=u'White', duration=480, ramp=10, fs=48000, maxLevel=101)[source]

Synthetise a complex Huggings Pitch.

Parameters:
F0 : float

The centre frequency of the F0 of the complex in hertz.

lowHarm : int

Lowest harmonic component number.

highHarm : int

Highest harmonic component number.

spectrumLevel : float

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 : float

Bandwidth of the frequency regions in which the phase transitions occurr.

bandwidthUnit : string (‘Hz’, ‘Cent’, ‘ERB’)

Defines whether the bandwith of the decorrelated bands is expressed in hertz (Hz), cents (Cent), or equivalent rectangular bandwidths (ERB).

dichoticDifference : string (‘IPD Linear’, ‘IPD Stepped’, ‘IPD Random’, ‘ITD’)

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.

dichoticDifferenceValue : float

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.

phaseRelationship : string (‘NoSpi’ or ‘NpiSo’)

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.

stretch : float

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.

noiseType : string (‘White’ or ‘Pink’)

The type of noise used to derive the Huggins Pitch.

duration : float

Complex duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

The array has dimensions (nSamples, 2).

References

[CH]Cramer, E. M., & Huggins, W. H. (1958). Creation of Pitch through Binaural Interaction. J. Acoust. Soc. Am., 30(5), 413.
[AS]Akeroyd, M. A., & Summerfield, a Q. (2000). The lateralization of simple dichotic pitches. J. Acoust. Soc. Am., 108(1), 316–334.
[ZH]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)
sndlib.makeIRN(delay=0.0022727272727272726, gain=1, iterations=6, configuration=u'Add Same', spectrumLevel=25, duration=280, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetise a iterated rippled noise

Parameters:
delay : float

delay in seconds

gain : float

The gain to apply to the delayed signal

iterations : int

The number of iterations of the delay-add cycle

configuration : string

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.

spectrumLevel : float

Intensity spectrum level of the noise in dB SPL.

duration : float

Noise duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’, ‘Both’, or ‘Dichotic’)

Channel in which the noise will be generated.

fs : int

Sampling frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.makePink(sig, fs)[source]

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.

Parameters:
sig : array of floats

The white noise to be turned into a pink noise.

fs : int

Sampling frequency of the sound.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.makePinkRef(sig, fs, refHz)[source]

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.

Parameters:
sig : array of floats

The white noise to be turned into a pink noise.

fs : int

Sampling frequency of the sound.

refHz : int

Reference frequency in Hz. The amplitude of the other frequencies will be scaled with respect to the amplitude of this frequency.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.makeRedRef(sig, fs, refHz)[source]

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.

Parameters:
sig : array of floats

The white noise to be turned into a red noise.

fs : int

Sampling frequency of the sound.

refHz : int

Reference frequency in Hz. The amplitude of the other frequencies will be scaled with respect to the amplitude of this frequency.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.makeSilence(duration=1000, fs=48000)[source]

Generate a silence.

This function just fills an array with zeros for the desired duration.

Parameters:
duration : float

Duration of the silence in milliseconds.

fs : int

Samplig frequency in Hz.

Returns:
snd : 2-dimensional array of floats

The array has dimensions (nSamples, 2).

Examples

>>> sil = makeSilence(duration=200, fs=48000)
sndlib.makeVioletRef(sig, fs, refHz)[source]

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.

Parameters:
sig : array of floats

The white noise to be turned into a violet noise.

fs : int

Sampling frequency of the sound.

refHz : int

Reference frequency in Hz. The amplitude of the other frequencies will be scaled with respect to the amplitude of this frequency.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.nextpow2(x)[source]

Next power of two.

Parameters:
x : int

Base number.

Returns:
out : float

The power to which 2 should be raised.

Examples

>>> nextpow2(511)
9
>>> 2**9
512
sndlib.phaseShift(sig, f1, f2, phaseShift, phaseShiftType, channel, fs)[source]

Shift the interaural phases of a sound within a given frequency region.

Parameters:
sig : array of floats

Input signal.

f1 : float

The start point of the frequency region to be phase-shifted in hertz.

f2 : float

The end point of the frequency region to be phase-shifted in hertz.

phaseShift : float

The amount of phase shift in radians.

phaseShiftType : string (‘Linear’, ‘Step’, ‘Random’)

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.

channel : string (one of ‘Right’, ‘Left’ or ‘Both’)

The channel in which to apply the phase shift.

fs : float

The sampling frequency of the sound.

Returns:
out : 2-dimensional array of floats

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
sndlib.pinkNoiseFromSin(compLevel=23, lowCmp=100, highCmp=1000, spacing=20, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Generate a pink noise by adding sinusoids spaced by a fixed interval in cents.

Parameters:
compLevel : float

Level of each sinusoidal component in dB SPL.

lowCmp : float

Frequency of the lowest noise component in hertz.

highCmp : float

Frequency of the highest noise component in hertz.

spacing : float

Spacing between the frequencies of the sinusoidal components in hertz.

duration : float

Noise duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the noise will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.pinkNoiseFromSin2(compLevel=23, lowCmp=100, highCmp=1000, spacing=20, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

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.

Parameters:
compLevel : float

Level of each sinusoidal component in dB SPL.

lowCmp : float

Frequency of the lowest noise component in hertz.

highCmp : float

Frequency of the highest noise component in hertz.

spacing : float

Spacing between the frequencies of the sinusoidal components in hertz.

duration : float

Noise duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the noise will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.pureTone(frequency=1000, phase=0, level=60, duration=980, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetise a pure tone.

Parameters:
frequency : float

Tone frequency in hertz.

phase : float

Starting phase in radians.

level : float

Tone level in dB SPL.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)
sndlib.scale(level, sig)[source]

Increase or decrease the amplitude of a sound signal.

Parameters:
level : float

Desired increment or decrement in dB SPL.

signal : array of floats

Signal to scale.

Returns:
sig : 2-dimensional array of floats

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
sndlib.setLevel_(level, snd, maxLevel, channel=u'Both')[source]

Set the RMS level of a sound signal to a given value.

Parameters:
level : float

The desired RMS level of the signal in dB SPL.

snd : array of floats

Signal whose level is to be set.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the level will be set.

Returns:
sig : 2-dimensional array of floats

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])
sndlib.spectralModNoise(spectrumLevel=25, duration=980, ramp=10, modAmp=10, modFreq=1, phase=u'Random', channel=u'Both', fs=48000, maxLevel=101)[source]

Generate a broadband noise with a modulated spectral envelope. The modulation is sinuisoidal on a log2 frequency, and logarithmic amplitude (dB) scale.

Parameters:
spectrumLevel : float

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.

duration : float

Noise duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

modAmp: float

Modulation amplitude (peak-to-trough difference) in dB.

modFreq: float

Modulation frequency in cycles-per-octave

modPhase: string

Starting modulation phase

channel : string (“Right”, “Left”, “Both”, or “Dichotic”)

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.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
sig : 2-dimensional array of floats

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)
sndlib.steepNoise(frequency1=440, frequency2=660, level=60, duration=180, ramp=10, channel=u'Both', fs=48000, maxLevel=101)[source]

Synthetise band-limited noise from the addition of random-phase sinusoids.

Parameters:
frequency1 : float

Start frequency of the noise.

frequency2 : float

End frequency of the noise.

level : float

Noise spectrum level.

duration : float

Tone duration (excluding ramps) in milliseconds.

ramp : float

Duration of the onset and offset ramps in milliseconds. The total duration of the sound will be duration+ramp*2.

channel : string (‘Right’, ‘Left’ or ‘Both’)

Channel in which the tone will be generated.

fs : int

Samplig frequency in Hz.

maxLevel : float

Level in dB SPL output by the soundcard for a sinusoid of amplitude 1.

Returns:
snd : 2-dimensional array of floats

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)