ICT - Intelligent Speech Technology

Offline

Learning Objectives

  1. Use terminal and pip commands to install more libraries

  2. Run Offline ASR & TTS scripts and experience voice interaction functions

  3. Understand the difference between online and offline ASR & TTS

  4. Use AI Agent to generate code to modify offline ASR & TTS code

  5. Understand and apply basic Python syntax such as, as, data type and input, random

  6. (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

bash
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

python
#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

python
# 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()
python
# 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)

python
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

python
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

input() lets you type text into the console and sends it to Python as a string. Python needs input() to get live text/numbers from the user typing on the keyboard, making programs interactive instead of fixed and unchangeable.


python
# 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

python
import random
# Get a random integer from 1 to 10
num = random.randint(1, 10)
print(num)
random.randint(a, b) returns a random integer number between a and b (including both a and b).


Generate random volume and rate for the TTS

python
# 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

"Hello", '123', "Python3"

Store names, messages, symbols, any text

integer (int)

Whole numbers without decimals

10, -5, 0, 100

Math calculations, counts, ages, indexes

float

Numbers with decimal points

3.14, -2.5, 0.0, 9.8

Precise calculations, heights, scores, decimals

python
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

Single ChoiceICT 2 MCQ 1#11
[0/1]

What is the data type of "25"?

Single ChoiceICT 2 MCQ 2#13
[0/1]

What is the result of "3"+"6"?

Single ChoiceICT 2 MCQ 3#14
[0/1]

Which one belongs to float type?

Single ChoiceICT 2 MCQ 4#15
[0/1]

Which function converts data to integer type?

Single ChoiceICT 2 MCQ 5#16
[0/1]

What is the result of int(7.8)?

Single ChoiceICT 2 MCQ 6#17
[0/1]

What is the output type of type(100.0)?

Single ChoiceICT 2 MCQ 7#18
[0/1]

What is the result of 5 + 2.5?

Single ChoiceICT 2 MCQ 8#19
[0/1]

Which statement is correct?

Single ChoiceICT 2 MCQ 9#20
[0/1]

Convert "18" to float, the result is?

Single ChoiceICT 2 MCQ 10#21
[0/1]

What does str(66) return?


AI Notes