Spaces:
Running
on
Zero
Running
on
Zero
fix: add dropdown to select audio format
Browse files
app.py
CHANGED
@@ -94,7 +94,7 @@ def move_stems_to_parent(input_dir):
|
|
94 |
print(f"Moving {instrumental_path} to {new_instrumental_path}")
|
95 |
shutil.move(instrumental_path, new_instrumental_path)
|
96 |
|
97 |
-
def combine_stems_for_all(input_dir):
|
98 |
for subdir, _, _ in os.walk(input_dir):
|
99 |
if subdir == input_dir:
|
100 |
continue
|
@@ -119,10 +119,13 @@ def combine_stems_for_all(input_dir):
|
|
119 |
# Trim silence from the end of the combined audio
|
120 |
trimmed_combined = trim_silence_at_end(combined)
|
121 |
|
122 |
-
|
123 |
-
|
|
|
124 |
print(f"Exported combined stems to {output_file}")
|
125 |
|
|
|
|
|
126 |
def trim_silence_at_end(audio_segment, silence_thresh=-50, chunk_size=10):
|
127 |
"""
|
128 |
Trims silence at the end of an AudioSegment.
|
@@ -169,7 +172,7 @@ def delete_folders_and_files(input_dir):
|
|
169 |
print("Cleanup completed.")
|
170 |
|
171 |
@spaces.GPU(duration=300) # Adjust the duration as needed
|
172 |
-
def process_audio(uploaded_file):
|
173 |
try:
|
174 |
yield "Processing audio...", None
|
175 |
|
@@ -204,12 +207,12 @@ def process_audio(uploaded_file):
|
|
204 |
move_stems_to_parent(OUTPUT_FOLDER)
|
205 |
|
206 |
yield "Combining stems...", None
|
207 |
-
combine_stems_for_all(OUTPUT_FOLDER)
|
208 |
|
209 |
yield "Cleaning up...", None
|
210 |
delete_folders_and_files(OUTPUT_FOLDER)
|
211 |
|
212 |
-
yield f"Audio processing completed successfully.",
|
213 |
except Exception as e:
|
214 |
error_msg = f"An error occurred: {str(e)}\n{traceback.format_exc()}"
|
215 |
logging.error(error_msg)
|
@@ -219,23 +222,16 @@ with gr.Blocks() as demo:
|
|
219 |
gr.Markdown("# Music Player and Processor")
|
220 |
|
221 |
file_upload = gr.File(label="Upload WAV file", file_types=[".wav"])
|
222 |
-
|
223 |
-
audio_output = gr.Audio(label="Audio Player")
|
224 |
|
225 |
process_button = gr.Button("Process Audio")
|
226 |
|
227 |
log_output = gr.Textbox(label="Processing Log", interactive=False)
|
228 |
-
processed_audio_output = gr.
|
229 |
-
|
230 |
-
file_upload.change(
|
231 |
-
fn=lambda file: file.name if file else None,
|
232 |
-
inputs=file_upload,
|
233 |
-
outputs=audio_output
|
234 |
-
)
|
235 |
|
236 |
process_button.click(
|
237 |
fn=process_audio,
|
238 |
-
inputs=file_upload,
|
239 |
outputs=[log_output, processed_audio_output],
|
240 |
show_progress=True
|
241 |
)
|
|
|
94 |
print(f"Moving {instrumental_path} to {new_instrumental_path}")
|
95 |
shutil.move(instrumental_path, new_instrumental_path)
|
96 |
|
97 |
+
def combine_stems_for_all(input_dir, output_format):
|
98 |
for subdir, _, _ in os.walk(input_dir):
|
99 |
if subdir == input_dir:
|
100 |
continue
|
|
|
119 |
# Trim silence from the end of the combined audio
|
120 |
trimmed_combined = trim_silence_at_end(combined)
|
121 |
|
122 |
+
# Determine the output file format
|
123 |
+
output_file = os.path.join(subdir, f"{song_name}.{output_format}")
|
124 |
+
trimmed_combined.export(output_file, format=output_format)
|
125 |
print(f"Exported combined stems to {output_file}")
|
126 |
|
127 |
+
return output_file
|
128 |
+
|
129 |
def trim_silence_at_end(audio_segment, silence_thresh=-50, chunk_size=10):
|
130 |
"""
|
131 |
Trims silence at the end of an AudioSegment.
|
|
|
172 |
print("Cleanup completed.")
|
173 |
|
174 |
@spaces.GPU(duration=300) # Adjust the duration as needed
|
175 |
+
def process_audio(uploaded_file, output_format):
|
176 |
try:
|
177 |
yield "Processing audio...", None
|
178 |
|
|
|
207 |
move_stems_to_parent(OUTPUT_FOLDER)
|
208 |
|
209 |
yield "Combining stems...", None
|
210 |
+
output_file = combine_stems_for_all(OUTPUT_FOLDER, output_format)
|
211 |
|
212 |
yield "Cleaning up...", None
|
213 |
delete_folders_and_files(OUTPUT_FOLDER)
|
214 |
|
215 |
+
yield f"Audio processing completed successfully.", output_file
|
216 |
except Exception as e:
|
217 |
error_msg = f"An error occurred: {str(e)}\n{traceback.format_exc()}"
|
218 |
logging.error(error_msg)
|
|
|
222 |
gr.Markdown("# Music Player and Processor")
|
223 |
|
224 |
file_upload = gr.File(label="Upload WAV file", file_types=[".wav"])
|
225 |
+
output_format = gr.Dropdown(label="Select Output Format", choices=["wav", "m4a"], value="m4a")
|
|
|
226 |
|
227 |
process_button = gr.Button("Process Audio")
|
228 |
|
229 |
log_output = gr.Textbox(label="Processing Log", interactive=False)
|
230 |
+
processed_audio_output = gr.File(label="Processed Audio")
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
232 |
process_button.click(
|
233 |
fn=process_audio,
|
234 |
+
inputs=[file_upload, output_format],
|
235 |
outputs=[log_output, processed_audio_output],
|
236 |
show_progress=True
|
237 |
)
|