add_pitchbends_by_index
Add pitchbend events to a specific track in a MIDI file by providing an array of pitchbend values with their timing.
Instructions
Add pitchbends to midi file by track index
Input 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:279-308 (handler)The tool registration and handler for 'add_pitchbends_by_index'. It registers the tool with schema, loads the MIDI file, gets the track by index, adds pitchbends via track.addPitchBend(), saves the file, and returns success.
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)PitchBendInterfaceSchema: Zod schema defining the pitchbend input shape. Requires a 'value' (number) and either 'time' or 'ticks'.
export const PitchBendInterfaceSchema = z.object({ value: z.number(), }).and(z.union([ z.object({ time: z.number(), }), z.object({ ticks: z.number(), }) ])) - src/main.ts:279-308 (registration)The tool is registered via server.tool() with name 'add_pitchbends_by_index', description, schema, and handler.
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/utils/file-util.ts:5-9 (helper)loadMidiFile: Helper that reads a MIDI file from disk and returns a Midi object.
export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi } - src/utils/obj-utils.ts:1-6 (helper)getTrackByIndex: Helper that retrieves a track from a 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] }