get_notes_by_index
Extract notes from a MIDI file by specifying the track index, enabling focused analysis and manipulation of specific track data within the file.
Instructions
Get notes 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:151-171 (registration)Registration of the 'get_notes_by_index' tool, including input schema (filePath: string, trackIndex: number) and the complete inline handler function that loads the MIDI file, retrieves the specified track, extracts and serializes its notes to JSON.server.tool( 'get_notes_by_index', 'Get notes 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 notes = track.notes.map((note: any) => note.toJSON()) return { content: [ { type: 'text', text: JSON.stringify(notes), }, ] } }) )
- src/main.ts:158-170 (handler)The core handler logic wrapped in error handling: loads MIDI using loadMidiFile, gets track using getTrackByIndex, maps notes to JSON, and returns as text content.withErrorHandling(({ filePath, trackIndex }) => { const midi = loadMidiFile(filePath) const track = getTrackByIndex(midi, trackIndex) const notes = track.notes.map((note: any) => note.toJSON()) return { content: [ { type: 'text', text: JSON.stringify(notes), }, ] } })
- src/main.ts:154-157 (schema)Input schema defined with Zod for parameters: filePath (absolute path to MIDI file) and trackIndex (numeric index of the track).{ 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 used in the handler to safely retrieve a track by index from the MIDI object, 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 used in the handler to load a MIDI file from disk using @tonejs/midi and fs.export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi }