add_controlchanges_by_index
Add control change messages to a specific track in a MIDI file using track index, enabling precise automation and parameter adjustments for MIDI editing.
Instructions
Add controlchanges 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 | |
| controlchanges | Yes |
Implementation Reference
- src/main.ts:256-276 (handler)Handler function that loads the MIDI file, retrieves the specified track, adds each control change using track.addCC(), saves the modified MIDI file, and returns a success message.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 defining the structure of a control change object: {number, value} unioned with either {time} or {ticks}, used for validating tool input parameters.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/main.ts:248-277 (registration)Registers the MCP tool 'add_controlchanges_by_index' with the server, including input schema validation using Zod and the error-handling wrapper around the 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/utils/obj-utils.ts:1-6 (helper)Utility function to safely retrieve a MIDI track by index, throwing an error if the index is out of bounds; 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] }