This is sample code for an Alexa-hosted skill that uses a custom text to speech (TTS) voice from Spokestack to replace Alexa's voice.
The skill itself isn't impressive; it simply repeats an encouraging message (see https://www.youtube.com/watch?v=xNx_gU57gQ4 for our inspiration) and exits. Its main purpose is to be a template demonstrating the use of SSML and a custom TTS service to replace the default voice on a smart speaker. The same SSML will work for Google Assistant, but the rest of the code in this repository is structured to run in Amazon's hosting infrastructure.
You'll need an Amazon developer account to set up this skill and a Spokestack account for it to produce responses.
- Log in to the Alexa developer console.
- On the "Skills" tab (which is selected by default at the time of writing), click
Create Skill
. - On the "Create a new skill" screen:
- Enter a name for your skill. Any name will do.
- Under "Choose a model..." select "Custom" (selected by default).
- Under "Choose a method..." select "Alexa-hosted (Python)".
- Click the "Create skill" button (you may have to scroll up to see it).
- On the next screen ("Choose a template..."), click the "Import skill" button.
- Enter this repository's URL (
https://github.com/spokestack/alexa-custom-tts
) in the "Import skill" box. - Click "Import".
Once you've clicked "Import", Amazon will take care of copying over the code and creating a new sandbox for your skill to run in. When the import completes:
- Click on the "Code" tab to finish setup. This will open the
lambda_function.py
file in a code editor. - Look for the "Customize your skill here!" section and customize it at will.
- The only things you need to change are
SPOKESTACK_CLIENT_ID
andSPOKESTACK_CLIENT_SECRET
, replacing the default values with a set of credentials from your account settings.
- The only things you need to change are
- When you're finished making changes, click "Save" at the top of the page.
- Click "Deploy" (next to "Save").
- Click over to the "Test" tab while you're waiting for the deployment to finish.
- In the dropdown next to "Test is disabled for this skill" (at the top of the page), you'll want to select "Development". This will let you test your skill directly on the page or on any Alexa-enabled devices connected to the account you used to create this skill.
That's it! Enjoy your new smart speaker voice!
The bulk of the sample code here is based on Amazon's Python SDK example. The only thing we're customizing is running the text of our responses (or response, in this case, because we're being lazy) through Spokestack's text to speech API. That gives us back a URL that points to the speech audio, and we're using the SSML audio
element to play the audio instead of having Amazon's TTS service synthesize the text for us in Alexa's voice and play that audio.
The code for the actual synthesis is in the spokestack.py
file. It makes a GraphQL request to Spokestack's TTS API and pulls out the resulting audio URL.
Yes! Spokestack's Maker tier lets you train your own TTS voice using a simple web tool and your own microphone. You can do it with as little as 5 minutes of data; once your voice is trained, simply plug its name into the VOICE
variable at the top of lambda_function.py
.
This happens if you ask Alexa to open a skill, but the skill's name isn't recognized. Development skills can run into this problem if the invocation name has been changed without rebuilding the skill's model. To ensure your skill name is up to date:
- Click on the "Build" tab in the development console.
- Click on "Invocation Name" in the Skill builder checklist.
- Double-check that your invocation name is what you want, then click "Save Model" at the top of the page.
- Click "Deploy Model".
When the model is updated, Amazon will notify you with a popup at the bottom of the screen. At this point, "open
skill name
" should work in the test console.