From f6236dcccadddc604c5dc9ae2fdd2fbb385a0e7a Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 12 Dec 2022 17:42:46 -0500 Subject: [PATCH] Truncate input audio --- voice_conversion/softvc/.gitignore | 1 + voice_conversion/softvc/cli_inference.py | 27 +++++++++++++++++++++++- voice_conversion/softvc/test.sh | 8 +++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100755 voice_conversion/softvc/test.sh diff --git a/voice_conversion/softvc/.gitignore b/voice_conversion/softvc/.gitignore index a06b66e..da18c05 100644 --- a/voice_conversion/softvc/.gitignore +++ b/voice_conversion/softvc/.gitignore @@ -11,5 +11,6 @@ __pycache__/ # Default output files output.wav +temp.wav metadata.json diff --git a/voice_conversion/softvc/cli_inference.py b/voice_conversion/softvc/cli_inference.py index 7490479..deaba29 100755 --- a/voice_conversion/softvc/cli_inference.py +++ b/voice_conversion/softvc/cli_inference.py @@ -34,7 +34,9 @@ parser.add_argument('--acoustic_model_filename', type=str, required=True) parser.add_argument('--hifigan_model_filename', type=str, required=True) parser.add_argument('--source_audio_filename', type=str, required=True) parser.add_argument('--output_audio_filename', type=str, default='output.wav') +parser.add_argument('--temporary_audio_filename', type=str, default='temp.wav') # used for truncation, etc. parser.add_argument('--output_metadata_filename', type=str, default='metadata.json') +parser.add_argument('--truncate_seconds', type=int, default=-1) # -1 is "do not truncate", 0 is invalid args = parser.parse_args() print('\n'.join(f'[arg] {k} = {v}' for k, v in vars(args).items())) @@ -123,6 +125,23 @@ def do_voice_conversion(hubert, acoustic, hifigan, audio_source): # =========================================== +def truncate_source_audio(audio_source_filename, output_filename, seconds): + # Run ffmpeg's ffprobe + command = [ + "ffmpeg", + "-i", + audio_source_filename, + "-t", + str(seconds), + "-c", + "copy", + output_filename, + ] + + pipe = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + out, err = pipe.communicate() + + def save_output_audio(audio_data, output_audio_filename): soundfile.write(args.output_audio_filename, audio_data.squeeze().cpu(), samplerate=16000) @@ -157,12 +176,18 @@ def save_metadata(audio_filename, output_metadata_filename): with open(output_metadata_filename, 'w') as json_file: json.dump(metadata, json_file) +intermediate_audio_filename = args.source_audio_filename + +if args.truncate_seconds not in [-1, 0]: + intermediate_audio_filename = args.temporary_audio_filename + truncate_source_audio(args.source_audio_filename, intermediate_audio_filename, args.truncate_seconds) + hubert = torch.hub.load("bshall/hubert:main", "hubert_soft").cuda() acoustic = load_acoustic_model(args.acoustic_model_filename) hifigan = load_hifigan_model(args.hifigan_model_filename) -source_audio = load_wav_data(args.source_audio_filename) +source_audio = load_wav_data(intermediate_audio_filename) target_audio = do_voice_conversion(hubert, acoustic, hifigan, source_audio) save_output_audio(target_audio, args.output_audio_filename) diff --git a/voice_conversion/softvc/test.sh b/voice_conversion/softvc/test.sh new file mode 100755 index 0000000..984841e --- /dev/null +++ b/voice_conversion/softvc/test.sh @@ -0,0 +1,8 @@ +#!/bin/bash +source python/bin/activate && ./cli_inference.py \ + --acoustic_model_filename /home/bt/models/voice-conversion/trump-acoustic.pt \ + --hifigan_model_filename /home/bt/models/voice-conversion/trump-hifigan.pt \ + --source_audio_filename samples/source/medium/gongiveittoya.wav \ + --output_audio_filename output.wav \ + --truncate_seconds 5 +