CodeWhiz & Kompot
CodeWhiz CodeWhiz
Привет, я тут что-то мутил, приложение, которое на ходу аккорды для импровизации генерит. Хочешь послушать, как оно работает?
Kompot Kompot
Отлично! Давай посмотрим, что у тебя получилось, я в деле.
CodeWhiz CodeWhiz
Here’s a quick sketch in JavaScript that builds a chord sequence and plays it with Tone.js. I’ll walk you through the bits that make it “magical.” ```js // 1. Basic setup import * as Tone from 'tone' const synth = new Tone.PolySynth(Tone.Synth).toDestination() // 2. Define a simple chord vocabulary const chords = { I: [0, 4, 7], // root, major third, perfect fifth ii: [2, 5, 9], iii: [4, 8, 11], IV: [5, 9, 12], V: [7, 11, 14], vi: [9, 12, 16], vii: [11, 14, 17] } // 3. Helper to get semitone offsets from a root note const scale = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] function getChord(root, chordName) { const rootIdx = scale.indexOf(root) const offsets = chords[chordName] return offsets.map(o => scale[(rootIdx + o) % 12]) } // 4. Random progression generator function randomProgression(length = 4) { const chordNames = Object.keys(chords) let prog = [] for (let i = 0; i < length; i++) { const idx = Math.floor(Math.random() * chordNames.length) prog.push(chordNames[idx]) } return prog } // 5. Play it out async function playProgression(root = 'C') { const seq = new Tone.Sequence( (time, chordName) => synth.triggerAttackRelease(getChord(root, chordName), '8n', time), randomProgression(), '4n' ) seq.start(0) Tone.Transport.start() } // 6. Hit the button playProgression('D') // change the root to whatever feels right ``` **How it works** 1. *Chord data*: I keep a simple dictionary of the seven diatonic chords. Each chord is expressed as semitone offsets from its root. 2. *Root‑centric*: The `getChord` helper pulls the right notes for any root. That means you can jam in any key by just swapping the root string. 3. *Randomizer*: `randomProgression` picks a handful of chords at random—no more than four by default. If you want more variation, tweak the `length` argument. 4. *Sequencing*: Tone.js’ `Sequence` does the timing. Each quarter‑note step triggers the synth with an eighth‑note release. 5. *Instant playback*: Just call `playProgression` with a root and you’ll hear a fresh four‑chord loop every time. If you’re comfortable in the browser console, paste the script, hit `playProgression('E')`, and let the AI‑driven harmony roll. Feel free to tweak the chord list, add seventh extensions, or swap the synth for a different voice—this skeleton is intentionally lightweight so you can experiment fast. Let me know how the loops sound and if you want a more sophisticated progression logic. Happy jamming!