Generate Uplifting Trance Music with the MusicGen Cognitive Actions

In the world of music generation, the andreasjansson/musicgen-trance Cognitive Actions offer a powerful toolset for developers looking to create music reminiscent of late-90s trance. By leveraging the MusicGen model, these pre-built actions allow for fine-tuning various parameters to achieve the desired sound, making it easier than ever to integrate dynamic music generation into your applications.
Prerequisites
To get started with the MusicGen Cognitive Actions, you will need an API key for authentication. This key should be passed in the request headers as a Bearer token. Ensure you have access to the Cognitive Actions platform and familiarize yourself with the action parameters to make the most out of this integration.
Cognitive Actions Overview
Generate Trance Music with MusicGen
Description: This action enables you to create music in the style of late-90s trance, providing flexibility in parameters such as duration, temperature, and output format. You can also use existing audio as a base for continuation or mimicry.
Category: Music Generation
Input
The input schema for this action includes several parameters that control the music generation process. Here’s a breakdown:
- seed (integer): Seed for the random number generator. Defaults to a random seed if set to None or -1.
- topK (integer): Limits sampling to the k most probable tokens. Default is 250.
- topP (number): Constrains sampling to tokens with a cumulative probability of p, default is 0 (top_k sampling).
- prompt (string): Describes the desired characteristics of the music. Example:
"trance, melodic, uplifting". - duration (integer): Duration of the generated audio in seconds. Default is 8 seconds.
- inputAudio (string): URI pointing to an audio file that influences the generated music.
- temperature (number): Dictates the variation in the sampling process, higher values yield more diverse outputs.
- continuation (boolean): Determines whether to continue the melody of the provided audio. Default is false.
- continuationEnd (integer): Specifies the end time for continuation within the input audio.
- musicGenWeights (string): Specifies which MusicGen model weights to use.
- continuationStart (integer): Start time of the audio file to use for continuation. Default is 0.
- outputAudioFormat (string): Format for the generated audio output, either
"wav"or"mp3". Default is"wav". - multiBandDiffusion (boolean): Enables decoding with MultiBand Diffusion if set to true.
- inputGuidanceStrength (integer): Increases the influence of input parameters on the output.
- audioNormalizationStrategy (string): Method for normalizing the audio, options include 'loudness', 'clip', 'peak', or 'rms'. Default is 'loudness'.
Here’s an example input for generating trance music:
{
"topK": 250,
"topP": 0,
"prompt": "trance, melodic, uplifting",
"duration": 20,
"temperature": 1,
"continuation": false,
"continuationStart": 0,
"outputAudioFormat": "wav",
"multiBandDiffusion": false,
"inputGuidanceStrength": 3,
"audioNormalizationStrategy": "loudness"
}
Output
The action typically returns a URL to the generated audio file. For instance, an example output could be:
https://assets.cognitiveactions.com/invocations/20972077-36e0-424d-9ca9-03a99179a36f/7484cfe8-cfe6-4c7f-a839-3a2c73625a22.wav
This URL points to the generated audio file, which can be played or downloaded.
Conceptual Usage Example (Python)
Below is a conceptual Python code snippet illustrating how you might call this action using a hypothetical Cognitive Actions execution endpoint:
import requests
import json
# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint
action_id = "06458a57-aac5-4cb8-adab-91df28093b8b" # Action ID for Generate Trance Music with MusicGen
# Construct the input payload based on the action's requirements
payload = {
"topK": 250,
"topP": 0,
"prompt": "trance, melodic, uplifting",
"duration": 20,
"temperature": 1,
"continuation": false,
"continuationStart": 0,
"outputAudioFormat": "wav",
"multiBandDiffusion": false,
"inputGuidanceStrength": 3,
"audioNormalizationStrategy": "loudness"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload} # Hypothetical structure
)
response.raise_for_status() # Raise an exception for bad status codes
result = response.json()
print("Action executed successfully:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body: {e.response.text}")
In this snippet:
- Replace
YOUR_COGNITIVE_ACTIONS_API_KEYwith your actual API key. - The
payloadvariable captures the input parameters for generating trance music. - The action ID specifies which action you are calling.
- The response is printed in a readable JSON format.
Conclusion
The Generate Trance Music with MusicGen action provides an exciting opportunity for developers to seamlessly create immersive trance music within their applications. By fine-tuning various parameters, you can tailor the output to fit specific needs, whether for games, apps, or other creative projects. Start integrating these Cognitive Actions today and explore the realms of automated music generation!