Amazon Polly 是將文字轉換成逼真說話方式的服務,能夠讓您建立會說話的應用程式和打造全新的啟用語音產品類別

Create an AWS account 

AWS 右上角「建立AWS帳戶」

Create a user with access to Polly 

AWS管理控制台 > AWS服務 > IAM

Add group 

存取管理 > 群組

  1. 建立新群組
  2. 連結政策 AmazonPollyFullAccess
    Imgur

Add user 

存取管理 > 使用者

  1. 選擇 AWS 存取類型
    Imgur
  2. Add user to group with Polly access Imgur
  3. 記好access key and a secret access key Imgur

Configure Mac to access AWS 

  1. 官網:安裝、更新和卸載 AWS CLI 第 2 版 於 macOS

    下載 macOS pkg 文件,並安裝

  2. AWS Config

    aws configure
    
    AWS Access Key ID [None]: [Your_AWS Access Key ID]
    AWS Secret Access Key [None]: [Your_AWS Secret Access Key]
    Default region name [None]: ap-northeast-1
    Default output format [None]:
    
  3. 安裝其他東西
    portaudio

    brew install portaudio
    

    boto3

    python3 -m pip install boto3
    

    pyaudio

    python3 -m pip install pyaudio
    
  4. Python library

    vim polly.py
    
    import boto3
    import pygame
    import os
    import time
    import io
       
    class Polly():
     OUTPUT_FORMAT='mp3'
    
     def __init__(self, voiceId):
         self.polly = boto3.client('polly') #access amazon web service
         self.VOICE_ID = voiceId
    
     def say(self, textToSpeech): #get polly response and play directly
         pollyResponse = self.polly.synthesize_speech(Text=textToSpeech, OutputFormat=self.OUTPUT_FORMAT, VoiceId=self.VOICE_ID)
            
         pygame.mixer.init()
         pygame.init()  # this is needed for pygame.event.* and needs to be called after mixer.init() otherwise no sound is played 
            
         if os.name != 'nt':
             pygame.display.set_mode((1, 1)) #doesn't work on windows, required on linux
                
         with io.BytesIO() as f: # use a memory stream
             f.write(pollyResponse['AudioStream'].read()) #read audiostream from polly
             f.seek(0)
             pygame.mixer.music.load(f)
             pygame.mixer.music.set_endevent(pygame.USEREVENT)
             pygame.event.set_allowed(pygame.USEREVENT)
             pygame.mixer.music.play()
             pygame.event.wait() # play() is asynchronous. This wait forces the speaking to be finished before closing
                
         while pygame.mixer.music.get_busy() == True:
             pass
    
     def saveToFile(self, textToSpeech, fileName): #get polly response and save to file
         pollyResponse = self.polly.synthesize_speech(Text=textToSpeech, OutputFormat=self.OUTPUT_FORMAT, VoiceId=self.VOICE_ID)
            
         with open(fileName, 'wb') as f:
             f.write(pollyResponse['AudioStream'].read())
             f.close()
    
  5. 編輯想要Polly說的話

    vim say.py
    
    from polly import Polly
       
    tts = Polly('Joanna')
    tts.say('Hi there, I\'m very glad you\'re reading my article. Leave a comment if you find it useful.')
    tts.saveToFile('Hi there, save the speech for later', 'joanna.mp3')
    
  6. 執行say.py

    python3 say.py
    

    接著會產生你要的音檔joanna.mp3

src: https://catqbat.com/make-your-pi-speak-with-text-to-speech-fom-amazon-polly/

官方開發指南

Python Example