SoundSequence class
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
SoundSequenceclass can be thought of as a combination of theSoundStimulusclass and theSequenceclass; hence SoundSequence. It combines the timing information of aSequenceobject with the auditory information (sound) of aSoundStimulusobject. In most research one would refer to aSoundSequenceas a trial (which is also the variable name used in some of the examples here). Remember that aSequenceobject is agnostic about the used time unit, so when constructing aSoundSequenceobject, you can specify the time unit of theSequenceobject using thesequence_time_unitparameter (see underSequence.__init__().One can construct a
SoundSequenceobject either by passing it a singleSoundStimulusobject (and aSequenceobject), or by passing it an array or list ofSoundStimulusobjects (and aSequenceobject).If a single
SoundStimulusobject is passed, this SoundStimulus sound is used for each event onset. Otherwise, eachSoundStimulussound is used for its respective event onsets. Of course, then the number ofSoundStimulusobjects in the iterable must be the same as the number of event onsets.SoundSequenceobjects 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 theSoundStimulusobjects are the same (undesirable), and that theSoundStimulusobjects’ number of channels are the same (probable).- __init__(sound, sequence, sequence_time_unit='ms', name=None)[source]#
Initialize a
SoundSequenceobject using aSoundStimulusobject, or list or array ofSoundStimulusobjects, and aSequenceobject.During the construction of a
SoundSequenceobject, sound is generated on the basis of the passedSoundStimulusobjects and the passedSequenceobject. 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 (
Union[SoundStimulus,list[SoundStimulus],ndarray[Any,dtype[SoundStimulus]]]) – Either a singleSoundStimulusobject (in which case the same sound is used for each event onset), or a list or array ofSoundStimulusobjects (in which case different sounds are used for each event onset).sequence (
Sequence) – ASequenceobject. This contains the timing information for the played events.sequence_time_unit (
str, default:'ms') – If theSequenceobject was created using seconds, use “s”. The default is milliseconds (“ms”).name (
Optional[str], default:None) – You can provide a name for theSoundSequencewhich 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=False)#
Returns a copy of itself. See
copy.copy()for more information.- Parameters
deep (
bool, default:False) – IfTrue, a deep copy is returned. IfFalse, a shallow copy is returned.
- property duration: numpy.float64#
Property that returns the summed total of the inter-onset intervals.
- property iois: numpy.ndarray#
The inter-onset intervals (IOIs) of the Sequence object. These are the intervals in milliseconds between the onset of an event, and the onset of the next event. This is the most important attribute of the Sequence class and is used throughout.
This getter returns a copy of the IOIs instead of the actual attribute.
- property mean_ioi: numpy.float64#
The average inter-onset interval (IOI).
- merge(other)[source]#
Merge this
SoundSequenceobject with one or multiple otherSoundSequenceobjects.Returns a new
SoundSequenceobject.- Parameters
other (
Union[SoundSequence,list[SoundSequence]]) – ASoundSequenceobject, or a list ofSoundSequenceobjects.- Returns
A
SoundSequenceobject.- Return type
- property onsets: numpy.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, theSoundSequencewill 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) >>> soundseq = SoundSequence(sound, seq) >>> soundseq.play(metronome=True)
- plot_sequence(linewidth=None, **kwargs)[source]#
Plot the
SoundSequenceobject as an event plot on the basis of the event onsets and their durations. Seethebeat.visualization.plot_single_sequence().- Parameters
linewidth (
Union[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
SoundSequenceobject as a waveform.- Parameters
**kwargs – Additional parameters (e.g. ‘title’, ‘dpi’ are passed to
thebeat.helpers.plot_waveform()).
Examples
>>> trial = SoundSequence(SoundStimulus.generate(), Sequence.generate_isochronous(n_events=10, ioi=500)) >>> trial.plot_waveform()
- stop()[source]#
Stop playing the
SoundSequencesound. 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()
- write_wav(filepath, metronome=False, metronome_amplitude=1.0)[source]#
- Parameters
filepath (
Union[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.metronome (
bool, default:False) – IfTrue, a metronome sound is added for playback.metronome_amplitude (
float, default:1.0) – 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
>>> soundseq = SoundSequence(SoundStimulus.generate(),Sequence.generate_isochronous(n_events=5,ioi=500)) >>> soundseq.write_wav('my_soundseq.wav')