sounds

This module defines classes to generate different sounds.

These classes are currently implemented: * Tone : a sinuosoidal pure tone * Noise : a burst of white noise * File : read from a file * Speech * Gap

The behavior of this module depends on prefs.get(‘AUDIOSERVER’). * If this is ‘jack’, or True:

Then import jack, define Jack_Sound, and all sounds inherit from that.

  • If this is ‘pyo’:

    Then import pyo, define PyoSound, and all sounds inherit from that.

  • If this is ‘docs’:

    Then import both jack and pyo, define both Jack_Sound and PyoSound, and all sounds inherit from object.

  • Otherwise:

    Then do not import jack or pyo, or define either Jack_Sound or PyoSound, and all sounds inherit from object.

Todo

Implement sound level and filter calibration

Classes:

Tone(frequency, duration[, amplitude])

The Humble Sine Wave

Noise(duration[, amplitude, channel])

Generates a white noise burst with specified parameters

File(path[, amplitude])

A .wav file.

Gap(duration, **kwargs)

A silent sound that does not pad its final chunk -- used for creating precise silent gaps in a continuous noise.

Gammatone(frequency, duration[, amplitude, ...])

Gammatone filtered noise, using timeseries.Gammatone -- see that class for the filter documentation.

Data:

STRING_PARAMS

These parameters should be given string columns rather than float columns.

Functions:

int_to_float(audio)

Convert 16 or 32 bit integer audio to 32 bit float.

class Tone(frequency, duration, amplitude=0.01, **kwargs)[source]

Bases: autopilot.stim.sound.base.Sound

The Humble Sine Wave

Parameters
  • frequency (float) – frequency of sin in Hz

  • duration (float) – duration of the sin in ms

  • amplitude (float) – amplitude of the sound as a proportion of 1.

  • **kwargs – extraneous parameters that might come along with instantiating us

Attributes:

PARAMS

type

Methods:

init_sound()

Create a sine wave table using pyo or numpy, depending on the server type.

PARAMS = ['frequency', 'duration', 'amplitude']
type = 'Tone'
init_sound()[source]

Create a sine wave table using pyo or numpy, depending on the server type.

table: Optional[numpy.ndarray]
class Noise(duration, amplitude=0.01, channel=None, **kwargs)[source]

Bases: autopilot.stim.sound.base.Sound

Generates a white noise burst with specified parameters

The type attribute is always “Noise”.

Initialize a new white noise burst with specified parameters.

The sound itself is stored as the attribute self.table. This can be 1-dimensional or 2-dimensional, depending on channel. If it is 2-dimensional, then each channel is a column.

Parameters
  • duration (float) – duration of the noise

  • amplitude (float) – amplitude of the sound as a proportion of 1.

  • channel (int or None) – which channel should be used If 0, play noise from the first channel If 1, play noise from the second channel If None, send the same information to all channels (“mono”)

  • **kwargs – extraneous parameters that might come along with instantiating us

Attributes:

PARAMS

type

Methods:

init_sound()

Defines self.table, the waveform that is played.

iter_continuous()

Continuously yield frames of audio.

PARAMS = ['duration', 'amplitude', 'channel']
type = 'Noise'
init_sound()[source]

Defines self.table, the waveform that is played.

The way this is generated depends on self.server_type, because parameters like the sampling rate cannot be known otherwise.

The sound is generated and then it is “chunked” (zero-padded and divided into chunks). Finally self.initialized is set True.

iter_continuous() Generator[source]

Continuously yield frames of audio. If this method is not overridden, just wraps table in a itertools.cycle object and returns from it.

Returns

A single frame of audio

Return type

np.ndarray

table: Optional[numpy.ndarray]
class File(path, amplitude=0.01, **kwargs)[source]

Bases: autopilot.stim.sound.base.Sound

A .wav file.

Todo

Generalize this to other audio types if needed.

Parameters
  • path (str) – Path to a .wav file relative to the prefs.get(‘SOUNDDIR’)

  • amplitude (float) – amplitude of the sound as a proportion of 1.

  • **kwargs – extraneous parameters that might come along with instantiating us

Attributes:

PARAMS

type

Methods:

init_sound()

Load the wavfile with scipy.io.wavfile , converting int to float as needed.

PARAMS = ['path', 'amplitude']
type = 'File'
init_sound()[source]

Load the wavfile with scipy.io.wavfile , converting int to float as needed.

Create a sound table, resampling sound if needed.

table: Optional[numpy.ndarray]
class Gap(duration, **kwargs)[source]

Bases: autopilot.stim.sound.base.Sound

A silent sound that does not pad its final chunk – used for creating precise silent gaps in a continuous noise.

Parameters

duration (float) – duration of gap in ms

Variables

gap_zero (bool) – True if duration is zero, effectively do nothing on play.

Attributes:

type

PARAMS

Methods:

init_sound()

Create and chunk an array of zeros according to Gap.duration

chunk([pad])

If gap is not duration == 0, call parent chunk.

buffer()

play()

type = 'Gap'
PARAMS = ['duration']
table: Optional[numpy.ndarray]
init_sound()[source]

Create and chunk an array of zeros according to Gap.duration

chunk(pad=False)[source]

If gap is not duration == 0, call parent chunk. :Parameters: pad (bool) – unused, passed to parent chunk

buffer()[source]
play()[source]
class Gammatone(frequency: float, duration: float, amplitude: float = 0.01, channel: Optional[int] = None, filter_kwargs: Optional[dict] = None, **kwargs)[source]

Bases: autopilot.stim.sound.sounds.Noise

Gammatone filtered noise, using timeseries.Gammatone – see that class for the filter documentation.

Parameters
  • frequency (float) – Center frequency of filter, in Hz

  • duration (float) – Duration of sound, in ms

  • amplitude (float) – Amplitude scaling of sound (absolute value 0-1, default is .01)

  • filter_kwargs (dict) – passed on to timeseries.Gammatone

Attributes:

type

PARAMS

type = 'Gammatone'
PARAMS = ['frequency', 'duration', 'amplitude', 'channel']
table: Optional[numpy.ndarray]
STRING_PARAMS = ['path', 'type', 'speaker', 'vowel', 'token', 'consonant']

These parameters should be given string columns rather than float columns.

Bother Jonny to do this better bc it’s really bad.

int_to_float(audio)[source]

Convert 16 or 32 bit integer audio to 32 bit float.

Parameters

audio (numpy.ndarray) – a numpy array of audio

Returns

Audio that has been rescaled and converted to a 32 bit float.

Return type

numpy.ndarray