Note
Go to the end to download the full example code.
Onset detection
The onset strength envelope and the peaks picked from it, as times in seconds.
import matplotlib.pyplot as plt
import numpy as np
import audio_samples as aus
from audio_samples import effects
Four decaying clicks half a second apart.
envelope = audio.onset_strength()[0]
onsets = audio.onset_detect(units="time")
print(onsets)
assert np.max(np.abs(onsets - times)) < 0.05
t = np.arange(envelope.size) * 512 / sr
fig, ax = plt.subplots(figsize=(7, 3))
ax.plot(t, envelope, label="Onset strength")
ax.vlines(onsets, 0, envelope.max(), color="C3", ls="--", label="Detected onsets")
ax.set(xlabel="Time (s)", title="Onset detection")
ax.legend()

[0.2554195 0.7662585 1.25387755 1.76471655]
<matplotlib.legend.Legend object at 0x7efce5105590>
Total running time of the script: (0 minutes 0.082 seconds)