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

124 lines
3.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "364a678c-2196-405c-b37d-c2b9a23d03b3",
"metadata": {},
"outputs": [],
"source": [
"!pip install praat-parselmouth eng-to-ipa numpy matplotlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20b96d14-2302-4335-bb56-7f25e64efa81",
"metadata": {},
"outputs": [],
"source": [
"import parselmouth\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import eng_to_ipa as ipa\n",
"from IPython.display import Audio"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf3bbd3e-e344-448b-9e5c-7e92fedd218d",
"metadata": {},
"outputs": [],
"source": [
"text = \"One is sustained entirely by systematic logical procedures, the other by consensual verification by contemporaries, by their predecessors represented through prevailing traditions, or by posterity for those who expect historic vindication.\"\n",
"ipa.convert(text)\n",
"\n",
"\n",
"# 'wən ɪz səˈsteɪnd ɪnˈtaɪərli baɪ ˌsɪstəˈmætɪk ˈlɑʤɪkəl prəˈsiʤərz, ðə ˈəðər baɪ kənˈsɛnʃuəl ˌvɛrəfəˈkeɪʃən baɪ kənˈtɛmpərˌɛriz, baɪ ðɛr ˈprɛdəˌsɛsərz ˌrɛprɪˈzɛnɪd θru prɪˈveɪlɪŋ trəˈdɪʃənz, ər baɪ pɑˈstɛrəti fər ðoʊz hu ɪkˈspɛkt hɪˈstɔrɪk vɪndəˈkeɪʃən.'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8cf9d9a-5eb3-40dd-8e4e-86269c3e9286",
"metadata": {},
"outputs": [],
"source": [
"sound_file = \"/Users/joker/Library/Mobile Documents/com~apple~CloudDocs/iDesktop/Enjoying-English-Drafts/audios/yet-it-is-a-fact.mp3\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "862e3987-61bb-4e8c-8796-819b9de787c3",
"metadata": {},
"outputs": [],
"source": [
"snd = parselmouth.Sound(sound_file)\n",
"\n",
"def draw_pitch(pitch):\n",
" # Extract selected pitch contour, and\n",
" # replace unvoiced samples by NaN to not plot\n",
" pitch_values = pitch.selected_array['frequency']\n",
" pitch_values[pitch_values==0] = np.nan\n",
" plt.plot(pitch.xs(), pitch_values, 'o', markersize=1)\n",
" plt.grid(False)\n",
" plt.ylim(0, pitch.ceiling/2) # `/2` remove noise?\n",
"\n",
"def draw(snd):\n",
" plt.figure()\n",
" \n",
" plt.plot(snd.xs(), snd.values.T)\n",
" plt.show() # or plt.savefig(\"sound.png\"), or plt.savefig(\"sound.pdf\")\n",
" \n",
" pitch = snd.to_pitch()\n",
" draw_pitch(pitch)\n",
" plt.show() # or plt.savefig(\"sound.png\"), or plt.savefig(\"sound.pdf\") \n",
"\n",
"draw(snd)\n",
"\n",
"Audio(data=snd.values, rate=snd.sampling_frequency, autoplay=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f22ba08d-1a57-41a0-b5b1-a409cfe32a84",
"metadata": {},
"outputs": [],
"source": [
"play_from = 4.5\n",
"end_with = 8.6\n",
"\n",
"snd_part = snd.extract_part(from_time=play_from, to_time=end_with, preserve_times=True)\n",
"\n",
"draw(snd_part)\n",
"\n",
"Audio(data=snd_part.values, rate=snd_part.sampling_frequency, autoplay=True)"
]
}
],
"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
}