11import io
22import os
3- import traceback
43from typing import Any , List , Tuple , cast
54from dask .distributed import Client
65from distributed import progress
@@ -69,47 +68,42 @@ def augment(params: Tuple[S3FileSystem, str, str, str]) -> None:
6968
7069 fs , file_path , source_directory , output_directory = params
7170
72- try :
73- file_dir = os .path .dirname (file_path )
74- stem_file_path = os .path .join (file_dir , 'stem.ogg' )
71+ file_dir = os .path .dirname (file_path )
72+ stem_file_path = os .path .join (file_dir , 'stem.ogg' )
73+ file_dir = os .path .dirname (file_path )
74+ relative_path = os .path .relpath (file_dir , source_directory )
75+ output_file_path = os .path .join (output_directory , relative_path + '-original' )
76+
77+ full_track_output_file_path = os .path .join (output_file_path , os .path .basename (file_path ))
78+
79+ if not fs .exists (full_track_output_file_path ):
80+ fs .makedirs (os .path .dirname (full_track_output_file_path ), exist_ok = True )
81+ if fs .exists (file_path ):
82+ fs .copy (file_path , full_track_output_file_path )
83+
84+ stem_output_file_path = os .path .join (output_file_path , os .path .basename (stem_file_path ))
85+
86+ if not fs .exists (stem_output_file_path ):
87+ fs .makedirs (os .path .dirname (full_track_output_file_path ), exist_ok = True )
88+ if fs .exists (stem_file_path ):
89+ fs .copy (stem_file_path , stem_output_file_path )
90+
91+ for i in range (AUGMENTATIONS_COUNT ):
7592 file_dir = os .path .dirname (file_path )
7693 relative_path = os .path .relpath (file_dir , source_directory )
77- output_file_path = os .path .join (output_directory , relative_path + '-original' )
78-
94+ output_file_path = os .path .join (output_directory , relative_path + f'-augmented{ i } ' )
7995 full_track_output_file_path = os .path .join (output_file_path , os .path .basename (file_path ))
80-
81- if not fs .exists (full_track_output_file_path ):
82- fs .makedirs (os .path .dirname (full_track_output_file_path ), exist_ok = True )
83- if fs .exists (file_path ):
84- fs .copy (file_path , full_track_output_file_path )
85-
8696 stem_output_file_path = os .path .join (output_file_path , os .path .basename (stem_file_path ))
8797
88- if not fs .exists (stem_output_file_path ):
89- fs .makedirs (os .path .dirname (full_track_output_file_path ), exist_ok = True )
90- if fs .exists (stem_file_path ):
91- fs .copy (stem_file_path , stem_output_file_path )
92-
93- for i in range (AUGMENTATIONS_COUNT ):
94- file_dir = os .path .dirname (file_path )
95- relative_path = os .path .relpath (file_dir , source_directory )
96- output_file_path = os .path .join (output_directory , relative_path + f'-augmented{ i } ' )
97- full_track_output_file_path = os .path .join (output_file_path , os .path .basename (file_path ))
98- stem_output_file_path = os .path .join (output_file_path , os .path .basename (stem_file_path ))
99-
100- if not fs .exists (full_track_output_file_path ) or not fs .exists (stem_output_file_path ):
101- fs .makedirs (output_file_path , exist_ok = True )
102- augment_pitch_and_tempo (
103- fs ,
104- [
105- (file_path , full_track_output_file_path ),
106- (stem_file_path , stem_output_file_path )
107- ]
108- )
109-
110- except Exception as e :
111- print (f'Error augmenting file { file_path } : { e } ' )
112- print (traceback .format_exc ())
98+ if not fs .exists (full_track_output_file_path ) or not fs .exists (stem_output_file_path ):
99+ fs .makedirs (output_file_path , exist_ok = True )
100+ augment_pitch_and_tempo (
101+ fs ,
102+ [
103+ (file_path , full_track_output_file_path ),
104+ (stem_file_path , stem_output_file_path )
105+ ]
106+ )
113107
114108
115109def augment_all (source_directory : str , output_directory : str ):
@@ -127,7 +121,7 @@ def augment_all(source_directory: str, output_directory: str):
127121 params_list : List [Tuple [S3FileSystem , str , str , str ]] = [(fs , file_path , source_directory , output_directory ) for file_path in files ]
128122
129123 print ('Augmenting audio tracks' )
130- futures = client .map (augment , params_list )
124+ futures = client .map (augment , params_list , retries = 2 )
131125 progress (futures )
132126
133127 return output_directory
0 commit comments