Skip to content

Commit

Permalink
Merge pull request #62 from christophehenry/resample-rate
Browse files Browse the repository at this point in the history
Add CLI option to force output resample rate
  • Loading branch information
kassoulet committed Jan 17, 2022
2 parents f1611d6 + 87732e8 commit 735fffe
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
9 changes: 9 additions & 0 deletions bin/soundconverter
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import sys
from optparse import OptionParser, OptionGroup
import locale
import gettext

_ = gettext.gettext
import pkg_resources

Expand Down Expand Up @@ -84,6 +85,8 @@ from soundconverter.interface.ui import gui_main
from soundconverter.util.logger import logger, update_verbosity
from soundconverter.gstreamer.converter import Converter

from soundconverter.interface.preferences import rates


def mode_callback(option, opt, value, parser, **kwargs):
"""Write the main mode (batch, gui, tags) into the options."""
Expand Down Expand Up @@ -148,6 +151,12 @@ def parse_command_line():
'-D', '--delete-original', action='store_true', dest='delete-original',
help=_('Deletes the original file when conversion is done.')
)
parser.add_option(
'-R', '--output-resample', dest='output-resample',
help=_('Resamples audio during conversion. Possible values: %(rates)s') %
{'rates': ", ".join([str(item) for item in rates])},
type=int
)

# batch mode settings
batch_option_group = OptionGroup(
Expand Down
17 changes: 17 additions & 0 deletions soundconverter/interface/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from gi.repository import GLib, Gio

from soundconverter.interface.preferences import rates
from soundconverter.util.soundfile import SoundFile
from soundconverter.util.settings import settings, set_gio_settings
from soundconverter.util.formats import get_quality_setting_name, \
Expand Down Expand Up @@ -127,6 +128,12 @@ def use_memory_gsettings(options):
gio_settings.set_boolean('limit-jobs', True)
gio_settings.set_int('number-of-jobs', 1)

resample = options.get('output-resample')

if resample is not None:
gio_settings.set_boolean('output-resample', True)
gio_settings.set_int('resample-rate', resample)


def validate_args(options):
"""Check if required command line args are provided.
Expand Down Expand Up @@ -229,6 +236,16 @@ def validate_args(options):
logger.error('opus bitrate should be between 6 and 510')
return False

resample = options.get('output-resample', None)

if resample and resample not in rates:
logger.error(
"Possible values for -R or --output-resample option are {rates}".format(
rates=", ".join([str(item for item in rates)])
)
)
return False

return True


Expand Down
4 changes: 2 additions & 2 deletions soundconverter/interface/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
('audio/ogg; codecs=opus', 'opusenc', 'Opus (.opus)'),
]

rates = [8000, 11025, 16000, 22050, 32000, 44100, 48000, 96000, 128000]


class PreferencesDialog(GladeWindow):
sensitive_names = [
Expand Down Expand Up @@ -219,7 +221,6 @@ def set_widget_initial_values(self):
cell = Gtk.CellRendererText()
self.resample_rate.pack_start(cell, True)
self.resample_rate.add_attribute(cell, 'text', 0)
rates = [8000, 11025, 16000, 22050, 32000, 44100, 48000, 96000, 128000]
rate = self.settings.get_int('resample-rate')
try:
idx = rates.index(rate)
Expand Down Expand Up @@ -488,7 +489,6 @@ def on_mp3_quality_changed(self, combobox):

def on_resample_rate_changed(self, combobox):
selected = combobox.get_active()
rates = [8000, 11025, 16000, 22050, 32000, 44100, 48000, 96000, 128000]
self.settings.set_int('resample-rate', rates[selected])
self.update_example()

Expand Down
18 changes: 18 additions & 0 deletions tests/testcases/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def test_validate_args(self):
'main': 'batch', 'output-path': '.',
'format': 'ogg', 'quality': 20
}))
self.assertFalse(validate_args({'output-resample': 160}))

def test_use_memory_gsettings_cbr(self):
use_memory_gsettings({
Expand Down Expand Up @@ -349,6 +350,23 @@ def test_set_delete_original_true(self):
gio_settings = get_gio_settings()
self.assertTrue(gio_settings.get_boolean('delete-original'))

def test_set_output_sample(self):
gio_settings = get_gio_settings()
self.assertFalse(gio_settings.get_boolean('output-resample'))
self.assertEqual(48000, gio_settings.get_int('resample-rate'))

use_memory_gsettings({
'output-path': '.',
'main': 'batch',
'format': 'ogg',
'quality': '0.5',
'output-resample': 44100
})

gio_settings = get_gio_settings()
self.assertTrue(gio_settings.get_boolean('output-resample'))
self.assertEqual(44100, gio_settings.get_int('resample-rate'))


if __name__ == "__main__":
unittest.main()
22 changes: 22 additions & 0 deletions tests/testcases/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,28 @@ def test_set_delete_original_true(self):
self.assertTrue(gio_settings.get_boolean('delete-original'))
self.assertFalse(os.path.isfile('tests/tmp/a.wav'))

def test_set_output_resample(self):
gio_settings = get_gio_settings()
self.assertFalse(gio_settings.get_boolean('output-resample'))
self.assertEqual(48000, gio_settings.get_int('resample-rate'))

os.system('cp "tests/test data/audio//b/c.mp3" "tests/tmp/c.mp3"')
self.assertTrue(os.path.isfile('tests/tmp/c.mp3'))

sample_rate = 8000

launch([
'-b', 'tests/tmp/c.mp3',
'-o', 'tests/tmp',
'-f', 'wav',
'-R', str(sample_rate)
])

gio_settings = get_gio_settings()
self.assertTrue(gio_settings.get_boolean('output-resample'))
self.assertEqual(gio_settings.get_int('resample-rate'), sample_rate)
self.assertEqual(self.get_bitrate('tests/tmp/c.wav'), sample_rate * 8 / 1000 * 2)

def test_conversion_no_tags(self):
launch([
'-b', 'tests/test data/no tags',
Expand Down

0 comments on commit 735fffe

Please sign in to comment.