Create Unique Musical Pieces with MusicGen Bacharach Cognitive Actions

23 Apr 2025
Create Unique Musical Pieces with MusicGen Bacharach Cognitive Actions

In the world of music generation, the andreasjansson/musicgen-bacharach-novox Cognitive Actions offer a powerful way to create original compositions based on textual prompts or existing audio. By utilizing the MusicGen Bacharach model, developers can seamlessly integrate music generation capabilities into their applications, enhancing user experiences and creating unique soundscapes. This article will guide you through the available actions, their functionalities, and how to implement them in your projects.

Prerequisites

Before diving into the integration of Cognitive Actions, ensure you have the following:

  • An API key for accessing the Cognitive Actions platform.
  • Basic understanding of JSON and how to make HTTP requests.

Authentication typically involves passing your API key in the request headers, allowing you to securely access the available actions.

Cognitive Actions Overview

Generate Music with MusicGen Bacharach

The Generate Music with MusicGen Bacharach action creates a musical piece based on your textual input or an existing audio file. This action allows for extensive customization, including setting the duration, output format, and more.

Input

The input for this action is structured as follows:

{
  "seed": 12345,
  "topK": 250,
  "topP": 0,
  "prompt": "hip hop in the style of burt bacharach",
  "duration": 8,
  "inputAudio": "http://example.com/audiofile.wav",
  "temperature": 1,
  "continuation": false,
  "continuationEnd": -1,
  "musicGenWeights": "",
  "audioOutputFormat": "wav",
  "continuationStart": 0,
  "multiBandDiffusion": false,
  "classifierFreeGuidance": 3,
  "audioNormalizationStrategy": "loudness"
}

Key properties include:

  • seed: An integer for randomization (optional).
  • topK: Limits sampling to the top K probable tokens (default is 250).
  • prompt: A string describing the music style or theme.
  • duration: Duration of the generated audio in seconds (default is 8).
  • inputAudio: A URI linking to an audio file for influence.
  • continuation: A boolean indicating if the music continues from the input audio.
  • audioOutputFormat: Specifies the format of the output audio ('wav' or 'mp3').

Output

The output of this action is a URI pointing to the generated audio file. For example:

https://assets.cognitiveactions.com/invocations/d8432520-0b73-4589-8cf3-9d550c600f45/66255860-b05c-4dbb-9588-d7a2bfca5de0.wav

This URI can be used to access the newly created musical piece.

Conceptual Usage Example (Python)

Here's a conceptual example of how you might call the Generate Music with MusicGen Bacharach action using Python:

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 = "3eba4135-5844-4d93-8344-71900e9d282c" # Action ID for Generate Music with MusicGen Bacharach

# Construct the input payload based on the action's requirements
payload = {
    "topK": 250,
    "topP": 0,
    "prompt": "hip hop in the style of burt bacharach",
    "duration": 8,
    "temperature": 1,
    "continuation": False,
    "audioOutputFormat": "wav",
    "continuationStart": 0,
    "multiBandDiffusion": False,
    "classifierFreeGuidance": 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 (4xx or 5xx)

    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 code snippet, you replace "YOUR_COGNITIVE_ACTIONS_API_KEY" with your actual API key. The payload variable is structured according to the input schema, and the request is sent to a hypothetical endpoint.

Conclusion

The MusicGen Bacharach Cognitive Action empowers developers to generate unique musical compositions with ease. By leveraging the flexibility of input options, you can create tailored musical pieces that fit a variety of applications, from entertainment to interactive experiences. Explore how you can incorporate these capabilities into your projects, and start creating music that resonates with your audience!