{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import torch\n", "import sovits.sovits_inferencer\n", "from pydub import AudioSegment\n", "\n", "import IPython.display as ipd\n", "\n", "sovits_inferencer = sovits.sovits_inferencer.SovitsInferencer(\"sovits/configs/hoshimi_base.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_file = \"干花.mp3\"\n", "trans = 0\n", "slice_db = -40\n", "\n", "\n", "input_dir_path = \"inputs\"\n", "output_dir_path = \"outputs\"\n", "ext = input_file.split(\".\")[-1]\n", "input_path = os.path.join(input_dir_path, input_file)\n", "input_name = os.path.basename(input_path).replace(f\".{ext}\", \"\")\n", "\n", "vocals_path = os.path.join(output_dir_path, \"htdemucs\", input_name, \"vocals.wav\")\n", "no_vocals_path = os.path.join(output_dir_path, \"htdemucs\", input_name, \"no_vocals.wav\")\n", "dst_vocals_path = os.path.join(output_dir_path, f\"{input_name}_vocals_{trans}.wav\")\n", "output_path = os.path.join(output_dir_path, f\"{input_name}_{trans}.wav\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.makedirs(output_dir_path, exist_ok=True)\n", "os.system(f'python -m demucs.separate -d {\"cuda\" if torch.cuda.is_available() else \"cpu\"} --two-stems=vocals {input_path} -o {output_dir_path}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ipd.display(ipd.Audio(vocals_path))\n", "ipd.display(ipd.Audio(no_vocals_path))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from gradio.processing_utils import audio_to_file\n", "\n", "msg, (sr, audio) = sovits_inferencer.infer(vocals_path, None, trans, slice_db, 1000)\n", "print(msg)\n", "audio_to_file(sr, audio, dst_vocals_path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ipd.display(ipd.Audio(dst_vocals_path))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def combine(vocals_path, no_vocals_path, output_path):\n", " sound1 = AudioSegment.from_wav(vocals_path)\n", " sound2 = AudioSegment.from_wav(no_vocals_path)\n", "\n", " combined = sound1.overlay(sound2).set_frame_rate(sound1.frame_rate)\n", " combined.export(output_path, format=\"wav\")\n", " return combined\n", "\n", "combine(dst_vocals_path, no_vocals_path, output_path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ipd.display(ipd.Audio(output_path))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "3d236089fc20612273a7c4c44c2149c93909997a9c6abd716b8f4ac89b8b9538" } } }, "nbformat": 4, "nbformat_minor": 2 }