Skip to main content

Separate audio sources

Learn how to create a source separation with the Klangio API.

1. Model Selection

Currently there are two different models to choose for the source separation. One to separate four stems (vocals, bass, drums, other) and one to separate six stems (vocals, bass, drums, piano, guitar, other).

One might be tempted to use only the six-stems model, to have a broader range of instruments as output. Depending on your use case, there are simple trade-offs to consider. The four-stem separation model is more accurate in the separation and generally processes the audio faster than the six-stems model. Make sure to check the models that best fit your needs.

2. Request via API

Example python snippet: (also see step 1 in Basic Job Workflow)

  import requests

API_KEY = 'YOUR_KLANGIO_API_KEY'

audio_file = open('{YOUR_FILE_NAME}', 'rb')

query_parameters = {
'model': 'six-stems', # optional, `four-stems` by default
'output': 'wav', # optional, `wav` by default
'webhook_url': '{YOUR_WEBHOOK_ENDPOINT_URL}' # optional, but recommended
}

multipart_params = {
'file': audio_file,
}

resp = requests.post(
'https://api.klang.io/source-separation',
headers={
'kl-api-key': API_KEY
},
params=query_parameters,
files=multipart_params
)

if resp.status_code == 200:
print(resp.json())
# {
# 'job_id': 'SOME_JOB_ID',
# 'creation_date': '2023-01-01',
# 'deletion_date': '2023-02-01',
# 'status_endpoint_url': 'https://api.klang.io/job/{SOME_JOB_ID}/status'
# }

3. Check Job Status

See step 2 in Basic Job Workflow

4. Get the Output Data

Once the source separation job is in state COMPLETED or the webhook notification is received, you can fetch the output results from the job/{job_id}/audio?stem_type={stem_type} endpoint.

Here is an example python code snippet to fetch the vocal part of the source separation: (also see step 3 in Basic Job Workflow)

  import requests

API_KEY = 'YOUR_KLANGIO_API_KEY'

resp = requests.get(
'https://api.klang.io/job/{YOUR_JOB_ID}/audio?stem_type=vocals', # stem_type is required
headers={
'kl-api-key': API_KEY
})

if resp.status_code == 200:
with open('vocals.mp3', 'wb') as file:
file.write(resp.content)