@echo off
setlocal enabledelayedexpansion

REM Get the dropped file's path
set INPUT_FILE=%1
echo Input file: %INPUT_FILE%

REM Run pre_process_given_text_file.py with the dropped file as input
echo Running pre_process_given_text_file.py...
python pre_process_given_text_file.py --input_file %INPUT_FILE%


set "PROCESSED_TEXT="

REM Read the contents of a file line by line
for /F "delims=" %%A in ('findstr "^" processed_speech.txt') do (
    Setlocal DisableDelayedExpansion
    set "LINE=%%A"
    Setlocal EnableDelayedExpansion
    set "line=!line:*:=!"
    set "line=!line:"=""q!"
    set "line=!line:^=^^!"
    call set "line=%%line:^!=""c^!%%"
    set "line=!line:""c=^!"
    set "line=!line:""q="!"
    FOR /F "delims=" %%B in (""!line!"") do (
      endlocal
      endlocal
      if defined PROCESSED_TEXT (
        set "PROCESSED_TEXT=!PROCESSED_TEXT!; %%~B" !
      ) ELSE (
        set "PROCESSED_TEXT=%%~B" !
      )
    )
)

echo Processed text: !PROCESSED_TEXT!

REM Activate the virtual environment
echo Activating virtual environment...
call "F:\voice clone tutorial\tortoise-tts-fast\venv\Scripts\activate.bat" || (
    echo Error: Failed to activate virtual environment. Press any key to exit.
    pause >nul
    exit /b
)

REM Change directory to the tortoise_tts.py script location
cd "F:\voice clone tutorial\tortoise-tts-fast\scripts"

REM Set the checkpoints directory and output directory
set CHECKPOINTS_DIR="F:\voice clone tutorial\check_point_tests"
set OUTPUT_BASE_DIR="F:\voice clone tutorial\check_point_comparison_v4"

REM Create check_point_comparison directory if it doesn't exist
if not exist %OUTPUT_BASE_DIR% mkdir %OUTPUT_BASE_DIR%

REM you can change  --preset depending on your GPU : ultra_fast - fast - standard - high_quality and the time you want to render

REM Iterate over all checkpoint files and run tortoise_tts.py for each
for %%f in (%CHECKPOINTS_DIR%\*.pth) do (
    echo Running tortoise_tts.py with checkpoint: %%~nf
    set CHECKPOINT_NAME=%%~nf
    set CHECKPOINT_PATH=%%f
    set OUTPUT_DIR=%OUTPUT_BASE_DIR%\!CHECKPOINT_NAME!
	set OUTPUT_DIR=!OUTPUT_DIR:"=!
	
    if not exist !OUTPUT_DIR! mkdir !OUTPUT_DIR! 2>nul
    python tortoise_tts.py --candidates 1 --voicefixer True --no_cache False --half False --low_vram True --vocoder "BigVGAN" --output_dir "!OUTPUT_DIR!" --diffusion_iterations 250 --seed 1 --preset fast --ar_checkpoint "!CHECKPOINT_PATH!" --text_split "gg" "!PROCESSED_TEXT!" || (
        echo Error: Failed to run tortoise_tts.py with checkpoint: !CHECKPOINT_NAME!. Skipping this checkpoint.
    )
)

REM Deactivate the virtual environment

echo Script execution complete. Press any key to exit.
pause >nul
