Learning Objectives
Use terminal and pip commands to install more libraries
Run Offline ASR & TTS scripts and experience voice interaction functions
Understand the difference between online and offline ASR & TTS
Use AI Agent to generate code to modify offline ASR & TTS code
Understand and apply basic Python syntax such as, as, data type and input, random
(Extra) Install translate library to generate foreign language speech
Activity 1 Install more libraries
Open Terminal in PyCharm

Use pip install commands to install offline ASR & TTS libraries
pip install pyttsx3 pywin32 speechrecognition SpeechRecognition[faster-whisper]Check installation success or failure messages

Activity 2 Run Online and Offline Code
Copy prepared online ASR & TTS script and run it
#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)
#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)Copy offline ASR & TTS script and run it
# TTS offline
import pyttsx3
e = pyttsx3.init()
txt = input('please input the text:')
e.say(txt)
e.save_to_file(txt,"pyttsx3.wav")
e.runAndWait()# ASR offline
import speech_recognition as sr
r = sr.Recognizer()
mic = sr.Microphone()
print("Start to record")
mic.__enter__()
audio = r.listen(mic)
result = r.recognize_faster_whisper(audio)
print("Result ", result)Test microphone input and voice output for both versions

Observe running speed and response results
Activity 3 Compare online and offline code
Summarize advantages and disadvantages of online vs offline voice services
Comparison Item | Online Mode |
Network Dependency | |
Response Latency | |
Data Security | |
Environmental Adaptability | |
Function Richness | |
Device Resource Occupation | |
Update Frequency | |
Usage Cost | |
Accuracy Level |
Discuss suitable usage scenarios for each type
Activity 4 Use AI Agent Coding
Input the prompts to AI Agent to modify TTS (e.g., change language, rate, volume)
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)
# modify these code so I can set different voice effect Let AI generate improved offline TTS code
import edge_tts
import os
# ====================== CUSTOMIZABLE SETTINGS ======================
# Text you want to convert to speech
TEXT = "Hello World! This is a custom voice effect test."
# Voice name (change to your favorite voice)
VOICE = "en-US-AriaNeural"
# Voice effects parameters (adjust these values!)
# rate: speech speed → "+X%" faster, "-X%" slower; default = +0%
SPEECH_RATE = "+0%"
# volume: sound volume → "+X%" louder, "-X%" quieter; default = +0%
SPEECH_VOLUME = "+0%"
# pitch: voice tone → "+XHz" higher, "-XHz" lower; default = +0Hz
SPEECH_PITCH = "+0Hz"
# ===================================================================
# Generate TTS with custom effects
tts = edge_tts.Communicate(
text=TEXT,
voice=VOICE,
rate=SPEECH_RATE,
volume=SPEECH_VOLUME,
pitch=SPEECH_PITCH
)
# Save and play the audio
OUTPUT_FILE = "tts_custom.mp3"
tts.save_sync(OUTPUT_FILE)
os.startfile(OUTPUT_FILE)Run the new code and experience the difference

Repeat the process
Activity 5 Syntax Learning Test
Use input() to get user text or commands
# change /txt = "Hello World!"/ to this
txt = input('Please input the text you want to speak: ')Input text in the python console to generate voice

Import random library and run the tese code
import random
# Get a random integer from 1 to 10
num = random.randint(1, 10)
print(num)Generate random volume and rate for the TTS
# Import edge_tts library for text-to-speech conversion
import edge_tts
# Import os library to play audio file
import os
# Import random library to get random speech speed
import random as rm
# Set text content
txt = input("Please input any words: ")
# Select voice tone
voice = "en-US-AriaNeural"
# Generate random speed value
speed_num = rm.randint(-20,20)
# Combine into standard speed format
random_speed = str(speed_num) + "%"
# Initialize TTS task
tts = edge_tts.Communicate(txt,voice,rate=random_speed)
# Define audio save path
path = "tts.mp3"
# Save audio file
tts.save_sync(path)
# Play audio automatically
os.startfile(path)
# Output current speed parameter
print("Current random speed: ",random_speed)Convert between string, int, and float data types
Data Type | Description | Example | Common Usage |
string (str) | Text data, enclosed in quotes |
| Store names, messages, symbols, any text |
integer (int) | Whole numbers without decimals |
| Math calculations, counts, ages, indexes |
float | Numbers with decimal points |
| Precise calculations, heights, scores, decimals |
str_num = "10"
int_num = 10
float_num = 10.0
# Show original data types
print("Original types:")
print("str_num:", str_num, type(str_num))
print("int_num:", int_num, type(int_num))
print("float_num:", float_num, type(float_num))
# Show the big difference: + operator behavior
print("Difference with + operator:")
print("String + String:", str_num + str_num)
print("Int + Int:", int_num + int_num)
print("Float + Float:", float_num + float_num)
# Convert between data types
print("Type conversion results:")
str_to_int = int(str_num)
print("String to Int:", str_to_int, type(str_to_int))
str_to_float = float(str_num)
print("String to Float:", str_to_float, type(str_to_float))
int_to_float = float(int_num)
print("Int to Float:", int_to_float, type(int_to_float))
float_to_int = int(float_num)
print("Float to Int:", float_to_int, type(float_to_int))
int_to_str = str(int_num)
print("Int to String:", int_to_str, type(int_to_str))Complete Python questions to verify grammar mastery
ICT 2 MCQ
What is the data type of "25"?
What is the result of "3"+"6"?
Which one belongs to float type?
Which function converts data to integer type?
What is the result of int(7.8)?
What is the output type of type(100.0)?
What is the result of 5 + 2.5?
Which statement is correct?
Convert "18" to float, the result is?
What does str(66) return?