Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-_ctypes.comeror-2147287038(comtypes模块)_Python_Python 3.x_Ctypes - Fatal编程技术网

Python-_ctypes.comeror-2147287038(comtypes模块)

Python-_ctypes.comeror-2147287038(comtypes模块),python,python-3.x,ctypes,Python,Python 3.x,Ctypes,编辑:我是偶然来到这里的,我意识到我没有把代码放在这里,代码是谁写的???。很抱歉。。。当我发表这个问题时,我只知道编程两三个月。我甚至没想过。该图书馆名为TTS,来自DeepHorizons。非常感谢您提供该图书馆!:-自从我问起,它似乎已经更新了 我一直试图用我在github上找到的python模块记录英语词典中的所有单词,该模块由我修改。我试着记录下从IVONA 2 Brian到网络故障的每一个单词,我可以在我的Raspberry Pi上有相同的声音,而不是espeak或pico tts声

编辑:我是偶然来到这里的,我意识到我没有把代码放在这里,代码是谁写的???。很抱歉。。。当我发表这个问题时,我只知道编程两三个月。我甚至没想过。该图书馆名为TTS,来自DeepHorizons。非常感谢您提供该图书馆!:-自从我问起,它似乎已经更新了

我一直试图用我在github上找到的python模块记录英语词典中的所有单词,该模块由我修改。我试着记录下从IVONA 2 Brian到网络故障的每一个单词,我可以在我的Raspberry Pi上有相同的声音,而不是espeak或pico tts声音。但我的问题是,我第一次只能录到大约81.000,然后是7.000,然后是2.000,我需要479.000。包含单词列表的文件也可以在github上找到,每个单词都位于新行中。我的脚本代码是:

#!/usr/bin/env python3
"""
This ia a helper to use the Microsoft SAPI

Needs to run on a windows system
Requires comtypes
"""

import os
import comtypes.client  # Importing comtypes.client will make the gen subpackage
try:
    from comtypes.gen import SpeechLib  # comtypes
except ImportError:
    # Generate the SpeechLib lib and any associated files
    engine = comtypes.client.CreateObject("SAPI.SpVoice")
    stream = comtypes.client.CreateObject("SAPI.SpFileStream")
    from comtypes.gen import SpeechLib


class Sapi(object):
    """A speech API using Microsofts SAPI through COM"""

    def __init__(self):
        super().__init__()
        self.voice = comtypes.client.CreateObject('Sapi.SpVoice')

    def get_voices(self, name=''):
        """Get a list of voices, search by name optional"""
        voice_list = []
        voices = self.voice.GetVoices()

        if name is not '':
            for voice in voices:
                if name in voice.GetDescription():
                    voice_list.append(voice)
                    break
            else:
                print('Voice not found')
        else:
            for voice in voices:
                voice_list.append(voice)

        return voice_list

    def get_voice_names(self):
        """Get the names of all the voices"""
        return [voice.GetDescription() for voice in self.get_voices()]

    def set_voice(self, voice):
        """Set the voice to the given voice"""
        if type(voice) is str:
            self.voice.Voice = self.get_voices(voice)[0]
        else:
            self.voice.Voice = voice
        return

    def get_audio_outputs(self, name=''):
        """Get the audio outputs, search for the one with the name if given"""
        output_list = []
        outputs = self.voice.GetAudioOutputs()

        if name is not '':
            for output in outputs:
                if name in output.GetDescription():
                    output_list.append(output)
                    break
            else:
                print('Audio output not found')
        else:
            for output in outputs:
                output_list.append(output)

        return output_list

    def get_audio_output_names(self):
        """Get the names of all the audio outpus"""
        return [output.GetDescription() for output in self.get_audio_outputs()]

    def set_audio_output(self, output):
        if type(output) is str:
            self.voice.AudioOutput = self.get_audio_outputs(output)[0]
        else:
            self.voice.AudioOutput = output
        return

    def say(self, message):
        self.voice.Speak(message)
        return
    
    def set_rate(self, rate):
        """Set the speed of the speaker
        -10 is slowest, 10 is fastest"""
        self.voice.Rate = rate

    def _create_stream(self, filename):
        """Create a file stream handler"""
        stream = comtypes.client.CreateObject('Sapi.SpFileStream')
        stream.Open(filename, SpeechLib.SSFMCreateForWrite)
        return stream

    def create_recording(self, filename, message):
        """Make a recording of the given message to the file
        The file should be a .wav as the output is
        PCM 22050 Hz 16 bit, Little engianness, Signed"""
        stream = self._create_stream(filename)
        temp_stream = self.voice.AudioOutputStream
        self.voice.AudioOutputStream = stream
        self.say(message)
        self.voice.AudioOutputStream = temp_stream


if __name__ == '__main__':
    v = Sapi()
#From here is my code, above is the code from the module I found.
with open("words.txt","r") as words:
    lines=words.read().splitlines()
words.close()
num_total=(len(lines))
os.mkdir("list")
num=0
for e in lines:
    word=""
    for i in e:
        if i=="/" or i==":" or i=="*" or i=="?" or i=="<" or i==">" or i=="|":
            word+=" "
        else:
            word+=i
    v.set_voice("Brian")
    v.set_rate(+1)
    v.create_recording("list/"+word+".wav", e)
我使用的是升华文本3,解释器中显示的是:

我不知道为什么这会停止。它停止,我删除文件中记录的单词,然后我运行脚本,它可以运行,但它会减少记录的单词数

有人能解释一下为什么会这样吗

编辑:微软安娜也是如此,如果它能帮助任何人理解这一点


提前谢谢。

过了一段时间,我想我应该把words.txt文件分成更小的文件,每个文件有10000个单词,每个单词都有一个python脚本,并在记录之前打印出来,看看问题出在哪里。它在“aux”上停了几秒钟,然后在“aux”上停了几秒钟。没有错误,我也不知道为什么,它没有记录这些错误。它用“con”和“con”给出了错误。知道这一点,我试着用eSpeak TTS应用程序录制这4个单词。它记录了这些文件,但我无法将它们保存在任何地方,因为这些文件名受它使用的Windows保护。现在脚本看起来是这样的:

#!/usr/bin/env python3
"""
This ia a helper to use the Microsoft SAPI

Needs to run on a windows system
Requires comtypes
"""

import os
import comtypes.client  # Importing comtypes.client will make the gen subpackage
try:
    from comtypes.gen import SpeechLib  # comtypes
except ImportError:
    # Generate the SpeechLib lib and any associated files
    engine = comtypes.client.CreateObject("SAPI.SpVoice")
    stream = comtypes.client.CreateObject("SAPI.SpFileStream")
    from comtypes.gen import SpeechLib


class Sapi(object):
    """A speech API using Microsofts SAPI through COM"""

    def __init__(self):
        super().__init__()
        self.voice = comtypes.client.CreateObject('Sapi.SpVoice')

    def get_voices(self, name=''):
        """Get a list of voices, search by name optional"""
        voice_list = []
        voices = self.voice.GetVoices()

        if name is not '':
            for voice in voices:
                if name in voice.GetDescription():
                    voice_list.append(voice)
                    break
            else:
                print('Voice not found')
        else:
            for voice in voices:
                voice_list.append(voice)

        return voice_list

    def get_voice_names(self):
        """Get the names of all the voices"""
        return [voice.GetDescription() for voice in self.get_voices()]

    def set_voice(self, voice):
        """Set the voice to the given voice"""
        if type(voice) is str:
            self.voice.Voice = self.get_voices(voice)[0]
        else:
            self.voice.Voice = voice
        return

    def get_audio_outputs(self, name=''):
        """Get the audio outputs, search for the one with the name if given"""
        output_list = []
        outputs = self.voice.GetAudioOutputs()

        if name is not '':
            for output in outputs:
                if name in output.GetDescription():
                    output_list.append(output)
                    break
            else:
                print('Audio output not found')
        else:
            for output in outputs:
                output_list.append(output)

        return output_list

    def get_audio_output_names(self):
        """Get the names of all the audio outpus"""
        return [output.GetDescription() for output in self.get_audio_outputs()]

    def set_audio_output(self, output):
        if type(output) is str:
            self.voice.AudioOutput = self.get_audio_outputs(output)[0]
        else:
            self.voice.AudioOutput = output
        return

    def say(self, message):
        self.voice.Speak(message)
        return

    def set_rate(self, rate):
        """Set the speed of the speaker
        -10 is slowest, 10 is fastest"""
        self.voice.Rate = rate

    def _create_stream(self, filename):
        """Create a file stream handler"""
        stream = comtypes.client.CreateObject('Sapi.SpFileStream')
        stream.Open(filename, SpeechLib.SSFMCreateForWrite)
        return stream

    def create_recording(self, filename, message):
        """Make a recording of the given message to the file
        The file should be a .wav as the output is
        PCM 22050 Hz 16 bit, Little engianness, Signed"""
        stream = self._create_stream(filename)
        temp_stream = self.voice.AudioOutputStream
        self.voice.AudioOutputStream = stream
        self.say(message)
        self.voice.AudioOutputStream = temp_stream


if __name__ == '__main__':
    v = Sapi()
#From here is my code, above is the code from the module I found.
num=9
print("Started!")
print()
for i in range(0,47):
    print(num)
    print()
    words=open("list_words/words"+str(num)+".txt","r")
    linhas=words.read().splitlines()
    num_total=(len(linhas))
    os.mkdir("list_words/list"+str(num))
    for e in linhas:
        word=""
        for i in e:
            if i=="/" or i==":" or i=="*" or i=="?" or i=="<" or i==">" or i=="|":
                word+=" "
            else:
                word+=i
        v.set_voice("Brian")
        v.set_rate(+1)
        #I added the try and except way, so every time it can't save the recorded file with its original name, it will save it with 'ERROR - ' before and save the word on a file (backup)
        try:
            v.create_recording("list_words/list"+str(num)+"/"+word+".wav", e)
        except comtypes.COMError:
            v.create_recording("list_words/list"+str(num)+"/ERROR - "+word+".wav", e)
            erros=open("errors_recorder.txt","a")
            erros.write("\n"+word)
    num+=1
print("Finished!")
现在,它可以记录所有的单词,而不会中断这个过程。我仍然分割了words.txt文件,因为我没有完成录制,我想确保如果再次出现任何错误,我不必搜索所有内容,只在10000个单词中找到它,打印每个单词

也许这可以帮助其他有问题的人,因为我不知道Windows不允许保存具有特定名称的文件

谢谢你