# HG changeset patch # User Amine Sehili # Date 1570380364 -7200 # Node ID 9907db0843cbc1a6e7e5b582114521df98b8acaf # Parent cd9897f4519fddc54e2312904f647d6bac3326be Refactor main script diff -r cd9897f4519f -r 9907db0843cb auditok/cmdline.py --- a/auditok/cmdline.py Sat Oct 05 15:03:22 2019 +0100 +++ b/auditok/cmdline.py Sun Oct 06 18:46:04 2019 +0200 @@ -22,6 +22,7 @@ from auditok import __version__, AudioRegion from .util import AudioDataSource +from .exceptions import EndOfProcessing, AudioEncodingWarning from .io import player_for from .cmdline_util import make_logger, make_kwargs, initialize_workers from . import workers @@ -374,7 +375,7 @@ logger = make_logger(args.debug, args.debug_file) kwargs = make_kwargs(args) reader, observers = initialize_workers( - args, logger=logger, **kwargs.io, **kwargs.miscellaneous + logger=logger, **kwargs.io, **kwargs.miscellaneous ) tokenizer_worker = workers.TokenizerWorker( reader, observers, logger=logger, **kwargs.split @@ -384,13 +385,19 @@ while True: time.sleep(1) if len(threading.enumerate()) == 1: - raise workers.EndOfProcessing + raise EndOfProcessing - except (KeyboardInterrupt, workers.EndOfProcessing): + except (KeyboardInterrupt, EndOfProcessing): if tokenizer_worker is not None: tokenizer_worker.stop_all() - if args.save_stream is not None: - reader.save_stream() + + if isinstance(reader, workers.StreamSaverWorker): + reader.join() + try: + reader.save_stream() + except AudioEncodingWarning as ae_warn: + print(str(ae_warn), file=sys.stderr) + if args.plot or args.save_image is not None: from .plotting import plot_detections @@ -399,8 +406,7 @@ reader.data, reader.sr, reader.sw, reader.ch ) detections = ( - (det.start, det.end) - for det in tokenizer_worker.detections + (det.start, det.end) for det in tokenizer_worker.detections ) plot_detections( record, @@ -413,4 +419,4 @@ if __name__ == "__main__": - sys.exit(main(None)) \ No newline at end of file + sys.exit(main(None)) diff -r cd9897f4519f -r 9907db0843cb auditok/cmdline_util.py --- a/auditok/cmdline_util.py Sat Oct 05 15:03:22 2019 +0100 +++ b/auditok/cmdline_util.py Sun Oct 06 18:46:04 2019 +0200 @@ -25,13 +25,14 @@ io_kwargs = { "input": args_ns.input, "audio_format": args_ns.input_format, - "max_read": args_ns.max_time, + "max_read": args_ns.max_read, "block_dur": args_ns.analysis_window, "sampling_rate": args_ns.sampling_rate, "sample_width": args_ns.sample_width, "channels": args_ns.channels, "use_channel": use_channel, "save_stream": args_ns.save_stream, + "save_detections_as": args_ns.save_detections_as, "export_format": args_ns.output_format, "large_file": args_ns.large_file, "frames_per_buffer": args_ns.frame_per_buffer, @@ -50,6 +51,7 @@ miscellaneous = { "echo": args_ns.echo, + "progress_bar": args_ns.progress_bar, "command": args_ns.command, "quiet": args_ns.quiet, "printf": args_ns.printf, diff -r cd9897f4519f -r 9907db0843cb auditok/exceptions.py --- a/auditok/exceptions.py Sat Oct 05 15:03:22 2019 +0100 +++ b/auditok/exceptions.py Sun Oct 06 18:46:04 2019 +0200 @@ -22,3 +22,9 @@ class AudioEncodingError(Exception): """Raised if audio data can not be encoded in the provided format""" + + +class AudioEncodingWarning(RuntimeWarning): + """Raised if audio data can not be encoded in the provided format + but saved as wav. + """ diff -r cd9897f4519f -r 9907db0843cb auditok/workers.py --- a/auditok/workers.py Sat Oct 05 15:03:22 2019 +0100 +++ b/auditok/workers.py Sun Oct 06 18:46:04 2019 +0200 @@ -12,7 +12,11 @@ from .util import AudioDataSource from .core import split from . import cmdline_util -from .exceptions import EndOfProcessing, AudioEncodingError +from .exceptions import ( + EndOfProcessing, + AudioEncodingError, + AudioEncodingWarning, +) _STOP_PROCESSING = "STOP_PROCESSING" @@ -150,7 +154,7 @@ return getattr(self._reader, name) -class StreamSaverWorker(Worker, AudioDataSource): +class StreamSaverWorker(Worker): def __init__( self, audio_reader, @@ -272,7 +276,7 @@ warn_msg += "'{}'. Either none of 'ffmpeg', 'avconv' or 'sox' " warn_msg += "is installed or this format is not recognized.\n" warn_msg += "Audio file was saved as '{}'" - raise RuntimeWarning( + raise AudioEncodingWarning( warn_msg.format( self._export_format, self._tmp_output_filename ) diff -r cd9897f4519f -r 9907db0843cb tests/test_cmdline_util.py --- a/tests/test_cmdline_util.py Sat Oct 05 15:03:22 2019 +0100 +++ b/tests/test_cmdline_util.py Sun Oct 06 18:46:04 2019 +0200 @@ -26,7 +26,7 @@ "_ArgsNamespece", [ "input", - "max_time", + "max_read", "analysis_window", "sampling_rate", "sample_width", @@ -38,6 +38,7 @@ "frame_per_buffer", "input_device_index", "save_stream", + "save_detections_as", "plot", "save_image", "min_duration", @@ -47,6 +48,7 @@ "strict_min_duration", "energy_threshold", "echo", + "progress_bar", "command", "quiet", "printf", @@ -57,25 +59,28 @@ @genty -class _TestCmdLineUtil(TestCase): +class TestCmdLineUtil(TestCase): @genty_dataset( - no_record=("stream.ogg", False, None, "mix", "mix", False), - no_record_plot=("stream.ogg", True, None, None, None, False), + no_record=("stream.ogg", None, False, None, "mix", "mix", False), + no_record_plot=("stream.ogg", None, True, None, None, None, False), no_record_save_image=( "stream.ogg", + None, True, "image.png", None, None, False, ), - record_plot=(None, True, None, None, None, True), - record_save_image=(None, False, "image.png", None, None, True), - int_use_channel=("stream.ogg", False, None, "1", 1, False), + record_plot=(None, None, True, None, None, None, True), + record_save_image=(None, None, False, "image.png", None, None, True), + int_use_channel=("stream.ogg", None, False, None, "1", 1, False), + save_detections_as=("stream.ogg", "{id}.wav", False, None, None, None, False) ) def test_make_kwargs( self, save_stream, + save_detections_as, plot, save_image, use_channel, @@ -97,6 +102,7 @@ None, 1, save_stream, + save_detections_as, plot, save_image, 0.2, @@ -106,8 +112,7 @@ False, 55, ) - misc = (False, None, True, None, "TIME_FORMAT", "TIMESTAMP_FORMAT") - + misc = (False, False, None, True, None, "TIME_FORMAT", "TIMESTAMP_FORMAT") args_ns = _ArgsNamespece(*(args + misc)) io_kwargs = { @@ -119,6 +124,7 @@ "channels": 2, "use_channel": exp_use_channel, "save_stream": save_stream, + "save_detections_as": save_detections_as, "audio_format": "raw", "export_format": "ogg", "large_file": True, @@ -139,6 +145,7 @@ miscellaneous = { "echo": False, "command": None, + "progress_bar": False, "quiet": True, "printf": None, "time_format": "TIME_FORMAT", diff -r cd9897f4519f -r 9907db0843cb tests/test_workers.py --- a/tests/test_workers.py Sat Oct 05 15:03:22 2019 +0100 +++ b/tests/test_workers.py Sun Oct 06 18:46:04 2019 +0200 @@ -4,6 +4,7 @@ from tempfile import TemporaryDirectory from genty import genty, genty_dataset from auditok import AudioRegion, AudioDataSource +from auditok.exceptions import AudioEncodingWarning from auditok.cmdline_util import make_logger from auditok.workers import ( TokenizerWorker, @@ -308,7 +309,7 @@ tokenizer.start_all() tokenizer.join() saver.join() - with self.assertRaises(RuntimeWarning) as rt_warn: + with self.assertRaises(AudioEncodingWarning) as rt_warn: saver.save_stream() warn_msg = "Couldn't save audio data in the desired format " warn_msg += "'ogg'. Either none of 'ffmpeg', 'avconv' or 'sox' "