-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
cof-module.ts
executable file
·260 lines (220 loc) · 10 KB
/
cof-module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
namespace cof {
interface NoteCircleState {
noteSegments: d3.Selection<Segment>;
noteText: d3.Selection<Segment>;
intervalSegments: d3.Selection<Segment>;
intervalText: d3.Selection<Segment>;
intervalNotes: d3.Selection<Segment>;
chordText: d3.Selection<Segment>;
chordSegments: d3.Selection<Segment>;
chordNotes: d3.Selection<Segment>;
}
export class NoteCircle {
indexer: (x: Segment) => string = (x) => x.index + "";
constructor(svg: d3.Selection<any>, noteIndexes: number[], label: string) {
let state = this.draw(svg, rotate(noteIndexes, 3), label);
let setCToNoonSubscriptionIndex = -1;
events.scaleChange.subscribe(scaleChnaged => {
this.update(scaleChnaged, state);
setCToNoonSubscriptionIndex = events.setCToNoon.resubscribe(setCToNoonEvent => {
let offset = setCToNoonEvent.isC ? 3 : 0;
svg.selectAll("*").remove();
state = this.draw(svg, rotate(noteIndexes, offset), label);
this.update(scaleChnaged, state);
}, setCToNoonSubscriptionIndex);
});
}
draw (svg: d3.Selection<any>, noteIndexes: number[], label: string) : NoteCircleState {
let pad = 50;
let chordRadius = 240;
let noteRadius = 200;
let degreeRadius = 135;
let innerRadius = 90;
let cof = svg
.append("g")
.attr("transform", "translate(" + (noteRadius + pad) + ", " + (noteRadius + pad) + ")");
cof.append("text")
.attr("text-anchor", "middle")
.attr("x", 0)
.attr("y", 0)
.text(label)
let segments = generateSegments(noteIndexes);
let noteArc = d3.svg.arc<Segment>()
.innerRadius(degreeRadius)
.outerRadius(noteRadius);
let degreeArc = d3.svg.arc<Segment>()
.innerRadius(innerRadius)
.outerRadius(degreeRadius);
let chordArc = d3.svg.arc<Segment>()
.innerRadius(noteRadius)
.outerRadius(chordRadius);
let noteSegments = cof.append("g").selectAll("path")
.data(segments, this.indexer)
.enter()
.append("path")
.attr("d", noteArc)
.attr("class", "note-segment")
.on("click", handleNoteClick);
let noteText = cof.append("g").selectAll("text")
.data(segments)
.enter()
.append("text")
.attr("x", function (x) { return noteArc.centroid(x)[0]; })
.attr("y", function (x) { return noteArc.centroid(x)[1] + 11; })
.text("")
.attr("class", "note-segment-text");
let intervalSegments = cof.append("g").selectAll("path")
.data(segments, this.indexer)
.enter()
.append("path")
.attr("d", degreeArc)
.attr("class", "interval-segment")
.on("click", handleIntervalClick);
let intervalNotes = cof.append("g").selectAll("circle")
.data(segments, this.indexer)
.enter()
.append("circle")
.style("pointer-events", "none")
.attr("r", 25)
.attr("cx", function (x) { return degreeArc.centroid(x)[0]; })
.attr("cy", function (x) { return degreeArc.centroid(x)[1]; })
.attr("class", "interval-note")
let intervalText = cof.append("g").selectAll("text")
.data(segments, this.indexer)
.enter()
.append("text")
.attr("x", function (x) { return degreeArc.centroid(x)[0]; })
.attr("y", function (x) { return degreeArc.centroid(x)[1] + 8; })
.text("")
.attr("class", "degree-segment-text");
let chordSegments = cof.append("g").selectAll("path")
.data(segments, this.indexer)
.enter()
.append("path")
.attr("d", chordArc)
.attr("class", "chord-segment")
.on("click", handleChordClick);
let chordNotes = cof.append("g").selectAll("circle")
.data(segments, this.indexer)
.enter()
.append("circle")
.style("pointer-events", "none")
.attr("r", 28)
.attr("cx", function (x) { return chordArc.centroid(x)[0]; })
.attr("cy", function (x) { return chordArc.centroid(x)[1]; })
.attr("class", "chord-segment-note");
let chordText = cof.append("g").selectAll("text")
.data(segments, this.indexer)
.enter()
.append("text")
.attr("x", function (x) { return chordArc.centroid(x)[0]; })
.attr("y", function (x) { return chordArc.centroid(x)[1] + 8; })
.text("")
.attr("class", "degree-segment-text");
return {
noteSegments: noteSegments,
noteText: noteText,
intervalSegments: intervalSegments,
intervalNotes: intervalNotes,
intervalText: intervalText,
chordSegments: chordSegments,
chordNotes: chordNotes,
chordText: chordText
};
}
update(scaleChnaged: events.ScaleChangedEvent, state: NoteCircleState): void {
let data: Segment[] = scaleChnaged.nodes.map(node => <Segment>{
startAngle: 0,
endAngle: 0,
scaleNote: {},
index: node.scaleNote.note.index,
node: node
});
state.noteSegments
.data(data, this.indexer)
.attr("class", (d, i) => "note-segment " +
(d.node.scaleNote.isScaleNote ? ((i === 0) ? "note-segment-tonic" : "note-segment-scale") : ""));
state.noteText
.data(data, this.indexer)
.text(d => d.node.scaleNote.note.label);
state.intervalSegments
.data(data, this.indexer)
.attr("class", d => d.node.scaleNote.isScaleNote ? "degree-segment-selected" : "interval-segment");
state.intervalText
.data(data, this.indexer)
.text(d => d.node.intervalName);
state.intervalNotes
.data(data, this.indexer)
.attr("class", d => d.node.toggle ? "interval-note-selected" : "interval-note")
.style("fill", d => d.node.toggle ? "#" + d.node.chordInterval.colour.toString(16) : "none")
.style("stroke-width", d => d.node.midiToggle ? "20px" : "2px")
.style("stroke", d => d.node.midiToggle ? "OrangeRed" : d.node.toggle ? "black" : "none");
state.chordText
.data(data, this.indexer)
.text(d => d.node.scaleNote.chord!.romanNumeral + "");
state.chordSegments
.data(data, this.indexer)
.attr("class", d => d.node.scaleNote.isScaleNote ? getChordSegmentClass(d.node.scaleNote.chord!) : "chord-segment");
state.chordNotes
.data(data, this.indexer)
.attr("class", d => d.node.isChordRoot ? getChordSegmentClass(d.node.scaleNote.chord!) : "chord-segment-note");
}
}
function getChordSegmentClass(chord: music.Chord): string {
if (chord.type === music.ChordType.Diminished) return "chord-segment-dim";
if (chord.type === music.ChordType.Augmented) return "chord-segment-aug";
if (chord.type === music.ChordType.Minor) return "chord-segment-minor";
if (chord.type === music.ChordType.Major) return "chord-segment-major";
throw "Unexpected ChordType";
}
function generateSegments(fifths: number[]): Segment[] {
let count = fifths.length;
let items: Array<Segment> = [];
let angle = (Math.PI * (2 / count));
for (let i: number = 0; i < count; i++) {
let itemAngle = (angle * i) - (angle / 2);
items.push({
startAngle: itemAngle,
endAngle: itemAngle + angle,
index: fifths[i],
node: music.nullNode
});
}
return items;
}
function handleNoteClick(segment: Segment, i: number): void {
events.tonicChange.publish({
noteSpec: replaceDoubleSharpsAndFlatsWithEquivalentNote(segment.node.scaleNote.note)
});
}
function replaceDoubleSharpsAndFlatsWithEquivalentNote(noteSpec: music.NoteSpec): music.NoteSpec {
if(Math.abs(noteSpec.offset) > 1) {
let naturalId = noteSpec.natural.id;
let newNaturalId = (noteSpec.offset > 0)
? naturalId + 1 % 7
: naturalId == 0 ? 6 : naturalId - 1;
let newNatural = music.naturals.filter(x => x.id === newNaturalId)[0];
return music.createNoteSpec(newNatural.index, noteSpec.index)
}
return noteSpec;
}
function handleChordClick(segment: Segment, i: number): void {
events.chordChange.publish({ chordIndex: segment.node.scaleNote.note.index });
}
function handleIntervalClick(segment: Segment, i: number): void {
events.toggle.publish({ index: segment.node.scaleNote.note.index });
}
function rotate(array: number[], offset: number): number[] {
let newArray: number[] = [];
for(let item of array) {
newArray.push((item + offset) % 12);
}
return newArray;
}
interface Segment {
readonly startAngle: number;
readonly endAngle: number;
readonly index: number;
readonly node: music.Node;
}
}