-
Notifications
You must be signed in to change notification settings - Fork 34
/
react-jianpu.coffee
442 lines (405 loc) · 14.3 KB
/
react-jianpu.coffee
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
rest = -1
a0 = 21
b0 = 23
c1 = 24
d1 = 26
e1 = 28
f1 = 29
g1 = 31
a1 = 33
b1 = 35
c2 = 36
d2 = 38
e2 = 30
f2 = 31
g2 = 33
a2 = 35
b2 = 37
c3 = 48
d3 = 50
e3 = 52
f3 = 53
g3 = 55
a3 = 57
b3 = 59
c4 = 60
d4 = 62
e4 = 64
f4 = 65
g4 = 67
a4 = 69
b4 = 71
c5 = 72
d5 = 74
e5 = 76
f5 = 77
g5 = 79
a5 = 81
b5 = 83
numberMap =
1: 0
2: 2
3: 4
4: 5
5: 7
6: 9
7: 11
notesMap =
0:
number: 1
accidental: 0
1:
number: 1
accidental: 1
2:
number: 2
accidental: 0
3:
number: 2
accidental: 1
4:
number: 3
accidental: 0
5:
number: 4
accidental: 0
6:
number: 4
accidental: 1
7:
number: 5
accidental: 0
8:
number: 5
accidental: 1
9:
number: 6
accidental: 0
10:
number: 6
accidental: 1
11:
number: 7
accidental: 0
Jianpu = React.createClass
getDefaultProps: ->
sectionsPerLine: 4
alignSections: false
analyser:
pitch: (pitch) ->
if pitch.base is rest
nOctaves: 0
number: 0
accidental: 0
else
diff = pitch.base - c4
unitPitch = diff %% 12
nOctaves: Math.floor(diff / 12)
number: notesMap[unitPitch].number
accidental: pitch.accidental
duration: (duration) ->
nCrotchets = Math.floor(duration / 8)
remain = duration % 8
if nCrotchets is 0
switch remain
when 1, 2, 4
main: remain
nDashes: 0
nDots: 0
when 3
main: 2
nDashes: 0
nDots: 1
when 6
main: 4
nDashes: 0
nDots: 1
when 7
main: 4
nDashes: 0
nDots: 2
else
throw "bad duration"
else if nCrotchets is 1
switch remain
when 0
main: 8
nDashes: 0
nDots: 0
when 4
main: 8
nDashes: 0
nDots: 1
when 6
main: 8
nDashes: 0
nDots 2
else
throw "bad duration"
else if nCrotchets is 3
switch remain
when 0
main: 8
nDashes: 2
nDots: 0
when 4
main: 8
nDashes: 2
nDots: 1
else
throw "bad duration"
else
switch remain
when 0
main: 8
nDashes: nCrotchets - 1
nDots: 0
else
throw "bad duration"
estimateMusicXSpan: (note, nextNote) ->
duration = @duration(note.duration)
nextDuration =
if nextNote
@duration(nextNote.duration)
else
null
if duration.main is nextDuration?.main
if duration.main < 8
20 + 10 * duration.nDots
else
if duration.nDashes > 0
40 + 40 * duration.nDashes + 20 * duration.nDots
else
20 + 20 * duration.nDots
else
40 + 40 * duration.nDashes + 20 * duration.nDots
estimateLyricsXSpan: (note) ->
if note.lyrics.exists
10 + calculateSize(note.lyrics.content, {font: "Arial", fontSize: "20px"}).width
else
0
estimateXSpan: (note, nextNote) ->
Math.max @estimateMusicXSpan(note, nextNote), @estimateLyricsXSpan(note)
estimateSectionXSpan: (section) ->
if section.length is 0
0
else
offset = 0
for note, i in section
offset += @estimateXSpan(note, section[i + 1])
offset + 20
render: ->
{song, width, height, sectionsPerLine, alignSections, highlight} = @props
heightPerLine = 200
marginBottom = 0
sectionPadding = 20
if not song?
<div />
else
sectionLength = parseFloat(song.time.upper) * 32 / parseFloat(song.time.lower)
offset = 0
sections = []
currentSection = []
for note in song.melody
duration = note.duration
offset += duration
currentSection.push note
if offset % sectionLength is 0
sections.push currentSection
currentSection = []
slurX = slurY = 0
x = y = 0
nSectionsThisLine = 0
computedSections = []
for section, i in sections
startX = x
startY = y
computedNotes = []
for note, j in section
pitch = @analyser.pitch(note.pitch)
duration = @analyser.duration(note.duration)
computedNotes.push
note: note
x: x
y: y
pitch: pitch
duration: duration
x += @analyser.estimateXSpan(note, section[j + 1])
computedSections.push
notes: computedNotes
startX: startX
startY: startY
x: x += sectionPadding
y: y
nSectionsThisLine++
if nSectionsThisLine >= sectionsPerLine
#next line
y += heightPerLine
x = 0
nSectionsThisLine = 0
if alignSections
sectionWidth = (0 for i in [0...sectionsPerLine] by 1)
nSectionsThisLine = 0
for section in computedSections
newWidth = section.x - section.startX
if newWidth > sectionWidth[nSectionsThisLine]
sectionWidth[nSectionsThisLine] = newWidth
nSectionsThisLine = (nSectionsThisLine + 1) % sectionsPerLine
sectionOffsets = [0]
for i in [0...sectionWidth.length] by 1
sectionOffsets[i + 1] = sectionOffsets[i] + sectionWidth[i]
nSectionsThisLine = 0
for section in computedSections
xSpan = section.x - section.startX
newstartX = sectionOffsets[nSectionsThisLine]
newx = sectionOffsets[nSectionsThisLine + 1]
delta = (newx - newstartX - xSpan) / section.notes.length
for noteInfo, i in section.notes
noteInfo.x = (noteInfo.x - section.startX) + delta * (i + 1) + newstartX
section.startX = newstartX
section.x = newx
nSectionsThisLine = (nSectionsThisLine + 1) % sectionsPerLine
for section in computedSections
section.slurs = []
for noteInfo in section.notes
if noteInfo.note.options?
slur = noteInfo.note.options.slur
if slur is "start"
slurX = noteInfo.x + 20
slurY = noteInfo.y + 40 - 10 * noteInfo.pitch.nOctaves
else if slur is "end"
slurEndX = noteInfo.x + 20
slurEndY = noteInfo.y + 40 - 10 * noteInfo.pitch.nOctaves
section.slurs.push [slurX, slurY, slurEndX, slurEndY]
if not width?
nSectionsThisLine = 0
width = -1
lastWidth = 0
for section in computedSections
lastWidth += section.x - section.startX
nSectionsThisLine = nSectionsThisLine + 1
if nSectionsThisLine is sectionsPerLine and width < lastWidth
width = lastWidth
lastWidth = 0
nSectionsThisLine = 0
if nSectionsThisLine < sectionsPerLine - 1 and currentSection.length > 0
lastWidth += @analyser.estimateSectionXSpan(currentSection)
if width < lastWidth
width = lastWidth
width += sectionPadding
if not height?
nLines = Math.ceil((computedSections.length + (if currentSection.length > 0 then 1 else 0)) / sectionsPerLine)
height = nLines * heightPerLine + marginBottom
<svg width={width} height={height}>
<text x={10} y={20} className="signature">{"#{song.key.left}=#{song.key.right} #{song.time.upper}/#{song.time.lower}"}</text>
{
for section, i in computedSections
{notes, slurs} = section
<g key={i}>
{
for noteInfo, j in notes
{note, pitch, duration} = noteInfo
<Jianpu.Note key={j}
note={note}
x={noteInfo.x}
y={noteInfo.y}
pitch={pitch}
duration={duration}
highlight={highlight is note}
/>
}
{
for slur, j in slurs
x1 = slur[0]
y1 = slur[1]
x2 = slur[2]
y2 = slur[3]
ctrl1X = x1+20
ctrl2X = x2-20
if ctrl1X > ctrl2X
ctrl1X = ctrl2X = (x1+x2)/2
<path key={"#{i},#{j}"} className="slur" d="M#{x1},#{y1} C#{ctrl1X},#{y1-20} #{ctrl2X},#{y2-20} #{x2},#{y2}" />
}
{
barX = section.x
barY = section.y
if i is sections.length - 1 and currentSection.length is 0
<g className="doublebar-line">
<line x1={barX - 7} y1={30 + barY} x2={barX - 7} y2={110 + barY} />
<line x1={barX} y1={30 + barY} x2={barX} y2={110 + barY} />
</g>
else
<g className="bar-line">
<line x1={barX} y1={30 + barY} x2={barX} y2={110 + barY} />
</g>
}
</g>
}
{
for note, i in currentSection
pitch = @analyser.pitch(note.pitch)
duration = @analyser.duration(note.duration)
comp = <Jianpu.Note key={i} note={note} x={x} y={y} pitch={pitch} duration={duration}/>
x += @analyser.estimateXSpan(note, section[i + 1])
comp
}
</svg>
Jianpu.Note = React.createClass
render: ->
{note, x, y, pitch, duration, highlight} = @props
{nOctaves, number, accidental} = pitch
{main, nDashes, nDots} = duration
lyrics = note.lyrics
nUnderDashes =
switch main
when 4 then 1
when 2 then 2
when 1 then 3
else 0
accidentalText =
switch accidental
when -2
"♭♭"
when -1
"♭"
when 0
null
when 1
"♯"
when 2
"♯♯"
<g transform={"translate(#{x}, #{y})"}>
{
if highlight
<rect className="highlight" x={5} y={20} width={40 + 40 * nDashes + 10 * nDots} height={100} />
}
{
if accidentalText
<text className="accidental" x={-5} y={80}>{accidentalText}</text>
}
<text className="note" x={10} y={85}>{number}</text>
{
if nOctaves > 0
for i in [0...nOctaves] by 1
<circle key={i} className="octave octave-high" cx={20} cy={40 - 10 * i} r={3.5} />
else if nOctaves < 0
for i in [0...-nOctaves] by 1
<circle key={i} className="octave octave-low" cx={20} cy={110 + nUnderDashes * 5 + 10 * i} r={3.5} />
}
{
for i in [0...nDashes] by 1
<line key={i} className="length-dash" x1={50 + 40 * i} y1={70} x2={40 * i + 70} y2={70} />
}
{
for i in [0...nUnderDashes] by 1
<line key={i} className="length-underline" x1={10} y1={95 + i * 5} x2={30} y2={95 + i * 5} />
}
{
for i in [0...nDots]
<circle key={i} className="length-dot" cx={40 + 40 * nDashes + 10 * i} cy={70} r={3.5} />
}
{
if lyrics.exists
<text className="lyrics" x={10} y={135}>{"#{if lyrics.hyphen then "-" else ""}#{lyrics.content}"}</text>
}
</g>