Finding Loop Points in Songs
Published: 2025-10-24
This example demonstrates how to use the Audjust Analyze API to find potential loops in a song. Loops are sections of a track that can be seamlessly repeated, which is useful for creating extended versions of songs or for use in applications like video games.
Prerequisites
- An Audjust API key. You can get one from the API console. It’s free to sign up and get started! If you need help generating an API key, check out this guide.
- Ability to run Python code.
- An audio file to analyze (e.g., MP3, WAV).
For this example, we’ll use the requests library to make HTTP requests and pydub to handle audio file manipulation. You can install these libraries using pip:
pip install requests pydub Example Code
Below is the example code that uploads an audio file, analyzes it for loop points, and prints out the best loops found. It also saves the best loop segment between 5 and 10 seconds as a separate audio file. Feel free to modify the code to suit your needs:
import requests
from pydub import AudioSegment
API_KEY = "YOUR_API_KEY_HERE" # Replace with your Audjust API key
LOCAL_MP3_PATH = "your_song.mp3" # Replace with your local mp3 file path
def upload_file(local_path):
# Step 1: Get upload URLs
resp = requests.get(
"https://api.audjust.com/upload", headers={"X-API-Key": API_KEY}
)
resp.raise_for_status()
data = resp.json()
storage_url = data["storageUrl"]
retrieval_url = data["retrievalUrl"]
# Step 2: Upload file to storageUrl
with open(local_path, "rb") as f:
put_resp = requests.put(storage_url, data=f)
put_resp.raise_for_status()
return retrieval_url
def get_structure_info(source_url):
resp = requests.post(
"https://api.audjust.com/structure",
headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
json={"sourceFileUrl": source_url},
)
resp.raise_for_status()
return resp.json()
def export_segment(start_ms, end_ms, out_path):
audio = AudioSegment.from_file(LOCAL_MP3_PATH)
segment = audio[start_ms:end_ms]
segment.export(out_path, format="mp3")
def main():
# Upload local mp3
print("Uploading file...")
source_url = upload_file(LOCAL_MP3_PATH)
print("Uploaded. Analyzing structure...")
info = get_structure_info(source_url)
# Find best loop between 5 and 10 seconds
best_loop = None
for loop in info.get("loops", []):
print(loop)
duration = (loop["endMs"] - loop["startMs"]) / 1000
if 5 <= duration <= 10:
if not best_loop or loop["score"] > best_loop["score"]:
best_loop = loop
if best_loop:
print(f"Best loop: {best_loop}")
export_segment(best_loop["startMs"], best_loop["endMs"], "best_loop.mp3")
print("Saved best_loop.mp3")
if __name__ == "__main__":
main() When you run this script, you should get something like below:
$ python getloops.py
Uploading file...
Uploaded. Analyzing structure...
{'score': 0.915, 'startMs': 72968, 'endMs': 148387}
{'score': 0.891, 'startMs': 56157, 'endMs': 145298}
{'score': 0.889, 'startMs': 11180, 'endMs': 160206}
{'score': 0.873, 'startMs': 69450, 'endMs': 131157}
{'score': 0.868, 'startMs': 157291, 'endMs': 164141}
{'score': 0.859, 'startMs': 133735, 'endMs': 140585}
{'score': 0.846, 'startMs': 130832, 'endMs': 151301}
{'score': 0.838, 'startMs': 59582, 'endMs': 66443}
{'score': 0.819, 'startMs': 7743, 'endMs': 14593}
Best loop: {'score': 0.868, 'startMs': 157291, 'endMs': 164141}
Saved best_loop.mp3 If you didn’t get any loops printed out, try changing the minimum and maximum length or using a different song that has more distinct sections. If everything worked, you should see a new file called best_loop.mp3 in your working directory containing the best loop segment found in the song. Give it a listen!
Troubleshooting
If you encounter any issues, ensure that:
- Your API key is correct and has sufficient credits.
- The audio file exists and the file path is correct.
Next Steps
You can further explore the Audjust API to analyze other aspects of your audio files, such as finding beat drops or sections. Check out the API documentation for more details on available endpoints and features.