Transform Your Audio into MIDI with the Basic Pitch Cognitive Actions

24 Apr 2025
Transform Your Audio into MIDI with the Basic Pitch Cognitive Actions

In the world of music production, the ability to convert audio files into MIDI format can streamline workflows for musicians and composers alike. The Basic Pitch Cognitive Actions provided by Spotify leverage advanced algorithms to achieve precise audio-to-MIDI transformations, preserving the musical nuances of the original recordings. This article will guide you through integrating this powerful functionality into your applications, enabling you to enhance your music-related projects effortlessly.

Prerequisites

Before you start using the Basic Pitch Cognitive Actions, ensure you have the following:

  • An API key for the Cognitive Actions platform. This key will allow you to authenticate your requests.
  • A basic understanding of making HTTP requests and handling JSON payloads.

Authentication typically involves including the API key in the headers of your requests, ensuring that your application can access the necessary functionalities securely.

Cognitive Actions Overview

Convert Audio to MIDI Using Basic Pitch

The Convert Audio to MIDI action allows you to take an audio file and convert it into MIDI format. This action is particularly beneficial for musicians looking to extract musical notes and details from their recordings, enabling easier editing and manipulation.

  • Category: Music

Input

The input for this action requires a single field:

  • audioFile (required): A URI pointing to the location of the audio file. This should be a valid URI format.

Example Input:

{
  "audioFile": "https://replicate.delivery/pbxt/IXhQ1wH0SCc1SYrM3q8A5lrRmvrOJluvn4uuiUY2VcEQHVxv/86bpm_G_piano_epic_phmMpU.wav"
}

Output

Upon successful execution, the action returns a URI pointing to the generated MIDI file.

Example Output:

https://assets.cognitiveactions.com/invocations/21c3f2f6-7f4c-4f01-9301-62e08ced34b9/c1d3c6b3-dca5-45ca-b667-225fc738b610.mid

Conceptual Usage Example (Python)

The following Python code snippet demonstrates how to call the Cognitive Actions execution endpoint to convert an audio file to MIDI format. Replace the placeholder values with your actual API key and 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 = "83eef59e-e360-4d6d-9215-d3a35eff48e4" # Action ID for Convert Audio to MIDI

# Construct the input payload based on the action's requirements
payload = {
    "audioFile": "https://replicate.delivery/pbxt/IXhQ1wH0SCc1SYrM3q8A5lrRmvrOJluvn4uuiUY2VcEQHVxv/86bpm_G_piano_epic_phmMpU.wav"
}

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 snippet, you'll notice where to input your API key and the action ID for the Convert Audio to MIDI functionality. The example payload includes the necessary audio file URI, and the structure of the request aligns with the requirements of the action.

Conclusion

With the Basic Pitch Cognitive Action, you can effortlessly transform audio files into MIDI format, enhancing your music production capabilities. By integrating this action into your applications, you can automate and streamline your workflow, allowing for more creativity and efficiency in your musical endeavors. Consider exploring additional use cases or combining this action with other Cognitive Actions to maximize its potential in your projects. Happy coding!