text2speech.py 1.31 KB
Newer Older
1
2
3
4
5
6
import base64
import boto3
import os
from contextlib import closing

# Source : http://ipa-reader.xyz/
7
8
9
10

# Liste des phonèmes acceptés pour le français : https://docs.aws.amazon.com/polly/latest/dg/ph-table-french.html
# Liste des tags ssml disponibles : https://docs.aws.amazon.com/polly/latest/dg/supportedtags.html#s-tag 

11
12
13
14
15
16
17
18
19
20
21
22
23
def lambda_handler(ipa, loc):
    # init polly
    polly = boto3.client("polly")

    # get submitted text string and selected voice
    text = ipa
    voice = loc

    # strip out slashes if submitted with text string
    if "/" in text:
        text = text.replace("/", "")

    # generate phoneme tag for polly to read
24
25
    text = text.replace(' ', "'></phoneme><break/><phoneme alphabet='ipa' ph='")
    phoneme = "<prosody rate='85%'><phoneme alphabet='ipa' ph='"+text+"'></phoneme></prosody>" #<prosody rate='85%'></prosody>
26
    
27
    
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    # send to polly, requesting mp3 back
    response = polly.synthesize_speech(
        OutputFormat="mp3",
        TextType="ssml",
        Text=phoneme,
        VoiceId=voice
    )

    print(response)
    # encode polly's returned audio stream as base64 and return
    if "AudioStream" in response:
        
        with closing(response["AudioStream"]) as stream:
            audio = base64.encodebytes(stream.read())

43
        return (audio.decode("ascii"),len(phoneme))