{ "cells": [ { "cell_type": "markdown", "id": "b6ee1ede", "metadata": {}, "source": [ "## Voice Style Control Demo" ] }, { "cell_type": "code", "execution_count": 19, "id": "b7f043ee", "metadata": {}, "outputs": [], "source": [ "import os\n", "import torch\n", "from openvoice import se_extractor\n", "from openvoice.api import BaseSpeakerTTS, ToneColorConverter" ] }, { "cell_type": "markdown", "id": "15116b59", "metadata": {}, "source": [ "### Initialization" ] }, { "cell_type": "code", "execution_count": 20, "id": "aacad912", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded checkpoint 'checkpoints/base_speakers/EN/checkpoint.pth'\n", "missing/unexpected keys: [] []\n", "Loaded checkpoint 'checkpoints/converter/checkpoint.pth'\n", "missing/unexpected keys: [] []\n" ] } ], "source": [ "ckpt_base = 'checkpoints/base_speakers/EN'\n", "ckpt_converter = 'checkpoints/converter'\n", "device=\"cuda:0\" if torch.cuda.is_available() else \"cpu\"\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": 21, "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": 22, "id": "55105eae", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenVoice version: v1\n", "[(0.0, 19.278375)]\n", "after vad: dur = 19.27798185941043\n" ] } ], "source": [ "reference_speaker = './resources/demo_speaker0.mp3' # This is the voice you want to clone\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": 23, "id": "73dc1259", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " > Text splitted to sentences.\n", "This audio is generated by OpenVoice.\n", " > ===========================\n", "ðɪs ˈɑdiˌoʊ ɪz ˈdʒɛnəɹˌeɪtɪd baɪ ˈoʊpən vɔɪs.\n", " length:45\n", " length:45\n" ] } ], "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": 24, "id": "fd022d38", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " > Text splitted to sentences.\n", "This audio is generated by OpenVoice.\n", " > ===========================\n", "ðɪs ˈɑdiˌoʊ ɪz ˈdʒɛnəɹˌeɪtɪd baɪ ˈoʊpən vɔɪs.\n", " length:45\n", " length:45\n" ] } ], "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.\"\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": 28, "id": "deff30a4-d430-4b4d-9772-b936f5b564c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded checkpoint 'checkpoints/base_speakers/ZH/checkpoint.pth'\n", "missing/unexpected keys: [] []\n" ] } ], "source": [ "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" ] }, { "cell_type": "code", "execution_count": 30, "id": "a71d1387", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " > Text splitted to sentences.\n", "毛岛灰绣眼鸟(学名:Zosterops mauritianus)是一种绣眼鸟科绣眼鸟属的鸟类,\n", "属于毛里求斯岛上两种特有种绣眼鸟之一,\n", "另一种是更为稀少的毛里求斯绣眼鸟.\n", "上半身整体为灰色, 下半身为灰白色,\n", "臀部和腋羽是十分显眼的白色.\n", "这种鸟栖息于次生林、森林和花园中[1].\n", "它与留尼汪灰绣眼鸟亲缘关系很近,\n", "曾经被认为是同种, 统称为马斯克林绣眼鸟[2]\n", " > ===========================\n", "mɑʊ↑t⁼ɑʊ↓↑ xweɪ→ ʃjoʊ↓ jɛn↓↑niɑʊ↓↑( ʃɥɛ↑miŋ↑,ts⁼eɪ↓oʊ→ɛ↑sɹ↓tʰi↓i↓a↓oʊ→pʰi→ɛ↑sɹ↓ ɛ↑mu↓eɪ→joʊ→a↓aɪ↓tʰi↓aɪ↓eɪ→ən→joʊ→ɛ↑sɹ↓) s`ɹ`↓ i→ts`⁼ʊŋ↓↑ ʃjoʊ↓ jɛn↓↑niɑʊ↓↑kʰə→ ʃjoʊ↓ jɛn↓↑niɑʊ↓↑ s`u↓↑ t⁼ə niɑʊ↓↑leɪ↓,\n", " length:199\n", " length:197\n", "s`u↓↑ɥ↑ mɑʊ↑li↓↑tʃʰjoʊ↑sɹ→ t⁼ɑʊ↓↑s`ɑŋ↓ liɑŋ↓↑ts`⁼ʊŋ↓↑ tʰə↓joʊ↓↑ts`⁼ʊŋ↓↑ ʃjoʊ↓ jɛn↓↑niɑʊ↓↑ ts`⁼ɹ`→i→,\n", " length:100\n", " length:100\n", "liŋ↓ i→ts`⁼ʊŋ↓↑ s`ɹ`↓ k⁼əŋ↓weɪ↑ ʃi→s`ɑʊ↓↑ t⁼ə mɑʊ↑li↓↑tʃʰjoʊ↑sɹ→ ʃjoʊ↓ jɛn↓↑niɑʊ↓↑.\n", " length:83\n", " length:83\n", "s`ɑŋ↓p⁼an↓s`ən→ ts`⁼əŋ↓↑tʰi↓↑ weɪ↓ xweɪ→sə↓, ʃja↓p⁼an↓s`ən→ weɪ↓ xweɪ→p⁼aɪ↑sə↓,\n", " length:80\n", " length:80\n", "tʰwən↑p⁼u↓ xə↑ iɛ↓ɥ↓↑ s`ɹ`↓ s`ɹ`↑fən→ ʃjɛn↓↑jɛn↓↑ t⁼ə p⁼aɪ↑sə↓.\n", " length:63\n", " length:63\n", "ts`⁼ə↓ts`⁼ʊŋ↓↑ niɑʊ↓↑ tʃʰi→ʃi→ ɥ↑ tsʰɹ↓s`əŋ→lin↑, sən→lin↑ xə↑ xwa→ɥæn↑ ts`⁼ʊŋ→[ i→].\n", " length:85\n", " length:83\n", "tʰa→ ɥ↓↑ ljoʊ↑ni↑uɑŋ→ xweɪ→ ʃjoʊ↓ jɛn↓↑niɑʊ↓↑ tʃʰin→ɥæn↑ k⁼wan→ʃi↓ xən↓↑tʃ⁼in↓,\n", " length:79\n", " length:79\n", "tsʰəŋ↑tʃ⁼iŋ→ p⁼eɪ↓ ɹ`ən↓weɪ↑ s`ɹ`↓ tʰʊŋ↑ts`⁼ʊŋ↓↑, tʰʊŋ↓↑ts`ʰəŋ→ weɪ↓ ma↓↑sɹ→kʰə↓lin↑ ʃjoʊ↓ jɛn↓↑niɑʊ↓↑[ əɹ`↓].\n", " length:111\n", " length:109\n" ] }, { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'builtin_function_or_method' and 'float'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[30], line 20\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[38;5;66;03m# 记录结束时间\u001b[39;00m\n\u001b[1;32m 19\u001b[0m end_time \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime\n\u001b[0;32m---> 20\u001b[0m execution_time \u001b[38;5;241m=\u001b[39m \u001b[43mend_time\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mstart_time\u001b[49m\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m代码执行时间: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexecution_time\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m 秒\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'builtin_function_or_method' and 'float'" ] } ], "source": [ "import time\n", "\n", "# 记录开始时间\n", "start_time = time.time()\n", "# Run the base speaker tts\n", "text = \"毛岛灰绣眼鸟(学名:Zosterops mauritianus)是一种绣眼鸟科绣眼鸟属的鸟类,属于毛里求斯岛上两种特有种绣眼鸟之一,另一种是更为稀少的毛里求斯绣眼鸟。上半身整体为灰色,下半身为灰白色,臀部和腋羽是十分显眼的白色。这种鸟栖息于次生林、森林和花园中[1]。它与留尼汪灰绣眼鸟亲缘关系很近,曾经被认为是同种,统称为马斯克林绣眼鸟[2]\"\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)\n", "# 记录结束时间\n", "end_time = time.time\n", "execution_time = end_time - start_time\n", "print(f\"代码执行时间: {execution_time} 秒\")" ] }, { "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." ] }, { "cell_type": "code", "execution_count": null, "id": "9628ffa1-1d60-4d1b-a9ed-619add064ebd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "377f4b72-dfca-4c58-8a5c-fea056538cc2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "31bf81ab-bac9-4996-8f47-8651052d713a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "32a84a29-9515-4aaa-b4ad-3a530e8259f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "abd802ad-93ac-4db2-9ee5-0ad78b54e09e", "metadata": {}, "outputs": [], "source": [] } ], "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.9.19" } }, "nbformat": 4, "nbformat_minor": 5 }