@kairotic/app 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.html +3 -2
- package/kairotic/assistant.cljs +9 -6
- package/kairotic/code.cljs +3 -3
- package/kairotic/docs.mjs +1 -1
- package/kairotic/help.cljs +82 -91
- package/kairotic/library.cljs +4 -3
- package/kairotic/packages/io.mjs +1 -1
- package/kairotic/packages/net.mjs +1 -1
- package/kairotic/packages/rhythm.mjs +22 -0
- package/kairotic/packages/theory/counterpoint.mjs +19 -80
- package/kairotic/packages/theory/mirror.mjs +1 -1
- package/kairotic/packages/theory/soft.mjs +150 -0
- package/kairotic/packages/theory.mjs +3 -15
- package/kairotic/ringaudio.cljs +1 -1
- package/package.json +1 -1
- package/sources.mjs +1 -1
package/kairotic/help.cljs
CHANGED
|
@@ -804,7 +804,7 @@
|
|
|
804
804
|
:see ["pcclock" "fourier" "mt"]}
|
|
805
805
|
|
|
806
806
|
"euclid"
|
|
807
|
-
{:package "@kairotic/
|
|
807
|
+
{:package "@kairotic/rhythm" :usage "euclid [k 3] [n 8] [rotation 0] [bars 1] [midi]"
|
|
808
808
|
:about "**A Euclidean rhythm: k onsets spread as evenly as they can be over n pulses**, by Bjorklund's algorithm — Euclid's, for the greatest common divisor, on ones and zeros. Toussaint showed that it writes rhythms of the whole world: `euclid 3 8` is the tresillo (3-3-2), `euclid 5 8` the cinquillo, `euclid 5 16` a rotation of the bossa nova, `euclid 7 12` the bembé.
|
|
809
809
|
|
|
810
810
|
**Drawn as a necklace**: the n pulses around a circle, time running clockwise from the top, the onsets joined into a polygon — the same geometry as a pitch-class set on `pcclock`. A necklace has no start; pressing a pulse makes the rhythm start there (a rotation), which may sound quite different.
|
|
@@ -817,7 +817,7 @@
|
|
|
817
817
|
[:midi "numbers on the right inlet are MIDI notes (62), not Hz"]]
|
|
818
818
|
:doc "A Euclidean rhythm (Bjorklund, after Toussaint): k onsets as evenly as they can be over n pulses, drawn as a necklace; pressing a pulse makes it start there. Out: a musical tree with an :rtm (the intervals as shares), the :rtm map alone, and the pulses as ones and zeros. The right inlet gives the pitches its notes take in turn."
|
|
819
819
|
:inlets ["a bang: send it · a number: k · [:k 5], [:n 16], [:rotation 2], [:bars 4], [:meter \"3/4\"] (or a map of them): change it, and send it when it is new"
|
|
820
|
-
"the pitches (cold, for the next time it sends): a list of Hz (a keyboard's keys held; MIDI with midi), a melody string (\"c4 e4 g4\"), a map with :pitches or :pitch, a pattern {:note [...]}
|
|
820
|
+
"the pitches (cold, for the next time it sends): a list of Hz (a keyboard's keys held; MIDI with midi), a melody string (\"c4 e4 g4\"), a map with :pitches or :pitch, a pattern {:note [...]}, notes or chords of a tree · :clear forgets them (every note c4)"]
|
|
821
821
|
:outlets ["the rhythm as a musical tree, for a tree box: [:voice [:measure {:meter [4 4]} [:rtm {:shares [3 3 2] :pitches …}]] …]"
|
|
822
822
|
"the :rtm map alone ({:shares [3 3 2]}, with the pitches if it has them): merge it with other pitches, and write it as a node with vector :rtm %"
|
|
823
823
|
"the pulses, ones and zeros ([1 0 0 1 0 0 1 0])"]
|
|
@@ -839,121 +839,112 @@
|
|
|
839
839
|
"(connect :b8 0 :b5 1)" "(connect :b9 0 :b5 1)" "(connect :b10 0 :b5 0)" "(connect :b6 0 :b11 0)" "(connect :b12 0 :b11 0)"
|
|
840
840
|
"(connect :b11 0 :b13 0)" "(connect :b11 1 :b13 1)" "(connect :b13 0 :b14 0)" "(connect :b13 0 :b14 1)")
|
|
841
841
|
:try "With five onsets ([:k 5] above), click {:pitches \"c4 e4 g4\"}, then [:bars 3] (it sends): C E G over the onsets, the color going on from bar to bar. Press ▶ and click the bang of mt play to hear it. Hold a chord on the keyboard and click bang above: the chord's notes, one onset each."}]
|
|
842
|
-
:see ["tree" "mt" "pcclock"
|
|
842
|
+
:see ["necklace" "tree" "mt" "pcclock"]}
|
|
843
843
|
|
|
844
|
-
"
|
|
845
|
-
{:package "@kairotic/
|
|
846
|
-
:about "**A
|
|
844
|
+
"necklace"
|
|
845
|
+
{:package "@kairotic/rhythm" :usage "necklace [n 16] [bars 1] [midi]"
|
|
846
|
+
:about "**A rhythm that repeats is a necklace**: k onsets among n pulses around a circle, time clockwise from the top. Seen so it is the same object as a pitch-class set on `pcclock`, and Toussaint measures it with the same geometry.
|
|
847
847
|
|
|
848
|
-
**
|
|
848
|
+
**Its measures** go out on the right: the **interval vector** — how often each distance between onsets occurs (the son clave: `[0 1 2 2 0 3 2 0]`) —, its **evenness** — the sum of the chords between all pairs of onsets (Block and Douthett: arcs would score the six 4/4 claves the same) — and `:even`, that sum against the Euclidean rhythm's; **deep** when each distance occurs a different number of times (the bembé, as the diatonic scale), **flat** when each occurs once; its **axes** of mirror symmetry, drawn across the circle; its longest **gap**; rhythmic **oddity** (no two onsets halve the cycle); and `:euclid`, the rotation of E(k,n) it is.
|
|
849
849
|
|
|
850
|
-
**
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
:
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
:
|
|
868
|
-
|
|
869
|
-
[":back" "the last note taken back"]
|
|
870
|
-
[":reset" "a new work"]
|
|
871
|
-
["[:system :septimal31]" "another system (or an EDN map) — these start a new work"]
|
|
872
|
-
["[:mode :hypodorian]" "another mode of the system"]
|
|
873
|
-
["[:k -4]" "the comes a 5th below"]
|
|
874
|
-
["[:bars 5]" "another length"]
|
|
875
|
-
["[:table true]" "a table canon: backwards and upside down"]
|
|
876
|
-
["[:axis 62]" "the axis of a table canon, a MIDI note (the final by default)"]
|
|
877
|
-
["[:temp 2]" "live: the box draws among its answers"]
|
|
878
|
-
["[:seed 7]" "live: its chance (:reset plays the same game again)"]]
|
|
879
|
-
:example (lines "(box :b1 \"msg :engine\" 40 30)" "(box :b2 \"cancrizans\" 40 80)" "(box :b3 \"pbind\" 40 150)" "(box :b4 \"pbind\" 40 260)"
|
|
880
|
-
"(box :b9 \"msg :start\" 40 370)" "(box :b5 \"player 90\" 40 420)" "(box :b6 \"player 90\" 240 420)" "(box :b7 \"synth~\" 40 490)"
|
|
881
|
-
"(box :b17 \"synth~\" 240 490)" "(box :b8 \"dac~\" 40 550)" "(box :b10 \"print\" 260 80)"
|
|
882
|
-
"(box :b19 \"tree\" 40 620)" "(resize :b19 460 200)" "(connect :b2 4 :b19 0)"
|
|
883
|
-
"(connect :b1 0 :b2 0)" "(connect :b2 0 :b3 0)" "(connect :b2 1 :b4 0)" "(connect :b3 0 :b5 1)" "(connect :b4 0 :b6 1)"
|
|
884
|
-
"(connect :b9 0 :b5 0)" "(connect :b9 0 :b6 0)" "(connect :b5 0 :b7 0)" "(connect :b6 0 :b17 0)"
|
|
885
|
-
"(connect :b7 0 :b8 0)" "(connect :b7 0 :b8 1)" "(connect :b17 0 :b8 0)" "(connect :b17 0 :b8 1)" "(connect :b2 3 :b10 0)")
|
|
886
|
-
:try "Click :engine until the work closes: the box writes it from both ends, and the dux and the comes reach the pbinds. Press ▶ and click :start: the crab sounds, each voice on its own synth~ (one synth~ plays one line: a note from the other player would take the place of the last). The tree box below shows the crab as a score: read the comes from the end and it is the dux. Play notes on a keyboard into it instead (the note at the start, then the one at the end)."
|
|
887
|
-
:steps [{:title "Played live"
|
|
888
|
-
:note "With live (Study 46) the crab is played beat by beat: your note, and at the same beat the box's answer — the end of your own line, written ahead. Two beats before the middle a cue; at the middle the rest of your line comes out as the score to read (outlet 4, a musical tree for a score), and the box plays your first half backwards while you read it. Both voices sound from its left outlet, after their voice ids (outlet 1): one synth~ plays both. The first note to read is the last end of your line the box wrote (with an odd number of beats the middle note is shared: the box plays it with you, an octave below); from there each voice plays what the other played, backwards."
|
|
889
|
-
:example (lines "(box :b11 \"keyboard 60 24\" 520 30)" "(box :b12 \"cancrizans live\" 520 150)" "(box :b13 \"synth~\" 520 230)" "(box :b15 \"loadbang\" 720 110)"
|
|
890
|
-
"(box :b16 \"html\" 860 30)" "(resize :b16 300 230)"
|
|
891
|
-
"(code :b16 \"[:div.col [:h4 \\\"beat \\\" :$t \\\" of \\\" :$n \\\" · \\\" :$phase] [:p.soft :$ways \\\" ways to close it\\\"] :$in]\")"
|
|
892
|
-
"(box :b14 \"tree\" 860 290)" "(resize :b14 460 180)"
|
|
893
|
-
"(connect :b15 0 :b12 0)" "(connect :b12 2 :b16 0)" "(connect :b12 3 :b16 0)" "(connect :b12 4 :b14 0)"
|
|
894
|
-
"(connect :b11 0 :b12 0)" "(connect :b12 0 :b13 0)" "(connect :b12 1 :b13 1)" "(connect :b13 0 :b8 0)" "(connect :b13 0 :b8 1)")
|
|
895
|
-
:try "Press ▶: the loadbang counts the futures. The html box shows where the crab is and its futuribles as a table — each note you can play, the ways it leaves to close the crab, and :end, what the box will answer at the end of your line. Play D on the keyboard: the box answers with D an octave below — both voices on one synth~, yours as the box takes it (corrected if it must be), with their voice ids (1→1). Play on: at the middle the tree box shows, as a score, what is left of your line to read — the box's answers so far, backwards —; at the end it shows the whole crab."}]
|
|
896
|
-
:see ["comes" "pbind" "player"]}
|
|
850
|
+
**It is an editor too**: pressing a pulse adds or takes away an onset. Its other outlets are `euclid`'s — a musical tree, its `:rtm` map, its pulses —, with the pitches on the right inlet the same way, so a rhythm drawn here goes into trees just as one made there."
|
|
851
|
+
:args [[:n "how many pulses before a rhythm arrives (a rhythm brings its own)"]
|
|
852
|
+
[:bars "how many bars the tree repeats it, the pitches going on"]
|
|
853
|
+
[:midi "numbers on the right inlet are MIDI notes (62), not Hz"]]
|
|
854
|
+
:doc "A cyclic rhythm on the circle and its geometry (Toussaint): drawn as a polygon with its axes of symmetry; pressing a pulse adds or takes away an onset. Out as euclid (a tree, the :rtm map, the pulses) and its measures: interval vector, evenness, deep, flat, axes, longest gap, oddity, the rotation of E(k,n) it is."
|
|
855
|
+
:inlets ["a rhythm: pulses ([1 0 0 1 0 0 1 0], euclid's right outlet), intervals ([3 3 2]), box notation (\"x..x..x.\"), an :rtm map ({:shares [3 3 2]}), a tree with an :rtm, or {:onsets [0 3 6] :n 8} · a number: that pulse added or taken away · :complement · [:rotate 1] · [:n 12] · [:bars 2] · [:meter \"3/4\"] · :clear · a bang sends it again"
|
|
856
|
+
"the pitches (cold), as euclid's: a list of Hz (MIDI with midi), a melody string, a map with :pitches or :pitch, a pattern {:note [...]} · :clear"]
|
|
857
|
+
:outlets ["the rhythm as a musical tree, for a tree box (as euclid's)" "the :rtm map alone, to merge with pitches" "the pulses, ones and zeros"
|
|
858
|
+
"its measures: {:n :k :onsets :intervals :ivec :evenness :even :deep :flat :axes :gap :oddity :euclid}"]
|
|
859
|
+
:messages [["\"x..x..x...x.x...\"" "the son clave, in box notation"]
|
|
860
|
+
["[3 3 2]" "the tresillo, as intervals"]
|
|
861
|
+
[":complement" "onsets and silences swapped"]
|
|
862
|
+
["[:rotate 1]" "it starts a pulse later"]
|
|
863
|
+
["[:n 12]" "a circle of twelve pulses (onsets past it are lost)"]]
|
|
864
|
+
:example (lines "(box :b1 \"msg \\\"x..x..x...x.x...\\\"\" 40 20)" "(box :b2 \"msg \\\"x..x...x..x.x...\\\"\" 220 20)" "(box :b3 \"msg \\\"x..x..x...x..x..\\\"\" 400 20)"
|
|
865
|
+
"(box :b4 \"necklace\" 40 70)" "(box :b5 \"print\" 40 340)" "(box :b6 \"tree\" 280 70)" "(resize :b6 420 160)"
|
|
866
|
+
"(connect :b1 0 :b4 0)" "(connect :b2 0 :b4 0)" "(connect :b3 0 :b4 0)" "(connect :b4 3 :b5 0)" "(connect :b4 0 :b6 0)")
|
|
867
|
+
:try "Click the son, the rumba and the bossa nova (three of Toussaint's six 4/4 claves): each necklace, its axes, and its measures in the REPL — the son's interval vector [0 1 2 2 0 3 2 0], the rumba less even than the bossa nova, the bossa nova E(5,16) from pulse 6. Press pulses to make one of your own: the tree box follows."
|
|
868
|
+
:see ["euclid" "pcclock" "fourier" "tree"]}
|
|
897
869
|
|
|
898
870
|
"comes"
|
|
899
|
-
{:package "@kairotic/theory" :usage "comes [system] [mode] [k -7] [d 3] [beats 2] [imitation 1] [bars
|
|
900
|
-
:about "**You play a line, the dux, and comes answers with a second voice, the comes, that
|
|
871
|
+
{:package "@kairotic/theory" :usage "comes [system] [mode] [speed 1] [k -7] [d 3] [beats 2] [imitation 1] [bars 0] [upbeat 0] [temp 0] [seed 1] [inverted] [fourth] [live] [nocues] [midi]"
|
|
872
|
+
:about "**You play a line, the dux, and comes answers with a second voice, the comes, that reads it** — by the rules of 16th-century counterpoint. A note that breaks a rule, or from which the work could not be finished, is corrected to the nearest one that can be played, and the box says why.
|
|
901
873
|
|
|
902
|
-
**
|
|
874
|
+
**The comes is a reader of the dux at a speed.** `1` (the default) is a canon: the dux again, `d` beats later. `0.5` reads it twice as slow, an augmentation. `-1` reads it backwards: a **crab**, the two voices sounding at once so that the work reads the same from either end. `inverted` turns the comes upside down: an inversion, or with `-1` a **table canon**, one score read by two players from opposite sides of a table. `k` is how far away it reads: −7 an octave below.
|
|
903
875
|
|
|
904
|
-
**
|
|
876
|
+
**The speed decides how it is played.** Forwards (1, 0.5) you play the dux and the comes answers. By turns (the default) each note you send is a beat, and the same note again, or `:hold`, holds it over the beat. With **live** a pulse on the right inlet (a `metro`) gives the beats: a note lasts until you play the next, held three beats in 2/2 the canon turns to 3/2, longer than a bar the section ends. Backwards (−1) the work cannot start at the beginning — the comes's first note is the dux's last — so it is written **from both ends to the middle**: by turns a note at the start, then one at the end; **live**, you play and the box answers at the same beat with the end of your own line, and at the middle the rest of your line comes out as a score to read while the box plays your first half backwards.
|
|
905
877
|
|
|
906
|
-
**
|
|
878
|
+
**Where it plays: a system and one of its modes.** The **system** is the scale, its tuning and its rules: `diatonic` (the default), `meantone`, `septimal31`, `pentatonic`, or your own as EDN. The **mode** — `dorian` (the default), `hypodorian`, `ionian`, `aeolian`…; `penta-C`, `penta-A`, `penta-D` in the pentatonic — sets the final, the range and the notes to begin on. Write them in the box in any order: `comes hypodorian`, `comes pentatonic penta-A -1 inverted`.
|
|
907
879
|
|
|
908
|
-
**What comes out.** Both voices as Hz with their voice ids — one `synth~`, connected 0→0 and 1→1, plays both —;
|
|
880
|
+
**What comes out.** Both voices sounding as Hz with their voice ids — one `synth~`, connected 0→0 and 1→1, plays both —; the **futuribles**, the notes you can play next, each with the ways it leaves to finish the work, counted exactly; where the work is; and, when it is finished, the whole work as a musical tree for a `tree` box (and `mt play` to hear it again)."
|
|
909
881
|
:args [[:system "the scale, its tuning and its rules: diatonic (by default), meantone, septimal31 or pentatonic"]
|
|
910
882
|
[:mode "one of the system's modes: dorian (by default), hypodorian, ionian, mixolydian, aeolian and their plagals; penta-C, penta-A, penta-D in the pentatonic"]
|
|
911
|
-
[:
|
|
912
|
-
[:
|
|
883
|
+
[:speed "how the comes reads the dux: 1 a canon · 0.5 twice as slow (an augmentation) · −1 backwards (a crab)"]
|
|
884
|
+
[:k "how far away the comes reads it, in steps of the scale: −7 an octave below, −4 a 5th below, 0 at the unison"]
|
|
885
|
+
[:d "forwards: how many beats after the dux the comes comes in"]
|
|
913
886
|
[:beats "beats in a bar: 2 (2/2) or 3 (3/2)"]
|
|
914
|
-
[:imitation "1 a strict canon · 0.5–0.9 the comes near the dux's notes · 0.2–0.4 only following its motion · below, free"]
|
|
915
|
-
[:bars "how long the
|
|
916
|
-
[:upbeat "beats of rest before the first note (an anacrusis)"]
|
|
917
|
-
[:temp "chance: at 0 always the most plausible; higher, the
|
|
887
|
+
[:imitation "forwards: 1 a strict canon · 0.5–0.9 the comes near the dux's notes · 0.2–0.4 only following its motion · below, free"]
|
|
888
|
+
[:bars "how long the work is, in bars: 0, the relation's own — 8 forwards, 6 backwards (a crab of more bars takes longer to count)"]
|
|
889
|
+
[:upbeat "forwards: beats of rest before the first note (an anacrusis)"]
|
|
890
|
+
[:temp "chance: at 0 always the most plausible; higher, the box draws among the candidates"]
|
|
918
891
|
[:seed "the seed of its chance: the same seed, the same game"]
|
|
919
|
-
[:
|
|
920
|
-
[:
|
|
921
|
-
[:
|
|
922
|
-
[:
|
|
923
|
-
[:nocues "live: it does not turn to 3/2 or end the section by itself; other boxes decide, with [:beats 3] and :end"]
|
|
892
|
+
[:inverted "the comes upside down, about the final (or [:axis 62]): an inversion, or with −1 a table canon"]
|
|
893
|
+
[:fourth "forwards: fourth species, your line a cantus firmus (a note a bar) and the comes syncopated"]
|
|
894
|
+
[:live "played on a pulse (the right inlet), or without one a note a beat, instead of by turns"]
|
|
895
|
+
[:nocues "live, forwards: it does not turn to 3/2 or end the section by itself; other boxes decide, with [:beats 3] and :end"]
|
|
924
896
|
[:midi "numbers in are MIDI notes (62), not Hz"]]
|
|
925
|
-
:doc "Counterpoint live:
|
|
926
|
-
:inlets ["the dux's next note: Hz (a number, or the list of notes held, as a keyboard or midiin sends them), [:midi 62], or an event (:freq from midiin, or a player's :hz) · :hold (by turns) · [:k -4], [:d 2], [:imitation 0.5], [:mode :hypodorian], [:system :pentatonic], [:beats 3], [:species 4]
|
|
897
|
+
:doc "Counterpoint live (Studies 41–46, 50): the comes reads the dux at a speed — 1 a canon, 0.5 an augmentation, −1 a crab, written from both ends to the middle —, upside down with inverted (an inversion, or a table canon). Each note is checked against the rules of the style and against the future, and corrected to the nearest one that can be played. Forwards you play the dux and the comes answers, by turns or live on a pulse; backwards, a note at the start and then one at the end, or live, the box answering at the end of your own line. The system (Study 42) is diatonic by default, or meantone, septimal31, pentatonic, or an EDN map; the finished work comes out as a musical tree."
|
|
898
|
+
:inlets ["the dux's next note (backwards: at the start, then at the end): Hz (a number, or the list of notes held, as a keyboard or midiin sends them), [:midi 62], or an event (:freq from midiin, or a player's :hz) · :hold (forwards, by turns) · :engine · :back · :reset · :end (forwards: the section ends) · [:speed -1], [:inverted true], [:k -4], [:d 2], [:imitation 0.5], [:mode :hypodorian], [:system :pentatonic], [:bars 5], [:beats 3], [:species 4], [:axis 62]: the work changed"
|
|
927
899
|
"live: the pulse (a bang from a metro or a pulse): a beat"]
|
|
928
|
-
:outlets ["the Hz of both voices sounding, the dux
|
|
929
|
-
:messages [["[:midi 62]" "a MIDI note as the
|
|
930
|
-
[":engine" "the box plays the next
|
|
931
|
-
[":hold" "by turns: the note held over the next beat (a tie)"]
|
|
932
|
-
[":back" "the last
|
|
933
|
-
[":reset" "a new
|
|
934
|
-
[":end" "the section ends
|
|
935
|
-
["[:
|
|
900
|
+
:outlets ["the Hz of both voices sounding, as a keyboard or a player sends them — the dux as played (or corrected) and the comes" "their voice ids, sent first (for a synth~'s right inlet): a note held keeps its id and goes on sounding" "the futuribles: the notes that can come next, with their futures ({:midi :name :futures …}; backwards live, with :end, what the box will answer at the end of your line)" "where the work is: forwards {:t :n :bar :beat :held :asked :played :corrected :reason :futures :obligations :section :meter …} or a cue {:cue :ternary|:end}; backwards {:step :steps :writing :beat :futures :dux :comes :closed …} or live {:t :phase :you :engine :ways …}, with the cue {:cue :middle :in 2}" "the work as a musical tree, both voices (for a tree box, its score): when it is finished; backwards live also at the middle, what is left of your line to read"]
|
|
901
|
+
:messages [["[:midi 62]" "a MIDI note as the next note (Hz, a keyboard's list or an event also)"]
|
|
902
|
+
[":engine" "the box plays the next note itself (backwards live: a beat, with the box playing yours)"]
|
|
903
|
+
[":hold" "forwards, by turns: the note held over the next beat (a tie)"]
|
|
904
|
+
[":back" "the last note taken back"]
|
|
905
|
+
[":reset" "a new work"]
|
|
906
|
+
[":end" "forwards: the section ends, the next note begins a new canon"]
|
|
907
|
+
["[:speed -1]" "another relation: −1 a crab, 0.5 an augmentation, 1 a canon (a new work)"]
|
|
908
|
+
["[:inverted true]" "the comes upside down: an inversion, or backwards a table canon (a new work)"]
|
|
909
|
+
["[:system :pentatonic]" "another system (or an EDN map {:tuning … :modes …})"]
|
|
936
910
|
["[:mode :hypodorian]" "another mode of the system"]
|
|
937
911
|
["[:k -4]" "the comes a 5th below"]
|
|
938
|
-
["[:d 2]" "the comes two beats after the dux"]
|
|
939
|
-
["[:imitation 0.5]" "less imitation
|
|
912
|
+
["[:d 2]" "forwards: the comes two beats after the dux"]
|
|
913
|
+
["[:imitation 0.5]" "forwards: less imitation, the comes near the dux's notes"]
|
|
914
|
+
["[:bars 5]" "another length"]
|
|
940
915
|
["[:beats 3]" "3/2"]
|
|
941
916
|
["[:species 4]" "fourth species (with [:beats 2])"]
|
|
942
|
-
["[:
|
|
943
|
-
["[:
|
|
944
|
-
["[:temp 1.5]" "chance in the comes, and in the dux of :engine (0: the futuribles' choice)"]
|
|
917
|
+
["[:axis 62]" "the axis of inverted, a MIDI note (the final by default)"]
|
|
918
|
+
["[:temp 1.5]" "chance in the box's choices (0: the futuribles' choice)"]
|
|
945
919
|
["[:seed 7]" "its chance: the same seed, the same game (:reset plays it again)"]]
|
|
946
920
|
:example (lines "(box :b1 \"keyboard 60 24\" 40 30)" "(box :b2 \"comes\" 40 130)" "(box :b3 \"synth~\" 40 200)"
|
|
947
921
|
"(box :b5 \"dac~\" 40 260)" "(box :b6 \"print\" 280 200)" "(box :b7 \"msg :engine\" 220 100)"
|
|
922
|
+
"(box :b4 \"tree\" 40 320)" "(resize :b4 460 180)"
|
|
948
923
|
"(connect :b1 0 :b2 0)" "(connect :b7 0 :b2 0)" "(connect :b2 0 :b3 0)" "(connect :b2 1 :b3 1)"
|
|
949
|
-
"(connect :b3 0 :b5 0)" "(connect :b3 0 :b5 1)" "(connect :b2 3 :b6 0)")
|
|
950
|
-
:try "Press ▶ and play D, E, F on the keyboard: the dux. From the fourth note on the comes answers an octave below, three beats later. Play a black key, or a note against the rules: the box takes the nearest one that can be played and prints why. Click :engine to let it go on alone."
|
|
924
|
+
"(connect :b3 0 :b5 0)" "(connect :b3 0 :b5 1)" "(connect :b2 3 :b6 0)" "(connect :b2 4 :b4 0)")
|
|
925
|
+
:try "Press ▶ and play D, E, F on the keyboard: the dux. From the fourth note on the comes answers an octave below, three beats later. Play a black key, or a note against the rules: the box takes the nearest one that can be played and prints why. Click :engine to let it go on alone: when the canon is finished the tree box shows it as a score."
|
|
951
926
|
:steps [{:title "Live, on a pulse"
|
|
952
|
-
:note "With live, a metro gives the beats: hold a key and the note is held over them (a tie); hold it three beats in 2/2 and the canon turns to 3/2; longer than a bar, and the section ends: the next note begins a new canon. That is all comes reads of time: with nocues it leaves the meter and the sections to other boxes, which send [:beats 3] and end."
|
|
927
|
+
:note "With live, a metro gives the beats: hold a key and the note is held over them (a tie); hold it three beats in 2/2 and the canon turns to 3/2; longer than a bar, and the section ends: the next note begins a new canon. That is all comes reads of time: with nocues it leaves the meter and the sections to other boxes, which send [:beats 3] and :end."
|
|
953
928
|
:example (lines "(box :b8 \"metro 600\" 380 100)" "(box :b9 \"msg 1\" 380 70)" "(box :b10 \"comes live\" 380 160)"
|
|
954
929
|
"(connect :b9 0 :b8 0)" "(connect :b8 0 :b10 1)" "(connect :b1 0 :b10 0)" "(connect :b10 0 :b3 0)" "(connect :b10 1 :b3 1)" "(connect :b10 3 :b6 0)")
|
|
955
|
-
:try "Press ▶, click 1 to start the pulse and play slowly: the comes answers on the beats. Hold a note three beats: the cue :ternary is printed."}
|
|
956
|
-
|
|
930
|
+
:try "Press ▶, click 1 to start the pulse and play slowly: the comes answers on the beats. Hold a note three beats: the cue :ternary is printed."}
|
|
931
|
+
{:title "Backwards: a crab, from both ends to the middle"
|
|
932
|
+
:note "With speed −1 the comes is the dux backwards, so the work is written from both ends: each step a note at the start, then one at the end, and the futuribles count the ways to close it in the middle. With inverted it is also upside down: a table canon. When it closes, the tree box shows it as a score — read the comes from the end and it is the dux — and mt play plays it, both voices on one synth~."
|
|
933
|
+
:example (lines "(box :b20 \"msg :engine\" 720 30)" "(box :b21 \"comes -1\" 720 80)" "(box :b22 \"tree\" 720 140)" "(resize :b22 460 180)"
|
|
934
|
+
"(box :b23 \"mt play\" 720 345)" "(box :b24 \"msg :bang\" 830 345)" "(box :b25 \"synth~\" 720 400)" "(box :b26 \"msg [:inverted true]\" 840 30)"
|
|
935
|
+
"(connect :b20 0 :b21 0)" "(connect :b26 0 :b21 0)" "(connect :b21 4 :b22 0)" "(connect :b22 0 :b23 0)" "(connect :b24 0 :b23 0)"
|
|
936
|
+
"(connect :b23 0 :b25 0)" "(connect :b23 1 :b25 1)" "(connect :b25 0 :b5 0)" "(connect :b25 0 :b5 1)")
|
|
937
|
+
:try "Click :engine until the crab closes: the box writes it from both ends, and the tree box shows it. Press ▶ and click the bang of mt play: both voices on one synth~. Click [:inverted true] and :engine again: a table canon. Play notes on the keyboard into it instead (the note at the start, then the one at the end)."}
|
|
938
|
+
{:title "Backwards, live"
|
|
939
|
+
:note "With −1 live the crab is played beat by beat (Study 46): your note, and at the same beat the box's answer — the end of your own line, written ahead. Two beats before the middle a cue; at the middle the rest of your line comes out as the score to read (outlet 4, for a tree box), and the box plays your first half backwards while you read it. Both voices sound from it, on one synth~."
|
|
940
|
+
:example (lines "(box :b11 \"keyboard 60 24\" 520 470)" "(box :b12 \"comes -1 live\" 520 590)" "(box :b13 \"synth~\" 520 660)" "(box :b15 \"loadbang\" 720 550)"
|
|
941
|
+
"(box :b16 \"html\" 860 470)" "(resize :b16 300 230)"
|
|
942
|
+
"(code :b16 \"[:div.col [:h4 \\\"beat \\\" :$t \\\" of \\\" :$n \\\" · \\\" :$phase] [:p.soft :$ways \\\" ways to close it\\\"] :$in]\")"
|
|
943
|
+
"(box :b14 \"tree\" 860 730)" "(resize :b14 460 180)"
|
|
944
|
+
"(connect :b15 0 :b12 0)" "(connect :b12 2 :b16 0)" "(connect :b12 3 :b16 0)" "(connect :b12 4 :b14 0)"
|
|
945
|
+
"(connect :b11 0 :b12 0)" "(connect :b12 0 :b13 0)" "(connect :b12 1 :b13 1)" "(connect :b13 0 :b5 0)" "(connect :b13 0 :b5 1)")
|
|
946
|
+
:try "Press ▶: the loadbang counts the futures. The html box shows where the crab is and its futuribles as a table — each note you can play, the ways it leaves to close the crab, and :end, what the box will answer at the end of your line. Play D on the keyboard: the box answers with D an octave below. Play on: at the middle the tree box shows what is left of your line to read; at the end, the whole crab."}]
|
|
947
|
+
:see ["keyboard" "midiin" "metro" "tree" "mt" "tonnetz"]}
|
|
957
948
|
|
|
958
949
|
"adsr~"
|
|
959
950
|
{:package "@kairotic/synth" :usage "adsr~ [a d s r]"
|
package/kairotic/library.cljs
CHANGED
|
@@ -175,13 +175,14 @@
|
|
|
175
175
|
"@kairotic/controls" {:version "0.2.3" :classes #{"knob" "keyboard" "scale"} :local "kairotic/packages/controls.mjs"}
|
|
176
176
|
"@kairotic/analysis" {:version "0.7.5" :classes #{"features~" "pitch~" "onset~" "chroma~" "spectrum~" "transcribe" "monitor" "form~" "realize~"}
|
|
177
177
|
:local "kairotic/packages/analysis.mjs"}
|
|
178
|
-
"@kairotic/theory" {:version "0.
|
|
178
|
+
"@kairotic/theory" {:version "0.13.0" :classes #{"pcclock" "fourier" "tonnetz" "comes"} :local "kairotic/packages/theory.mjs"}
|
|
179
|
+
"@kairotic/rhythm" {:version "0.1.0" :classes #{"euclid" "necklace"} :local "kairotic/packages/rhythm.mjs"}
|
|
179
180
|
"@kairotic/data" {:version "0.1.4" :classes #{"fetch" "edn"} :local "kairotic/packages/data.mjs"}
|
|
180
181
|
"@kairotic/patterns" {:version "0.2.1" :classes #{"pbind" "pattern" "player" "render"} :local "kairotic/packages/patterns.mjs"}
|
|
181
182
|
"@kairotic/spectral" {:version "0.1.2" :classes #{"shift~"} :local "kairotic/packages/spectral.mjs"}
|
|
182
|
-
"@kairotic/io" {:version "0.2.
|
|
183
|
+
"@kairotic/io" {:version "0.2.5" :classes #{"midiin" "midiout" "oscin" "oscout" "motion" "gamepad" "serial" "control"}
|
|
183
184
|
:local "kairotic/packages/io.mjs"}
|
|
184
|
-
"@kairotic/net" {:version "0.1.
|
|
185
|
+
"@kairotic/net" {:version "0.1.4" :classes #{"share" "shared" "pulse"} :local "kairotic/packages/net.mjs"}})
|
|
185
186
|
|
|
186
187
|
;; Official packages that carry modules for the page itself rather than box classes (nothing to bring into
|
|
187
188
|
;; a work): where their files are, npm first at the pinned version, then the page's own copy.
|
package/kairotic/packages/io.mjs
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Generated by packages/build.mjs
|
|
2
|
+
export default {
|
|
3
|
+
"package": "@kairotic/rhythm",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"api": 1,
|
|
6
|
+
"author": "bonuoq",
|
|
7
|
+
"license": "MPL-2.0",
|
|
8
|
+
"components": [
|
|
9
|
+
{
|
|
10
|
+
"class": "euclid",
|
|
11
|
+
"license": "MPL-2.0",
|
|
12
|
+
"doc": "A Euclidean rhythm (Bjorklund's algorithm, after Toussaint): k onsets spread as evenly as they can be over n pulses — euclid 3 8 is the tresillo (3-3-2), euclid 5 8 the cinquillo, euclid 5 16 a rotation of the bossa nova, euclid 7 12 the bembé. Drawn as a necklace: the pulses around a circle, time clockwise from the top, the onsets joined into a polygon; pressing a pulse makes the rhythm start there (a rotation). A bang sends it; [:k 5], [:n 16], [:rotation 2], [:bars 4], [:meter \"3/4\"] (or a map of them) change it and send it when it is new, a number is k. Rhythm apart from pitch: out of the left outlet a musical tree, a bar of n pulses (4/4 for 8 or 16, 12/8 for 12, n/8 up to 12, else n/16; [:meter …] for another) with an :rtm whose shares are the intervals between onsets (a negative share a silence); bars repeat it, the pitches going on. Its right inlet (cold) takes the pitches the notes take in turn, starting again when they run out (talea and color): a map with :pitches or :pitch (kept as it is), a pattern ({:note [...]}), a melody string (\"c4 e4 g4\"), a list of Hz (the keys held on a keyboard; MIDI with midi), notes or chords of a tree; :clear forgets them (every note c4). Outlet 1 is the :rtm map alone ({:shares [3 3 2] :pitches …}), to merge with other pitches (merge) and write as a node (vector :rtm %); outlet 2 the pulses, ones and zeros ([1 0 0 1 0 0 1 0]).",
|
|
13
|
+
"source": ";; euclid · a Euclidean rhythm: k onsets spread as evenly as they can be over n pulses\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Bjorklund's algorithm (Toussaint, \"The Euclidean algorithm generates traditional musical rhythms\", 2005):\n;; E(3,8) is the tresillo, E(5,8) the cinquillo, E(5,16) a rotation of the bossa nova, E(7,12) the bembé.\n;; The rhythm is drawn as a necklace — the n pulses around a circle, time running clockwise from the top,\n;; the onsets joined into a polygon — and pressing a pulse makes the rhythm start there (a rotation).\n;; What it sends is rhythm apart from pitch, as an :rtm of kairotic.mt (shares: the intervals between\n;; onsets, a negative share a silence), so that it can be merged with a map of pitches ({:pitches …}, the\n;; talea and color of an isorhythm) into a musical tree: the box does it with what reaches its right inlet,\n;; and its outlet 1 gives the bare :rtm map to merge elsewhere (merge, then vector :rtm %).\n(let [;; Bjorklund: k ones and n − k zeros, the remainder paired again and again (Euclid's algorithm)\n bjorklund (fn [k n]\n (cond (<= n 0) []\n (<= k 0) (vec (repeat n 0))\n (>= k n) (vec (repeat n 1))\n :else (loop [a (vec (repeat k [1])) b (vec (repeat (- n k) [0]))]\n (if (<= (count b) 1)\n (vec (apply concat (concat a b)))\n (let [m (min (count a) (count b))]\n (recur (mapv into (subvec a 0 m) (subvec b 0 m))\n (if (> (count a) m) (subvec a m) (subvec b m))))))))\n ;; a necklace read from pulse r on: the rhythm in a fixed position against the beat\n rotate (fn [xs r] (let [n (count xs)] (if (zero? n) xs (let [r (mod r n)] (vec (concat (subvec xs r) (subvec xs 0 r)))))))\n onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))\n ;; the intervals between onsets, around the circle (3 3 2): the rhythm as a necklace\n intervals-of (fn [ps] (let [os (onsets-of ps) n (count ps)]\n (if (empty? os) [] (mapv (fn [a b] (- b a)) os (conj (subvec os 1) (+ n (first os)))))))\n ;; the shares of an :rtm: from each onset to the next one (or the end of the bar); the pulses before the\n ;; first onset a silence (a negative share): 01001001 → [-1 3 3 1]\n shares-of (fn [ps] (let [os (onsets-of ps) n (count ps)]\n (cond (zero? n) []\n (empty? os) [(- n)]\n :else (vec (concat (when (pos? (first os)) [(- (first os))])\n (map (fn [a b] (- b a)) os (conj (subvec os 1) n)))))))\n ;; the meter of a bar of n pulses, when none is asked: the pulse a written value\n pow2? (fn [x] (and (pos? x) (== x (js/Math.pow 2 (js/Math.round (js/Math.log2 x))))))\n meter-of (fn [n] (cond (and (>= n 4) (pow2? n)) [4 4]\n (and (>= n 12) (zero? (mod n 3)) (pow2? (/ n 3))) [12 8]\n (< n 4) [n 4]\n (<= n 12) [n 8]\n :else [n 16]))\n ;; a meter asked for: [3 4], \"3/4\", \"7/8[2+2+3]\", or nil (as the pulses say)\n meter-arg (fn [v] (cond (and (sequential? v) (= 2 (count v)) (every? number? v)) (vec v)\n (string? v) v\n (keyword? v) (name v)\n :else nil))\n ;; pitches, as theory boxes take them: Hz by default, MIDI with midi; the MIDI notes kept to 3 decimals\n hz->m (fn [x] (when (and (number? x) (pos? x)) (/ (js/Math.round (* 1000 (+ 69 (* 12 (js/Math.log2 (/ x 440)))))) 1000)))\n ;; what reaches the right inlet, as the pitches of the :rtm (or nil, to forget them): a map of pitches\n ;; ({:pitches …} or {:pitch …}, kept as they are), a pattern ({:note [...]}, MIDI;\n ;; nil a rest), a melody string (\"c4 e4 g4\"), a list of pitches (Hz, as a keyboard sends the keys held;\n ;; MIDI with midi), a list of notes or chords of a tree, or one pitch\n pitches-of (fn [midi? v]\n (let [p (fn [x] (if midi? x (hz->m x)))]\n (cond (#{:clear} v) nil\n (map? v) (cond (or (contains? v :pitches) (contains? v :pitch)) (select-keys v [:pitches :pitch])\n (sequential? (:note v)) {:pitches (mapv (fn [x] (if (number? x) x \"r\")) (:note v))}\n :else :ignore)\n (string? v) {:pitches v}\n (number? v) (if-let [m (p v)] {:pitch m} :ignore)\n (and (sequential? v) (keyword? (first v))) {:pitches [v]}\n (and (sequential? v) (seq v) (every? number? v)) (let [ms (vec (keep p v))] (if (seq ms) {:pitches ms} :ignore))\n (and (sequential? v) (seq v)) {:pitches (vec v)}\n :else :ignore)))\n ;; the color goes on over the bars: bar i begins where the notes of the bar before left it\n tokens (fn [ps] (cond (string? ps) (vec (remove #{\"\"} (.split (.trim ps) (js/RegExp. \"\\\\s+\"))))\n (sequential? ps) (vec ps)\n :else nil))\n color-at (fn [pm skip]\n (let [ts (tokens (:pitches pm))]\n (if (or (empty? ts) (zero? (mod skip (count ts))))\n pm\n (let [r (mod skip (count ts)) ts2 (vec (concat (subvec ts r) (subvec ts 0 r)))]\n (assoc pm :pitches (if (string? (:pitches pm)) (apply str (interpose \" \" ts2)) ts2))))))\n ;; everything the box knows: its words, changed by messages\n params (fn [args st]\n (let [o (merge args (:over st))\n n (max 1 (js/Math.round (or (:n o) 8)))]\n {:n n :k (api/clamp (js/Math.round (or (:k o) 3)) 0 n) :rotation (mod (js/Math.round (or (:rotation o) 0)) n)\n :bars (api/clamp (js/Math.round (or (:bars o) 1)) 1 64) :meter (or (:meter o) (meter-of n))}))\n ;; the rhythm as data: the pulses, its necklace, and the :rtm with the pitches merged\n rhythm (fn [{:keys [k n rotation bars meter]} pm]\n (let [ps (rotate (bjorklund k n) rotation)\n rtm (merge {:shares (shares-of ps)} (or pm {}))\n bar (fn [i] (let [a (color-at rtm (* i k))]\n (if (zero? i) [:measure {:meter meter} [:rtm a]] [:measure [:rtm a]])))]\n {:pulses ps :onsets (onsets-of ps) :intervals (intervals-of ps) :rtm rtm\n :tree (into [:voice] (map bar (range bars)))}))\n shown-of (fn [p r] (merge (select-keys p [:k :n :rotation :bars]) (select-keys r [:pulses :onsets :intervals])))\n ;; what goes out: the tree (0), the :rtm map (1), the pulses (2), the last first (as Max does)\n outs (fn [r] [[2 (:pulses r)] [1 (:rtm r)] [0 (:tree r)]])\n ;; a change: sent when what it makes is new (the same again changes nothing); a bang sends it anyway\n changed (fn [args st over always?]\n (let [st2 (assoc st :over over) p (params args st2) r (rhythm p (:pm st2))]\n (if (and (not always?) (= (:tree r) (:sent st)))\n {:state st2 :show (shown-of p r)}\n {:state (assoc st2 :sent (:tree r)) :show (shown-of p r) :out (outs r)})))\n polar (fn [cx cy R i n] (let [a (* 2 js/Math.PI (/ i n))] [(+ cx (* R (js/Math.sin a))) (- cy (* R (js/Math.cos a)))]))]\n {;; ── about ──\n :class \"euclid\"\n :license \"MPL-2.0\"\n :doc \"A Euclidean rhythm (Bjorklund's algorithm, after Toussaint): k onsets spread as evenly as they can be over n pulses — euclid 3 8 is the tresillo (3-3-2), euclid 5 8 the cinquillo, euclid 5 16 a rotation of the bossa nova, euclid 7 12 the bembé. Drawn as a necklace: the pulses around a circle, time clockwise from the top, the onsets joined into a polygon; pressing a pulse makes the rhythm start there (a rotation). A bang sends it; [:k 5], [:n 16], [:rotation 2], [:bars 4], [:meter \\\"3/4\\\"] (or a map of them) change it and send it when it is new, a number is k. Rhythm apart from pitch: out of the left outlet a musical tree, a bar of n pulses (4/4 for 8 or 16, 12/8 for 12, n/8 up to 12, else n/16; [:meter …] for another) with an :rtm whose shares are the intervals between onsets (a negative share a silence); bars repeat it, the pitches going on. Its right inlet (cold) takes the pitches the notes take in turn, starting again when they run out (talea and color): a map with :pitches or :pitch (kept as it is), a pattern ({:note [...]}), a melody string (\\\"c4 e4 g4\\\"), a list of Hz (the keys held on a keyboard; MIDI with midi), notes or chords of a tree; :clear forgets them (every note c4). Outlet 1 is the :rtm map alone ({:shares [3 3 2] :pitches …}), to merge with other pitches (merge) and write as a node (vector :rtm %); outlet 2 the pulses, ones and zeros ([1 0 0 1 0 0 1 0]).\"\n :usage \"euclid [k 3] [n 8] [rotation 0] [bars 1] [midi]\"\n :io [2 3]\n :size [180 200]\n\n ;; ── arguments ──\n :args [[:k 3] [:n 8] [:rotation 0] [:bars 1] [:midi false]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args]}]\n (let [st (or state {:over {} :pm nil})\n over (:over st)\n set-over (fn [m] (changed args st (merge over m) false))]\n (if (= inlet 1)\n (let [pm (pitches-of (:midi args) value)]\n (if (= pm :ignore) {} {:state (assoc st :pm pm)}))\n (cond\n (#{:bang} value) (changed args st over true)\n (number? value) (set-over {:k value})\n (and (vector? value) (= :pressed (first value)) (number? (second value)))\n (let [p (params args st)] (set-over {:rotation (+ (:rotation p) (second value))}))\n (and (vector? value) (= 2 (count value)) (#{:k :n :rotation :bars} (first value)) (number? (second value)))\n (set-over {(first value) (second value)})\n (and (vector? value) (= :meter (first value))) (set-over {:meter (meter-arg (second value))})\n (map? value) (set-over (cond-> (select-keys value [:k :n :rotation :bars])\n (contains? value :meter) (assoc :meter (meter-arg (:meter value)))))\n :else {}))))\n ;; pressing a pulse: the rhythm starts there (sent once, on release)\n :gesture (fn [{:keys [up hit]}] (when (and up (number? hit)) [:pressed hit]))\n\n ;; ── view ──\n :draw (fn [{:keys [shown w h args]}]\n (let [p (or shown (let [n (max 1 (js/Math.round (or (:n args) 8)))\n k (api/clamp (js/Math.round (or (:k args) 3)) 0 n)\n ps (rotate (bjorklund k n) (or (:rotation args) 0))]\n {:k k :n n :pulses ps :onsets (onsets-of ps) :intervals (intervals-of ps)}))\n n (:n p) ps (:pulses p) os (:onsets p)\n cx (/ w 2) cy (/ (- h 30) 2) R (- (min cx cy) 16)\n r (cond (> n 32) 2.2 (> n 16) 3 :else 4.5)]\n (concat\n [[:circle {:cx cx :cy cy :r R :stroke :grid}]]\n ;; the start of the bar, at the top\n (let [[x1 y1] (polar cx cy (- R 9) 0 n) [x2 y2] (polar cx cy (+ R 9) 0 n)]\n [[:line {:x1 x1 :y1 y1 :x2 x2 :y2 y2 :stroke :kairos :weight 1.5 :opacity 0.8}]])\n (when (> (count os) 1) [[:path {:points (mapv (fn [i] (polar cx cy R i n)) os) :closed true :stroke :accent :weight 2}]])\n (for [i (range n)]\n (let [[x y] (polar cx cy R i n) on (= 1 (nth ps i 0))]\n [:group {:hit i}\n [[:circle {:cx x :cy y :r (if on (+ r 1.5) r) :fill (if on :accent :bg) :stroke (if on :accent :soft)}]]]))\n [[:text {:x cx :y (- h 15) :anchor :middle :size 10 :fill :ink}\n (str \"E(\" (:k p) \",\" n \")\" (when (pos? (or (:rotation p) 0)) (str \" ↻\" (:rotation p))))]\n [:text {:x cx :y (- h 3) :anchor :middle :size 9 :fill :soft}\n (if (seq (:intervals p)) (apply str (interpose \"-\" (:intervals p))) \"silence\")]])))})\n"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"class": "necklace",
|
|
17
|
+
"license": "MPL-2.0",
|
|
18
|
+
"doc": "A cyclic rhythm on the circle and its geometry (Toussaint): k onsets among n pulses around a circle, time clockwise from the top — the same object as a pitch-class set on pcclock. A rhythm arrives as pulses (ones and zeros, as euclid's right outlet sends them), as intervals between onsets ([3 3 2]), as box notation (\"x..x..x.\"), as an :rtm map ({:shares [3 3 2]}, euclid's middle outlet), a tree with an :rtm, or {:onsets [0 3 6] :n 8}; it is drawn as a polygon, with its axes of mirror symmetry. Pressing a pulse (or a number, its index) adds or takes away an onset. :complement swaps onsets and silences, [:rotate 1] starts it a pulse later, [:n 12] makes the circle longer or shorter, :clear empties it, [:bars 4] and [:meter \"3/4\"] as euclid, a bang sends it again. Out, as euclid: a musical tree (left: a bar of n pulses with an :rtm whose shares are the intervals between onsets), the :rtm map alone (1), the pulses (2); and its measures (3): {:n :k :onsets :intervals :ivec — how often each distance occurs —, :evenness — the sum of the chords between all pairs (Block and Douthett) —, :even — that sum against the Euclidean rhythm's, 1 as even as it —, :deep — each distance a different number of times —, :flat — each once —, :axes — of mirror symmetry, in pulses —, :gap — the longest interval —, :oddity — no two onsets halving the cycle —, :euclid — the rotation of E(k,n) it is, or nil}. Its right inlet (cold) takes the pitches the notes take in turn, as euclid's.",
|
|
19
|
+
"source": ";; necklace · a cyclic rhythm on the circle, and its geometry (Toussaint)\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; A rhythm that repeats is a necklace: k onsets among n pulses around a circle, time clockwise from the\n;; top. Seen so, it is the same object as a pitch-class set on pcclock, and the same geometry measures it\n;; (Toussaint, \"The Geometry of Musical Rhythm\"): its full-interval vector (how often each distance between\n;; onsets occurs), its evenness (the sum of the chords between all pairs of onsets, after Block and Douthett;\n;; arcs are too coarse: the six 4/4 claves would all score the same), whether it is deep (each distance a\n;; different number of times, as the diatonic scale and the bembé), flat (each distance once), its axes of\n;; mirror symmetry, its longest gap, rhythmic oddity (no two onsets halving the cycle), and whether it is a\n;; rotation of the Euclidean rhythm E(k,n). Pressing a pulse adds or takes away an onset: the necklace is\n;; also an editor. What it sends is as euclid's — a musical tree, its :rtm map, its pulses —, so it merges\n;; with pitches the same way (the right inlet, or merge with outlet 1), and its measures go out on the right.\n(let [TAU (* 2 js/Math.PI)\n bjorklund (fn [k n]\n (cond (<= n 0) []\n (<= k 0) (vec (repeat n 0))\n (>= k n) (vec (repeat n 1))\n :else (loop [a (vec (repeat k [1])) b (vec (repeat (- n k) [0]))]\n (if (<= (count b) 1)\n (vec (apply concat (concat a b)))\n (let [m (min (count a) (count b))]\n (recur (mapv into (subvec a 0 m) (subvec b 0 m))\n (if (> (count a) m) (subvec a m) (subvec b m))))))))\n rotate (fn [xs r] (let [n (count xs)] (if (zero? n) xs (let [r (mod r n)] (vec (concat (subvec xs r) (subvec xs 0 r)))))))\n onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))\n intervals-of (fn [ps] (let [os (onsets-of ps) n (count ps)]\n (if (empty? os) [] (mapv (fn [a b] (- b a)) os (conj (subvec os 1) (+ n (first os)))))))\n shares-of (fn [ps] (let [os (onsets-of ps) n (count ps)]\n (cond (zero? n) []\n (empty? os) [(- n)]\n :else (vec (concat (when (pos? (first os)) [(- (first os))])\n (map (fn [a b] (- b a)) os (conj (subvec os 1) n)))))))\n r3 (fn [x] (/ (js/Math.round (* 1000 x)) 1000))\n ;; ── what arrives, as pulses (ones and zeros), or nil when it is not a rhythm ──\n whole? (fn [x] (and (number? x) (== x (js/Math.round x))))\n from-shares (fn [sh] (when (and (sequential? sh) (seq sh) (every? (fn [x] (and (whole? x) (not (zero? x)))) sh))\n (vec (mapcat (fn [x] (if (pos? x) (cons 1 (repeat (dec x) 0)) (repeat (- x) 0))) sh))))\n from-onsets (fn [os n] (when (and (whole? n) (pos? n) (sequential? os) (every? whole? os))\n (let [on (set (map (fn [o] (mod o n)) os))] (mapv (fn [i] (if (on i) 1 0)) (range n)))))\n ;; the first :rtm of a tree (as euclid sends it)\n rtm-in (fn rtm-in [t] (when (sequential? t)\n (if (= :rtm (first t)) (second t) (some rtm-in (filter sequential? t)))))\n ;; a list: ones and zeros are pulses; other whole numbers, the intervals between onsets (3 3 2)\n pulses-of-list (fn [v] (when (and (sequential? v) (seq v) (every? whole? v))\n (if (every? #{0 1} v) (vec v) (when (every? pos? v) (from-shares v)))))\n pulses-of (fn [v]\n (cond\n ;; box notation (TUBS): \"x..x..x.\" (x, X or 1 an onset; . - _ or 0 a pulse without one)\n (string? v) (let [cs (remove #{\" \"} (seq v))]\n (when (and (seq cs) (every? #{\"x\" \"X\" \"1\" \".\" \"-\" \"_\" \"0\"} cs))\n (mapv (fn [c] (if (#{\"x\" \"X\" \"1\"} c) 1 0)) cs)))\n (map? v) (cond (contains? v :shares) (from-shares (:shares v))\n (contains? v :pulses) (pulses-of-list (:pulses v))\n (contains? v :onsets) (from-onsets (:onsets v) (:n v)))\n (and (sequential? v) (keyword? (first v))) (some-> (rtm-in v) :shares from-shares)\n :else nil))\n ;; ── its geometry ──\n dist (fn [n a b] (let [d (js/Math.abs (- a b))] (min d (- n d))))\n pairs (fn [os] (for [i (range (count os)) j (range (inc i) (count os))] [(nth os i) (nth os j)]))\n ivec-of (fn [n os] (let [c (frequencies (map (fn [[a b]] (dist n a b)) (pairs os)))]\n (mapv (fn [d] (get c d 0)) (range 1 (inc (quot n 2))))))\n evenness-of (fn [n os] (reduce + 0 (map (fn [[a b]] (* 2 (js/Math.sin (/ (* js/Math.PI (dist n a b)) n)))) (pairs os))))\n ;; the axes of mirror symmetry, in pulses: x → a − x keeps the set; the axis passes through a/2 (and a/2 + n/2)\n axes-of (fn [n os] (let [s (set os)]\n (if (empty? s) []\n (vec (for [a (range n) :when (= s (set (map (fn [x] (mod (- a x) n)) os)))] (/ a 2))))))\n measures (fn [ps]\n (let [n (count ps) os (onsets-of ps) k (count os) iv (ivec-of n os) ev (evenness-of n os)\n e (bjorklund k n) eev (evenness-of n (onsets-of e))\n er (first (filter (fn [r] (= ps (rotate e r))) (range (max n 1))))]\n {:n n :k k :onsets os :intervals (intervals-of ps) :ivec iv\n :evenness (r3 ev) :even (if (pos? eev) (r3 (/ ev eev)) 1)\n :deep (and (>= k 2) (= (count (distinct iv)) (count iv)))\n :flat (and (seq iv) (every? #{1} iv))\n :axes (axes-of n os)\n :gap (if (seq os) (apply max (intervals-of ps)) n)\n :oddity (or (odd? n) (not-any? (fn [[a b]] (= (* 2 (dist n a b)) n)) (pairs os)))\n :euclid er}))\n ;; ── as euclid sends a rhythm: the meter of a bar of n pulses, the pitches, the color over bars ──\n pow2? (fn [x] (and (pos? x) (== x (js/Math.pow 2 (js/Math.round (js/Math.log2 x))))))\n meter-of (fn [n] (cond (and (>= n 4) (pow2? n)) [4 4]\n (and (>= n 12) (zero? (mod n 3)) (pow2? (/ n 3))) [12 8]\n (< n 4) [n 4]\n (<= n 12) [n 8]\n :else [n 16]))\n meter-arg (fn [v] (cond (and (sequential? v) (= 2 (count v)) (every? number? v)) (vec v)\n (string? v) v\n (keyword? v) (name v)\n :else nil))\n hz->m (fn [x] (when (and (number? x) (pos? x)) (/ (js/Math.round (* 1000 (+ 69 (* 12 (js/Math.log2 (/ x 440)))))) 1000)))\n pitches-of (fn [midi? v]\n (let [p (fn [x] (if midi? x (hz->m x)))]\n (cond (#{:clear} v) nil\n (map? v) (cond (or (contains? v :pitches) (contains? v :pitch)) (select-keys v [:pitches :pitch])\n (sequential? (:note v)) {:pitches (mapv (fn [x] (if (number? x) x \"r\")) (:note v))}\n :else :ignore)\n (string? v) {:pitches v}\n (number? v) (if-let [m (p v)] {:pitch m} :ignore)\n (and (sequential? v) (keyword? (first v))) {:pitches [v]}\n (and (sequential? v) (seq v) (every? number? v)) (let [ms (vec (keep p v))] (if (seq ms) {:pitches ms} :ignore))\n (and (sequential? v) (seq v)) {:pitches (vec v)}\n :else :ignore)))\n tokens (fn [ps] (cond (string? ps) (vec (remove #{\"\"} (.split (.trim ps) (js/RegExp. \"\\\\s+\"))))\n (sequential? ps) (vec ps)\n :else nil))\n color-at (fn [pm skip]\n (let [ts (tokens (:pitches pm))]\n (if (or (empty? ts) (zero? (mod skip (count ts))))\n pm\n (let [r (mod skip (count ts)) ts2 (vec (concat (subvec ts r) (subvec ts 0 r)))]\n (assoc pm :pitches (if (string? (:pitches pm)) (apply str (interpose \" \" ts2)) ts2))))))\n rhythm (fn [ps st]\n (let [k (count (onsets-of ps)) bars (:bars st) meter (or (:meter st) (meter-of (count ps)))\n rtm (merge {:shares (shares-of ps)} (or (:pm st) {}))\n bar (fn [i] (let [a (color-at rtm (* i k))]\n (if (zero? i) [:measure {:meter meter} [:rtm a]] [:measure [:rtm a]])))]\n {:rtm rtm :tree (into [:voice] (map bar (range bars)))}))\n ;; what goes out (the last first, as Max does): the measures (3), the pulses (2), the :rtm map (1), the tree (0)\n outs (fn [ps st] (let [r (rhythm ps st)] [[3 (measures ps)] [2 ps] [1 (:rtm r)] [0 (:tree r)]]))\n ;; a new state: sent when the rhythm, its bars or its meter are new (the same again changes nothing); a bang anyway\n settle (fn [st st2 always?]\n (let [ps (:pulses st2) key [ps (:bars st2) (:meter st2)]\n show (assoc (measures ps) :pulses ps)]\n (cond (empty? ps) {:state st2}\n (and (not always?) (= key (:sent st))) {:state st2 :show show}\n :else {:state (assoc st2 :sent key) :show show :out (outs ps st2)})))\n polar (fn [cx cy R i n] (let [a (* TAU (/ i n))] [(+ cx (* R (js/Math.sin a))) (- cy (* R (js/Math.cos a)))]))]\n {;; ── about ──\n :class \"necklace\"\n :license \"MPL-2.0\"\n :doc \"A cyclic rhythm on the circle and its geometry (Toussaint): k onsets among n pulses around a circle, time clockwise from the top — the same object as a pitch-class set on pcclock. A rhythm arrives as pulses (ones and zeros, as euclid's right outlet sends them), as intervals between onsets ([3 3 2]), as box notation (\\\"x..x..x.\\\"), as an :rtm map ({:shares [3 3 2]}, euclid's middle outlet), a tree with an :rtm, or {:onsets [0 3 6] :n 8}; it is drawn as a polygon, with its axes of mirror symmetry. Pressing a pulse (or a number, its index) adds or takes away an onset. :complement swaps onsets and silences, [:rotate 1] starts it a pulse later, [:n 12] makes the circle longer or shorter, :clear empties it, [:bars 4] and [:meter \\\"3/4\\\"] as euclid, a bang sends it again. Out, as euclid: a musical tree (left: a bar of n pulses with an :rtm whose shares are the intervals between onsets), the :rtm map alone (1), the pulses (2); and its measures (3): {:n :k :onsets :intervals :ivec — how often each distance occurs —, :evenness — the sum of the chords between all pairs (Block and Douthett) —, :even — that sum against the Euclidean rhythm's, 1 as even as it —, :deep — each distance a different number of times —, :flat — each once —, :axes — of mirror symmetry, in pulses —, :gap — the longest interval —, :oddity — no two onsets halving the cycle —, :euclid — the rotation of E(k,n) it is, or nil}. Its right inlet (cold) takes the pitches the notes take in turn, as euclid's.\"\n :usage \"necklace [n 16] [bars 1] [midi]\"\n :io [2 4]\n :size [200 240]\n\n ;; ── arguments ──\n :args [[:n 16] [:bars 1] [:midi false]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args]}]\n (let [st (or state {:pulses (vec (repeat (max 1 (js/Math.round (or (:n args) 16))) 0))\n :bars (api/clamp (js/Math.round (or (:bars args) 1)) 1 64) :meter nil :pm nil})\n ps (:pulses st) n (count ps)\n to (fn [m] (settle st (merge st m) false))\n toggle (fn [i] (let [i (mod (js/Math.round i) n)] (to {:pulses (update ps i (fn [x] (- 1 x)))})))]\n (if (= inlet 1)\n (let [pm (pitches-of (:midi args) value)]\n (if (= pm :ignore) {} {:state (assoc st :pm pm)}))\n (cond\n (#{:bang} value) (settle st st true)\n (#{:clear} value) (to {:pulses (vec (repeat n 0))})\n (#{:complement} value) (to {:pulses (mapv (fn [x] (- 1 x)) ps)})\n (number? value) (toggle value)\n (and (vector? value) (= :pressed (first value)) (number? (second value))) (toggle (second value))\n (and (vector? value) (= :rotate (first value)) (number? (second value))) (to {:pulses (rotate ps (js/Math.round (second value)))})\n (and (vector? value) (= :n (first value)) (number? (second value)))\n (let [m (api/clamp (js/Math.round (second value)) 1 128)]\n (to {:pulses (vec (take m (concat ps (repeat 0))))}))\n (and (vector? value) (= :bars (first value)) (number? (second value))) (to {:bars (api/clamp (js/Math.round (second value)) 1 64)})\n (and (vector? value) (= :meter (first value))) (to {:meter (meter-arg (second value))})\n :else (if-let [p (or (pulses-of-list value) (pulses-of value))]\n (to {:pulses p})\n {})))))\n ;; pressing a pulse adds or takes away an onset (sent once, on release)\n :gesture (fn [{:keys [up hit]}] (when (and up (number? hit)) [:pressed hit]))\n\n ;; ── view ──\n :draw (fn [{:keys [shown w h args]}]\n (let [p (or shown (let [ps (vec (repeat (max 1 (js/Math.round (or (:n args) 16))) 0))] (assoc (measures ps) :pulses ps)))\n n (:n p) ps (:pulses p) os (:onsets p)\n cx (/ w 2) cy (/ (- h 44) 2) R (- (min cx cy) 16)\n r (cond (> n 32) 2.2 (> n 16) 3 :else 4.5)\n flags (remove nil? [(str \"even \" (:even p))\n (let [a (count (:axes p))] (case a 0 \"asymmetric\" 1 \"1 axis\" (str a \" axes\")))\n (when (:deep p) \"deep\") (when (:flat p) \"flat\")\n (when (and (even? n) (:oddity p) (pos? (:k p))) \"oddity\")\n (when (:euclid p) (str \"E(\" (:k p) \",\" n \")\" (when (pos? (:euclid p)) (str \" ↻\" (:euclid p)))))])]\n (concat\n [[:circle {:cx cx :cy cy :r R :stroke :grid}]]\n (let [[x1 y1] (polar cx cy (- R 9) 0 n) [x2 y2] (polar cx cy (+ R 9) 0 n)]\n [[:line {:x1 x1 :y1 y1 :x2 x2 :y2 y2 :stroke :kairos :weight 1.5 :opacity 0.8}]])\n ;; the axes of symmetry, across the circle\n (for [a (:axes p)]\n (let [[x1 y1] (polar cx cy (+ R 7) a n) [x2 y2] (polar cx cy (+ R 7) (+ a (/ n 2)) n)]\n [:line {:x1 x1 :y1 y1 :x2 x2 :y2 y2 :stroke :kairos :weight 1 :opacity 0.5}]))\n (when (> (count os) 1) [[:path {:points (mapv (fn [i] (polar cx cy R i n)) os) :closed true :stroke :accent :weight 2}]])\n (for [i (range n)]\n (let [[x y] (polar cx cy R i n) on (= 1 (nth ps i 0))]\n [:group {:hit i}\n [[:circle {:cx x :cy y :r (if on (+ r 1.5) r) :fill (if on :accent :bg) :stroke (if on :accent :soft)}]]]))\n [[:text {:x cx :y (- h 29) :anchor :middle :size 10 :fill :ink}\n (str (:k p) \"/\" n \" · \" (if (seq (:intervals p)) (apply str (interpose \"-\" (:intervals p))) \"silence\"))]\n [:text {:x cx :y (- h 16) :anchor :middle :size 9 :fill :soft} (str \"<\" (apply str (interpose \" \" (:ivec p))) \">\")]\n [:text {:x cx :y (- h 3) :anchor :middle :size 9 :fill :soft} (apply str (interpose \" · \" flags))]])))})\n"
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
};
|
|
@@ -5,10 +5,13 @@
|
|
|
5
5
|
// dissonance and the mode for each voice and between them, ties and suspensions, fourth species, the system as
|
|
6
6
|
// data, the degree of imitation, the comes inverted or augmented, the futures counted exactly over pairs). After it, what the box adds for playing (`live`): the soft
|
|
7
7
|
// rules of Study 41 as costs, the mode as a spec, and a note asked for (a pitch, any key) corrected to the nearest
|
|
8
|
-
// legal note with a future.
|
|
8
|
+
// legal note with a future. The soft rules are soft.mjs's, the module every engine of Kairotic weighs with; the box
|
|
9
|
+
// adds its own two (the mode, the imitation).
|
|
9
10
|
//
|
|
10
11
|
// A pitch is a diatonic step on the white keys, 0 = C4; the box speaks MIDI and Hz.
|
|
11
|
-
|
|
12
|
+
|
|
13
|
+
import * as soft from "./soft.mjs";
|
|
14
|
+
|
|
12
15
|
// ─────────────────────────────── the kernel (Study 43) ───────────────────────────────
|
|
13
16
|
|
|
14
17
|
const mod = (a, m) => ((a % m) + m) % m;
|
|
@@ -516,12 +519,8 @@ export const MODES = {
|
|
|
516
519
|
aeolian: { final: 5, tenor: 4, plagal: false }, hypoaeolian: { final: 5, tenor: 2, plagal: true },
|
|
517
520
|
};
|
|
518
521
|
|
|
519
|
-
/** The soft rules of Study 41 and their weights (kairotic/canon.cljs there). */
|
|
520
|
-
export const WEIGHTS =
|
|
521
|
-
"leap-size": 1, tie: 1.5, "suspension-life": 1.5, "contrary-after-leap": 1.2, "step-back": 1.2, "leap-after-leap": 0.6, return: 1,
|
|
522
|
-
hover: 0.6, run: 1.5, open: 1, climax: 1.5, sequence: 1.5, motion: 2.5, modal: 1, "direct-perfect": 3,
|
|
523
|
-
"imperfect-run": 2, passing: 1, alternation: 1.5, imitation: 1, memory: 1, novelty: 0.3,
|
|
524
|
-
};
|
|
522
|
+
/** The soft rules of Study 41 and their weights (kairotic/canon.cljs there): soft.mjs's, shared by every engine. */
|
|
523
|
+
export const WEIGHTS = soft.defaultWeights();
|
|
525
524
|
|
|
526
525
|
const LETTERS = ["C", "D", "E", "F", "G", "A", "B"];
|
|
527
526
|
const CHROMA = ["C", "C♯", "D", "E♭", "E", "F", "F♯", "G", "G♯", "A", "B♭", "B"];
|
|
@@ -531,10 +530,8 @@ export const midiOf = (s) => 60 + semis(s);
|
|
|
531
530
|
export const midiName = (m) => CHROMA[mod(Math.round(m), 12)] + (Math.floor(Math.round(m) / 12) - 1);
|
|
532
531
|
/** Semitones above C4 for a frequency. */
|
|
533
532
|
export const semisOfHz = (hz) => 12 * Math.log2(hz / 261.6255653005986);
|
|
534
|
-
const LEAP = { 0: 0, 1: 0, 2: 0.2, 3: 0.4, 4: 0.5, 5: 0.8, 7: 0.8 };
|
|
535
533
|
const bitsOf = (c) => (c > 0 ? Math.log2(c) : -1);
|
|
536
|
-
const
|
|
537
|
-
const total = (parts) => Object.values(parts).reduce((a, v) => a + v, 0);
|
|
534
|
+
const total = soft.total;
|
|
538
535
|
|
|
539
536
|
/**
|
|
540
537
|
* A canon to play live, note by note. o: {mode, k, d, beats, bars, upbeat, imitation, species, maxHold, spacing,
|
|
@@ -594,68 +591,22 @@ export function live(o = {}) {
|
|
|
594
591
|
const w = { ...WEIGHTS, ...(o.weights || {}) };
|
|
595
592
|
const strong = (t) => (t + phase) % b === 0;
|
|
596
593
|
|
|
594
|
+
// the soft rules: those every engine of Kairotic weighs with (soft.mjs), and the comes box's own (modal, imitation)
|
|
597
595
|
/** The soft cost of x after a line (its own shape), by rule; modal: whether the mode's notes count (the dux). */
|
|
598
596
|
function melodicParts(line, x, t, modal) {
|
|
599
|
-
const parts = {}
|
|
597
|
+
const parts = {};
|
|
600
598
|
if (modal && (mod(x, N) === mod(tonic, N) || mod(x, N) === mod(tenor, N))) parts.modal = -(w.modal * (b > 1 && strong(t) ? 1.5 : 1));
|
|
601
|
-
|
|
602
|
-
if (x === prev) { parts.tie = w.tie; return clean(parts); }
|
|
603
|
-
// the rest on the notes struck: a note held is one note
|
|
604
|
-
line = line.filter((y, i) => i === 0 || y !== line[i - 1]);
|
|
605
|
-
const tl2 = line.length, m2 = tl2 >= 2 ? prev - line[tl2 - 2] : null;
|
|
606
|
-
return struck(line, x, m2, parts);
|
|
607
|
-
}
|
|
608
|
-
function struck(line, x, m, parts) {
|
|
609
|
-
const tl = line.length, prev = line[tl - 1];
|
|
610
|
-
const s = x - prev, nn = Math.abs(s), all = [...line, x];
|
|
611
|
-
const mv = []; for (let i = 1; i < tl; i++) mv.push(line[i] - line[i - 1]); mv.push(s);
|
|
612
|
-
const L = mv.length;
|
|
613
|
-
if (nn > 0) parts["leap-size"] = w["leap-size"] * (LEAP[nn] ?? 1.5);
|
|
614
|
-
if (tl >= 2 && nn > 0 && x === line[tl - 2]) parts.return = w.return;
|
|
615
|
-
const hov = line.slice(0, -1).slice(-3).filter((y) => y === x).length;
|
|
616
|
-
if (hov) parts.hover = hov * w.hover;
|
|
617
|
-
if (m !== null && Math.abs(m) >= 2 && nn > 0 && sign(m) === sign(s)) parts["contrary-after-leap"] = w["contrary-after-leap"] * (nn >= 2 ? 2 : 1);
|
|
618
|
-
if (m !== null && Math.abs(m) >= 3 && nn > 1) parts["step-back"] = w["step-back"];
|
|
619
|
-
if (m !== null && Math.abs(m) >= 2 && nn >= 2) parts["leap-after-leap"] = w["leap-after-leap"];
|
|
620
|
-
if (nn > 0) { let run = 0; for (let i = L - 1; i >= 0 && mv[i] !== 0 && sign(mv[i]) === sign(s); i--) run++; if (run > 4) parts.run = w.run; }
|
|
621
|
-
if (tl >= 5 && Math.max(...all) - Math.min(...all) < 4) parts.open = w.open;
|
|
622
|
-
if (tl > 1 && s > 0 && x === Math.max(...line)) parts.climax = w.climax;
|
|
623
|
-
const same = (i, j, len) => { for (let q = 0; q < len; q++) if (mv[i + q] !== mv[j + q]) return false; return true; };
|
|
624
|
-
if (L >= 4 && mv[L - 1] === mv[L - 3] && mv[L - 2] === mv[L - 4] && mv[L - 1] !== mv[L - 2]) parts.sequence = w.sequence;
|
|
625
|
-
else if (L >= 6 && same(L - 3, L - 6, 3)) parts.sequence = (2 / 3) * w.sequence;
|
|
626
|
-
else if (m !== null && nn > 0) { for (let i = 0; i < L - 3; i++) if (mv[i] === m && mv[i + 1] === s) { parts.memory = -w.memory; break; } }
|
|
627
|
-
if (m !== null && m === s && nn > 0) parts.novelty = w.novelty;
|
|
628
|
-
return clean(parts);
|
|
599
|
+
return Object.assign(parts, soft.melodicParts(line, x, w));
|
|
629
600
|
}
|
|
630
601
|
|
|
631
602
|
/** The soft cost of the vertical [x c] at the next time, by rule. */
|
|
632
603
|
function verticalParts(D, C, x, c) {
|
|
633
|
-
const
|
|
634
|
-
if (c === null ||
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
parts.motion = 0.8 * w.motion;
|
|
640
|
-
const cls = mod(h - l, N);
|
|
641
|
-
if (quality(l, h) === "perfect" && cls === fifth && cls !== 0) parts["direct-perfect"] = w["direct-perfect"];
|
|
642
|
-
let run = 1;
|
|
643
|
-
for (let i = t - 1; i >= 1; i--) {
|
|
644
|
-
const p = D[i], q = C[i], p0 = D[i - 1], q0 = C[i - 1];
|
|
645
|
-
if (q === null || q0 === null) break;
|
|
646
|
-
const [ll, hh] = K.lowerFirst(p, q);
|
|
647
|
-
if (quality(ll, hh) === "imperfect" && mod(hh - ll, N) === cls && sign(p - p0) === sign(q - q0) && p !== p0) run++; else break;
|
|
648
|
-
}
|
|
649
|
-
if (quality(l, h) === "imperfect" && run > 3) parts["imperfect-run"] = w["imperfect-run"];
|
|
650
|
-
}
|
|
651
|
-
if (b > 1 && !strong(t) && K.dissonant(x, c)) parts.passing = -w.passing;
|
|
652
|
-
if (K.suspensionAt([...D, x], [...C, c], t)) parts["suspension-life"] = -w["suspension-life"];
|
|
653
|
-
if (t >= 2) {
|
|
654
|
-
const vc = (i) => (C[i] === null ? null : mod(Math.abs(D[i] - C[i]), N));
|
|
655
|
-
const iv = mod(Math.abs(x - c), N), i1 = vc(t - 1), i2 = vc(t - 2);
|
|
656
|
-
if (i1 !== null && i2 !== null && iv === i2 && iv !== i1) parts.alternation = w.alternation;
|
|
657
|
-
}
|
|
658
|
-
return clean(parts);
|
|
604
|
+
const t = D.length;
|
|
605
|
+
if (c === null || !t || C[t - 1] === null) return {};
|
|
606
|
+
return soft.verticalParts(D, C, x, c, {
|
|
607
|
+
N, strong, quality, fifth, lowerFirst: K.lowerFirst, dissonant: K.dissonant,
|
|
608
|
+
suspension: K.suspensionAt([...D, x], [...C, c], t),
|
|
609
|
+
}, w);
|
|
659
610
|
}
|
|
660
611
|
|
|
661
612
|
/** What the comes's c costs for how it imitates the dux, by the degree. */
|
|
@@ -680,18 +631,6 @@ export function live(o = {}) {
|
|
|
680
631
|
return parts;
|
|
681
632
|
}
|
|
682
633
|
|
|
683
|
-
/** The comes's best note with the dux's x next: {note, futures, cost, parts}, or null when none has a future. */
|
|
684
|
-
/** A choice among candidates by cost: at temperature 0 (or without a random number r in [0, 1)) the cheapest;
|
|
685
|
-
* above it a softmax of -cost/temperature, drawn with r. */
|
|
686
|
-
function softPick(cands, cost, temp, r) {
|
|
687
|
-
const cs = cands.slice().sort((p, q) => cost(p) - cost(q) || (p.note ?? -100) - (q.note ?? -100));
|
|
688
|
-
if (!cs.length || !(temp > 0.001) || r === undefined || r === null) return cs[0] || null;
|
|
689
|
-
const m = cost(cs[0]), ws = cs.map((c) => Math.exp((m - cost(c)) / temp)), x = r * ws.reduce((a, v) => a + v, 0);
|
|
690
|
-
let acc = 0;
|
|
691
|
-
for (let j = 0; j < cs.length; j++) { acc += ws[j]; if (x < acc) return cs[j]; }
|
|
692
|
-
return cs[cs.length - 1];
|
|
693
|
-
}
|
|
694
|
-
|
|
695
634
|
/** The comes's note with the dux's x next: the cheapest (at temperature 0), or one drawn by temp with r. */
|
|
696
635
|
function bestComes(D, C, x, temp = 0, r = null) {
|
|
697
636
|
const cands = [];
|
|
@@ -700,7 +639,7 @@ export function live(o = {}) {
|
|
|
700
639
|
const parts = pairParts(D, C, x, e.note);
|
|
701
640
|
cands.push({ note: e.note, futures: e.futures, cost: total(parts), parts });
|
|
702
641
|
}
|
|
703
|
-
return
|
|
642
|
+
return soft.pick(cands, (c) => c.cost, temp, r);
|
|
704
643
|
}
|
|
705
644
|
|
|
706
645
|
/** The dux's candidates next: {note, reason} or {note, futures, cost, comes, score} (score: its cost and the
|
|
@@ -761,7 +700,7 @@ export function live(o = {}) {
|
|
|
761
700
|
/** The engine's own next pair, as play gives it, or null: the most plausible, or with opt {temp, r, r2} the
|
|
762
701
|
* dux's note drawn by temp with r among the futuribles (by their score), the comes's with r2. */
|
|
763
702
|
function engine(D, C, opt = {}) {
|
|
764
|
-
const f =
|
|
703
|
+
const f = soft.pick(futuribles(D, C), (c) => c.score, opt.temp, opt.r);
|
|
765
704
|
return f ? play(D, C, semis(f.note), { temp: opt.temp, r: opt.r2 }) : null;
|
|
766
705
|
}
|
|
767
706
|
|