77
88from __future__ import print_function
99
10- from . import file_info
10+ from pathlib import Path
11+ from typing import Union , Optional , List
12+
13+ from typing_extensions import Literal
14+
1115from . import core
12- from .log import logger
13- from .core import ENCODING_VALS
14- from .core import is_number
15- from .core import sox
16- from .core import play
16+ from . import file_info
17+ from .core import ENCODING_VALS , EncodingValue
1718from .core import SoxError
1819from .core import SoxiError
1920from .core import VALID_FORMATS
20-
21+ from .core import is_number
22+ from .core import play
23+ from .core import sox
24+ from .log import logger
2125from .transform import Transformer
2226
23-
2427COMBINE_VALS = [
2528 'concatenate' , 'merge' , 'mix' , 'mix-power' , 'multiply'
2629]
2730
31+ CombineType = Literal ['concatenate' , 'merge' , 'mix' , 'mix-power' , 'multiply' ]
32+
2833
2934class Combiner (Transformer ):
3035 '''Audio file combiner.
@@ -38,8 +43,11 @@ class Combiner(Transformer):
3843 def __init__ (self ):
3944 super (Combiner , self ).__init__ ()
4045
41- def build (self , input_filepath_list , output_filepath , combine_type ,
42- input_volumes = None ):
46+ def build (self ,
47+ input_filepath_list : Union [str , Path ],
48+ output_filepath : Union [str , Path ],
49+ combine_type : CombineType ,
50+ input_volumes : Optional [List [float ]] = None ):
4351 '''Builds the output_file by executing the current set of commands.
4452
4553 Parameters
@@ -116,7 +124,10 @@ def build(self, input_filepath_list, output_filepath, combine_type,
116124 logger .info ("[SoX] {}" .format (out ))
117125 return True
118126
119- def preview (self , input_filepath_list , combine_type , input_volumes = None ):
127+ def preview (self ,
128+ input_filepath_list : List [Union [str , Path ]],
129+ combine_type : CombineType ,
130+ input_volumes : Optional [List [float ]] = None ):
120131 '''Play a preview of the output with the current set of effects
121132
122133 Parameters
@@ -155,8 +166,13 @@ def preview(self, input_filepath_list, combine_type, input_volumes=None):
155166
156167 play (args )
157168
158- def set_input_format (self , file_type = None , rate = None , bits = None ,
159- channels = None , encoding = None , ignore_length = None ):
169+ def set_input_format (self ,
170+ file_type : Optional [List [str ]] = None ,
171+ rate : Optional [List [float ]] = None ,
172+ bits : Optional [List [int ]] = None ,
173+ channels : Optional [List [int ]] = None ,
174+ encoding : Optional [List [EncodingValue ]] = None ,
175+ ignore_length : Optional [List [bool ]] = None ):
160176 '''Sets input file format arguments. This is primarily useful when
161177 dealing with audio files without a file extension. Overwrites any
162178 previously set input file arguments.
@@ -312,7 +328,8 @@ def set_input_format(self, file_type=None, rate=None, bits=None,
312328 return self
313329
314330
315- def _validate_file_formats (input_filepath_list , combine_type ):
331+ def _validate_file_formats (input_filepath_list : List [Union [str , Path ]],
332+ combine_type : CombineType ):
316333 '''Validate that combine method can be performed with given files.
317334 Raises IOError if input file formats are incompatible.
318335 '''
@@ -322,7 +339,8 @@ def _validate_file_formats(input_filepath_list, combine_type):
322339 _validate_num_channels (input_filepath_list , combine_type )
323340
324341
325- def _validate_sample_rates (input_filepath_list , combine_type ):
342+ def _validate_sample_rates (input_filepath_list : List [Path ],
343+ combine_type : CombineType ):
326344 ''' Check if files in input file list have the same sample rate
327345 '''
328346 sample_rates = [
@@ -332,11 +350,12 @@ def _validate_sample_rates(input_filepath_list, combine_type):
332350 raise IOError (
333351 "Input files do not have the same sample rate. The {} combine "
334352 "type requires that all files have the same sample rate"
335- .format (combine_type )
353+ .format (combine_type )
336354 )
337355
338356
339- def _validate_num_channels (input_filepath_list , combine_type ):
357+ def _validate_num_channels (input_filepath_list : List [Path ],
358+ combine_type : CombineType ):
340359 ''' Check if files in input file list have the same number of channels
341360 '''
342361 channels = [
@@ -347,12 +366,14 @@ def _validate_num_channels(input_filepath_list, combine_type):
347366 "Input files do not have the same number of channels. The "
348367 "{} combine type requires that all files have the same "
349368 "number of channels"
350- .format (combine_type )
369+ .format (combine_type )
351370 )
352371
353372
354- def _build_input_format_list (input_filepath_list , input_volumes = None ,
355- input_format = None ):
373+ def _build_input_format_list (input_filepath_list : List [Path ],
374+ input_volumes : Optional [List [float ]] = None ,
375+ input_format : Optional [List [List [str ]]] = None ) \
376+ -> List [str ]:
356377 '''Set input formats given input_volumes.
357378
358379 Parameters
@@ -426,7 +447,8 @@ def _build_input_format_list(input_filepath_list, input_volumes=None,
426447 return input_format_list
427448
428449
429- def _build_input_args (input_filepath_list , input_format_list ):
450+ def _build_input_args (input_filepath_list : List [Path ],
451+ input_format_list : List [str ]) -> List [str ]:
430452 ''' Builds input arguments by stitching input filepaths and input
431453 formats together.
432454 '''
@@ -446,7 +468,7 @@ def _build_input_args(input_filepath_list, input_format_list):
446468 return input_args
447469
448470
449- def _validate_combine_type (combine_type ):
471+ def _validate_combine_type (combine_type : List [ CombineType ] ):
450472 '''Check that the combine_type is valid.
451473
452474 Parameters
@@ -462,7 +484,7 @@ def _validate_combine_type(combine_type):
462484 )
463485
464486
465- def _validate_volumes (input_volumes ):
487+ def _validate_volumes (input_volumes : List [ float ] ):
466488 '''Check input_volumes contains a valid list of volumes.
467489
468490 Parameters
@@ -479,5 +501,5 @@ def _validate_volumes(input_volumes):
479501 if not core .is_number (vol ):
480502 raise ValueError (
481503 "Elements of input_volumes must be numbers: found {}"
482- .format (vol )
504+ .format (vol )
483505 )
0 commit comments