generate_melody
Create a melody by specifying a scale and root note, with optional control over note count.
Instructions
Generate melody from scale
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scale | Yes | Scale name | |
| root | Yes | Root note | |
| length | No | Number of notes |
Implementation Reference
- src/server/tools/generate.ts:59-71 (schema)Schema definition for the generate_melody tool: name, description, and inputSchema with scale (string, required), root (string, required), and length (number, optional).
{ name: 'generate_melody', description: 'Generate melody from scale', inputSchema: { type: 'object', properties: { scale: { type: 'string', description: 'Scale name' }, root: { type: 'string', description: 'Root note' }, length: { type: 'number', description: 'Number of notes' }, }, required: ['scale', 'root'], }, }, - src/server/tools/generate.ts:180-188 (handler)Handler for generate_melody: validates root note, scale name, and optional length; builds the scale via ctx.theory.generateScale(); generates the melody via ctx.generator.generateMelody(scale, length); appends the result to the current pattern; returns a confirmation string.
case 'generate_melody': { InputValidator.validateRootNote(args.root); InputValidator.validateScaleName(args.scale); if (args.length !== undefined) InputValidator.validatePositiveInteger(args.length, 'length'); const scale = ctx.theory.generateScale(args.root, args.scale); const melody = ctx.generator.generateMelody(scale, args.length || 8); await appendOrSet(melody, ctx); return `Generated melody in ${args.root} ${args.scale}`; } - Core generateMelody() implementation on PatternGenerator class. Takes a scale array, length (default 8), and octaveRange (default [3,5]). Creates a note sequence that prefers stepwise motion (70% probability) over leaps, with random octave selection, outputting Strudel-compatible code: note('...').s('triangle').
/** * Generates a melodic pattern from a scale * @param scale - Array of note names to use * @param length - Number of notes in the melody (default: 8) * @param octaveRange - Tuple of min and max octave numbers (default: [3, 5]) * @returns Strudel melody pattern code */ generateMelody(scale: string[], length: number = 8, octaveRange: [number, number] = [3, 5]): string { const notes = []; let lastNoteIndex = Math.floor(Math.random() * scale.length); for (let i = 0; i < length; i++) { // Create more musical intervals (prefer steps over leaps) const stepProbability = 0.7; const useStep = Math.random() < stepProbability; let noteIndex: number; if (useStep) { // Move by step (1 or 2 scale degrees) const step = Math.random() < 0.5 ? 1 : -1; noteIndex = (lastNoteIndex + step + scale.length) % scale.length; } else { // Leap to any note noteIndex = Math.floor(Math.random() * scale.length); } const note = scale[noteIndex]; const octave = octaveRange[0] + Math.floor(Math.random() * (octaveRange[1] - octaveRange[0] + 1)); notes.push(`${note.toLowerCase()}${octave}`); lastNoteIndex = noteIndex; } return `note("${notes.join(' ')}").s("triangle")`; } - src/server/server.ts:210-226 (registration)Registration wiring in server.ts: generate_melody is listed among generation tools that require write access (line 220) and is handled specially to allow execution without browser initialization (line 298). The tool is exposed via ...generateModule.tools (line 114).
private requiresInitialization(toolName: string): boolean { const toolsRequiringInit = [ 'write', 'append', 'insert', 'replace', 'play', 'pause', 'stop', 'clear', 'get_pattern', 'analyze', 'analyze_spectrum', 'analyze_rhythm', 'transpose', 'reverse', 'stretch', 'humanize', 'generate_variation', 'add_effect', 'add_swing', 'set_tempo', 'save', 'undo', 'redo', 'validate_pattern_runtime' ]; const toolsRequiringWrite = [ 'generate_pattern', 'generate_drums', 'generate_bassline', 'generate_melody', 'generate_chord_progression', 'generate_euclidean', 'generate_polyrhythm', 'generate_fill' ]; return toolsRequiringInit.includes(toolName) || toolsRequiringWrite.includes(toolName); } - src/server/tools/ai.ts:500-508 (helper)Additional call site in ai.ts where generateMelody is invoked with 8 notes and style-specific effects appended (e.g., for AI-generated melodic parts).
const scale = ctx.theory.generateScale(key, scaleName); const effects: Record<string, string> = { 'techno': '.delay(0.25).room(0.2)', 'house': '.room(0.3).gain(0.6)', 'dnb': '.delay(0.125).room(0.2).gain(0.5)', 'ambient': '.room(0.7).delay(0.5).gain(0.4)', 'trap': '.gain(0.5).room(0.15)', 'jungle': '.delay(0.125).room(0.25).gain(0.55)', 'jazz': '.room(0.4).gain(0.5)', }; return ctx.generator.generateMelody(scale, 8, octaveRange) + (effects[style] || '.room(0.3).gain(0.5)'); }