get_controlchanges_by_index
Retrieve control change events from a specific track in a MIDI file by providing the file path and track index.
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 |
Implementation Reference
- src/main.ts:195-215 (registration)Registration of the 'get_controlchanges_by_index' tool on the MCP server, including its schema definition and handler invocation.
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)Handler function for 'get_controlchanges_by_index' - loads the MIDI file, retrieves the track by index, extracts control changes via track.toJSON().controlChanges, and returns them as JSON.
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:198-201 (schema)Input schema for the tool: 'filePath' (string) and 'trackIndex' (number), both defined using Zod.
{ filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), }, - src/utils/obj-utils.ts:1-6 (helper)Helper function 'getTrackByIndex' used by the handler to retrieve a track from the MIDI file by its index.
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 'loadMidiFile' used by the handler to read and parse the MIDI file from disk.
export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi }