How to Generate Speech and Music using AI

In this recipe, you'll learn how to use audio generation and voice and sound selection endpoints to generate an AI audio advert, complete with AI generated script.


Pre-Requisites for this API Recipe

You will need to have set up your development environment and installed AudioStack to complete this tutorial. Follow the steps here to get set up. If you want to generate an AI audio advert without writing any code, find out more about SonicSell here.

Now you're ready to get going, copy and paste the code below into a new Python file and run it. Don't forget to add your API key into line 11!

This file tests all the functionality of Sonicsell advert generation and produces one ready-to-use ad!
It also reports the time taken to generate it from start to finish

import os
import audiostack
import time

audiostack.api_key = "YOUR_API_KEY"

start_time = time.time()

productName = "AudioStack"
productDescription = "AudioStack's enterprise-grade audio AI technology seamlessly integrates into your product or workflow and cuts your production cycles to seconds while making your budgets go further."
moodName = "dreamy" #exciting, dark, contemplative, weird, upbeat, neutral, meditative, cool, happy, mellow, reflective, emotional, hopeful, alarming, chill, dreamy, friendly, sweet, passionate, suspense
toneName = "confident"

print("Generating your AI advert script...")
advert = audiostack.Content.Script.generate_advert(

adText =['adText']
adName =['adName']
scriptId =['scriptId']
print(f"Your AI Generated Ad: {adText}")

voices = audiostack.Speech.Voice.select_for_content(
    content= adText, tone= toneName

voiceNames =['voices']

recommendedVoice = voiceNames[0]['alias']
recommendedSpeed = voiceNames[0]['speed']
print(f"Recommended Voice: {recommendedVoice}")
print(f"Recommended Speed: {recommendedSpeed}")

print(f"Generating speech for {recommendedVoice}") 
speech = audiostack.Speech.TTS.create(scriptId=scriptId, voice=recommendedVoice, speed=recommendedSpeed)  

print("Selecting the perfect sound template...")
soundTemplates = audiostack.Production.Sound.Template.select_for_script(scriptId=scriptId, mood=moodName)  
soundTemplateNames =['templates']

recommendedTemplate = soundTemplateNames[0]['name']
print(f"Recommended Sound Template: {recommendedTemplate}")

print("Creating your advert...")
mix = audiostack.Production.Mix.create(speechItem=speech, soundTemplate=recommendedTemplate, masteringPreset="balanced")  # Use a string for 'masteringPreset'

print("Downloading your MP3")
encoder = audiostack.Delivery.Encoder.encode_mix(productionItem=mix, preset="mp3")"{adName}__{recommendedVoice}", path=".")  

end_time = time.time()
duration = end_time - start_time

print(f"Creating, producing and mixing this entire advert took just {duration} seconds")

Here, you can see we have generated an advert for AudioStack, with a default length (around 30 seconds) and using a recommended Voice and Sound Template.

You can customise the advert by simply changing the values in lines 15-18 to include your product name, description, and (if required) desired mood and tone. Don't have a mood or tone in mind? Don't worry, these are optional!


Congratulations on creating your first AI audio advert with AudioStack API

What’s Next

Find out more about these endpoints in our API reference