drumscript.detect_tempo

detect_tempo(audio_path, full=False)[source]

Estimates the global tempo (BPM) of a given audio file or pre-loaded audio array.

This function utilizes spectral onset envelope detection to accurately estimate the global tempo of a percussive track.

Parameters:
  • audio_path (str or np.ndarray) – File path (str) OR a pre-loaded audio data array (np.ndarray).

  • full (bool, optional) – Return a detailed stats dictionary instead of just the float if True.

Returns:

The estimated BPM, or a dictionary containing the BPM and sample rate.

Return type:

float or dict

Note

Passing a pre-loaded np.ndarray is significantly faster if you are running multiple analysis functions on the exact same audio file.

Examples:

Detect tempo directly from a file:

import drumscript as ds
bpm = ds.detect_tempo("drum_loop.wav")
print(f"Tempo: {bpm} BPM")

Detect tempo from a pre-loaded array:

y, sr = ds.load_audio("drum_loop.wav")
stats = ds.detect_tempo(y, full=True)
print(stats['bpm'])