"""使用本机 SAPI 语音合成生成 assets/voices/*.wav""" import os import sys OUTPUT_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "assets", "voices") VOICES = { "hooking.wav": "挂包中", "ready.wav": "挂包成功", "releasing.wav": "放包中", "arrive_1.wav": "到达1号工位", "arrive_2.wav": "到达2号工位", "arrive_3.wav": "到达3号工位", "arrive_4.wav": "到达4号工位", "arrive_5.wav": "到达5号工位", } def main(): import struct os.makedirs(OUTPUT_DIR, exist_ok=True) import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") SSFMCreateForWrite = 3 SAFT16kHz16BitMono = 4 for filename, text in VOICES.items(): path = os.path.join(OUTPUT_DIR, filename) stream = win32com.client.Dispatch("SAPI.SpFileStream") af = win32com.client.Dispatch("SAPI.SpAudioFormat") af.Type = SAFT16kHz16BitMono stream.Format = af stream.Open(path, SSFMCreateForWrite) speaker.AudioOutputStream = stream speaker.Speak(text) stream.Close() with open(path, 'rb+') as f: data = bytearray(f.read()) fmt_chunk = b'fmt ' fmt_pos = data.find(fmt_chunk) if fmt_pos == -1: print(f" 跳过音量放大(无 fmt chunk)") else: channels = struct.unpack_from('