0

我正在我的树莓派 3b 上制作一个 gTTS 语音助手,比如 Siri。谷歌文本到语音的作品,但是当我使用 SpeechRecognition 和 Pyaudio 进行输入时,我得到了错误。我已经意识到我可以使这段代码更简单,它会做同样的事情,但我不需要改变它。这是代码:

import os
from gtts import gTTS
import time
import speech_recognition as sr

text_to_read = "Hello my name is Alex. What's your name? "


language = 'en'
slow_audio_speed = False
filename = '1.mp3'
filename2 = '2.mp3'
filename3 = '3.mp3'
filename4 = '4.mp3'
filename5 = '5.mp3'
filename6 = '6.mp3'
filename7 = '7.mp3'
filename8 = '8.mp3'
 filename9 = '9.mp3'
filename10 = '10.mp3'
filename11 = '11.mp3'
filename12 = '12.mp3'
filename13 = '13.mp3'

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said

def reading_from_string():
    audio_created = gTTS(text=text_to_read, lang=language,
                         slow=slow_audio_speed)
    audio_created.save(filename)


    os.system(f'omxplayer --no-keys {filename}')
def reading_from_string2():
    audio_created2 = gTTS(text=text_to_read2, lang=language,
                         slow=slow_audio_speed)
    audio_created2.save(filename2)

    os.system(f'omxplayer --no-keys {filename2}')
def reading_from_string3():
    audio_created3 = gTTS(text=text_to_read3, lang=language,
                         slow=slow_audio_speed)
    audio_created3.save(filename3)

    os.system(f'omxplayer --no-keys {filename3}')

def reading_from_string4():
    audio_created4 = gTTS(text=text_to_read4, lang=language,
                         slow=slow_audio_speed)
    audio_created4.save(filename4)

    os.system(f'omxplayer --no-keys {filename4}')

def reading_from_string5():
    audio_created5 = gTTS(text=text_to_read5, lang=language,
                         slow=slow_audio_speed)
    audio_created5.save(filename5)

    os.system(f'omxplayer --no-keys {filename5}')

def reading_from_string6():
    audio_created6 = gTTS(text=text_to_read6, lang=language,
                     slow=slow_audio_speed)
    audio_created6.save(filename6)

    os.system(f'omxplayer --no-keys {filename6}')
def reading_from_string11():
    audio_created11 = gTTS(text=text_to_read11, lang=language,
                         slow=slow_audio_speed)
    audio_created11.save(filename11)

    os.system(f'omxplayer --no-keys {filename11}')

def reading_from_string12():
    audio_created12 = gTTS(text=text_to_read12, lang=language,
                         slow=slow_audio_speed)
    audio_created12.save(filename12)

    os.system(f'omxplayer --no-keys {filename12}')
     
def reading_from_string13():
    audio_created13 = gTTS(text=text_to_read13, lang=language,
                         slow=slow_audio_speed)
    audio_created13.save(filename13)

    os.system(f'omxplayer --no-keys {filename13}')

f=open('name.txt','r+')
x=f.read()
 f.close()
reading_from_string()
answer = get_audio()
if answer == x :
    text_to_read11 = "Hello"
    reading_from_string11()
    f=open('name.txt','r')
    g=f.read()
    text_to_read12 = g
    reading_from_string12()
    f.close()
    text_to_read13 = 'Good to see you again'
    reading_from_string13()

else:
    name=get_audio()
    f=open('name.txt', 'w')
    f.write(name)
    f.close()
    text_to_read2 = 'Hello'
    reading_from_string2()
    text_to_read3 = (name)
    time.sleep(0.5)
    reading_from_string3()
    text_to_read4= 'What do you like'
    reading_from_string4()
    text_to_read6 = get_audio()
    y=open('likes.txt','w')
    x=text_to_read6
    y.write(x)
    y.close()
    text_to_read5 = 'storing memory'
    reading_from_string5()
    time.sleep(1)
    reading_from_string6()

这是错误:

Audio codec mp3float channels 1 samplerate 24000 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.front.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM front
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM surround21
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM surround21
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.surround40.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM surround40
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM surround41
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM surround50
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM surround51
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.surround71.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM surround71
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM iec958
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM spdif
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_hdmi.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM spdif
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM bluealsa
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM bluealsa
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Exception: FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent
Audio codec mp3float channels 1 samplerate 24000 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)
Traceback (most recent call last):
  File "/home/pi/Desktop/coding/Alex.py", line 111, in <module>
    reading_from_string12()
  File "/home/pi/Desktop/coding/Alex.py", line 88, in reading_from_string12
    slow=slow_audio_speed)
  File "/home/pi/.local/lib/python3.7/site-packages/gtts/tts.py", line 120, in __init__
    assert text, 'No text to speak'
AssertionError: No text to speak

我正在使用 TKGOU UM6 USB 麦克风。你知道出了什么问题吗?

谢谢你。

4

0 回答 0