music.note

music.note(freq=220, duration=2, waveform_table=array([-1., -0.99975586, -0.99951172, ..., -0.99926758, -0.99951172, -0.99975586], shape=(16384,)), number_of_samples=None, sample_rate=None)[source]

Synthesize a basic musical note.

Parameters:
freqscalar

The frequency of the note in Hertz.

durationscalar

The duration of the note in seconds.

waveform_tablearray_like

The table with the waveform to synthesize the sound.

number_of_samplesinteger or None

The number of samples in the sound. If given, duration is ignored, and zero is no samples.

sample_rateinteger or None

The sample rate. None, the default, is the rate music.get_sample_rate() returns.

Returns:
resultndarray

A numpy array where each value is a PCM sample of the note.

Parameters:
  • freq (float)

  • duration (float)

  • waveform_table (ArrayLike)

  • number_of_samples (int | None)

  • sample_rate (int | None)

Return type:

NDArray[float64]

See also

note_with_vibrato

A note with vibrato.

tremolo

A tremolo envelope.

Notes

The note is the waveform table read at the rate its frequency sets, as the article’s eq:lut writes it [1]:

\[t_i = \tilde{l}_{\gamma_i \bmod \tilde{\Lambda}}, \qquad \gamma_i = \left\lfloor i f \frac{\tilde{\Lambda}}{f_s} \right\rfloor, \qquad i = 0, 1, \ldots, \Lambda - 1,\]

where \(\tilde{l}\) is waveform_table, \(\tilde{\Lambda}\) its length, \(f\) is freq, \(f_s\) the sample rate and \(\Lambda = \lfloor f_s \Delta \rfloor\) the samples duration lasts (eq:dur). The index is truncated rather than interpolated, as MASS reads the table, so a waveform whose partials reach past the Nyquist frequency folds them back into the band; bandlimited_note() renders one without them.

A vibrato or an FM changes the frequency at each sample, so it is folded into this lookup, by note_with_vibrato() and note_with_fm(). A tremolo or an AM changes the amplitude, and is an envelope to multiply the note by: tremolo() and am().

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

>>> write_wav_mono(note())  # writes a WAV file of a note
>>> s = horizontal_stack(*[note(i, j) for i, j in
...                       zip([200, 500, 100], [2, 1, 2])])
>>> s2 = note(440, 1.5, waveform_table=WAVEFORM_SAWTOOTH)