{
"cells": [
{
"cell_type": "markdown",
"id": "9ab2965f-103f-4c4f-b438-f337686d55c7",
"metadata": {},
"source": [
"[Onsei: Japanese pitch accent practice tool](https://github.com/itsupera/onsei)\n",
"================================================================================\n",
"\n",
"Click here for instructions !
\n",
"\n",
"
\n",
"Any feedback or suggestion ? Please tell me in this Gitter chat\n",
"
{sentence_html}
'\n", " else:\n", " w_sentence.value = ''\n", "\n", "\n", "def update_sample(sample):\n", " global teacher_rec\n", " global sentence\n", "\n", " sentence = Sentence(sample[\"sentence\"])\n", "\n", " with w_sentence.hold_sync(), w_audio.hold_sync(), fig_teacher.hold_sync(), fig_student.hold_sync(), fig_cmp.hold_sync(), w_cmp_result.hold_sync():\n", " update_sentence()\n", "\n", " teacher_rec = SpeechRecord(sample['filename'], sentence, name=\"Teacher\");\n", "\n", " w_audio.value = get_sample_audio_data(sample);\n", " \n", " fig_teacher.update_data(teacher_rec);\n", " fig_student.clear();\n", " fig_cmp.clear();\n", " \n", " w_cmp_result.value = \"\"\n", "\n", "\n", "update_sample(w_sample_selector.selected_sample());\n", "\n", "\n", "def sample_changed(change):\n", " update_sample(dict(change[\"new\"]));\n", "\n", "w_sample_selector.observe(sample_changed, 'value')\n", "\n", "\n", "def get_student_wav_filename():\n", " try:\n", " w_recorder.save('test.webm')\n", " except ValueError as exc:\n", " if str(exc).startswith('No data'):\n", " w_cmp_result.value = f\"Record something first !\"\n", " raise exc\n", " \n", " !ffmpeg -hide_banner -loglevel error -y -i test.webm -ar 16000 -ac 1 test.wav\n", " return 'test.wav'\n", "\n", "\n", "def run_compare(_):\n", " global teacher_rec\n", " global student_rec\n", "\n", " student_wav_filename = get_student_wav_filename()\n", " # Alternatively, here is a sample:\n", " #student_wav_filename = \"data/itsu_ga_ii_ka_wakarimasen.wav\"\n", "\n", " student_rec = SpeechRecord(student_wav_filename, sentence, name=\"Student\");\n", " fig_student.update_data(student_rec);\n", " \n", " alignment_method = w_alignment_method_dropdown.value\n", "\n", " try:\n", " student_rec.align_with(teacher_rec, method=alignment_method)\n", " mean_distance = student_rec.compare_pitch();\n", " w_cmp_result.value = f\"Success !\\nMean distance = {mean_distance:.2f}\"\n", " except Exception as exc:\n", " w_cmp_result.value = \"FAILED !\"\n", " raise exc\n", "\n", " fig_cmp.update_data(teacher_rec, student_rec)\n", "\n", "\n", "w_compare_btn.on_click(run_compare)\n", "\n", "# Update the comparison if we change the alignment method\n", "w_alignment_method_dropdown.observe(run_compare, 'value')\n", "\n", "\n", "# Layout\n", "\n", "w_tab = widgets.Tab()\n", "w_tab.children = [w_sample_selector, w_upload_sample]\n", "w_tab.set_title(0, \"Samples\")\n", "w_tab.set_title(1, \"Upload new samples\")\n", "\n", "box = widgets.Box([\n", " widgets.Box([\n", " w_tab,\n", " w_options_accordion\n", " ]),\n", " w_sentence,\n", " widgets.Box([\n", " widgets.VBox([widgets.Label(value=\"Teacher's recording:\"), w_audio], layout=widgets.Layout(width='33%')),\n", " widgets.VBox([widgets.Label(value=\"Your recording:\"), w_recorder], layout=widgets.Layout(width='33%')),\n", " widgets.VBox([w_compare_btn, w_cmp_result], layout=widgets.Layout(width='33%')),\n", " ]),\n", " fig_cmp,\n", " fig_student,\n", " fig_teacher,\n", "], layout=widgets.Layout(display=\"flex\", flex_flow=\"column\", align_items=\"stretch\", align_content=\"center\")\n", ")\n", "\n", "display(box)" ] }, { "cell_type": "code", "execution_count": null, "id": "a13585bc-778a-4899-9bad-489d4646d299", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }