How to Add Loudness Presets to AudioStack

Introduction

This tutorial will guide you through using the Audiostack API to create, mix, and deliver production-ready audio assets. We will cover creating scripts, synthesizing speech, mixing audio, and delivering the final product. Ensure you have your API key ready.

Setup

First, you'll need to install the audiostack library. You can do this using pip:

pip install audiostack

Next, ensure you have your Audiostack API key set up as an environment variable. If you haven't already done this, you can set it up in your operating system's environment variables or use the following Python code to set it manually:

import audiostack
import os

# Set your API key
audiostack.api_key = os.environ['AUDIOSTACK_API_KEY']

Creating a Script

Scripts are the foundation of your audio production. You can create scripts and manage your production assets using the Content API.

script = """
<as:section name="main" soundsegment="main">
With AudioStack, you can create compelling, synthetic audio adverts in minutes. Add your copy here to get started.
</as:section>
"""

print("Creating the script...")
script = audiostack.Content.Script.create(
    scriptText=script, scriptName="test", projectName="mastering_test"
)

Synthesizing Speech

You can synthesize speech using top-quality AI voice models or your own cloned voice. In this example, we will use the "Cosmo" voice model.

presets = ["musicenhanced", "balanced"]

for preset in presets:
    print(f"Synthesizing speech for Cosmo with preset `{preset}`")
    speech = audiostack.Speech.TTS.create(scriptItem=script, voice="Cosmo", speed=1)

Mixing Audio

Mixing is a critical step where you dynamically combine content, apply sound designs, and master your audio for a professional finish.

We are also going to apply presets - you saw musicenhanced and balanced - and we'll listen to both.

You can find out more here Lists available mastering presets.

    print(f"Mixing the speech with template `sound_affects` using `{preset}` preset")
    mix = audiostack.Production.Mix.create(
        speechItem=speech,
        soundTemplate="sound_affects",
        masteringPreset=preset,
    )

Downloading the Audio File

Once the mix is complete, you can download the WAV file to your local machine.

    print("Downloading the wav file...")
    file_name = f"cosmo_sound_affects_{preset}"
    mix.download(fileName=file_name)
    current_directory = os.path.dirname(os.path.abspath(__file__))
    print(f"File downloaded to: {current_directory}/{file_name}.wav")

Encoding and Delivery

Finally, you can encode your audio to a variety of formats and host it using the Delivery API. In this example, we'll encode the audio to MP3 and generate a public URL.

    print("Encoding and hosting the audio file...")
    delivery = audiostack.Delivery.Encoder.encode_mix(
        productionItem=mix,
        preset="mp3_high",
        public=True,
    )
    print("MP3 file URL:", delivery.url)

Full Script

Here is the complete script for creating, mixing, and delivering audio using Audiostack:

import audiostack
import os

# Set your API key
audiostack.api_key = os.environ['AUDIOSTACK_API_KEY']

# Create script
script = """
<as:section name="main" soundsegment="main">
With AudioStack, you can create compelling, synthetic audio adverts in minutes. Add your copy here to get started.
</as:section>
"""

print("Creating the script...")
script = audiostack.Content.Script.create(
    scriptText=script, scriptName="test", projectName="mastering_test"
)

# Presets for mastering
presets = ["musicenhanced", "balanced"]

for preset in presets:
    # Synthesize speech
    print(f"Synthesizing speech for Cosmo with preset `{preset}`")
    speech = audiostack.Speech.TTS.create(scriptItem=script, voice="Cosmo", speed=1)

    # Mix audio
    print(f"Mixing the speech with template `sound_affects` using `{preset}` preset")
    mix = audiostack.Production.Mix.create(
        speechItem=speech,
        soundTemplate="sound_affects",
        masteringPreset=preset,
    )

    # Download the WAV file
    print("Downloading the wav file...")
    file_name = f"cosmo_sound_affects_{preset}"
    mix.download(fileName=file_name)
    current_directory = os.path.dirname(os.path.abspath(__file__))
    print(f"File downloaded to: {current_directory}/{file_name}.wav")

    # Encode and host the audio file
    print("Encoding and hosting the audio file...")
    delivery = audiostack.Delivery.Encoder.encode_mix(
        productionItem=mix,
        preset="mp3_high",
        public=True,
    )
    print("MP3 file URL:", delivery.url)

This script demonstrates how to use the Audiostack API to create professional audio content efficiently. Adjust the parameters and presets as needed for your specific use case.