Python Text to Speech in Macintosh

后端 未结 3 2198
攒了一身酷
攒了一身酷 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: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)

提交回复
热议问题