librosa.tone#

librosa.tone(frequency, *, sr=22050, length=None, duration=None, phi=None, taper=False)[source]#

Construct a pure tone (cosine) signal at a given frequency.

Parameters:
frequencyfloat > 0 or str

frequency (in Hz) or note name in SPN format (e.g., ‘A4’)

srnumber > 0

desired sampling rate of the output signal

lengthint > 0

desired number of samples in the output signal. When both duration and length are defined, length takes priority.

durationfloat > 0

desired duration in seconds. When both duration and length are defined, length takes priority.

phifloat or None

phase offset, in radians. If unspecified, defaults to -np.pi * 0.5.

taperbool

If True, taper the edges of the signal with a cosine fade-in and fade-out of one period duration.

Returns:
tone_signalnp.ndarray [shape=(length,), dtype=float64]

Synthesized pure sine tone signal

Raises:
ParameterValueError
  • If frequency is not provided.

  • If neither length nor duration are provided.

Examples

Generate a pure sine tone A4

>>> tone = librosa.tone("A4", duration=1, sr=22050)

Or generate the same signal using length

>>> tone = librosa.tone("A4", sr=22050, length=22050)

Display spectrogram

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> S = librosa.feature.melspectrogram(y=tone)
>>> librosa.display.specshow(
...     S, vscale="dBFS[power]", x_axis="time", y_axis="mel", ax=ax
... )
../../_images/librosa-tone-1.png