get_controlchanges_by_index
Extract control change data from a specific track in a MIDI file by providing the file path and track index number.
Instructions
Get controlchanges from midi file by track index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absoulate File Path to midi file | |
| trackIndex | Yes | Track index number |
Implementation Reference
- src/main.ts:202-214 (handler)Handler function that loads MIDI file using loadMidiFile, retrieves track using getTrackByIndex, extracts control changes with track.toJSON().controlChanges, and returns JSON-formatted text 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/main.ts:195-215 (registration)Registration of the get_controlchanges_by_index tool via server.tool, including name, description, input schema, and error-wrapped handler.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:198-201 (schema)Zod input schema validating filePath (string) and trackIndex (number).{ 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 to retrieve a specific track from the MIDI object by index with bounds checking.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 to load a MIDI file from disk using @tonejs/midi and fs.readFileSync.export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi }