add_notes_by_index
Insert notes into a MIDI file at a specific track index. Define notes by name, pitch, or MIDI number, and specify timing in seconds or ticks. Modify MIDI compositions with precision.
Instructions
Add notes to midi file by track index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absoulate File Path to midi file | |
| notes | Yes | ||
| trackIndex | Yes | Track index number |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"filePath": {
"description": "Absoulate File Path to midi file",
"type": "string"
},
"notes": {
"items": {
"allOf": [
{
"anyOf": [
{
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"type": {
"const": "name",
"type": "string"
}
},
"required": [
"type",
"name"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"octave": {
"type": "number"
},
"pitch": {
"type": "string"
},
"type": {
"const": "pitch",
"type": "string"
}
},
"required": [
"type",
"pitch",
"octave"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"midi": {
"type": "number"
},
"type": {
"const": "midi",
"type": "string"
}
},
"required": [
"type",
"midi"
],
"type": "object"
}
]
},
{
"properties": {
"noteOffVelocity": {
"type": "number"
},
"velocity": {
"type": "number"
}
},
"type": "object"
},
{
"anyOf": [
{
"additionalProperties": false,
"properties": {
"duration": {
"type": "number"
},
"time": {
"type": "number"
},
"timeType": {
"const": "seconds",
"type": "string"
}
},
"required": [
"timeType",
"time"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"durationTicks": {
"type": "number"
},
"ticks": {
"type": "number"
},
"timeType": {
"const": "ticks",
"type": "string"
}
},
"required": [
"timeType",
"ticks"
],
"type": "object"
}
]
}
]
},
"type": "array"
},
"trackIndex": {
"description": "Track index number",
"type": "number"
}
},
"required": [
"filePath",
"trackIndex",
"notes"
],
"type": "object"
}
Implementation Reference
- src/main.ts:225-244 (handler)Handler function that loads the MIDI file, retrieves the specified track, adds the provided notes to it, 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:220-223 (schema)Input schema definition for the tool parameters using Zod.{ filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), notes: z.array(NoteConstructorInterfaceSchema)
- src/types/types.ts:37-39 (schema)Zod schema for individual note objects used in the 'notes' array parameter.export const NoteConstructorInterfaceSchema = PitchDescriptionSchema .and(VelocityDescriptionSchema) .and(TimeDescriptionSchema)
- src/main.ts:217-246 (registration)MCP server tool registration call for 'add_notes_by_index'.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/utils/obj-utils.ts:1-6 (helper)Utility 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] }