music.Sequencer

class music.Sequencer(sample_rate=<factory>, events=<factory>)[source]

Bases: object

Schedules notes and renders them as audio.

Attributes:
sample_rateinteger

The sampling frequency in Hertz, used for every note rendered and for turning each event’s start time into a sample offset. It defaults to music.get_sample_rate()’s rate when the sequencer is made.

eventslist of NoteEvent

The scheduled notes. Normally built with add_note() rather than passed in, and rendered in order of their start times rather than the order they were added.

Raises:
ValueError

If sample_rate is not positive. At zero every note rendered as no samples, and the sequence as silence of no length.

Parameters:
  • sample_rate (int)

  • events (List[NoteEvent])

See also

music.StimulationSession

phases in sequence rather than notes at offsets, for stimulation protocols.

Examples

>>> seq = Sequencer()
>>> for i, freq in enumerate([440, 550, 660]):
...     seq.add_note(freq, start=i * 0.25, duration=1.0)
>>> len(seq.events)
3
>>> seq.write("chord.wav")
__init__(sample_rate=<factory>, events=<factory>)
Parameters:
  • sample_rate (int)

  • events (List[NoteEvent])

Return type:

None

Methods

__init__([sample_rate, events])

add_note(freq, start, duration[, ...])

Add a note event to the sequencer.

render()

Render all scheduled events and return the audio array.

write(filename[, bit_depth, normalize])

Write the rendered audio to a file, as write_audio() does.

Attributes

sample_rate: int
events: List[NoteEvent]
add_note(freq, start, duration, vibrato_freq=0.0, max_pitch_dev=0.0, adsr_params=None, spatial=None)[source]

Add a note event to the sequencer.

Parameters:
freqscalar

The note’s frequency in Hertz.

startscalar

When it starts, in seconds from the start of the sequence.

durationscalar

How long it lasts, in seconds.

vibrato_freqscalar

The rate of a vibrato in Hertz. With max_pitch_dev, both nonzero, the note is rendered by note_with_vibrato().

max_pitch_devscalar

The depth of that vibrato.

adsr_paramsdict, optional

Arguments for adsr(), applied to the note.

spatialdict, optional

Arguments for localize(), which makes the note stereo.

Raises:
ValueError

If start or duration is negative or not finite, or adsr_params or spatial names sonic_vector or sample_rate, which the sequencer supplies itself. A negative start was placed at a negative sample offset and failed with a broadcasting error, NaN and infinity failed in round() when rendering, and a second sample_rate as a TypeError there. A negative duration rendered no note, silently, and one that was not finite failed when rendering.

If freq, vibrato_freq or max_pitch_dev is NaN or infinite, which the note’s oscillator refuses only when the sequence renders, without saying which note it was.

Parameters:
Return type:

None

render()[source]

Render all scheduled events and return the audio array.

Returns:
ndarray

The notes summed, each from its start, as long as the last one to end: mono, or (2, nsamples) once any note is placed with spatial, the mono ones then in both channels. No notes render no samples.

Return type:

ndarray

Notes

Each note starts at the sample nearest its start. The mix is made once, as long as the note that ends last, and each note is added where it starts, so a render costs the length of the piece and of its notes. It used to grow the mix by a copy for every note, which cost each one the length of everything before it: 2,000 notes in a row, 200 s of audio, took half a minute to render, a tenth of a second of it synthesis.

write(filename, bit_depth=16, normalize=True)[source]

Write the rendered audio to a file, as write_audio() does.

Parameters:
filenamestr

The path to write: .wav or .flac.

bit_depthinteger

The bit depth of the samples written.

normalizebool

Normalize to full scale, as by default, or write the samples as they are, refusing a sound that reaches past [-1, 1], which keeps the level of a sequence written beside others.

Raises:
ValueError

If there are no notes to write: the empty render reached the normalization, which blamed a duration computed as zero. Or as write_audio() raises it.

Parameters:
Return type:

None