add_pitchbends_by_index
Add pitchbends to a MIDI file by specifying a track index and defining pitchbend values with precise timing or ticks for enhanced 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 | |
| pitchbends | Yes | ||
| trackIndex | Yes | Track index number |
Implementation Reference
- src/main.ts:287-307 (handler)Core handler function that loads the MIDI file using loadMidiFile, retrieves the track using getTrackByIndex, iterates over pitchbends and calls track.addPitchBend for each, then saves the file with saveMidiFile.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:282-286 (schema)Zod input schema for the 'add_pitchbends_by_index' tool parameters.{ filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), pitchbends: z.array(PitchBendInterfaceSchema) },
- src/types/types.ts:53-62 (schema)Zod schema defining the structure of a single pitchbend object: value 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)Registration of the 'add_pitchbends_by_index' tool using server.tool, including name, description, input 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/obj-utils.ts:1-6 (helper)Helper function to safely retrieve a track by index from the MIDI object, 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] }