MidiJamSession / README.md
adricl's picture
Setup for processing via transformers
ef96802
metadata
tags:
  - gradio-custom-component
  - File
  - MIDI
  - LLM
title: gradio_midijamsession
short_description: Human AI Jam Sessions with Music LLMs
colorFrom: blue
colorTo: yellow
sdk: docker
pinned: false
app_file: space.py

gradio_midijamsession

Static Badge

Human AI Jam Sessions with Music LLMs

Installation

pip install gradio_midijamsession

Usage

"""
Demo app for MidiJamSession component.
Transposes all incoming MIDI notes by +2 semitones.
"""

import gradio as gr
from gradio_midijamsession import MidiJamSession
import io
from processor import transpose_midi

# Create the Gradio interface
with gr.Blocks(title="MIDI Jam Session Demo") as demo:
    gr.Markdown("""
    # 🎹 MIDI Jam Session Demo
    
    This demo shows the MidiJamSession component in action:
    
    1. **Select your MIDI input device** - Connect your MIDI keyboard or controller
    2. **Select your MIDI output device** - Choose where to send the processed MIDI
    3. **Play some notes** - The component will record your input
    4. **Wait 5 seconds** - After inactivity, the recording is sent for processing
    5. **Listen to the result** - Notes are transposed up by 2 semitones and played back
    
    ## How it works
    
    - The frontend uses the **Web MIDI API** to capture live MIDI input
    - After 5 seconds of inactivity, the recording is converted to a MIDI file using **@tonejs/midi**
    - The backend receives the MIDI file and uses **mido** to transpose all notes by +2 semitones
    - The processed MIDI is sent back and played on your selected output device
    """)
    
    midi_session = MidiJamSession(
        label="MIDI Jam Session",
        bpm=120,
        interactive=True,
    )
    
    # Connect the processing function
    midi_session.change(
        fn=transpose_midi,
        inputs=midi_session,
        outputs=midi_session,
    )


if __name__ == "__main__":
    demo.launch()

MidiJamSession

Initialization

name type default description
value
str | bytes | Callable | None
value = None Default MIDI file to display, given as a str file path, URL, or bytes.
bpm
int
value = 120 The tempo to use when creating MIDI files from recorded messages. Defaults to 120.
label
str | I18nData | None
value = None The label for this component.
every
Timer | float | None
value = None Continuously calls `value` to recalculate it if `value` is a function.
inputs
Component | Sequence[Component] | set[Component] | None
value = None Components that are used as inputs to calculate `value` if `value` is a function.
show_label
bool | None
value = None If True, will display label.
container
bool
value = True If True, will place the component in a container.
scale
int | None
value = None Relative size compared to adjacent Components.
min_width
int
value = 160 Minimum pixel width.
height
int | str | float | None
value = None The height of the component.
interactive
bool | None
value = None If True, will allow users to interact with MIDI devices.
visible
bool | Literal['hidden']
value = True If False, component will be hidden.
elem_id
str | None
value = None An optional string that is assigned as the id of this component in the HTML DOM.
elem_classes
list[str] | str | None
value = None An optional list of strings that are assigned as the classes of this component.
render
bool
value = True If False, component will not render in the Blocks context.
key
int | str | tuple[int | str, ...] | None
value = None In a gr.render, Components with the same key across re-renders are treated as the same component.
preserved_by_key
list[str] | str | None
value = "value" A list of parameters from this component's constructor to preserve across re-renders.

Events

name description
change Triggered when the value of the MidiJamSession changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See .input() for a listener that is only triggered by user input.
upload This listener is triggered when the user uploads a file into the MidiJamSession.
clear This listener is triggered when the user clears the MidiJamSession using the clear button for the component.

User function

The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).

  • When used as an Input, the component only impacts the input signature of the user function.
  • When used as an output, the component only impacts the return signature of the user function.

The code snippet below is accurate in cases where the component is used as both an input and an output.

  • As output: Is passed, the MIDI file contents as bytes.
  • As input: Should return, mIDI data as a file path (str/Path) or raw bytes.
def predict(
    value: bytes| None
) -> str| bytes| pathlib.Path| None:
    return value