Files
everyone-can-use-english/new-edition-drafts/jupyter-notebooks/edge-tts.ipynb
2024-01-23 08:44:16 +08:00

163 lines
4.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "b94f103d-ac43-4d13-83cb-eb5090220881",
"metadata": {},
"source": [
"# EdgeTTS\n",
"\n",
"https://github.com/rany2/edge-tts\n",
"\n",
"edge-tts is a Python module that allows you to use Microsoft Edge's online text-to-speech service from within your Python code or using the provided edge-tts or edge-playback command."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77deb08f-fec3-4327-b2f9-1c893aacaddc",
"metadata": {},
"outputs": [],
"source": [
"!pip install edge-tts\n",
"!pip install pygame"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9aad6634-e833-4ec4-b285-84e051329712",
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"import edge_tts\n",
"import pygame\n",
"\n",
"TEXT = \"\"\"\n",
"Once upon a time, the King's youngest son became filled with the desire to go abroad, and see the world.\n",
"\"\"\"\n",
"\n",
"VOICE = \"en-US-GuyNeural\" # Male\n",
"# VOICE = \"en-US-AnaNeural\" # Female\n",
"# VOICE = \"en-US-AndrewNeural\" # Male\n",
"# VOICE = \"en-US-AriaNeural\" # Female\n",
"# VOICE = \"en-US-AvaNeural\" # Female\n",
"# VOICE = \"en-US-BrianNeural\" # Male\n",
"# VOICE = \"en-US-ChristopherNeural\" # Male\n",
"# VOICE = \"en-US-EmmaNeural\" # Female\n",
"# VOICE = \"en-US-EricNeural\" # Male\n",
"# VOICE = \"en-US-GuyNeural\" # Male\n",
"# VOICE = \"en-US-JennyNeural\" # Female\n",
"# VOICE = \"en-US-MichelleNeural\" # Female\n",
"# VOICE = \"en-US-RogerNeural\" # Male\n",
"# VOICE = \"en-US-SteffanNeural\" # Male\n",
"# VOICE = \"en-GB-LibbyNeural\" # Female\n",
"# VOICE = \"en-GB-MaisieNeural\" # Female\n",
"# VOICE = \"en-GB-RyanNeural\" # Male\n",
"# VOICE = \"en-GB-SoniaNeural\" # Female\n",
"# VOICE = \"en-GB-ThomasNeural\" # Male\n",
"# VOICE = \"en-AU-NatashaNeural\" # Female\n",
"# VOICE = \"en-AU-WilliamNeural\" # Male\n",
"# VOICE = \"en-CA-ClaraNeural\" # Female\n",
"# VOICE = \"en-CA-LiamNeural\" # Male\n",
"\n",
"OUTPUT_FILE_NAME = f\"{VOICE}_{TEXT[:15]}.mp3\"\n",
"\n",
"OUTPUT_FILE = f\"{OUTPUT_FILE_NAME}.mp3\"\n",
"WEBVTT_FILE = f\"{OUTPUT_FILE_NAME}.vtt\"\n",
"\n",
"communicate = edge_tts.Communicate(TEXT, VOICE)\n",
"await communicate.save(OUTPUT_FILE)\n",
"\n",
"submaker = edge_tts.SubMaker()\n",
"with open(OUTPUT_FILE, \"wb\") as file:\n",
" async for chunk in communicate.stream():\n",
" if chunk[\"type\"] == \"audio\":\n",
" file.write(chunk[\"data\"])\n",
" elif chunk[\"type\"] == \"WordBoundary\":\n",
" submaker.create_sub((chunk[\"offset\"], chunk[\"duration\"]), chunk[\"text\"])\n",
"\n",
"with open(WEBVTT_FILE, \"w\", encoding=\"utf-8\") as file:\n",
" file.write(submaker.generate_subs())\n",
"\n",
"print(\"Files created!\") "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "862f0948",
"metadata": {},
"outputs": [],
"source": [
"pygame.mixer.init()\n",
"pygame.mixer.music.load(OUTPUT_FILE)\n",
"pygame.mixer.music.play()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a73c84c-e221-4f2e-982a-0312929ffba4",
"metadata": {},
"outputs": [],
"source": [
"!pip install amfm_decompy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc271b37-fdf4-4888-bf46-31086b2299f3",
"metadata": {},
"outputs": [],
"source": [
"import amfm_decompy.basic_tools as basic\n",
"import amfm_decompy.pYAAPT as pYAAPT\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"#load audio 注意必须是wav\n",
"signal = basic.SignalObj(\"/Users/joker/Desktop/Speaking Training/The Case Against Education/Clips/Chapter 01 (Selection) 01.wav\")\n",
"\n",
"#pYAAPT pitches 生成pitches文件\n",
"pitch = pYAAPT.yaapt(signal, frame_length = 40, tda_frame_length=40,f0_min=75, f0_max=600)\n",
"\n",
"#plot \n",
"plt.plot(pitch.samp_values)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6a4cae2-78c1-4211-ac7b-e4339c95d945",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.8.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}