Learning Objectives
Launch PyCharm and create new Python projects and files correctly.
Use PyCharm terminal and pip commands to install required third-party libraries.
Run Python ASR and TTS scripts and experience voice interaction functions.
Modify key code parameters to achieve different ASR and TTS effects.
Understand and apply basic Python syntax such as import, print, variable, comments, functions and parameters.
(Extra) Combine ASR and TTS codes to implement a simple voice chat.
Activity 1 PyCharm Basic Operation
Launch PyCharm

Create a new Python project and sample py file.

Recognize project directory, code editing area.

Copy and run a simple hello world code
print('Hello World')
Change the code to run again
print('Anything that you like')
Activity 2 Library Installation & Environment Configuration
Open the built-in terminal window in PyCharm.

Input standard pip commands to install speechrecognition, pyaudio and edge-tts libraries.
pip install speechrecognition pyaudio edge-tts
Check the installation feedback information to confirm successful configuration.

Activity 3 Run & Experience ASR & TTS Program
Copy and run official ASR sample code.
#ASR
import speech_recognition as sr
rec = sr.Recognizer()
mic = sr.Microphone()
print("Start to record")
mic.__enter__()
audio = rec.listen(mic)
txt = rec.recognize_google(audio, language='en-US')
print("Result ", txt)Speak English sentences to test online voice recognition (ASR).

Check the text recognition results printed on the console.

Copy and run official TTS sample code.
#TTS
import edge_tts
import os
# You can change "Hello World!" to any text
txt = "Hello World!"
voice = "en-US-AriaNeural"
tts = edge_tts.Communicate(txt, voice)
path = "tts.mp3"
tts.save_sync(path)
os.startfile(path)Listen to the automatic speech synthesis and playback (TTS).

Confirm the generated audio file is saved successfully.

Activity 4 Code Modification & Effect Debugging
Locate the TTS voice parameter and text content in the code.

Modify English voice types (male/female neural voice).
Voice Code | Language | Gender |
en-US-AriaNeural | American English | Female |
en-GB-RyanNeural | British English | Male |
zh-CN-XiaoxiaoNeural | Mandarin Chinese | Female |
zh-HK-HiuGaaiNeural | Cantonese | Female |
zh-TW-HsiaoChenNeural | Taiwan Chinese | Female |
Change custom text content for speech synthesis.
txt = "Anything you like"Adjust audio file save name and path parameters.
path = 'anywhere.mp3'Re-run the program and compare different TTS effects.

Modify the ASR code to recognise different language.
Language | Code |
English | en-US |
简体中文 | zh-CN |
粤语 | zh-HK |

Activity 5 Syntax Learning Test
Mark core syntax lines including import, print and comments in code.
Identify function structures and adjustable parameters in the script.
Finish teacher’s syntax checking questions.
Syntax | Description | Code Example |
import | Import external functional libraries |
|
comment | Add explanatory notes for code |
|
variable | Store data and status information |
|
function | Reusable code block to realize functions |
|
parameters | Data passed into functions |
|
Output information to console |
|
Which library is used to realize voice-to-text function?
What can the microphone object do in the code?
What does the listen function mainly complete?
Which parameter sets the recognition language of voice?
Which library turns text into voice sound?
What file format is usually used to store voice audio?
Which command installs Python third-party libraries?
Which problem will broken string code bring?
What is the function of os.startfile()?
Which basic grammar is used in this program?
(Extra) Activity 6 Advanced Integrated Task
Integrate two functional modules into one complete program.
Realize the process of voice input → text recognition → automatic voice playback.
Debug program errors to ensure stable one-click operation.
Complete the full closed-loop voice interaction function.
