add_notes_by_index
Insert musical notes into a specific track of a MIDI file using track index, enabling precise composition and arrangement modifications.
Instructions
Add notes 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 | |
| notes | Yes |
Implementation Reference
- src/main.ts:225-245 (handler)The core handler logic for the 'add_notes_by_index' tool: loads the MIDI file, retrieves the specified track, adds the provided notes to the track, and saves the modified file.withErrorHandling(({ filePath, trackIndex, notes }) => { // 读取文件 const midi = loadMidiFile(filePath) // 查找轨道 const track = getTrackByIndex(midi, trackIndex) // 添加音符 notes.forEach(note => { track.addNote(note) }) // 保存文件 saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: 'add note success', }, ] } })
- src/main.ts:217-246 (registration)Registration of the 'add_notes_by_index' tool using server.tool, including description, input schema, and handler.server.tool( 'add_notes_by_index', 'Add notes to midi file by track index', { filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), notes: z.array(NoteConstructorInterfaceSchema) }, withErrorHandling(({ filePath, trackIndex, notes }) => { // 读取文件 const midi = loadMidiFile(filePath) // 查找轨道 const track = getTrackByIndex(midi, trackIndex) // 添加音符 notes.forEach(note => { track.addNote(note) }) // 保存文件 saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: 'add note success', }, ] } }) )
- src/types/types.ts:37-39 (schema)Zod schema for individual note constructor interface, combining pitch, velocity, and time descriptions. Used in the tool's notes array.export const NoteConstructorInterfaceSchema = PitchDescriptionSchema .and(VelocityDescriptionSchema) .and(TimeDescriptionSchema)
- src/utils/obj-utils.ts:1-6 (helper)Helper function 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] }