get_pitchbends_by_index
Retrieves pitchbend events from a specified track in a MIDI file by providing the file path and track index.
Instructions
Get pitchbends 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:173-193 (registration)Tool registered with MCP server under name 'get_pitchbends_by_index'. Schema: filePath (string) and trackIndex (number). Handler loads MIDI file, gets track by index, extracts pitchBends from track.toJSON(), and returns them as JSON.
server.tool( 'get_pitchbends_by_index', 'Get pitchbends 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 pitchbends = track.toJSON().pitchBends return { content: [ { type: 'text', text: JSON.stringify(pitchbends), }, ] } }) ) - src/main.ts:180-193 (handler)Handler logic: loads midi file via loadMidiFile, gets track via getTrackByIndex, extracts pitchbends from track.toJSON().pitchBends, returns JSON stringified result.
withErrorHandling(({ filePath, trackIndex }) => { const midi = loadMidiFile(filePath) const track = getTrackByIndex(midi, trackIndex) const pitchbends = track.toJSON().pitchBends return { content: [ { type: 'text', text: JSON.stringify(pitchbends), }, ] } }) ) - src/utils/obj-utils.ts:1-5 (helper)Helper function getTrackByIndex used by the handler to retrieve a track from the MIDI by 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.
export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi } - src/types/types.ts:53-62 (schema)PitchBendInterfaceSchema defines the Zod validation schema for pitchbend objects used in add_pitchbends_by_index tool (not directly used by get_pitchbends_by_index but related type).
export const PitchBendInterfaceSchema = z.object({ value: z.number(), }).and(z.union([ z.object({ time: z.number(), }), z.object({ ticks: z.number(), }) ]))