add_controlchanges_by_index
Insert control change messages into a MIDI file by specifying the track index, enabling users to modify specific tracks with precision for enhanced MIDI file editing.
Instructions
Add controlchanges to midi file by track index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| controlchanges | Yes | ||
| filePath | Yes | Absoulate File Path to midi file | |
| trackIndex | Yes | Track index number |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"controlchanges": {
"items": {
"allOf": [
{
"properties": {
"number": {
"type": "number"
},
"value": {
"type": "number"
}
},
"required": [
"number",
"value"
],
"type": "object"
},
{
"anyOf": [
{
"additionalProperties": false,
"properties": {
"time": {
"type": "number"
}
},
"required": [
"time"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"ticks": {
"type": "number"
}
},
"required": [
"ticks"
],
"type": "object"
}
]
}
]
},
"type": "array"
},
"filePath": {
"description": "Absoulate File Path to midi file",
"type": "string"
},
"trackIndex": {
"description": "Track index number",
"type": "number"
}
},
"required": [
"filePath",
"trackIndex",
"controlchanges"
],
"type": "object"
}
Implementation Reference
- src/main.ts:256-276 (handler)Inline handler function for the 'add_controlchanges_by_index' tool. It loads the MIDI file, retrieves the specified track, adds each control change event to the track using track.addCC(), and saves the modified file.withErrorHandling(({ filePath, trackIndex, controlchanges }) => { // 读取文件 const midi = loadMidiFile(filePath) // 查找轨道 const track = getTrackByIndex(midi, trackIndex) // 添加控制器变化 controlchanges.forEach(controlchange => { track.addCC(controlchange) }) // 保存文件 saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: 'add controlchange success', }, ] } })
- src/main.ts:248-277 (registration)Registration of the tool 'add_controlchanges_by_index' using server.tool(), specifying name, description, input schema, and handler.server.tool( 'add_controlchanges_by_index', 'Add controlchanges to midi file by track index', { filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), controlchanges: z.array(ControlChangeInterfaceSchema) }, withErrorHandling(({ filePath, trackIndex, controlchanges }) => { // 读取文件 const midi = loadMidiFile(filePath) // 查找轨道 const track = getTrackByIndex(midi, trackIndex) // 添加控制器变化 controlchanges.forEach(controlchange => { track.addCC(controlchange) }) // 保存文件 saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: 'add controlchange success', }, ] } }) )
- src/types/types.ts:41-51 (schema)Zod schema definition for individual ControlChange objects, used in the tool's input schema as z.array(this). Defines controller number, value, and time/ticks.export const ControlChangeInterfaceSchema = z.object({ number: z.number(), 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 retrieve a track by index from the MIDI object, with bounds checking. Used in the tool 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] }
- src/main.ts:23-39 (helper)Generic error-handling wrapper applied to all tool handlers, catching exceptions and returning formatted error response.const withErrorHandling = <T extends Record<string, any>>(handler: (args: T) => any) => { return async (args: T) => { try { return await handler(args) } catch (e) { return { isError: true, content: [ { type: 'text', text: `Error: ${e}`, }, ] } } } }