set_tempo
Set the tempo of a MIDI file by specifying the file path and BPM value.
Instructions
Set tempo for midi file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absoulate File Path to midi file | |
| bpm | Yes | BPM |
Implementation Reference
- src/main.ts:67-87 (registration)Registration and handler for the 'set_tempo' tool. Uses server.tool() to register the tool name, description, schema (filePath, bpm), and handler logic.
server.tool( 'set_tempo', 'Set tempo for midi file', { filePath: z.string().describe('Absoulate File Path to midi file'), bpm: z.number().describe('BPM'), }, withErrorHandling(({ filePath, bpm }) => { const midi = loadMidiFile(filePath) midi.header.setTempo(bpm) saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: `Set tempo to ${bpm}`, }, ] } }) ) - src/main.ts:74-86 (handler)Handler function for set_tempo: loads a MIDI file, sets the tempo via midi.header.setTempo(bpm), saves the file, and returns a success message.
withErrorHandling(({ filePath, bpm }) => { const midi = loadMidiFile(filePath) midi.header.setTempo(bpm) saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: `Set tempo to ${bpm}`, }, ] } }) - src/main.ts:70-73 (schema)Zod schema for set_tempo parameters: filePath (string) and bpm (number).
{ filePath: z.string().describe('Absoulate File Path to midi file'), bpm: z.number().describe('BPM'), }, - src/utils/file-util.ts:5-9 (helper)Helper function 'loadMidiFile' used by the handler to read a MIDI file from disk.
export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi } - src/utils/file-util.ts:11-15 (helper)Helper function 'saveMidiFile' used by the handler to write the modified MIDI file back to disk.
export function saveMidiFile(midi: any, filePath: string): void { const arrayBuffer = midi.toArray() // 将ArrayBuffer转换为Buffer并写入文件 fs.writeFileSync(filePath, Buffer.from(arrayBuffer)) }