get_controlchanges_by_index
Extract control changes from a MIDI file by specifying the track index. Input the file path and track index to retrieve detailed control change data for analysis or manipulation.
Instructions
Get controlchanges from midi file by track index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absoulate File Path to midi file | |
| trackIndex | Yes | Track index number |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"filePath": {
"description": "Absoulate File Path to midi file",
"type": "string"
},
"trackIndex": {
"description": "Track index number",
"type": "number"
}
},
"required": [
"filePath",
"trackIndex"
],
"type": "object"
}
Implementation Reference
- src/main.ts:195-215 (registration)Registers the 'get_controlchanges_by_index' tool. Includes input schema for filePath and trackIndex, and the inline handler that loads the MIDI file using loadMidiFile, gets the track using getTrackByIndex, extracts controlChanges from track.toJSON(), and returns as JSON text.server.tool( 'get_controlchanges_by_index', 'Get controlchanges from midi file by track index', { filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), }, withErrorHandling(({ filePath, trackIndex }) => { const midi = loadMidiFile(filePath) const track = getTrackByIndex(midi, trackIndex) const controlchanges = track.toJSON().controlChanges return { content: [ { type: 'text', text: JSON.stringify(controlchanges), }, ] } }) )
- src/main.ts:202-214 (handler)The core handler function for the tool, wrapped by withErrorHandling. Loads MIDI, retrieves track, gets controlChanges, and formats response.withErrorHandling(({ filePath, trackIndex }) => { const midi = loadMidiFile(filePath) const track = getTrackByIndex(midi, trackIndex) const controlchanges = track.toJSON().controlChanges return { content: [ { type: 'text', text: JSON.stringify(controlchanges), }, ] } })
- src/types/types.ts:41-51 (schema)Zod schema defining the structure of ControlChange objects, which matches the format of the controlchanges returned by the tool's handler.export const ControlChangeInterfaceSchema = z.object({ number: z.number(), value: z.number(), }).and(z.union([ z.object({ time: z.number(), }), z.object({ ticks: z.number(), }) ]))
- src/utils/obj-utils.ts:1-6 (helper)Helper function called in the handler to safely retrieve a track by index from the MIDI object.export function getTrackByIndex(midi: any, trackIndex: number) { if (trackIndex < 0 || trackIndex >= midi.tracks.length) { throw new Error('Track index out of range') } return midi.tracks[trackIndex] }
- src/utils/file-util.ts:5-9 (helper)Helper function called in the handler to load and parse the MIDI file from the given file path.export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi }