How to Make a Beautiful Audio Advertisement with your own sound templates in Seconds
Example using some beautiful music and blending it with synthetic speech
In this example, you'll create your own sound template using the API, and combine this with synthetic speech to create an audio asset.
If you haven't already set up your development environment, you can find out how to do this here.
Download the Example Files
While you'll find step by step instructions for this code example (using Python and interacting with the Python SDK), you'll need some audio files saved to the correct location on your machine to create a sound template. You can find all the files you need in this Github example.
Define your API Key and Create a Script
Whenever you use AudioStack API, you will need to identify yourself in some way, so that we know where to save your assets. Copy and paste the below example into a python file, and add your API key in line 3 to identify yourself. You can see that from line 5 a new script is created - you can change the text to customise it as much or as little as you like.
import audiostack
import os
audiostack.api_key = os.environ["AUDIOSTACK_API_KEY"] #Β fill up
script = """
<as:section name="intro" soundsegment="intro">
Make some custom templates with audiostack S.D.K.!. Now that I've introduced the topic lets move on to another section!</as:section>
<as:section name="main" soundsegment="main">
On this test we show you how to use custom music to generate templates.
You can add several segments an associate them to script sections.
Lets skip to the outro!
</as:section>
<as:section name="outro" soundsegment="outro">
And so we are now in the outro and its time to say Goood bye good bye
</as:section>
"""
Sections are marked in the script using the <as:section> tags.
Using sections, you can control which part of your sound design (segment) is played while the script is spoken.
Create a Custom Sound Template
Now, you're ready to create a sound template, which enables you to combine your sound design or music with speech. With AudioStack, you'll need to create an empty template first. Then, you can specify what each segment will consist of.
# CREATING CUSTOM TEMPLATE
# 1. Create an empty template
try:
template = audiostack.Production.Sound.Template.create(templateName="sound_templates_demo")
except Exception as e:
response = audiostack.Production.Sound.Template.delete(templateName="sound_templates_demo")
raise ValueError("template already existed so we cleared it for you, just re-run the demo")
#Β 2. Paths to our template segments / This segments can be
segments =[
{
"name": "intro",
"path": "demo_segments/br_intro.wav"
},
{
"name": "main",
"path": "demo_segments/br_main.wav"
},
{
"name": "outro",
"path": "demo_segments/br_outro.wav"
}
]
Upload Each Segment of the Template
Once you've created a sound template with different segments specified, you can upload each segment of the template to your organisation's media storage, and assign them to your template.
# # 3. Upload each segment of our template to audiostack media storage and assign it to the
# template previously created
for segment in segments:
# add to media storage
response = audiostack.Content.Media.create(segment["path"])
media_id =response.data.get('mediaId')
# assign to our template
response = audiostack.Production.Sound.Segment.create(
templateName="sound_templates_demo", soundSegmentName=segment["name"],
mediaId=media_id
)
Sound template names need to be unique.
Using the Sound Template
You can now combine the script created earlier with the sound template, by copying and pasting the following code into your script:
#Β 4. Template created! Now lets make use of it:
script = audiostack.Content.Script.create(scriptText=script, scriptName="demo", projectName="demo")
speech = audiostack.Speech.TTS.create(
scriptItem=script,
voice="wren",
speed=1,
)
mix = audiostack.Production.Mix.create(
speechId=speech.speechId,
soundTemplate="sound_templates_demo",
masteringPreset="balanced",
)
encoded = audiostack.Delivery.Encoder.encode_mix(
productionItem=mix,
preset='wav',
loudnessPreset = 'spotify'
)
encoded.download(fileName=f"demo_sound_templates")
print("Downloaded -> ", encoded.data)
# AUTOMATIC TAGGING PARAMETERS AND LISTING
# Once our template is created our automatic tagging engine will trigger and analyze the template.
# List some of the parameters of the auto-tagger:
response = audiostack.Production.Sound.Parameter.get()
parameters = response.data.get("parameters")
for k, v in parameters.items():
print(f"\n* Parameter: {k}\n{v}")
# Use this parameters filter available templates. Lets try using mood:
response = audiostack.Production.Sound.Template.list(instruments="synth")
templates = response.data.get("templates")
print(f"{len(templates)} matching templates found!! Try them out!")
for i, tmplt in enumerate(templates):
print(f"{i} - {tmplt.get('alias')}")
# Lets delete our demo template
response = audiostack.Production.Sound.Template.delete(templateName="sound_templates_demo")
You can now run your script and listen to your beautiful music π§
Updated 3 months ago