Creating a Basic Audio Program with AudioStack and Saving as MP3 File Type
In this tutorial, we will create a basic audio program using the AudioStack API. We will take a simple text, convert it to speech, and create an audio mix. Finally, we'll encode the mix into the MP3 format and download it. This showcases the power of the API.
Prerequisites
- Python installed on your system.
- An API key from AudioStack. http://platform.audiostack.ai
- The
audiostack
library installed. You can install it using pip:pip install audiostack
Steps to Create the Audio Program
-
Import Required Libraries: Start by importing the necessary libraries.
import audiostack import os
-
Set Up Your API Key: Assign your AudioStack API key to the
api_key
variable.audiostack.api_key = "YOUR_API_KEY"
Replace
"YOUR_API_KEY"
with your actual API key. -
Define the Text: Create a string variable with the text you want to convert to speech.
text = """Hello world"""
-
Create the Script: Use the AudioStack API to create a script from your text.
print(f"Creating your script...") script = audiostack.Content.Script.create(scriptText=text) print(script)
-
Generate Speech: Convert the script to speech.
print(f"Generating speech...") speech = audiostack.Speech.TTS.create(scriptItem=script, voice="wren") print(speech)
-
Create the Mix: Combine the speech with a loudness preset to create an audio mix.
print(f"Creating your mix...") mix = audiostack.Production.Mix.create( speechItem=speech, loudnessPreset="radio", preset="mp3" ) print(mix)
-
Download the file.
encoder.download(file=".")
This will download the encoded audio file to your current directory.
Full Code
Here is the complete code to create a basic audio program:
import audiostack
import os
# Set up your API key
audiostack.api_key = "YOUR_API_KEY"
# Define the text to convert to speech
text = """Hello world"""
# Create the script from the text
print(f"Creating your script...")
script = audiostack.Content.Script.create(scriptText=text)
print(script)
# Generate speech from the script
print(f"Generating speech...")
speech = audiostack.Speech.TTS.create(scriptItem=script, voice="wren")
print(speech)
# Create an audio mix from the speech
print(f"Creating your mix...")
mix = audiostack.Production.Mix.create(
speechItem=speech,
loudnessPreset="radio",
preset="mp3"
)
print(mix)
# Encode the mix into the desired format and download it
encoder = audiostack.Delivery.Encoder.encode_mix(productionItem=mix, preset="mp3")
encoder.download(file=".")
Explanation
- Creating the Script: We first create a script using the text provided. This script is then used as input for the text-to-speech (TTS) process.
- Generating Speech: The script is converted to speech using the TTS API.
- Creating the Mix: The speech is combined with a mastering preset to create a polished audio mix.
- Encoding the Mix: The final mix is encoded into a specific format (MP3 in this case) and downloaded.
This tutorial demonstrates the basic workflow for creating an audio program with AudioStack, from text to a downloadable audio file.
π MP3 File Type
The MP3 file type is a popular audio format developed by the Moving Picture Experts Group (MPEG). Here are some key points about MP3 files:
- Purpose: Widely used for storing and streaming compressed audio.
- Quality: Offers a good balance between file size and sound quality. Bitrate settings allow for varying levels of compression.
- Compression: Utilizes lossy compression, which reduces file size by removing some audio data. The degree of compression can be adjusted.
- Compatibility: Supported by virtually all audio playback devices and software, making it one of the most universally accepted formats.
- File Size: Significantly smaller than uncompressed formats like WAV, making it ideal for storage and streaming over the internet.
MP3 files are known for their efficient compression and broad compatibility, making them a popular choice for music distribution and personal audio libraries.
Updated 5 months ago