autobpm: Do not import 'beat' subpackage immediately to improve startup time

This commit is contained in:
Šarūnas Nejus 2024-08-15 17:31:30 +01:00
parent 5b5c564a69
commit e3776f1910
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
2 changed files with 9 additions and 3 deletions

View file

@ -15,7 +15,7 @@
from typing import Iterable
from librosa import beat, load
import librosa
from soundfile import LibsndfileError
from beets import ui, util
@ -63,7 +63,9 @@ class AutoBPMPlugin(BeetsPlugin):
continue
try:
y, sr = load(util.syspath(item.path), res_type="kaiser_fast")
y, sr = librosa.load(
util.syspath(item.path), res_type="kaiser_fast"
)
except LibsndfileError as exc:
self._log.error(
"LibsndfileError: failed to load {0} {1}",
@ -80,7 +82,7 @@ class AutoBPMPlugin(BeetsPlugin):
continue
kwargs = self.config["beat_track_kwargs"].flatten()
tempo, _ = beat.beat_track(y=y, sr=sr, **kwargs)
tempo, _ = librosa.beat.beat_track(y=y, sr=sr, **kwargs)
bpm = round(tempo[0] if isinstance(tempo, Iterable) else tempo)
item["bpm"] = bpm
self._log.info(

View file

@ -60,6 +60,10 @@ Other changes:
* :doc:`plugins/autobpm`: Add plugin dependencies to `pyproject.toml` under
the `autobpm` extra and update the plugin installation instructions in the
docs.
Since importing the bpm calculation functionality from ``librosa`` takes
around 4 seconds, update the plugin to only do so when it actually needs to
calculate the bpm. Previously this import was being done immediately, so
every ``beet`` invocation was being delayed by a couple of seconds.
:bug:`5185`
2.0.0 (May 30, 2024)