SoundSequence class#
The SoundSequence class is a combination of the sound of a SoundStimulus (or stimuli) and the timing information from a Sequence.
- class thebeat.core.SoundSequence(sound, sequence, sequence_time_unit='ms', name=None)[source]#
The
SoundSequence
class can be thought of as a combination of theSoundStimulus
class and theSequence
class; hence SoundSequence. It combines the timing information of aSequence
object with the auditory information (sound) of aSoundStimulus
object. In most research one would refer to aSoundSequence
as a trial (which is also the variable name used in some of the examples here). Remember that aSequence
object is agnostic about the used time unit, so when constructing aSoundSequence
object, you can specify the time unit of theSequence
object using thesequence_time_unit
parameter (see underSequence.__init__()
.One can construct a
SoundSequence
object either by passing it a singleSoundStimulus
object (and aSequence
object), or by passing it an array or list ofSoundStimulus
objects (and aSequence
object).If a single
SoundStimulus
object is passed, this SoundStimulus sound is used for each event onset. Otherwise, eachSoundStimulus
sound is used for its respective event onsets. Of course, then the number ofSoundStimulus
objects in the iterable must be the same as the number of event onsets.SoundSequence
objects can be plotted, played, written to disk, statistically analyzed, and more… During construction, checks are done to ensure you dit not accidentally use sounds that are longer than the IOIs (impossible), that the sampling frequencies of all theSoundStimulus
objects are the same (undesirable), and that theSoundStimulus
objects’ number of channels are the same (probable).- __init__(sound, sequence, sequence_time_unit='ms', name=None)[source]#
Initialize a
SoundSequence
object using aSoundStimulus
object, or list or array ofSoundStimulus
objects, and aSequence
object.During the construction of a
SoundSequence
object, sound is generated on the basis of the passedSoundStimulus
objects and the passedSequence
object. A warning is issued if the frame number, where one of the sounds would be placed, had to be rounded off. To get rid of this warning, you can use theSequence.round_onsets()
method before passing it to the :py:class`SoundSequence` constructor, or try a different sampling frequency for the :py:class`SoundStimulus` sound.- Parameters:
sound (
SoundStimulus
|list
[SoundStimulus
] |ndarray
[Any
,dtype
[SoundStimulus
]]) – Either a singleSoundStimulus
object (in which case the same sound is used for each event onset), or a list or array ofSoundStimulus
objects (in which case different sounds are used for each event onset).sequence (
Sequence
) – ASequence
object. This contains the timing information for the played events.sequence_time_unit (
str
, default:'ms'
) – If theSequence
object was created using seconds, use “s”. The default is milliseconds (“ms”).name (
Optional
[str
], default:None
) – You can provide a name for theSoundSequence
which is sometimes used (e.g. when printing the object, or when plotting one). You can always retrieve this attribute fromSoundSequence.name
.
Examples
>>> sound = SoundStimulus.generate(freq=440) >>> seq = Sequence.generate_isochronous(n_events=5, ioi=500) >>> trial = SoundSequence(sound, seq)
>>> from random import randint >>> sounds = [SoundStimulus.generate(freq=randint(100, 1000)) for x in range(5)] >>> seq = Sequence.generate_isochronous(n_events=5, ioi=500) >>> trial = SoundSequence(sounds, seq)
- copy(deep=True)[source]#
Returns a copy of itself. See
copy.copy()
for more information.- Parameters:
deep (
bool
, default:True
) – IfTrue
, a deep copy is returned. IfFalse
, a shallow copy is returned.
- property duration: float64#
Property that returns the summed total of the inter-onset intervals.
- property mean_ioi: float64#
The average inter-onset interval (IOI).
- merge(other)[source]#
Merge this
SoundSequence
object with one or multiple otherSoundSequence
objects.Returns a new
SoundSequence
object.- Parameters:
other (
SoundSequence
|list
[SoundSequence
]) – ASoundSequence
object, or a list ofSoundSequence
objects.- Returns:
A
SoundSequence
object.- Return type:
- property onsets: ndarray#
Returns the event onsets (t values) on the basis of the sequence objects’ inter-onset intervals (IOIs).
- play(loop=False, metronome=False, metronome_amplitude=1.0)[source]#
This method uses the
sounddevice.play()
to play the object’s audio.- Parameters:
loop (
bool
, default:False
) – IfTrue
, theSoundSequence
will continue playing until theSoundSequence.stop()
method is called.metronome (
bool
, default:False
) – IfTrue
, a metronome sound is added for playback.metronome_amplitude (
float
, default:1.0
) – If desired, when playing the object with a metronome sound you can adjust the metronome amplitude. A value between 0 and 1 means a less loud metronome, a value larger than 1 means a louder metronome sound.
Examples
>>> sound = SoundStimulus.generate(offramp_ms=10) >>> seq = Sequence.generate_random_normal(n_events=10, mu=500, sigma=50) >>> seq.round_onsets() >>> soundseq = SoundSequence(sound, seq) >>> soundseq.play(metronome=True)
- plot_sequence(linewidth=None, **kwargs)[source]#
Plot the
SoundSequence
object as an event plot on the basis of the event onsets and their durations. Seethebeat.visualization.plot_single_sequence()
.- Parameters:
linewidth (
UnionType
[float
,list
[float
],ndarray
[Any
,dtype
[float
]],None
], default:None
) – The desired width of the bars (events). Defaults to the event durations. Can be a single value that will be used for each onset, or a list or array of values (i.e with a value for each respective onsets).**kwargs – Additional parameters (e.g. ‘title’, ‘dpi’ etc.) are passed to
thebeat._helpers.plot_single_sequence()
.
Examples
>>> sound = SoundStimulus.generate() >>> seq = Sequence.generate_isochronous(n_events=5,ioi=500) >>> trial = SoundSequence(sound, seq) >>> trial.plot_sequence()
- plot_waveform(**kwargs)[source]#
Plot the
SoundSequence
object as a waveform.- Parameters:
**kwargs – Additional parameters (e.g. ‘title’, ‘dpi’ are passed to
thebeat.helpers.plot_waveform()
).
Examples
>>> sound = SoundStimulus.generate() >>> seq = Sequence.generate_isochronous(n_events=10,ioi=500) >>> trial = SoundSequence(sound, seq) >>> trial.plot_waveform()
- stop()[source]#
Stop playing the
SoundSequence
sound. Callssounddevice.stop()
.- Return type:
Examples
>>> import time >>> sound = SoundStimulus.generate() >>> seq = Sequence([500, 300, 800]) >>> soundseq = SoundSequence(sound, seq) >>> soundseq.play() >>> time.sleep(secs=1) >>> soundseq.stop()
- static write_multichannel_wav(soundsequences, filepath, dtype=<class 'numpy.int16'>)[source]#
This is a static method that is used to write multiple
SoundSequence
objects to a single multichannel .wav file. This can e.g. be useful when creating stimuli that have a synchronization stimulus on one of the other (e.g. third) channels. This may be useful e.g. for EEG experiments.- Parameters:
soundsequences (
list
[SoundSequence
]) – A list ofSoundSequence
objects.
Examples
>>> sound = SoundStimulus.generate() >>> seq1 = Sequence.generate_random_normal(n_events=5, mu=500, sigma=50) >>> seq1.round_onsets() >>> seq2 = Sequence.generate_isochronous(n_events=5, ioi=500) >>> trial1 = SoundSequence(sound, seq1) >>> trial2 = SoundSequence(sound, seq2) >>> SoundSequence.write_multichannel_wav([trial1, trial2], 'my_soundseqs.wav')
- write_wav(filepath, dtype=<class 'numpy.int16'>, metronome=False, metronome_ioi=None, metronome_amplitude=None)[source]#
- Parameters:
filepath (
str
|PathLike
) – The output destination for the .wav file. Either pass e.g. a Path object, or a pass a string. Of course be aware of OS-specific filepath conventions.dtype (
str
|dtype
, default:<class 'numpy.int16'>
) – The data type of the samples. Defaults tonp.int16
, meaning that the samples are saved as 16-bit integers.metronome (
bool
, default:False
) – IfTrue
, a metronome sound is added for playback.metronome_ioi (
Optional
[float
], default:None
) – If desired, when playing the StimSequence with a metronome sound you can adjust the metronome inter-onset interval (IOI). Defaults to the mean IOI of the sequence.metronome_amplitude (
Optional
[float
], default:None
) – If desired, when playing the StimSequence with a metronome sound you can adjust the metronome amplitude. A value between 0 and 1 means a less loud metronme, a value larger than 1 means a louder metronome sound.
Examples
>>> sound = SoundStimulus.generate() >>> seq = Sequence.generate_isochronous(n_events=5,ioi=500) >>> soundseq = SoundSequence(sound, seq) >>> soundseq.write_wav('my_soundseq.wav')