Add Background Music
curl --request POST \
--url https://api.example.com/tts/add-music \
--header 'Content-Type: application/json' \
--data '
{
"music_volume": 123,
"fade_duration": 123,
"api_key": "<string>"
}
'import requests
url = "https://api.example.com/tts/add-music"
payload = {
"music_volume": 123,
"fade_duration": 123,
"api_key": "<string>"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({music_volume: 123, fade_duration: 123, api_key: '<string>'})
};
fetch('https://api.example.com/tts/add-music', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/tts/add-music",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'music_volume' => 123,
'fade_duration' => 123,
'api_key' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/tts/add-music"
payload := strings.NewReader("{\n \"music_volume\": 123,\n \"fade_duration\": 123,\n \"api_key\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/tts/add-music")
.header("Content-Type", "application/json")
.body("{\n \"music_volume\": 123,\n \"fade_duration\": 123,\n \"api_key\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/tts/add-music")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"music_volume\": 123,\n \"fade_duration\": 123,\n \"api_key\": \"<string>\"\n}"
response = http.request(request)
puts response.read_bodyText-to-Speech
Add Background Music
Add background music to an existing audio file
POST
/
tts
/
add-music
Add Background Music
curl --request POST \
--url https://api.example.com/tts/add-music \
--header 'Content-Type: application/json' \
--data '
{
"music_volume": 123,
"fade_duration": 123,
"api_key": "<string>"
}
'import requests
url = "https://api.example.com/tts/add-music"
payload = {
"music_volume": 123,
"fade_duration": 123,
"api_key": "<string>"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({music_volume: 123, fade_duration: 123, api_key: '<string>'})
};
fetch('https://api.example.com/tts/add-music', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/tts/add-music",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'music_volume' => 123,
'fade_duration' => 123,
'api_key' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/tts/add-music"
payload := strings.NewReader("{\n \"music_volume\": 123,\n \"fade_duration\": 123,\n \"api_key\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/tts/add-music")
.header("Content-Type", "application/json")
.body("{\n \"music_volume\": 123,\n \"fade_duration\": 123,\n \"api_key\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/tts/add-music")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"music_volume\": 123,\n \"fade_duration\": 123,\n \"api_key\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body
Add background music to an existing audio file. The music will be looped to match the audio length and faded in/out for smooth transitions.
Request
file
required
Audio file to add music to (MP3, WAV, etc.)
file
required
Background music file (MP3, WAV, etc.)
number
default:"-20"
Music volume in decibels (dB). Range: -40 to 0
- Lower values = quieter music
- Higher values = louder music
- Default: -20 dB (background level)
integer
default:"2000"
Fade in/out duration in milliseconds. Default: 2000ms (2 seconds)
string
required
Your GistMag API key
Response
Returns the combined audio file (voice + music) as MP3.Credit Cost
1 credit per request (in addition to any credits used to generate the original audio).Example Request
curl -X POST https://api.gistmag.co.uk/tts/add-music \
-F "audio=@speech.mp3" \
-F "music=@background.mp3" \
-F "music_volume=-20" \
-F "fade_duration=2000" \
-F "api_key=your_api_key_here" \
--output speech_with_music.mp3
const formData = new FormData();
formData.append('audio', audioFile);
formData.append('music', musicFile);
formData.append('music_volume', '-20');
formData.append('fade_duration', '2000');
formData.append('api_key', 'your_api_key_here');
const response = await fetch('https://api.gistmag.co.uk/tts/add-music', {
method: 'POST',
body: formData
});
const audioBlob = await response.blob();
// Use the combined audio
import requests
with open('speech.mp3', 'rb') as audio_file, \
open('background.mp3', 'rb') as music_file:
files = {
'audio': audio_file,
'music': music_file
}
data = {
'music_volume': -20,
'fade_duration': 2000,
'api_key': 'your_api_key_here'
}
response = requests.post(
'https://api.gistmag.co.uk/tts/add-music',
files=files,
data=data
)
with open('speech_with_music.mp3', 'wb') as f:
f.write(response.content)
How It Works
- The music file is adjusted to the specified volume
- If the music is shorter than the audio, it’s looped to match the length
- Music is trimmed to exactly match the audio duration
- Fade in/out effects are applied for smooth transitions
- The voice and music are combined into a single audio file
Tips
- Use instrumental music to avoid conflicts with voice
- Adjust
music_volumeto ensure the voice remains clear - Longer
fade_durationcreates smoother transitions - The music will automatically loop if shorter than the audio