add_pitchbends_by_index
Add pitch bend effects to a specific track in a MIDI file using track index, pitch values, and timing parameters for musical expression.
Instructions
Add pitchbends to 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 | |
| pitchbends | Yes |
Implementation Reference
- src/main.ts:287-307 (handler)The handler function that loads the MIDI file using loadMidiFile, retrieves the track by index using getTrackByIndex, iterates over the provided pitchbends and adds each one to the track using track.addPitchBend(pitchbend), then saves the modified MIDI file.withErrorHandling(({ filePath, trackIndex, pitchbends }) => { // 读取文件 const midi = loadMidiFile(filePath) // 查找轨道 const track = getTrackByIndex(midi, trackIndex) // 添加弯音 pitchbends.forEach(pitchbend => { track.addPitchBend(pitchbend) }) // 保存文件 saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: 'add pitchbend success', }, ] } })
- src/main.ts:279-308 (registration)Registers the 'add_pitchbends_by_index' tool with the MCP server, including description, input schema, and the handler function.server.tool( 'add_pitchbends_by_index', 'Add pitchbends to midi file by track index', { filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), pitchbends: z.array(PitchBendInterfaceSchema) }, withErrorHandling(({ filePath, trackIndex, pitchbends }) => { // 读取文件 const midi = loadMidiFile(filePath) // 查找轨道 const track = getTrackByIndex(midi, trackIndex) // 添加弯音 pitchbends.forEach(pitchbend => { track.addPitchBend(pitchbend) }) // 保存文件 saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: 'add pitchbend success', }, ] } }) )
- src/types/types.ts:53-62 (schema)Zod schema defining the structure of a single pitchbend event: {value: number, time?: number | ticks?: number}.export const PitchBendInterfaceSchema = z.object({ 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 to safely retrieve a MIDI track by index, throwing an error if out of range. Used in the handler.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] }