From 365fa4347eca720fdcea84a2c840aec154f4651c Mon Sep 17 00:00:00 2001 From: "Andrew G. Dunn" Date: Fri, 12 Oct 2012 07:48:52 -0400 Subject: [PATCH 1/3] Added processor/thread detection, by default will now use maximum available processor count instead of 2. Idea adapted from soundconverter, credits in function. --- beetsplug/convert.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index 089962c21..c5d871e5b 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -16,6 +16,7 @@ """ import logging import os +import sys import shutil from subprocess import Popen, PIPE @@ -29,6 +30,32 @@ DEVNULL = open(os.devnull, 'wb') conf = {} +def _cpu_count(): + """ Returns the number of CPUs in the system. + Code was adapted from observing the soundconverter project: + https://github.com/kassoulet/soundconverter + """ + if sys.platform == 'win32': + try: + num = int(os.environ['NUMBER_OF_PROCESSORS']) + except (ValueError, KeyError): + num = 0 + elif sys.platform == 'darwin': + try: + num = int(os.popen('sysctl -n hw.ncpu').read()) + except ValueError: + num = 0 + else: + try: + num = os.sysconf('SC_NPROCESSORS_ONLN') + except (ValueError, OSError, AttributeError): + num = 0 + if num >= 1: + return num + else: + return 1 + + def _embed(path, items): """Embed an image file, located at `path`, into each item. """ @@ -135,7 +162,8 @@ def convert_func(lib, config, opts, args): class ConvertPlugin(BeetsPlugin): def configure(self, config): conf['dest'] = ui.config_val(config, 'convert', 'dest', None) - conf['threads'] = ui.config_val(config, 'convert', 'threads', 2) + conf['threads'] = ui.config_val(config, 'convert', 'threads', + _cpu_count) conf['flac'] = ui.config_val(config, 'convert', 'flac', 'flac') conf['lame'] = ui.config_val(config, 'convert', 'lame', 'lame') conf['opts'] = ui.config_val(config, 'convert', @@ -150,7 +178,8 @@ class ConvertPlugin(BeetsPlugin): cmd.parser.add_option('-a', '--album', action='store_true', help='choose albums instead of tracks') cmd.parser.add_option('-t', '--threads', action='store', type='int', - help='change the number of threads (default 2)') + help='change the number of threads, \ + defaults to maximum availble processors ') cmd.parser.add_option('-d', '--dest', action='store', help='set the destination directory') cmd.func = convert_func From 4ee39ed9da3b218266046b601f688e94f59e4b74 Mon Sep 17 00:00:00 2001 From: "Andrew G. Dunn" Date: Fri, 12 Oct 2012 08:25:28 -0400 Subject: [PATCH 2/3] Forgot to actually call the function --- beetsplug/convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index c5d871e5b..3f76e70c3 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -163,7 +163,7 @@ class ConvertPlugin(BeetsPlugin): def configure(self, config): conf['dest'] = ui.config_val(config, 'convert', 'dest', None) conf['threads'] = ui.config_val(config, 'convert', 'threads', - _cpu_count) + _cpu_count()) conf['flac'] = ui.config_val(config, 'convert', 'flac', 'flac') conf['lame'] = ui.config_val(config, 'convert', 'lame', 'lame') conf['opts'] = ui.config_val(config, 'convert', From 780cbed80937520ae1be8ae5334320e1b082cb42 Mon Sep 17 00:00:00 2001 From: "Andrew G. Dunn" Date: Fri, 12 Oct 2012 16:57:12 -0400 Subject: [PATCH 3/3] Made a simple update to the documentation --- docs/plugins/convert.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/plugins/convert.rst b/docs/plugins/convert.rst index f9d2b120c..f62cd963c 100644 --- a/docs/plugins/convert.rst +++ b/docs/plugins/convert.rst @@ -45,11 +45,12 @@ will be copied). ``opts`` are the encoding options that are passed to ``lame`` The ``dest`` sets the directory the files will be converted (or copied) to. This is a required setting and has to be set either in ``.beetsconfig`` or on the commandline. Finally ``threads`` lets you determine the number of threads -to use for encoding (default: 2). An example configuration:: +to use for encoding. By default the convert plugin will detect the maximum +available cores within a system and use them all. An example configuration:: [convert] embed:false max_bitrate:200 opts:-V4 dest:/home/user/MusicForPhone - threads:4 + threads:4 \ No newline at end of file