import gradio as gr import matchering as mg import os import shutil import uuid import sys # Define the function that will be called by Gradio def apply_matchering(target_audio_path, reference_audio_path): if target_audio_path is None or reference_audio_path is None: raise gr.Error("Please upload both target and reference audio files.") # --- DEBUGGING START --- print("\n--- DEBUGGING MATCHERING MODULE ---") try: print(f"matchering module path: {mg.__file__}") print(f"matchering version: {getattr(mg, '__version__', 'Version not found')}") print("\nContents of 'matchering' module (mg):") for attr in dir(mg): if not attr.startswith('__'): print(f" - {attr}") if hasattr(mg, 'core'): print("\nContents of 'matchering.core' module (mg.core):") for attr in dir(mg.core): if not attr.startswith('__'): print(f" - {attr}") else: print("\n'matchering' module does NOT have a 'core' attribute.") if hasattr(mg, 'defaults'): print("\nContents of 'matchering.defaults' module (mg.defaults):") for attr in dir(mg.defaults): if not attr.startswith('__'): print(f" - {attr}") else: print("\n'matchering' module does NOT have a 'defaults' attribute.") except Exception as debug_e: print(f"Error during debugging matchering module inspection: {debug_e}") print("--- DEBUGGING MATCHERING MODULE END ---\n") # --- DEBUGGING END --- run_id = str(uuid.uuid4()) temp_output_dir = os.path.join("/tmp", f"matchering_output_{run_id}") os.makedirs(temp_output_dir, exist_ok=True) output_filename = "matched_audio.wav" output_file_path = os.path.join(temp_output_dir, output_filename) try: print(f"Starting matchering process...") print(f"Target audio: {target_audio_path}") print(f"Reference audio: {reference_audio_path}") print(f"Output will be saved to: {output_file_path}") if hasattr(mg, 'process'): print("Attempting to call mg.process...") # *** CRITICAL CHANGE: Pass format, subtype, and bitrate as POSITIONAL arguments *** # Based on matchering 2.0.6 Result class signature: __init__(self, file: str, format: str, subtype: str, bitrate: int) mg.process( target=target_audio_path, reference=reference_audio_path, results=[mg.Result(output_file_path, 'WAV', 'PCM_16', 320)] # <--- THIS IS THE FIX ) else: raise AttributeError("The 'matchering.process' function was not found in the installed 'matchering' module. This indicates a problem with the library installation or usage.") print(f"Matchering completed successfully. Output: {output_file_path}") return output_file_path except Exception as e: print(f"An error occurred during matchering: {e}") if os.path.exists(temp_output_dir): shutil.rmtree(temp_output_dir) raise gr.Error(f"Matchering failed: {e}. Please check your audio files and try again.") finally: pass # Create the Gradio interface interface = gr.Interface( fn=apply_matchering, inputs=[ gr.Audio(type="filepath", label="Upload Target Audio (e.g., your mix)"), gr.Audio(type="filepath", label="Upload Reference Audio (e.g., a professionally mastered track)") ], outputs=gr.Audio(type="filepath", label="Matched Output Audio"), title="Matchering Audio Processing Tool", description="Upload your target audio and a reference audio to apply matchering and achieve a similar sonic profile.", allow_flagging="never" ) # Launch the Gradio application if __name__ == "__main__": interface.launch()