music.mix_many

music.mix_many(sonic_vectors, end=False, offset=0, sample_rate=None)[source]

Mix sonic vectors of arbitrary lengths.

The operation consists in summing sample by sample [1]. This function helps when the sonic_vectors are not of the same size.

Parameters:
sonic_vectorslist of sonic_arrays

The sonic vectors to be summed, mono or stereo. If any is stereo, each mono one is put in both channels.

endbool

If True, sync the final samples. If False (default) sync the initial samples.

offsetlist of scalars

The offset of each sound in seconds: the silence put before it, so that with end false it begins that long after the start of the mix. A sound past the end of the list has none, as no sound has by default.

sample_rateinteger or None

The sample rate. Only used if offset is supplied. None, the default, is the rate music.get_sample_rate() returns.

Returns:
Sndarray

A numpy array where each value is a PCM sample of the resulting sound. No sounds mix to no samples.

Raises:
ValueError

If offset is a number rather than a list, holds more offsets than there are sounds, or holds a negative one. Each failed in Python’s words or numpy’s, and an array of offsets, which is taken now as a list is, failed as numpy’s ambiguous truth value.

Notes

The mix is made once, and each sound added where it lies, so it costs the length of the mix and of the sounds. Each sound used to be padded to the length of the whole mix and the copies summed: 600 notes of a tenth of a second, at their offsets across a minute, held 600 minutes of samples, some 12 GB, and took 25 seconds.

Cite the following article whenever you use this function.

References

[1]

Fabbri, Renato, et al. “Musical elements in the discrete-time representation of sound.” arXiv preprint arXiv:abs/1412.6853 (2017)

Examples

>>> mixed = mix_many([note(220, 2), note(440, 1)])
>>> write_wav_mono(mixed)  # writes a WAV of the two notes summed