From 05d6ba5c25967cd7cbac767ae8a2fac36fbd31d3 Mon Sep 17 00:00:00 2001
From: YCHuang2112 <96855801+YCHuang2112@users.noreply.github.com>
Date: Wed, 23 Aug 2023 03:36:51 +0800
Subject: [PATCH 1/3] example for jupyternotebook
asyncio.run() can not be called in jupyter-notebook, jupyter-lab
---
.gitignore | 5 +++-
README.rst | 7 ++++--
examples/basic.ipynb | 54 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 63 insertions(+), 3 deletions(-)
create mode 100644 examples/basic.ipynb
diff --git a/.gitignore b/.gitignore
index 090d639..2959219 100644
--- a/.gitignore
+++ b/.gitignore
@@ -129,4 +129,7 @@ dmypy.json
.pyre/
voice.wav
-audio.zip
\ No newline at end of file
+audio.zip
+
+# vscode
+.vscode/*
\ No newline at end of file
diff --git a/README.rst b/README.rst
index 1f9513f..7f4ec47 100644
--- a/README.rst
+++ b/README.rst
@@ -42,5 +42,8 @@ Example
f.write(await audio_query.synthesis(speaker=4))
- if __name__ == "__main__":
- asyncio.run(main())
+ if __name__ == "__main__":
+ ## already in asyncio (in a Jupyter notebook, for example)
+ # await main()
+ ## otherwise
+ asyncio.run(main())
\ No newline at end of file
diff --git a/examples/basic.ipynb b/examples/basic.ipynb
new file mode 100644
index 0000000..c973f30
--- /dev/null
+++ b/examples/basic.ipynb
@@ -0,0 +1,54 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from voicevox import Client\n",
+ "import asyncio\n",
+ "\n",
+ "\n",
+ "async def main():\n",
+ " async with Client() as client:\n",
+ " audio_query = await client.create_audio_query(\"こんにちは!\", speaker=1)\n",
+ " with open(\"voice.wav\", \"wb\") as f:\n",
+ " f.write(await audio_query.synthesis(speaker=1))\n",
+ "\n",
+ "\n",
+ "if __name__ == \"__main__\":\n",
+ " await main()\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "base",
+ "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.13"
+ },
+ "orig_nbformat": 4
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
From 52a87cf4240559cc714c385cd94721213f531ef7 Mon Sep 17 00:00:00 2001
From: YCHuang2112 <96855801+YCHuang2112@users.noreply.github.com>
Date: Wed, 23 Aug 2023 09:30:50 +0800
Subject: [PATCH 2/3] add playing voice in basic.ipynb
an notebook version demo of how to use the package, and also adding the sound playing function.
---
examples/basic.ipynb | 93 ++++++++++++++++++++++++++++++++++++++++----
1 file changed, 86 insertions(+), 7 deletions(-)
diff --git a/examples/basic.ipynb b/examples/basic.ipynb
index c973f30..7b10b5d 100644
--- a/examples/basic.ipynb
+++ b/examples/basic.ipynb
@@ -1,5 +1,27 @@
{
"cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 44,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Requirement already satisfied: sounddevice in c:\\users\\benve\\anaconda3\\envs\\py39t\\lib\\site-packages (0.4.6)\n",
+ "Requirement already satisfied: CFFI>=1.0 in c:\\users\\benve\\anaconda3\\envs\\py39t\\lib\\site-packages (from sounddevice) (1.15.1)\n",
+ "Requirement already satisfied: pycparser in c:\\users\\benve\\anaconda3\\envs\\py39t\\lib\\site-packages (from CFFI>=1.0->sounddevice) (2.21)\n"
+ ]
+ }
+ ],
+ "source": [
+ "!pip install voicevox-client\n",
+ "!pip install numpy\n",
+ "!pip install torchaudio\n",
+ "!pip install sounddevice"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 1,
@@ -8,25 +30,82 @@
"source": [
"from voicevox import Client\n",
"import asyncio\n",
- "\n",
- "\n",
+ "from IPython.display import Audio\n",
+ "import torchaudio\n",
+ "import sounddevice as sd"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "output_path = \"voice.wav\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ " \n",
+ " "
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
"async def main():\n",
" async with Client() as client:\n",
" audio_query = await client.create_audio_query(\"こんにちは!\", speaker=1)\n",
- " with open(\"voice.wav\", \"wb\") as f:\n",
+ " with open(output_path, \"wb\") as f:\n",
" f.write(await audio_query.synthesis(speaker=1))\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
- " await main()\n"
+ " await main()\n",
+ "\n",
+ "Audio(output_path)"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 5,
"metadata": {},
"outputs": [],
- "source": []
+ "source": [
+ "# Play the audio using sounddevice\n",
+ "def play_audio(wav_file):\n",
+ " # load wave file\n",
+ " data, sr = torchaudio.load(wav_file)\n",
+ " # play audio\n",
+ " sd.play(data[0].numpy(), sr)\n",
+ " # wait until finish playing\n",
+ " sd.wait()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "play_audio(output_path)"
+ ]
}
],
"metadata": {
@@ -45,7 +124,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.9.17"
},
"orig_nbformat": 4
},
From 612ad8b4467ed76b47f2fed2429d34e6efd04a2d Mon Sep 17 00:00:00 2001
From: YCHuang2112 <96855801+YCHuang2112@users.noreply.github.com>
Date: Wed, 23 Aug 2023 09:35:52 +0800
Subject: [PATCH 3/3] update
---
examples/basic.ipynb | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/examples/basic.ipynb b/examples/basic.ipynb
index 7b10b5d..46dd90e 100644
--- a/examples/basic.ipynb
+++ b/examples/basic.ipynb
@@ -1,5 +1,14 @@
{
"cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## VOICEVOX Simple Demo\n",
+ "\n",
+ "##### Remember to turn on your VOICEVOX first"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 44,