How to Generate an Audio Ad in Less than One Minute
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 6!
import os
import time
import audiostack
# Set the API key for AudioStack from environment variables
audiostack.api_key = os.environ['AUDIOSTACK_API_KEY']
# Record the start time
start_time = time.time()
# Define product details and desired mood and tone
product_name = "AudioStack"
product_description = (
"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."
)
mood_name = "dreamy" # Other options: exciting, dark, contemplative, weird, upbeat, neutral, meditative, etc.
tone_name = "confident"
# Use AI to generate your script
print("Generating your script...")
advert = audiostack.Content.Script.generate_advert(
product_name=product_name,
product_description=product_description,
mood=mood_name
)
ad_text = advert.data['adText']
ad_name = advert.data['adName']
script_id = advert.data['scriptId']
print(f"Your AI Generated Ad: {ad_text}")
# Select the best voice for the advert script based on content and tone
voices = audiostack.Speech.Voice.select_for_content(
content=ad_text,
tone=tone_name
)
voice_names = voices.data['voices']
recommended_voice = voice_names[0]['alias']
recommended_speed = voice_names[0]['speed']
print(f"Recommended Voice: {recommended_voice}")
print(f"Recommended Speed: {recommended_speed}")
# Generate speech using the recommended voice and speed
print(f"Generating speech for {recommended_voice}")
speech = audiostack.Speech.TTS.create(
scriptId=script_id,
voice=recommended_voice,
speed=recommended_speed
)
# Select the perfect sound template based on the script and mood
print("Selecting the perfect sound template...")
sound_templates = audiostack.Production.Sound.Template.select_for_script(
scriptId=script_id,
mood=mood_name
)
sound_template_names = sound_templates.data['templates']
recommended_template = sound_template_names[0]['name']
print(f"Recommended Sound Template: {recommended_template}")
# Create the final advert mix
print("Creating your advert...")
mix = audiostack.Production.Mix.create(
speechItem=speech,
soundTemplate=recommended_template,
masteringPreset="balanced"
)
# Download the final advert as an MP3 file
print("Downloading your MP3")
encoder = audiostack.Delivery.Encoder.encode_mix(
productionItem=mix,
preset="mp3"
)
encoder.download(
fileName=f"{ad_name}__{recommended_voice}",
path="."
)
# Record the end time and calculate the duration
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 the AudioStack API all in less than a minute!
Updated 3 months ago
Find out more about these endpoints in our API reference