Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements the "auto" configuration option. #7

Merged
merged 3 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bpmanalyser:
quiet: no
```

Heads up! THe `auto` option is NOT YET IMPLEMENTED! It will be used to execute the analysis during import.
You can set the `auto` option if you would like to execute the analysis during import. In this case, the `threads` option is ignored (beets import is already multithreaded).


The other configuration options can also be set from the command line when running the plugin.
Expand Down
20 changes: 18 additions & 2 deletions beetsplug/bpmanalyser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# Created: 2/23/20, 10:50 PM
# License: See LICENSE.txt

import logging

from beets.plugins import BeetsPlugin
from beets.util import cpu_count
from beetsplug.bpmanalyser.command import BpmAnalyserCommand

from beetsplug.bpmanalyser.command import BpmAnayserCommand
log = logging.getLogger('beets.bpmanalyser')


class BpmAnalyserPlugin(BeetsPlugin):
Expand All @@ -23,5 +25,19 @@ def __init__(self):
'quiet': False
})

# On-import analysis.
if self.config['auto']:
self.import_stages = [self.imported]

def commands(self):
return [BpmAnayserCommand(self.config)]
return [BpmAnalyserCommand(self.config)]

def imported(self, session, task):
# Add BPM for imported items.
for item in task.imported_items():
if not self.config['force'] and item['bpm'] != 0:
item_path = item.get("path").decode("utf-8")
log.debug("Skipping item with existing BPM[{0}]...".format(item_path))
return
else:
BpmAnalyserCommand(self.config).analyse(item)
58 changes: 31 additions & 27 deletions beetsplug/bpmanalyser/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@

import logging
import os
import sys
from concurrent import futures
from optparse import OptionParser
from subprocess import Popen, PIPE
import sys

from beets.library import Library as BeatsLibrary
from beets.library import Library as BeatsLibrary, Item
from beets.ui import Subcommand, decargs

# Module methods
log = logging.getLogger('beets.bpmanalyser')


class BpmAnayserCommand(Subcommand):
class BpmAnalyserCommand(Subcommand):
config = None
lib = None
query = None
parser = None

cfg_auto = False
cfg_dry_run = False
cfg_write = True
cfg_threads = 1
Expand All @@ -36,7 +35,6 @@ class BpmAnayserCommand(Subcommand):
def __init__(self, cfg):
self.config = cfg.flatten()

self.cfg_auto = self.config.get("auto")
self.cfg_dry_run = self.config.get("dry-run")
self.cfg_write = self.config.get("write")
self.cfg_threads = self.config.get("threads")
Expand Down Expand Up @@ -92,7 +90,7 @@ def __init__(self, cfg):
)

# Keep this at the end
super(BpmAnayserCommand, self).__init__(
super(BpmAnalyserCommand, self).__init__(
parser=self.parser,
name='bpmanalyser',
help=u'analyse your songs for tempo and write it into the bpm tag'
Expand Down Expand Up @@ -121,13 +119,38 @@ def show_version_information(self):
"Bpm Analyser(beets-bpmanalyser) plugin for Beets: v{0}".format(
__version__))

def analyse_songs(self):
def find_analyser_script(self):
module_path = os.path.dirname(__file__)
self.analyser_script_path = os.path.join(module_path, "get_song_bpm.py")
log.debug("External script path: {}".format(self.analyser_script_path))
if not os.path.isfile(self.analyser_script_path):
raise FileNotFoundError("Analyser script not found!")

def analyse(self, item: Item):
if not self.analyser_script_path:
self.find_analyser_script()

item_path = item.get("path").decode("utf-8")
log.debug("Analysing[{0}]...".format(item_path))

bpm, errors = self.get_bpm_from_analyser_script(item_path)

if bpm == 0:
self._say("Bpm[ERROR]: - {}".format(item_path))
# self._say("Bpm[ERROR]: - {}".format(errors))
else:
self._say("Bpm[{}]: {}".format(bpm, item_path))

if not self.cfg_dry_run:
if bpm != 0:
item['bpm'] = bpm
if self.cfg_write:
item.try_write()
item.store()

def analyse_songs(self):
self.find_analyser_script()

# Setup the query
query = self.query
if not self.cfg_force:
Expand All @@ -139,26 +162,7 @@ def analyse_songs(self):
# a limited number of items per run
items = self.lib.items(self.query)

def analyse(item):
item_path = item.get("path").decode("utf-8")
log.debug("Analysing[{0}]...".format(item_path))

bpm, errors = self.get_bpm_from_analyser_script(item_path)

if bpm == 0:
self._say("Bpm[ERROR]: - {}".format(item_path))
# self._say("Bpm[ERROR]: - {}".format(errors))
else:
self._say("Bpm[{}]: {}".format(bpm, item_path))

if not self.cfg_dry_run:
if bpm != 0:
item['bpm'] = bpm
if self.cfg_write:
item.try_write()
item.store()

self.execute_on_items(items, analyse, msg='Analysing tempo...')
self.execute_on_items(items, self.analyse, msg='Analysing tempo...')

def get_bpm_from_analyser_script(self, item_path):
log.debug(
Expand Down