generate_euclidean
Create Euclidean rhythms for music patterns by distributing hits across steps. Specify hits, steps, and optional sound to generate rhythmic sequences for Strudel.cc live coding.
Instructions
Generate Euclidean rhythm
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hits | Yes | Number of hits | |
| steps | Yes | Total steps | |
| sound | No | Sound to use |
Implementation Reference
- MCP tool handler for 'generate_euclidean' that validates inputs, calls PatternGenerator.generateEuclideanPattern, appends to current pattern, writes it, and returns success message.case 'generate_euclidean': InputValidator.validateEuclidean(args.hits, args.steps); if (args.sound) { InputValidator.validateStringLength(args.sound, 'sound', 100, false); } const euclidean = this.generator.generateEuclideanPattern( args.hits, args.steps, args.sound || 'bd' ); const currentEuc = await this.getCurrentPatternSafe(); const newEucPattern = currentEuc ? currentEuc + '\n' + euclidean : euclidean; await this.writePatternSafe(newEucPattern); return `Generated Euclidean rhythm (${args.hits}/${args.steps})`;
- src/server/EnhancedMCPServerFixed.ts:468-480 (registration)Tool registration in getTools() including name, description, and input schema for validation.{ name: 'generate_euclidean', description: 'Generate Euclidean rhythm', inputSchema: { type: 'object', properties: { hits: { type: 'number', description: 'Number of hits' }, steps: { type: 'number', description: 'Total steps' }, sound: { type: 'string', description: 'Sound to use' } }, required: ['hits', 'steps'] } },
- Helper method that generates the Strudel pattern code using the Euclidean rhythm from MusicTheory and a specified sound.generateEuclideanPattern(hits: number, steps: number, sound: string = "bd"): string { const rhythm = this.theory.generateEuclideanRhythm(hits, steps); return `s("${sound}").struct("${rhythm}")`; }
- src/services/MusicTheory.ts:112-126 (helper)Core Euclidean rhythm algorithm that computes the hit/rest pattern as a string of '1' and '~' using the mathematical distribution.generateEuclideanRhythm(hits: number, steps: number): string { if (hits > steps) { throw new Error('Hits cannot exceed steps'); } const pattern: boolean[] = new Array(steps).fill(false); const interval = steps / hits; for (let i = 0; i < hits; i++) { const index = Math.floor(i * interval); pattern[index] = true; } return pattern.map(hit => hit ? '1' : '~').join(' '); }