6、AB按键切换语言合成项目
#MicroPython动手做(25)——语音合成与语音识别
#AB按键切换语言合成项目
from mpython import *
import network
import time
import ntptime
from xunfei import *
import audio
my_wifi = wifi()
my_wifi.connectWiFi("zh", "zy1567")
def on_button_a_down(_):
global Audio, Text
time.sleep_ms(10)
if button_a.value() == 1: return
rgb[0] = (int(102), int(0), int(0))
rgb.write()
time.sleep_ms(1)
Text = "A键被按下"
Audio = "tts.pcm"
speech_tts = Xunfei_speech("5ec66b", "5d32b259f15b2902d81b9efd22926", "3aace39c0ecea76ef46a200300826", mode=MODE_TTS, AudioFile=Audio, Text=Text)
print("Processing, please wait....")
speech_tts.tts()
oled.fill(0)
oled.DispChar(" A键被按下", 0, 16, 1)
oled.show()
audio.player_init()
audio.set_volume(120)
audio.play(Audio)
time.sleep(2)
oled.fill(0)
rgb[0] = (0, 0, 0)
rgb.write()
time.sleep_ms(1)
oled.show()
def on_button_b_down(_):
global Audio, Text
time.sleep_ms(10)
if button_b.value() == 1: return
rgb[2] = (int(102), int(0), int(0))
rgb.write()
time.sleep_ms(1)
Text = "B键被按下"
Audio = "tts.pcm"
speech_tts = Xunfei_speech("5ec66b", "5d32b259f15b2902d81b9efd22926", "3aace39c0ecea76ef46a200300826", mode=MODE_TTS, AudioFile=Audio, Text=Text)
print("Processing, please wait....")
speech_tts.tts()
oled.fill(0)
oled.DispChar(" B键被按下", 0, 16, 1)
oled.show()
audio.player_init()
audio.set_volume(120)
audio.play(Audio)
time.sleep(2)
oled.fill(0)
rgb[2] = (0, 0, 0)
rgb.write()
time.sleep_ms(1)
oled.show()
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
rgb[1] = (int(0), int(51), int(0))
rgb.write()
time.sleep_ms(1)
while True:
try:
ntptime.settime(8, "time.windows.com")
break
except:
pass
|