{ "cells": [ { "cell_type": "markdown", "id": "b6ee1ede", "metadata": {}, "source": [ "## Voice Style Control Demo" ] }, { "cell_type": "code", "execution_count": 3, "id": "b7f043ee", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Importing the dtw module. When using in academic works please cite:\n", " T. Giorgino. Computing and Visualizing Dynamic Time Warping Alignments in R: The dtw Package.\n", " J. Stat. Soft., doi:10.18637/jss.v031.i07.\n", "\n" ] } ], "source": [ "import os\n", "import torch\n", "import se_extractor\n", "from api import BaseSpeakerTTS, ToneColorConverter" ] }, { "cell_type": "code", "execution_count": 2, "id": "237202b7-4e8d-4445-a5b0-41db367e4977", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/workspace/OpenVoice\n" ] } ], "source": [ "cd OpenVoice" ] }, { "cell_type": "code", "execution_count": 5, "id": "a79d4a34-ce9c-4ae3-b79a-c730b8885df6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Archive: checkpoints_1226.zip\n", " creating: checkpoints/checkpoints/\n", " creating: checkpoints/checkpoints/converter/\n", " inflating: checkpoints/checkpoints/converter/config.json \n", " inflating: checkpoints/checkpoints/converter/checkpoint.pth \n", " creating: checkpoints/checkpoints/base_speakers/\n", " creating: checkpoints/checkpoints/base_speakers/ZH/\n", " inflating: checkpoints/checkpoints/base_speakers/ZH/config.json \n", " inflating: checkpoints/checkpoints/base_speakers/ZH/checkpoint.pth \n", " inflating: checkpoints/checkpoints/base_speakers/ZH/zh_default_se.pth \n", " creating: checkpoints/checkpoints/base_speakers/EN/\n", " inflating: checkpoints/checkpoints/base_speakers/EN/config.json \n", " inflating: checkpoints/checkpoints/base_speakers/EN/en_style_se.pth \n", " inflating: checkpoints/checkpoints/base_speakers/EN/en_default_se.pth \n", " inflating: checkpoints/checkpoints/base_speakers/EN/checkpoint.pth \n" ] } ], "source": [ "!unzip checkpoints_1226.zip -d checkpoints" ] }, { "cell_type": "code", "execution_count": 7, "id": "175d6dfc-b170-46de-8a44-8ec5a4e3b333", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: huggingface_hub in /opt/conda/lib/python3.10/site-packages (0.17.3)\n", "Requirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from huggingface_hub) (3.13.1)\n", "Requirement already satisfied: fsspec in /opt/conda/lib/python3.10/site-packages (from huggingface_hub) (2023.12.2)\n", "Requirement already satisfied: requests in /opt/conda/lib/python3.10/site-packages (from huggingface_hub) (2.31.0)\n", "Requirement already satisfied: tqdm>=4.42.1 in /opt/conda/lib/python3.10/site-packages (from huggingface_hub) (4.65.0)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/conda/lib/python3.10/site-packages (from huggingface_hub) (6.0.1)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/conda/lib/python3.10/site-packages (from huggingface_hub) (4.7.1)\n", "Requirement already satisfied: packaging>=20.9 in /opt/conda/lib/python3.10/site-packages (from huggingface_hub) (23.1)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface_hub) (2.0.4)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface_hub) (3.4)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface_hub) (1.26.18)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface_hub) (2023.11.17)\n", "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install huggingface_hub" ] }, { "cell_type": "code", "execution_count": 8, "id": "35b568bf-9951-40b9-843f-0af4e918567f", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f30315ee57c9418688ab0d9be585f948", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value='
6\u001b[0m base_speaker_tts \u001b[38;5;241m=\u001b[39m \u001b[43mBaseSpeakerTTS\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mckpt_base\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[38;5;124;43m/config.json\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdevice\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdevice\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 7\u001b[0m base_speaker_tts\u001b[38;5;241m.\u001b[39mload_ckpt(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mckpt_base\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/checkpoint.pth\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 9\u001b[0m tone_color_converter \u001b[38;5;241m=\u001b[39m ToneColorConverter(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mckpt_converter\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/config.json\u001b[39m\u001b[38;5;124m'\u001b[39m, device\u001b[38;5;241m=\u001b[39mdevice)\n", "File \u001b[0;32m/workspace/OpenVoice/api.py:21\u001b[0m, in \u001b[0;36mOpenVoiceBaseClass.__init__\u001b[0;34m(self, config_path, device)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcuda\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m device:\n\u001b[1;32m 19\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mcuda\u001b[38;5;241m.\u001b[39mis_available()\n\u001b[0;32m---> 21\u001b[0m hps \u001b[38;5;241m=\u001b[39m \u001b[43mutils\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_hparams_from_file\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconfig_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 23\u001b[0m model \u001b[38;5;241m=\u001b[39m SynthesizerTrn(\n\u001b[1;32m 24\u001b[0m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mgetattr\u001b[39m(hps, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msymbols\u001b[39m\u001b[38;5;124m'\u001b[39m, [])),\n\u001b[1;32m 25\u001b[0m hps\u001b[38;5;241m.\u001b[39mdata\u001b[38;5;241m.\u001b[39mfilter_length \u001b[38;5;241m/\u001b[39m\u001b[38;5;241m/\u001b[39m \u001b[38;5;241m2\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m,\n\u001b[1;32m 26\u001b[0m n_speakers\u001b[38;5;241m=\u001b[39mhps\u001b[38;5;241m.\u001b[39mdata\u001b[38;5;241m.\u001b[39mn_speakers,\n\u001b[1;32m 27\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhps\u001b[38;5;241m.\u001b[39mmodel,\n\u001b[1;32m 28\u001b[0m )\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[1;32m 30\u001b[0m model\u001b[38;5;241m.\u001b[39meval()\n", "File \u001b[0;32m/workspace/OpenVoice/utils.py:7\u001b[0m, in \u001b[0;36mget_hparams_from_file\u001b[0;34m(config_path)\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_hparams_from_file\u001b[39m(config_path):\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mconfig_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mutf-8\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[1;32m 8\u001b[0m data \u001b[38;5;241m=\u001b[39m f\u001b[38;5;241m.\u001b[39mread()\n\u001b[1;32m 9\u001b[0m config \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mloads(data)\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'checkpoints/base_speakers/EN/config.json'" ] } ], "source": [ "ckpt_base = 'checkpoints/base_speakers/EN'\n", "ckpt_converter = 'checkpoints/converter'\n", "device = 'cuda:0'\n", "output_dir = 'outputs'\n", "\n", "base_speaker_tts = BaseSpeakerTTS(f'{ckpt_base}/config.json', device=device)\n", "base_speaker_tts.load_ckpt(f'{ckpt_base}/checkpoint.pth')\n", "\n", "tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device=device)\n", "tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')\n", "\n", "os.makedirs(output_dir, exist_ok=True)" ] }, { "cell_type": "markdown", "id": "7f67740c", "metadata": {}, "source": [ "### Obtain Tone Color Embedding" ] }, { "cell_type": "markdown", "id": "f8add279", "metadata": {}, "source": [ "The `source_se` is the tone color embedding of the base speaker. \n", "It is an average of multiple sentences generated by the base speaker. We directly provide the result here but\n", "the readers feel free to extract `source_se` by themselves." ] }, { "cell_type": "code", "execution_count": null, "id": "63ff6273", "metadata": {}, "outputs": [], "source": [ "source_se = torch.load(f'{ckpt_base}/en_default_se.pth').to(device)" ] }, { "cell_type": "markdown", "id": "4f71fcc3", "metadata": {}, "source": [ "The `reference_speaker.mp3` below points to the short audio clip of the reference whose voice we want to clone. We provide an example here. If you use your own reference speakers, please **make sure each speaker has a unique filename.** The `se_extractor` will save the `targeted_se` using the filename of the audio and **will not automatically overwrite.**" ] }, { "cell_type": "code", "execution_count": null, "id": "55105eae", "metadata": {}, "outputs": [], "source": [ "reference_speaker = 'resources/example_reference.mp3'\n", "target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir='processed', vad=True)" ] }, { "cell_type": "markdown", "id": "a40284aa", "metadata": {}, "source": [ "### Inference" ] }, { "cell_type": "code", "execution_count": null, "id": "73dc1259", "metadata": {}, "outputs": [], "source": [ "save_path = f'{output_dir}/output_en_default.wav'\n", "\n", "# Run the base speaker tts\n", "text = \"This audio is generated by OpenVoice.\"\n", "src_path = f'{output_dir}/tmp.wav'\n", "base_speaker_tts.tts(text, src_path, speaker='default', language='English', speed=1.0)\n", "\n", "# Run the tone color converter\n", "encode_message = \"@MyShell\"\n", "tone_color_converter.convert(\n", " audio_src_path=src_path, \n", " src_se=source_se, \n", " tgt_se=target_se, \n", " output_path=save_path,\n", " message=encode_message)" ] }, { "cell_type": "markdown", "id": "6e3ea28a", "metadata": {}, "source": [ "**Try with different styles and speed.** The style can be controlled by the `speaker` parameter in the `base_speaker_tts.tts` method. Available choices: friendly, cheerful, excited, sad, angry, terrified, shouting, whispering. Note that the tone color embedding need to be updated. The speed can be controlled by the `speed` parameter. Let's try whispering with speed 0.9." ] }, { "cell_type": "code", "execution_count": null, "id": "fd022d38", "metadata": {}, "outputs": [], "source": [ "source_se = torch.load(f'{ckpt_base}/en_style_se.pth').to(device)\n", "save_path = f'{output_dir}/output_whispering.wav'\n", "\n", "# Run the base speaker tts\n", "text = \"This audio is generated by OpenVoice with a half-performance model.\"\n", "src_path = f'{output_dir}/tmp.wav'\n", "base_speaker_tts.tts(text, src_path, speaker='whispering', language='English', speed=0.9)\n", "\n", "# Run the tone color converter\n", "encode_message = \"@MyShell\"\n", "tone_color_converter.convert(\n", " audio_src_path=src_path, \n", " src_se=source_se, \n", " tgt_se=target_se, \n", " output_path=save_path,\n", " message=encode_message)" ] }, { "cell_type": "markdown", "id": "5fcfc70b", "metadata": {}, "source": [ "**Try with different languages.** OpenVoice can achieve multi-lingual voice cloning by simply replace the base speaker. We provide an example with a Chinese base speaker here and we encourage the readers to try `demo_part2.ipynb` for a detailed demo." ] }, { "cell_type": "code", "execution_count": null, "id": "a71d1387", "metadata": {}, "outputs": [], "source": [ "\n", "ckpt_base = 'checkpoints/base_speakers/ZH'\n", "base_speaker_tts = BaseSpeakerTTS(f'{ckpt_base}/config.json', device=device)\n", "base_speaker_tts.load_ckpt(f'{ckpt_base}/checkpoint.pth')\n", "\n", "source_se = torch.load(f'{ckpt_base}/zh_default_se.pth').to(device)\n", "save_path = f'{output_dir}/output_chinese.wav'\n", "\n", "# Run the base speaker tts\n", "text = \"今天天气真好,我们一起出去吃饭吧。\"\n", "src_path = f'{output_dir}/tmp.wav'\n", "base_speaker_tts.tts(text, src_path, speaker='default', language='Chinese', speed=1.0)\n", "\n", "# Run the tone color converter\n", "encode_message = \"@MyShell\"\n", "tone_color_converter.convert(\n", " audio_src_path=src_path, \n", " src_se=source_se, \n", " tgt_se=target_se, \n", " output_path=save_path,\n", " message=encode_message)" ] }, { "cell_type": "markdown", "id": "8e513094", "metadata": {}, "source": [ "**Tech for good.** For people who will deploy OpenVoice for public usage: We offer you the option to add watermark to avoid potential misuse. Please see the ToneColorConverter class. **MyShell reserves the ability to detect whether an audio is generated by OpenVoice**, no matter whether the watermark is added or not." ] } ], "metadata": { "interpreter": { "hash": "9d70c38e1c0b038dbdffdaa4f8bfa1f6767c43760905c87a9fbe7800d18c6c35" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }