Skip to main content
Glama

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
NameRequiredDescriptionDefault
filePathYesAbsoulate File Path to midi file
trackIndexYesTrack index number
notesYes

Implementation Reference

  • 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', }, ] } }) )
  • 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)
  • 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] }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/xiaolaa2/midi-file-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server