Python Text to Speech in Macintosh

后端 未结 3 2197
攒了一身酷
攒了一身酷 2020-12-28 21:45

Are there any libraries in Python that does or allows Text To Speech Conversion using Mac Lion\'s built in text to speech engine? I did google but most are windows based. I

相关标签:
3条回答
  • 2020-12-28 22:25

    Wouldn't it be much simpler to do this?

    from os import system
    system('say Hello world!')
    

    You can enter man say to see other things you can do with the say command.

    However, if you want some more advanced features, importing AppKit would also be a possibility, although some Cocoa/Objective C knowledge is needed.

    from AppKit import NSSpeechSynthesizer
    speechSynthesizer = NSSpeechSynthesizer.alloc().initWithVoice_("com.apple.speech.synthesis.voice.Bruce")
    speechSynthesizer.startSpeakingString_('Hi! Nice to meet you!')
    

    If you would like to see more things you can do with NSSpeechSynthesizer take a look at Apple's documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSpeechSynthesizer_Class/Reference/Reference.html

    0 讨论(0)
  • 2020-12-28 22:30

    If you are targeting Mac OS X as your platform - PyObjC and NSSpeechSynthesizer is your best bet.

    Here is a quick example for you

    #!/usr/bin/env python
    
    from  AppKit import NSSpeechSynthesizer
    import time
    import sys
    
    
    if len(sys.argv) < 2:
       text = raw_input('type text to speak> ')
    else:
       text = sys.argv[1]
    
    nssp = NSSpeechSynthesizer
    
    ve = nssp.alloc().init()
    
    for voice in nssp.availableVoices():
       ve.setVoice_(voice)
       print voice
       ve.startSpeakingString_(text)
    
       while not ve.isSpeaking():
          time.sleep(0.1)
    
       while ve.isSpeaking():
          time.sleep(0.1)
    

    Please note that AppKit module is part of PyObjC bridge and should be already installed on your Mac. No need to install it if you are using OS provided python (/usr/bin/python)

    0 讨论(0)
  • 2020-12-28 22:34

    This might work:

    import subprocess
    subprocess.call(["say","Hello World! (MESSAGE)"])
    
    0 讨论(0)
提交回复
热议问题