@r_masseater/song-format 0.0.0 → 0.2.0

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.
Files changed (53) hide show
  1. package/LICENSE +87 -0
  2. package/README.md +92 -1
  3. package/dist/analysis.d.mts +95 -0
  4. package/dist/analysis.mjs +328 -0
  5. package/dist/analysis.mjs.map +1 -0
  6. package/dist/convert-als.d.mts +18 -0
  7. package/dist/convert-als.mjs +504 -0
  8. package/dist/convert-als.mjs.map +1 -0
  9. package/dist/convert-midi.d.mts +18 -0
  10. package/dist/convert-midi.mjs +273 -0
  11. package/dist/convert-midi.mjs.map +1 -0
  12. package/dist/fumx-language-server.mjs +23 -0
  13. package/dist/fumx-language-server.mjs.map +1 -0
  14. package/dist/fumx-module-CmHFC-eI.mjs +12513 -0
  15. package/dist/fumx-module-CmHFC-eI.mjs.map +1 -0
  16. package/dist/fumx-module-DJQrnEVm.mjs +2660 -0
  17. package/dist/fumx-module-DJQrnEVm.mjs.map +1 -0
  18. package/dist/grammar-86_uhiTo.mjs +9836 -0
  19. package/dist/grammar-86_uhiTo.mjs.map +1 -0
  20. package/dist/index-Cup9xTad.d.mts +105 -0
  21. package/dist/index-DPt8MZ6e.d.mts +796 -0
  22. package/dist/index-JXjnMHXw.d.mts +1350 -0
  23. package/dist/index.d.mts +9 -0
  24. package/dist/index.mjs +4 -0
  25. package/dist/language.d.mts +13 -0
  26. package/dist/language.mjs +3 -0
  27. package/dist/mapper.mjs +14 -0
  28. package/dist/mapper.mjs.map +1 -0
  29. package/dist/midi-to-fumx.mjs +1223 -0
  30. package/dist/midi-to-fumx.mjs.map +1 -0
  31. package/dist/read-song-CNrfcHrY.mjs +123 -0
  32. package/dist/read-song-CNrfcHrY.mjs.map +1 -0
  33. package/dist/read-song-CSIhaAEB.mjs +122 -0
  34. package/dist/read-song-CSIhaAEB.mjs.map +1 -0
  35. package/dist/run-main-Bf32CgzU.mjs +26 -0
  36. package/dist/run-main-Bf32CgzU.mjs.map +1 -0
  37. package/dist/song-3Zm9g84K.d.mts +1478 -0
  38. package/dist/song-CYFBSTbH.mjs +387 -0
  39. package/dist/song-CYFBSTbH.mjs.map +1 -0
  40. package/dist/song-report.mjs +351 -0
  41. package/dist/song-report.mjs.map +1 -0
  42. package/dist/ticks-CXehHP2w.mjs +27 -0
  43. package/dist/ticks-CXehHP2w.mjs.map +1 -0
  44. package/dist/transform-DQm5wTlA.mjs +489 -0
  45. package/dist/transform-DQm5wTlA.mjs.map +1 -0
  46. package/dist/vite.d.mts +3 -0
  47. package/dist/vite.mjs +128 -0
  48. package/dist/vite.mjs.map +1 -0
  49. package/dist/write-CS-ZjoQB.mjs +746 -0
  50. package/dist/write-CS-ZjoQB.mjs.map +1 -0
  51. package/package.json +127 -6
  52. package/skills/read-fumx/SKILL.md +86 -0
  53. package/skills/use-fumx-tools/SKILL.md +65 -0
package/LICENSE ADDED
@@ -0,0 +1,87 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 masseater
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ @tanstack/eslint-plugin-start
26
+
27
+ MIT License
28
+
29
+ Copyright (c) 2021-present Tanner Linsley
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining a copy
32
+ of this software and associated documentation files (the "Software"), to deal
33
+ in the Software without restriction, including without limitation the rights
34
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
35
+ copies of the Software, and to permit persons to whom the Software is
36
+ furnished to do so, subject to the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be included in all
39
+ copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
44
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
46
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
47
+ SOFTWARE.
48
+
49
+ ---
50
+
51
+ @typescript-eslint/utils
52
+
53
+ MIT License
54
+
55
+ Copyright (c) 2019 typescript-eslint and other contributors
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining a copy
58
+ of this software and associated documentation files (the "Software"), to deal
59
+ in the Software without restriction, including without limitation the rights
60
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
61
+ copies of the Software, and to permit persons to whom the Software is
62
+ furnished to do so, subject to the following conditions:
63
+
64
+ The above copyright notice and this permission notice shall be included in all
65
+ copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
68
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
69
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
70
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
71
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
72
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
73
+ SOFTWARE.
74
+
75
+ ---
76
+
77
+ globals
78
+
79
+ MIT License
80
+
81
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
82
+
83
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
84
+
85
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
86
+
87
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,94 @@
1
1
  # @r_masseater/song-format
2
2
 
3
- This version only reserves the name. Releases come from CI.
3
+ Tooling for `.fumx` songs: import a song from TypeScript with Vite, edit it with a language server, convert a MIDI file to `.fumx` with a check against the original, and print a report of its keys, chords and repeated bars.
4
+
5
+ It requires [Vite+](https://viteplus.dev/) and Node.js 26 or later. `effect` and `vite-plus` are peer dependencies. It also holds the grammar and the reader of `.fumx`.
6
+
7
+ ```sh
8
+ vp add @r_masseater/song-format effect vite-plus
9
+ ```
10
+
11
+ ## Read a song
12
+
13
+ ```ts
14
+ import { readSong } from "@r_masseater/song-format";
15
+ import { Effect } from "effect";
16
+
17
+ const text = `song "lint me"
18
+
19
+ instrument lead sample "lead.flac" root C4
20
+
21
+ track lead instrument lead
22
+
23
+ bar meter 4/4 tempo 120bpm {
24
+ lead: C4 1/4, E4 1/4, B5 1/2 vel 0.9
25
+ lead.cc 64: 127 1/2, 0 1/2
26
+ }
27
+ `;
28
+
29
+ const source = await Effect.runPromise(
30
+ readSong({ path: "/song.fumx", read: () => Effect.succeed(text) }),
31
+ );
32
+ if (source.status === "valid") {
33
+ const tooHigh = source.song.notes.filter((note) => note.midi > 79);
34
+ }
35
+ ```
36
+
37
+ `readSong` follows `import` statements through `read`, so the files can come from disk, memory or a network. An invalid file gives `issues` with a path, line and column. The messages are in Japanese. `outline.comments` lists the `//` and `/* */` comments of the entry file with their line and column, so tools such as linters can read directives from them.
38
+
39
+ Times are integer ticks, `TICKS_PER_WHOLE` to a whole note; `fractionOf` prints them as `1/4`. `@r_masseater/song-format/analysis` estimates what the notes imply but the file does not write: the key of a range of bars (`keyIn`), the chord on each beat (`chordsOf`) and bars that repeat. `analyze` returns all of them for a song, and `soundingsOf` lists the notes of unmuted tracks with their track role, source line and column, and the spelling and pitch class as written. Chord names follow that spelling, so `E G C` is `CM/E`, not `Em#5`.
40
+
41
+ `@r_masseater/song-format/language` exports the Langium grammar and modules for editors and language servers.
42
+
43
+ ## Import a song
44
+
45
+ Add the Vite plugin. TypeScript then reads a compiled song as a value.
46
+
47
+ ```ts
48
+ import { fumx } from "@r_masseater/song-format/vite";
49
+ import { defineConfig } from "vite-plus";
50
+
51
+ export default defineConfig({ plugins: [fumx()] });
52
+ ```
53
+
54
+ ```ts
55
+ import { song } from "./song.fumx";
56
+ ```
57
+
58
+ The plugin writes `song.d.fumx.ts` next to each song. For the TypeScript server to follow the import, list the package as a content mapper in `tsconfig.json`.
59
+
60
+ ```json
61
+ {
62
+ "compilerOptions": { "allowArbitraryExtensions": true },
63
+ "contentMappers": [{ "package": "@r_masseater/song-format", "extensions": [".fumx"] }]
64
+ }
65
+ ```
66
+
67
+ ## Commands
68
+
69
+ `song-report <file>` prints, as JSON, the keys, chords and repeated bars derived from a song file. A reader that cannot listen to the song checks what was written from this output.
70
+
71
+ `midi-to-fumx <file>` converts a Standard MIDI File to `.fumx`. It reads the written `.fumx` back, compares it with the events of the original MIDI, and fails if even one differs.
72
+
73
+ `fumx-language-server` serves the `.fumx` language over stdio for editors.
74
+
75
+ ```sh
76
+ vp exec song-report song.fumx
77
+ vp exec midi-to-fumx song.mid
78
+ ```
79
+
80
+ ## Convert from code
81
+
82
+ ```ts
83
+ import { midiToFumx } from "@r_masseater/song-format/convert-midi";
84
+ ```
85
+
86
+ `midiToFumx` takes a parsed MIDI file and returns the `.fumx` text. `compareWithMidi` reports how the written song differs from the original.
87
+
88
+ ## Agent skills
89
+
90
+ The package ships agent skills in `skills/`. Add `permissions: { "@r_masseater/song-format": { skills: true } }` to `pnpm-workspace.yaml` and `vp install` links them into the skill directories of your agents.
91
+
92
+ ## License
93
+
94
+ [MIT](LICENSE)
@@ -0,0 +1,95 @@
1
+ import "./index-DPt8MZ6e.mjs";
2
+ import { d as Song, u as Role } from "./song-3Zm9g84K.mjs";
3
+ import { Option, Schema } from "effect";
4
+ export declare const AnalysisSchema: Schema.Struct<{
5
+ readonly bars: Schema.Int;
6
+ readonly key: Schema.OptionFromNullOr<Schema.Struct<{
7
+ readonly fromBar: Schema.Int;
8
+ readonly toBar: Schema.Int;
9
+ readonly tonic: Schema.String;
10
+ readonly pitchClass: Schema.Int;
11
+ readonly mode: Schema.Literals<readonly ["major", "minor"]>;
12
+ readonly correlation: Schema.Finite;
13
+ }>>;
14
+ readonly keys: Schema.$Array<Schema.Struct<{
15
+ readonly fromBar: Schema.Int;
16
+ readonly toBar: Schema.Int;
17
+ readonly tonic: Schema.String;
18
+ readonly pitchClass: Schema.Int;
19
+ readonly mode: Schema.Literals<readonly ["major", "minor"]>;
20
+ readonly correlation: Schema.Finite;
21
+ }>>;
22
+ readonly chords: Schema.$Array<Schema.Struct<{
23
+ readonly from: Schema.Struct<{
24
+ readonly bar: Schema.Int;
25
+ readonly offset: Schema.String;
26
+ }>;
27
+ readonly to: Schema.Struct<{
28
+ readonly bar: Schema.Int;
29
+ readonly offset: Schema.String;
30
+ }>;
31
+ readonly start: Schema.Int;
32
+ readonly end: Schema.Int;
33
+ readonly symbol: Schema.String;
34
+ readonly notes: Schema.$Array<Schema.String>;
35
+ }>>;
36
+ readonly repeats: Schema.$Array<Schema.Struct<{
37
+ fromBar: Schema.Int;
38
+ toBar: Schema.Int;
39
+ }>>;
40
+ }>;
41
+ type Analysis = typeof AnalysisSchema.Type;
42
+ export declare const analyze: (song: Song) => Analysis;
43
+ export declare const ChordSpanSchema: Schema.Struct<{
44
+ readonly from: Schema.Struct<{
45
+ readonly bar: Schema.Int;
46
+ readonly offset: Schema.String;
47
+ }>;
48
+ readonly to: Schema.Struct<{
49
+ readonly bar: Schema.Int;
50
+ readonly offset: Schema.String;
51
+ }>;
52
+ readonly start: Schema.Int;
53
+ readonly end: Schema.Int;
54
+ readonly symbol: Schema.String;
55
+ readonly notes: Schema.$Array<Schema.String>;
56
+ }>;
57
+ type ChordSpan = typeof ChordSpanSchema.Type;
58
+ export declare const chordsOf: (song: Song) => readonly ChordSpan[];
59
+ export declare const BarRangeSchema: Schema.Struct<{
60
+ fromBar: Schema.Int;
61
+ toBar: Schema.Int;
62
+ }>;
63
+ type BarRange = typeof BarRangeSchema.Type;
64
+ export declare const KeySpanSchema: Schema.Struct<{
65
+ readonly fromBar: Schema.Int;
66
+ readonly toBar: Schema.Int;
67
+ readonly tonic: Schema.String;
68
+ readonly pitchClass: Schema.Int;
69
+ readonly mode: Schema.Literals<readonly ["major", "minor"]>;
70
+ readonly correlation: Schema.Finite;
71
+ }>;
72
+ type KeySpan = typeof KeySpanSchema.Type;
73
+ export declare const keyIn: (song: Song) => (range: BarRange) => Option.Option<KeySpan>;
74
+ interface Span {
75
+ readonly start: number;
76
+ readonly end: number;
77
+ }
78
+ interface Sounding extends Span {
79
+ readonly track: string;
80
+ readonly line: number;
81
+ readonly column: number;
82
+ readonly bar: number;
83
+ readonly role: Option.Option<Role>;
84
+ readonly midi: number;
85
+ readonly spelling: string;
86
+ readonly pitchClass: string;
87
+ }
88
+ export declare const overlapOf: (left: Span) => (right: Span) => number;
89
+ export declare const soundingsOf: (song: Song) => readonly Sounding[];
90
+ export declare const positionOf: (song: Song) => (tick: number) => {
91
+ readonly bar: number;
92
+ readonly offset: string;
93
+ };
94
+ export type { Analysis, BarRange, ChordSpan, KeySpan, Sounding, Span };
95
+ //# sourceMappingURL=analysis.d.mts.map
@@ -0,0 +1,328 @@
1
+ import { n as fractionOf, t as TICKS_PER_WHOLE } from "./ticks-CXehHP2w.mjs";
2
+ import { Array, Option, Order, Record, Schema, String as String$1, pipe } from "effect";
3
+ import { Chord, Note } from "tonal";
4
+ // src/features/analyze-song/model/sounding.ts
5
+ const NONE$2 = 0;
6
+ const FIRST$3 = 1;
7
+ const CENTS = /[+-]\d+c$/u;
8
+ const pitchClassOf$1 = (note) => {
9
+ const written = Note.pitchClass(String$1.replace(CENTS, "")(note.spelling));
10
+ if (String$1.isNonEmpty(written)) return written;
11
+ return Note.pitchClass(Note.fromMidi(note.midi));
12
+ };
13
+ const overlapOf = (left) => (right) => Math.max(NONE$2, Math.min(left.end, right.end) - Math.max(left.start, right.start));
14
+ const soundingsOf = (song) => {
15
+ const audible = pipe(song.tracks, Array.filter((track) => !track.mixer.muted));
16
+ return pipe(song.notes, Array.flatMap((note) => pipe(audible, Array.findFirst((track) => track.name === note.track), Option.map((track) => ({
17
+ track: note.track,
18
+ line: note.sourceLine,
19
+ column: note.sourceColumn,
20
+ bar: note.bar,
21
+ role: track.role,
22
+ midi: note.midi,
23
+ spelling: note.spelling,
24
+ pitchClass: pitchClassOf$1(note),
25
+ start: note.start,
26
+ end: note.start + Math.round(note.length * note.gate)
27
+ })), Option.toArray)));
28
+ };
29
+ const positionOf = (song) => (tick) => pipe(song.bars, Array.findLast((bar) => bar.start <= tick), Option.match({
30
+ onNone: () => ({
31
+ bar: FIRST$3,
32
+ offset: fractionOf(tick)
33
+ }),
34
+ onSome: (bar) => ({
35
+ bar: bar.index,
36
+ offset: fractionOf(tick - bar.start)
37
+ })
38
+ }));
39
+ // src/features/analyze-song/model/chords.ts
40
+ const FIRST$2 = 1;
41
+ const HELD_SHARE = .25;
42
+ const UNKNOWN$1 = "?";
43
+ const PositionSchema = Schema.Struct({
44
+ bar: Schema.Int,
45
+ offset: Schema.String
46
+ });
47
+ const ChordSpanSchema = Schema.Struct({
48
+ from: PositionSchema,
49
+ to: PositionSchema,
50
+ start: Schema.Int,
51
+ end: Schema.Int,
52
+ symbol: Schema.String,
53
+ notes: Schema.Array(Schema.String)
54
+ });
55
+ const heldIn = (window) => (note) => overlapOf(note)(window) >= (window.end - window.start) * HELD_SHARE;
56
+ const isHarmonic = (note) => Option.exists(note.role, (role) => role === "bass" || role === "harmony");
57
+ const OCTAVE$1 = 12;
58
+ const UNISON = 0;
59
+ const samePitchClass = (left, right) => (left - right) % OCTAVE$1 === UNISON;
60
+ const byMidi = Order.mapInput(Order.Number, (note) => note.midi);
61
+ const harmonicOrAll = (held) => {
62
+ const harmonic = pipe(held, Array.filter(isHarmonic));
63
+ if (Array.isReadonlyArrayNonEmpty(harmonic)) return harmonic;
64
+ return held;
65
+ };
66
+ const chordNotesIn = (notes) => (window) => pipe(notes, Array.filter(heldIn(window)), harmonicOrAll, (held) => Array.sort(held, byMidi), Array.dedupeWith((left, right) => samePitchClass(left.midi, right.midi)), Array.map((note) => note.pitchClass));
67
+ const spells = (notes) => (symbol) => {
68
+ const spelled = Chord.get(symbol).notes;
69
+ return spelled.length === notes.length && pipe(spelled, Array.every((note) => Array.contains(notes, note)));
70
+ };
71
+ const symbolOf = (notes) => {
72
+ const candidates = Chord.detect(Array.copy(notes));
73
+ return pipe(candidates, Array.findFirst(spells(notes)), Option.orElse(() => Array.head(candidates)), Option.getOrElse(() => UNKNOWN$1));
74
+ };
75
+ const beatWindowsOf = (song) => pipe(song.bars, Array.flatMap((bar) => {
76
+ const beat = TICKS_PER_WHOLE / bar.meter.denominator;
77
+ return Array.makeBy(bar.meter.numerator, (index) => ({
78
+ start: bar.start + index * beat,
79
+ end: bar.start + (index + FIRST$2) * beat
80
+ }));
81
+ }));
82
+ const extend = (spans, beat) => Option.match(Array.last(spans), {
83
+ onNone: () => [beat],
84
+ onSome: (last) => {
85
+ if (last.symbol !== beat.symbol || last.end !== beat.start) return [...spans, beat];
86
+ return [...Array.dropRight(spans, FIRST$2), {
87
+ ...last,
88
+ end: beat.end
89
+ }];
90
+ }
91
+ });
92
+ const beatChordOf = (notes) => (window) => {
93
+ const held = chordNotesIn(notes)(window);
94
+ if (!Array.isReadonlyArrayNonEmpty(held)) return Option.none();
95
+ return Option.some({
96
+ ...window,
97
+ notes: held,
98
+ symbol: symbolOf(held)
99
+ });
100
+ };
101
+ const chordsOf = (song) => {
102
+ const notes = soundingsOf(song);
103
+ const beats = pipe(beatWindowsOf(song), Array.map(beatChordOf(notes)), Array.getSomes);
104
+ const at = positionOf(song);
105
+ return pipe(Array.reduce(beats, [], extend), Array.map((span) => ({
106
+ from: at(span.start),
107
+ to: at(span.end),
108
+ start: span.start,
109
+ end: span.end,
110
+ symbol: span.symbol,
111
+ notes: span.notes
112
+ })));
113
+ };
114
+ // src/features/analyze-song/model/keys.ts
115
+ const NONE$1 = 0;
116
+ const OCTAVE = 12;
117
+ const SQUARE = 2;
118
+ const ROUND = 1e3;
119
+ const UNKNOWN = "?";
120
+ const MAJOR_PROFILE = {
121
+ tonic: 6.35,
122
+ minorSecond: 2.23,
123
+ majorSecond: 3.48,
124
+ minorThird: 2.33,
125
+ majorThird: 4.38,
126
+ fourth: 4.09,
127
+ tritone: 2.52,
128
+ fifth: 5.19,
129
+ minorSixth: 2.39,
130
+ majorSixth: 3.66,
131
+ minorSeventh: 2.29,
132
+ majorSeventh: 2.88
133
+ };
134
+ const MINOR_PROFILE = {
135
+ tonic: 6.33,
136
+ minorSecond: 2.68,
137
+ majorSecond: 3.52,
138
+ minorThird: 5.38,
139
+ majorThird: 2.6,
140
+ fourth: 3.53,
141
+ tritone: 2.54,
142
+ fifth: 4.75,
143
+ minorSixth: 3.98,
144
+ majorSixth: 2.69,
145
+ minorSeventh: 3.34,
146
+ majorSeventh: 3.17
147
+ };
148
+ const MAJOR_TONICS = [
149
+ "C",
150
+ "Db",
151
+ "D",
152
+ "Eb",
153
+ "E",
154
+ "F",
155
+ "F#",
156
+ "G",
157
+ "Ab",
158
+ "A",
159
+ "Bb",
160
+ "B"
161
+ ];
162
+ const MINOR_TONICS = [
163
+ "C",
164
+ "C#",
165
+ "D",
166
+ "Eb",
167
+ "E",
168
+ "F",
169
+ "F#",
170
+ "G",
171
+ "G#",
172
+ "A",
173
+ "Bb",
174
+ "B"
175
+ ];
176
+ const ModeSchema = Schema.Literals(["major", "minor"]);
177
+ const barRangeFields = {
178
+ fromBar: Schema.Int,
179
+ toBar: Schema.Int
180
+ };
181
+ const BarRangeSchema = Schema.Struct(barRangeFields);
182
+ const KeySpanSchema = Schema.Struct({
183
+ ...barRangeFields,
184
+ tonic: Schema.String,
185
+ pitchClass: Schema.Int,
186
+ mode: ModeSchema,
187
+ correlation: Schema.Finite
188
+ });
189
+ const pitchClassOf = (midi) => (midi % OCTAVE + OCTAVE) % OCTAVE;
190
+ const addWeight = (window) => (weights, note) => {
191
+ const share = overlapOf(note)(window);
192
+ const pitchClass = pitchClassOf(note.midi);
193
+ return pipe(weights, Array.map((weight, index) => {
194
+ if (index === pitchClass) return weight + share;
195
+ return weight;
196
+ }));
197
+ };
198
+ const histogramOf = (window) => (notes) => Array.reduce(notes, Array.makeBy(OCTAVE, () => NONE$1), addWeight(window));
199
+ const add = (sum, value) => sum + value;
200
+ const sumOf = (values) => Array.reduce(values, NONE$1, add);
201
+ const meanOf = (values) => sumOf(values) / values.length;
202
+ const spreadOf = (values) => {
203
+ const center = meanOf(values);
204
+ const squares = pipe(values, Array.map((value) => (value - center) ** SQUARE));
205
+ return Math.sqrt(sumOf(squares));
206
+ };
207
+ const correlationOf = (profile) => (histogram) => {
208
+ const histogramMean = meanOf(histogram);
209
+ const profileMean = meanOf(profile);
210
+ const covariance = sumOf(pipe(Array.zip(histogram, profile), Array.map(([weight, expected]) => (weight - histogramMean) * (expected - profileMean))));
211
+ const scale = spreadOf(histogram) * spreadOf(profile);
212
+ if (scale === NONE$1) return NONE$1;
213
+ return covariance / scale;
214
+ };
215
+ const rotated = (tonic) => (profile) => {
216
+ const weights = Record.values(profile);
217
+ return Array.makeBy(OCTAVE, (index) => weights[(index - tonic + OCTAVE) % OCTAVE] ?? NONE$1);
218
+ };
219
+ const candidatesAt = (histogram) => (tonic) => [{
220
+ tonic: MAJOR_TONICS[tonic] ?? UNKNOWN,
221
+ pitchClass: tonic,
222
+ mode: "major",
223
+ correlation: correlationOf(rotated(tonic)(MAJOR_PROFILE))(histogram)
224
+ }, {
225
+ tonic: MINOR_TONICS[tonic] ?? UNKNOWN,
226
+ pitchClass: tonic,
227
+ mode: "minor",
228
+ correlation: correlationOf(rotated(tonic)(MINOR_PROFILE))(histogram)
229
+ }];
230
+ const byCorrelation = Order.flip(Order.mapInput(Order.Number, (candidate) => candidate.correlation));
231
+ const edgeOf = (song) => (index, end) => pipe(song.bars, Array.findFirst((bar) => bar.index === index), Option.match({
232
+ onNone: () => NONE$1,
233
+ onSome: (bar) => bar.start + Number(end) * bar.length
234
+ }));
235
+ const keyIn = (song) => (range) => {
236
+ const edge = edgeOf(song);
237
+ const histogram = histogramOf({
238
+ start: edge(range.fromBar, false),
239
+ end: edge(range.toBar, true)
240
+ })(soundingsOf(song));
241
+ if (pipe(histogram, Array.every((weight) => weight === NONE$1))) return Option.none();
242
+ const candidates = pipe(Array.range(NONE$1, 11), Array.flatMap(candidatesAt(histogram)));
243
+ return pipe(Array.sort(candidates, byCorrelation), Array.head, Option.map((best) => ({
244
+ fromBar: range.fromBar,
245
+ toBar: range.toBar,
246
+ tonic: best.tonic,
247
+ pitchClass: best.pitchClass,
248
+ mode: best.mode,
249
+ correlation: Math.round(best.correlation * ROUND) / ROUND
250
+ })));
251
+ };
252
+ // src/features/analyze-song/model/repeats.ts
253
+ const FIRST$1 = 1;
254
+ const SECOND = 2;
255
+ const fingerprintOf = (song) => (bar) => {
256
+ const notes = pipe(song.notes, Array.filter((note) => note.bar === bar), Array.map((note) => `${note.track}:${String(note.offset)}:${String(note.midi)}:${String(note.length)}`));
257
+ const hits = pipe(song.hits, Array.filter((hit) => hit.bar === bar), Array.map((hit) => `${hit.track}:${String(hit.offset)}:${hit.voice}`));
258
+ const slices = pipe(song.slices, Array.filter((slice) => slice.bar === bar), Array.map((slice) => `${slice.track}:${String(slice.offset)}:${slice.sample}:${String(slice.index)}`));
259
+ return pipe(Array.sort([
260
+ ...notes,
261
+ ...hits,
262
+ ...slices
263
+ ], Order.String), Array.join("|"));
264
+ };
265
+ const repeatOf = (prints) => (bar) => {
266
+ const print = prints[bar - FIRST$1] ?? "";
267
+ return print !== "" && print === prints[bar - SECOND];
268
+ };
269
+ const joinRun = (runs, bar) => Option.match(Array.last(runs), {
270
+ onNone: () => [{
271
+ fromBar: bar - FIRST$1,
272
+ toBar: bar
273
+ }],
274
+ onSome: (last) => {
275
+ if (last.toBar !== bar - FIRST$1) return [...runs, {
276
+ fromBar: bar - FIRST$1,
277
+ toBar: bar
278
+ }];
279
+ return [...Array.dropRight(runs, FIRST$1), {
280
+ ...last,
281
+ toBar: bar
282
+ }];
283
+ }
284
+ });
285
+ const repeatsOf = (song) => {
286
+ const bars = song.bars.length;
287
+ const prints = pipe(Array.range(FIRST$1, bars), Array.map(fingerprintOf(song)));
288
+ const repeated = pipe(Array.range(SECOND, bars), Array.filter(repeatOf(prints)));
289
+ return Array.reduce(repeated, [], joinRun);
290
+ };
291
+ // src/features/analyze-song/model/analysis.ts
292
+ const FIRST = 1;
293
+ const NONE = 0;
294
+ const PHRASE_BARS = 4;
295
+ const AnalysisSchema = Schema.Struct({
296
+ bars: Schema.Int,
297
+ key: Schema.OptionFromNullOr(KeySpanSchema),
298
+ keys: Schema.Array(KeySpanSchema),
299
+ chords: Schema.Array(ChordSpanSchema),
300
+ repeats: Schema.Array(BarRangeSchema)
301
+ });
302
+ const phrasesOf = (bars) => Array.makeBy(Math.ceil(bars / PHRASE_BARS), (phrase) => ({
303
+ fromBar: phrase * PHRASE_BARS + FIRST,
304
+ toBar: Math.min(bars, (phrase + FIRST) * PHRASE_BARS)
305
+ }));
306
+ const analyze = (song) => {
307
+ const bars = song.bars.length;
308
+ if (bars === NONE) return {
309
+ bars,
310
+ key: Option.none(),
311
+ keys: [],
312
+ chords: [],
313
+ repeats: []
314
+ };
315
+ return {
316
+ bars,
317
+ key: keyIn(song)({
318
+ fromBar: FIRST,
319
+ toBar: bars
320
+ }),
321
+ keys: pipe(phrasesOf(bars), Array.map(keyIn(song)), Array.getSomes),
322
+ chords: chordsOf(song),
323
+ repeats: repeatsOf(song)
324
+ };
325
+ };
326
+ export { AnalysisSchema, BarRangeSchema, ChordSpanSchema, KeySpanSchema, analyze, chordsOf, keyIn, overlapOf, positionOf, soundingsOf };
327
+
328
+ //# sourceMappingURL=analysis.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"analysis.mjs","names":["NONE","FIRST","pitchClassOf","TonalNote","Str","Arr","FIRST","UNKNOWN","OCTAVE","Arr","NONE","Arr","FIRST","Arr","Arr"],"sources":["../src/features/analyze-song/model/sounding.ts","../src/features/analyze-song/model/chords.ts","../src/features/analyze-song/model/keys.ts","../src/features/analyze-song/model/repeats.ts","../src/features/analyze-song/model/analysis.ts"],"sourcesContent":["import { Array as Arr, Option, String as Str, pipe } from \"effect\";\nimport { Note as TonalNote } from \"tonal\";\n\nimport type { Note, Role, Song } from \"#/entities/song/index.ts\";\nimport { fractionOf } from \"#/shared/lib/ticks.ts\";\n\nconst NONE = 0;\nconst FIRST = 1;\n\nexport interface Span {\n readonly start: number;\n readonly end: number;\n}\n\nexport interface Sounding extends Span {\n readonly track: string;\n readonly line: number;\n readonly column: number;\n readonly bar: number;\n readonly role: Option.Option<Role>;\n readonly midi: number;\n readonly spelling: string;\n readonly pitchClass: string;\n}\n\nconst CENTS = /[+-]\\d+c$/u;\n\nconst pitchClassOf = (note: Note): string => {\n const written = TonalNote.pitchClass(Str.replace(CENTS, \"\")(note.spelling));\n if (Str.isNonEmpty(written)) {\n return written;\n }\n return TonalNote.pitchClass(TonalNote.fromMidi(note.midi));\n};\n\nexport const overlapOf =\n (left: Span) =>\n (right: Span): number =>\n Math.max(NONE, Math.min(left.end, right.end) - Math.max(left.start, right.start));\n\nexport const soundingsOf = (song: Song): readonly Sounding[] => {\n const audible = pipe(\n song.tracks,\n Arr.filter((track) => !track.mixer.muted),\n );\n return pipe(\n song.notes,\n Arr.flatMap((note) =>\n pipe(\n audible,\n Arr.findFirst((track) => track.name === note.track),\n Option.map((track) => ({\n track: note.track,\n line: note.sourceLine,\n column: note.sourceColumn,\n bar: note.bar,\n role: track.role,\n midi: note.midi,\n spelling: note.spelling,\n pitchClass: pitchClassOf(note),\n start: note.start,\n end: note.start + Math.round(note.length * note.gate),\n })),\n Option.toArray,\n ),\n ),\n );\n};\n\nexport const positionOf =\n (song: Song) =>\n (tick: number): { readonly bar: number; readonly offset: string } =>\n pipe(\n song.bars,\n Arr.findLast((bar) => bar.start <= tick),\n Option.match({\n onNone: () => ({ bar: FIRST, offset: fractionOf(tick) }),\n onSome: (bar) => ({ bar: bar.index, offset: fractionOf(tick - bar.start) }),\n }),\n );\n","import { Array as Arr, Option, Order, Schema, pipe } from \"effect\";\nimport { Chord } from \"tonal\";\n\nimport type { Song } from \"#/entities/song/index.ts\";\nimport { TICKS_PER_WHOLE } from \"#/shared/lib/ticks.ts\";\n\nimport { overlapOf, positionOf, soundingsOf } from \"./sounding.ts\";\nimport type { Sounding, Span } from \"./sounding.ts\";\n\nconst FIRST = 1;\nconst HELD_SHARE = 0.25;\nconst UNKNOWN = \"?\";\n\nconst PositionSchema = Schema.Struct({ bar: Schema.Int, offset: Schema.String });\n\nexport const ChordSpanSchema = Schema.Struct({\n from: PositionSchema,\n to: PositionSchema,\n start: Schema.Int,\n end: Schema.Int,\n symbol: Schema.String,\n notes: Schema.Array(Schema.String),\n});\n\nexport type ChordSpan = typeof ChordSpanSchema.Type;\n\ninterface BeatChord extends Span {\n readonly symbol: string;\n readonly notes: readonly string[];\n}\n\nconst heldIn =\n (window: Span) =>\n (note: Sounding): boolean =>\n overlapOf(note)(window) >= (window.end - window.start) * HELD_SHARE;\n\nconst isHarmonic = (note: Sounding): boolean =>\n Option.exists(note.role, (role) => role === \"bass\" || role === \"harmony\");\n\nconst OCTAVE = 12;\n\nconst UNISON = 0;\n\nconst samePitchClass = (left: number, right: number): boolean => (left - right) % OCTAVE === UNISON;\n\nconst byMidi = Order.mapInput(Order.Number, (note: Sounding) => note.midi);\n\nconst harmonicOrAll = (held: readonly Sounding[]): readonly Sounding[] => {\n const harmonic = pipe(held, Arr.filter(isHarmonic));\n if (Arr.isReadonlyArrayNonEmpty(harmonic)) {\n return harmonic;\n }\n return held;\n};\n\nconst chordNotesIn =\n (notes: readonly Sounding[]) =>\n (window: Span): readonly string[] =>\n pipe(\n notes,\n Arr.filter(heldIn(window)),\n harmonicOrAll,\n (held) => Arr.sort(held, byMidi),\n Arr.dedupeWith((left, right) => samePitchClass(left.midi, right.midi)),\n Arr.map((note) => note.pitchClass),\n );\n\nconst spells =\n (notes: readonly string[]) =>\n (symbol: string): boolean => {\n const spelled = Chord.get(symbol).notes;\n return (\n spelled.length === notes.length &&\n pipe(\n spelled,\n Arr.every((note) => Arr.contains(notes, note)),\n )\n );\n };\n\nconst symbolOf = (notes: readonly string[]): string => {\n const candidates = Chord.detect(Arr.copy(notes));\n return pipe(\n candidates,\n Arr.findFirst(spells(notes)),\n Option.orElse(() => Arr.head(candidates)),\n Option.getOrElse(() => UNKNOWN),\n );\n};\n\nconst beatWindowsOf = (song: Song): readonly Span[] =>\n pipe(\n song.bars,\n Arr.flatMap((bar) => {\n const beat = TICKS_PER_WHOLE / bar.meter.denominator;\n return Arr.makeBy(bar.meter.numerator, (index) => ({\n start: bar.start + index * beat,\n end: bar.start + (index + FIRST) * beat,\n }));\n }),\n );\n\nconst extend = (spans: readonly BeatChord[], beat: BeatChord): readonly BeatChord[] =>\n Option.match(Arr.last(spans), {\n onNone: () => [beat],\n onSome: (last) => {\n if (last.symbol !== beat.symbol || last.end !== beat.start) {\n return [...spans, beat];\n }\n return [...Arr.dropRight(spans, FIRST), { ...last, end: beat.end }];\n },\n });\n\nconst beatChordOf =\n (notes: readonly Sounding[]) =>\n (window: Span): Option.Option<BeatChord> => {\n const held = chordNotesIn(notes)(window);\n if (!Arr.isReadonlyArrayNonEmpty(held)) {\n return Option.none();\n }\n return Option.some({ ...window, notes: held, symbol: symbolOf(held) });\n };\n\nexport const chordsOf = (song: Song): readonly ChordSpan[] => {\n const notes = soundingsOf(song);\n const beats = pipe(beatWindowsOf(song), Arr.map(beatChordOf(notes)), Arr.getSomes);\n const at = positionOf(song);\n return pipe(\n Arr.reduce(beats, [] as readonly BeatChord[], extend),\n Arr.map((span) => ({\n from: at(span.start),\n to: at(span.end),\n start: span.start,\n end: span.end,\n symbol: span.symbol,\n notes: span.notes,\n })),\n );\n};\n","import { Array as Arr, Option, Order, Record, Schema, pipe } from \"effect\";\n\nimport type { Song } from \"#/entities/song/index.ts\";\n\nimport { overlapOf, soundingsOf } from \"./sounding.ts\";\nimport type { Sounding, Span } from \"./sounding.ts\";\n\nconst FIRST = 1;\nconst NONE = 0;\nconst OCTAVE = 12;\nconst SQUARE = 2;\nconst ROUND = 1000;\nconst UNKNOWN = \"?\";\n\nconst MAJOR_PROFILE = {\n tonic: 6.35,\n minorSecond: 2.23,\n majorSecond: 3.48,\n minorThird: 2.33,\n majorThird: 4.38,\n fourth: 4.09,\n tritone: 2.52,\n fifth: 5.19,\n minorSixth: 2.39,\n majorSixth: 3.66,\n minorSeventh: 2.29,\n majorSeventh: 2.88,\n};\n\nconst MINOR_PROFILE = {\n tonic: 6.33,\n minorSecond: 2.68,\n majorSecond: 3.52,\n minorThird: 5.38,\n majorThird: 2.6,\n fourth: 3.53,\n tritone: 2.54,\n fifth: 4.75,\n minorSixth: 3.98,\n majorSixth: 2.69,\n minorSeventh: 3.34,\n majorSeventh: 3.17,\n};\n\nconst MAJOR_TONICS = [\"C\", \"Db\", \"D\", \"Eb\", \"E\", \"F\", \"F#\", \"G\", \"Ab\", \"A\", \"Bb\", \"B\"];\nconst MINOR_TONICS = [\"C\", \"C#\", \"D\", \"Eb\", \"E\", \"F\", \"F#\", \"G\", \"G#\", \"A\", \"Bb\", \"B\"];\n\nconst ModeSchema = Schema.Literals([\"major\", \"minor\"]);\n\ntype Mode = typeof ModeSchema.Type;\n\nconst barRangeFields = { fromBar: Schema.Int, toBar: Schema.Int };\n\nexport const BarRangeSchema = Schema.Struct(barRangeFields);\n\nexport type BarRange = typeof BarRangeSchema.Type;\n\nexport const KeySpanSchema = Schema.Struct({\n ...barRangeFields,\n tonic: Schema.String,\n pitchClass: Schema.Int,\n mode: ModeSchema,\n correlation: Schema.Finite,\n});\n\nexport type KeySpan = typeof KeySpanSchema.Type;\n\ninterface Candidate {\n readonly tonic: string;\n readonly pitchClass: number;\n readonly mode: Mode;\n readonly correlation: number;\n}\n\nconst pitchClassOf = (midi: number): number => ((midi % OCTAVE) + OCTAVE) % OCTAVE;\n\nconst addWeight =\n (window: Span) =>\n (weights: readonly number[], note: Sounding): readonly number[] => {\n const share = overlapOf(note)(window);\n const pitchClass = pitchClassOf(note.midi);\n return pipe(\n weights,\n Arr.map((weight, index) => {\n if (index === pitchClass) {\n return weight + share;\n }\n return weight;\n }),\n );\n };\n\nconst histogramOf =\n (window: Span) =>\n (notes: readonly Sounding[]): readonly number[] =>\n Arr.reduce(\n notes,\n Arr.makeBy(OCTAVE, () => NONE),\n addWeight(window),\n );\n\nconst add = (sum: number, value: number): number => sum + value;\n\nconst sumOf = (values: readonly number[]): number => Arr.reduce(values, NONE, add);\n\nconst meanOf = (values: readonly number[]): number => sumOf(values) / values.length;\n\nconst spreadOf = (values: readonly number[]): number => {\n const center = meanOf(values);\n const squares = pipe(\n values,\n Arr.map((value) => (value - center) ** SQUARE),\n );\n return Math.sqrt(sumOf(squares));\n};\n\nconst correlationOf =\n (profile: readonly number[]) =>\n (histogram: readonly number[]): number => {\n const histogramMean = meanOf(histogram);\n const profileMean = meanOf(profile);\n const covariance = sumOf(\n pipe(\n Arr.zip(histogram, profile),\n Arr.map(([weight, expected]) => (weight - histogramMean) * (expected - profileMean)),\n ),\n );\n const scale = spreadOf(histogram) * spreadOf(profile);\n if (scale === NONE) {\n return NONE;\n }\n return covariance / scale;\n };\n\nconst rotated =\n (tonic: number) =>\n (profile: Readonly<Record<string, number>>): readonly number[] => {\n const weights = Record.values(profile);\n return Arr.makeBy(OCTAVE, (index) => weights[(index - tonic + OCTAVE) % OCTAVE] ?? NONE);\n };\n\nconst candidatesAt =\n (histogram: readonly number[]) =>\n (tonic: number): readonly Candidate[] => [\n {\n tonic: MAJOR_TONICS[tonic] ?? UNKNOWN,\n pitchClass: tonic,\n mode: \"major\",\n correlation: correlationOf(rotated(tonic)(MAJOR_PROFILE))(histogram),\n },\n {\n tonic: MINOR_TONICS[tonic] ?? UNKNOWN,\n pitchClass: tonic,\n mode: \"minor\",\n correlation: correlationOf(rotated(tonic)(MINOR_PROFILE))(histogram),\n },\n ];\n\nconst byCorrelation = Order.flip(\n Order.mapInput(Order.Number, (candidate: Candidate) => candidate.correlation),\n);\n\nconst edgeOf =\n (song: Song) =>\n (index: number, end: boolean): number =>\n pipe(\n song.bars,\n Arr.findFirst((bar) => bar.index === index),\n Option.match({\n onNone: () => NONE,\n onSome: (bar) => bar.start + Number(end) * bar.length,\n }),\n );\n\nexport const keyIn =\n (song: Song) =>\n (range: BarRange): Option.Option<KeySpan> => {\n const edge = edgeOf(song);\n const histogram = histogramOf({\n start: edge(range.fromBar, false),\n end: edge(range.toBar, true),\n })(soundingsOf(song));\n const silent = pipe(\n histogram,\n Arr.every((weight) => weight === NONE),\n );\n if (silent) {\n return Option.none();\n }\n const candidates = pipe(Arr.range(NONE, OCTAVE - FIRST), Arr.flatMap(candidatesAt(histogram)));\n return pipe(\n Arr.sort(candidates, byCorrelation),\n Arr.head,\n Option.map((best) => ({\n fromBar: range.fromBar,\n toBar: range.toBar,\n tonic: best.tonic,\n pitchClass: best.pitchClass,\n mode: best.mode,\n correlation: Math.round(best.correlation * ROUND) / ROUND,\n })),\n );\n };\n","import { Array as Arr, Option, Order, pipe } from \"effect\";\n\nimport type { Song } from \"#/entities/song/index.ts\";\n\nimport type { BarRange } from \"./keys.ts\";\n\nconst FIRST = 1;\nconst SECOND = 2;\n\nconst fingerprintOf =\n (song: Song) =>\n (bar: number): string => {\n const notes = pipe(\n song.notes,\n Arr.filter((note) => note.bar === bar),\n Arr.map(\n (note) =>\n `${note.track}:${String(note.offset)}:${String(note.midi)}:${String(note.length)}`,\n ),\n );\n const hits = pipe(\n song.hits,\n Arr.filter((hit) => hit.bar === bar),\n Arr.map((hit) => `${hit.track}:${String(hit.offset)}:${hit.voice}`),\n );\n const slices = pipe(\n song.slices,\n Arr.filter((slice) => slice.bar === bar),\n Arr.map(\n (slice) => `${slice.track}:${String(slice.offset)}:${slice.sample}:${String(slice.index)}`,\n ),\n );\n return pipe(Arr.sort([...notes, ...hits, ...slices], Order.String), Arr.join(\"|\"));\n };\n\nconst repeatOf =\n (prints: readonly string[]) =>\n (bar: number): boolean => {\n const print = prints[bar - FIRST] ?? \"\";\n return print !== \"\" && print === prints[bar - SECOND];\n };\n\nconst joinRun = (runs: readonly BarRange[], bar: number): readonly BarRange[] =>\n Option.match(Arr.last(runs), {\n onNone: () => [{ fromBar: bar - FIRST, toBar: bar }],\n onSome: (last) => {\n if (last.toBar !== bar - FIRST) {\n return [...runs, { fromBar: bar - FIRST, toBar: bar }];\n }\n return [...Arr.dropRight(runs, FIRST), { ...last, toBar: bar }];\n },\n });\n\nexport const repeatsOf = (song: Song): readonly BarRange[] => {\n const bars = song.bars.length;\n const prints = pipe(Arr.range(FIRST, bars), Arr.map(fingerprintOf(song)));\n const repeated = pipe(Arr.range(SECOND, bars), Arr.filter(repeatOf(prints)));\n return Arr.reduce(repeated, [] as readonly BarRange[], joinRun);\n};\n","import { Array as Arr, Option, Schema, pipe } from \"effect\";\n\nimport type { Song } from \"#/entities/song/index.ts\";\n\nimport { ChordSpanSchema, chordsOf } from \"./chords.ts\";\nimport { BarRangeSchema, KeySpanSchema, keyIn } from \"./keys.ts\";\nimport type { BarRange } from \"./keys.ts\";\nimport { repeatsOf } from \"./repeats.ts\";\n\nconst FIRST = 1;\nconst NONE = 0;\nconst PHRASE_BARS = 4;\n\nexport const AnalysisSchema = Schema.Struct({\n bars: Schema.Int,\n key: Schema.OptionFromNullOr(KeySpanSchema),\n keys: Schema.Array(KeySpanSchema),\n chords: Schema.Array(ChordSpanSchema),\n repeats: Schema.Array(BarRangeSchema),\n});\n\nexport type Analysis = typeof AnalysisSchema.Type;\n\nconst phrasesOf = (bars: number): readonly BarRange[] =>\n Arr.makeBy(Math.ceil(bars / PHRASE_BARS), (phrase) => ({\n fromBar: phrase * PHRASE_BARS + FIRST,\n toBar: Math.min(bars, (phrase + FIRST) * PHRASE_BARS),\n }));\n\nexport const analyze = (song: Song): Analysis => {\n const bars = song.bars.length;\n if (bars === NONE) {\n return { bars, key: Option.none(), keys: [], chords: [], repeats: [] };\n }\n return {\n bars,\n key: keyIn(song)({ fromBar: FIRST, toBar: bars }),\n keys: pipe(phrasesOf(bars),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AM,CAAC,CAAC;CAC3E,OAAOA,MAAI,OAAO,UAAU,CAAC,GAA0B,OAAO;AAChE;;ACjDA,MAAM,QAAQ;AACd,MAAM,OAAO;AACb,MAAM,cAAc;AAEpB,MAAa,iBAAiB,OAAO,OAAO;CAC1C,MAAM,OAAO;CACb,KAAK,OAAO,iBAAiB,aAAa;CAC1C,MAAM,OAAO,MAAM,aAAa;CAChC,QAAQ,OAAO,MAAM,eAAe;CACpC,SAAS,OAAO,MAAM,cAAc;AACtC,CAAC;AAID,MAAM,aAAa,SACjBC,MAAI,OAAO,KAAK,KAAK,OAAO,WAAW,IAAI,YAAY;CACrD,SAAS,SAAS,cAAc;CAChC,OAAO,KAAK,IAAI,OAAO,SAAS,SAAS,WAAW;AACtD,EAAE;AAEJ,MAAa,WAAW,SAAyB;CAC/C,MAAM,OAAO,KAAK,KAAK;CACvB,IAAI,SAAS,MACX,OAAO;EAAE;EAAM,KAAK,OAAO,KAAK;EAAG,MAAM,CAAC;EAAG,QAAQ,CAAC;EAAG,SAAS,CAAC;CAAE;CAEvE,OAAO;EACL;EACA,KAAK,MAAM,IAAI,CAAC,CAAC;GAAE,SAAS;GAAO,OAAO;EAAK,CAAC;EAChD,MAAM,KAAK,UAAU,IAAI,GAAGA,MAAI,IAAI,MAAM,IAAI,CAAC,GAAGA,MAAI,QAAQ;EAC9D,QAAQ,SAAS,IAAI;EACrB,SAAS,UAAU,IAAI;CACzB;AACF"}
@@ -0,0 +1,18 @@
1
+ import { t as Written } from "./index-Cup9xTad.mjs";
2
+ import { Effect, Schema } from "effect";
3
+ declare const UnreadableAls_base: Schema.Class<UnreadableAls, Schema.TaggedStruct<"UnreadableAls", {
4
+ readonly message: Schema.String;
5
+ }>, import("effect/Cause").YieldableError & {
6
+ readonly brand: unique symbol;
7
+ }>;
8
+ export declare class UnreadableAls extends UnreadableAls_base {}
9
+ interface LiveSetFile {
10
+ readonly bytes: Uint8Array;
11
+ readonly title: string;
12
+ }
13
+ interface Fumx extends Written {
14
+ readonly offGrid: number;
15
+ }
16
+ export declare const alsToFumx: (input: LiveSetFile) => Effect.Effect<Fumx, UnreadableAls, never>;
17
+ export type { Fumx, LiveSetFile };
18
+ //# sourceMappingURL=convert-als.d.mts.map