-
Notifications
You must be signed in to change notification settings - Fork 0
/
clinkster.asm
800 lines (675 loc) · 14.4 KB
/
clinkster.asm
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
%include "music.asm"
;; ********** Definitions **********
global Clinkster_GenerateMusic
global _Clinkster_GenerateMusic@0
global Clinkster_StartMusic
global _Clinkster_StartMusic@0
extern __imp__waveOutOpen@24
extern __imp__waveOutPrepareHeader@12
extern __imp__waveOutWrite@12
extern __imp__waveOutGetPosition@12
%define SAMPLE_RATE 44100
%define WAVE_SIZE 65536
;; ********** Public variables **********
section MusBuf bss align=4
Clinkster_MusicBuffer:
_Clinkster_MusicBuffer:
.align24
resw (TOTAL_SAMPLES*2)
;; ********** System structures **********
section WaveForm rdata align=1
_WaveFormat:
dw 1,2
dd SAMPLE_RATE
dd SAMPLE_RATE*4
dw 4,16,0
section WaveHdr data align=4
_WaveHdr:
dd Clinkster_MusicBuffer
dd (TOTAL_SAMPLES*4)
dd 0,0,0,0,0,0
section wavehand bss align=4
_WaveOutHandle:
.align16
resd 1
section WaveTime data align=4
_WaveTime:
dd 4,0,0,0,0,0,0,0
;; ********** Internal buffers **********
section MixBuf bss align=4
resd MAX_DELAY_LENGTH
_MixingBuffer:
.align24
resd TOTAL_SAMPLES
section InstrBuf bss align=4
_InstrumentBuffer:
.align16
resd MAX_INSTRUMENT_SUBSAMPLES
section InstrRen bss align=4
resd 256
_InstrumentRender:
.align16
resd MAX_INSTRUMENT_SUBSAMPLES
section InstrSto bss align=4
_InstrumentStore:
.align16
resd MAX_TOTAL_INSTRUMENT_SAMPLES
section InstrPoi bss align=4
_InstrumentPointers:
.align16
resd MAX_TRACK_INSTRUMENT_RENDERS+1
section wforms bss align=4
waveforms:
.align16
resd 6*WAVE_SIZE
;; ********** Instrument parameter access **********
section paramw rdata align=4
param_weights:
;; Define alle 4 til samme tal --
dd 0.125 ; Release
dd 0.125 ; Decay
dd 0.125 ; Attack
%if USES_GAIN
dd 0.125 ; Gain
%endif
%if USES_INDEXDECAY
dd 0.0009765625 ; IndexDecay
%endif
dd 0.0009765625 ; M PitchDecay
dd 0.0009765625 ; B PitchDecay
dd 0.083333333333 ; M Pitch
dd 0.083333333333 ; B Pitch
dd 0.0000152587890625 ; Volume
%if USES_PANNING
dd 0.0000152587890625 ; Volume
%endif
dd 0.03125 ; Sustain
dd 16307 ; RandomSeed
dd 1 ; Layers
dd 4096.0 ; Index
dd 0.125 ; Index Spread
dd 0.0009765625 ; M Detune
dd 0.0009765625 ; B Detune
dd 65536 ; ModWave
dd 65536 ; BaseWave
struc instr_params
ip_basewave: resd 1
ip_modwave: resd 1
ip_bdetune: resd 1
ip_mdetune: resd 1
ip_indexspr: resd 1
ip_index: resd 1
ip_layers: resd 1
ip_randomseed: resd 1
ip_sustain: resd 1
ip_volume: resd 1+USES_PANNING
ip_bpitch: resd 1
ip_mpitch: resd 1
ip_bpitchd: resd 1
ip_mpitchd: resd 1
%if USES_INDEXDECAY
ip_indexd: resd 1
%endif
%if USES_GAIN
ip_gain: resd 1
%endif
ip_attack: resd 1
ip_decay: resd 1
ip_release: resd 1
endstruc
%define ip_INT 0
%define ip_FLOAT 0
%define IP(f,t) dword [instrparams + ip_ %+ f + ip_ %+ t]
%define IPI(f,i,t) dword [instrparams + ip_ %+ f + ip_ %+ t + i]
;; ********** Internal constants and tables **********
section resamp rdata align=4
resamplefilter:
db -1,-2,-4,-4,-2,3,14,30,51,98,116,126
db 126,116,98,51,30,14,3,-2,-4,-4,-2,-1
resamplefilter_end:
FILTER_SIZE equ (resamplefilter_end-resamplefilter)
section wavestep rdata align=4
c_wavestep: dd 0.000030517578125
section basefreq rdata align=4
c_basefreq: dd 2.86698696365342
section halfnote rdata align=4
c_halfnote: dd 1.05946309435929
section finalamp rdata align=4
c_finalamp: dd 32767
section velfac rdata align=4
c_velocityfac: dd 0.007874015748031496
section delaystr rdata align=4
c_delaystr: dd DELAY_STRENGTH
section offset rdata align=4
section tempo rdata align=4
c_ticklength: dd SUBSAMPLES_PER_TICK/4*4
section half rdata align=4
c_onehalf: dd 0.5
;; ********** Internal global variables **********
section vars bss align=8
vars_align16
layer_random: resd 1
stereo: resd 1 ; 0 for left channel, 2 for right channel
noteposptr: resd 1
notesamptr: resd 1
instrparams:
resb instr_params_size
layer_params:
layer_bfreq: resq 1
layer_mfreq: resq 1
layer_index: resq 1
layer_bpitch: resq 1
layer_mpitch: resq 1
layer_bpitchd: resq 1
layer_mpitchd: resq 1
%if USES_INDEXDECAY
layer_indexd: resq 1
%endif
%if USES_GAIN
layer_gain: resq 1
%endif
layer_attack: resq 1
layer_decay: resq 1
layer_release: resq 1
layer_phasetmp: resd 1
;; ********** Generate the sound for one layer **********
section mklayer text align=1
makelayer:
mov edx, layer_params
; Init random variables for layer
fild word [layer_random]
mov ecx, dword [layer_random]
ror ecx, cl
dec ecx
mov dword [layer_random], ecx
fld IP(bdetune, FLOAT)
fmul st0, st0
fmulp st1, st0
fadd st0, st1
fstp qword [edx]
add edx, byte 8
fild word [layer_random]
mov ecx, dword [layer_random]
ror ecx, cl
dec ecx
mov dword [layer_random], ecx
fld IP(mdetune, FLOAT)
fmul st0, st0
fmulp st1, st0
fadd st0, st1
fstp qword [edx]
add edx, byte 8
fild word [layer_random]
mov ecx, dword [layer_random]
ror ecx, cl
dec ecx
mov dword [layer_random], ecx
fmul IP(indexspr, FLOAT)
fadd IP(index, FLOAT)
fstp qword [edx]
add edx, byte 8
; Init exponentiated variables for layer
mov edi, instrparams+ip_bpitch
mov ecx, 7+USES_INDEXDECAY+USES_GAIN
.powloop:
fld dword [edi]
fld1
fld st1
fprem
fstp st1
f2xm1
fld1
faddp st1, st0
fscale
fstp qword [edx]
add edx, byte 8
fstp st0
scasd
loop .powloop
mov ebx, edx ; phasetmp
; Loop over samples
fldz ; b phase
fldz ; m phase
mov edi, _InstrumentBuffer
; Calculate max note size
xor eax, eax
%if USES_VELOCITY
lodsb ; Skip velocity
%endif
%if USES_LONG_NOTES
cmp [esi], byte 0
jge near .short_notelen
lodsb
not al
shl eax, 8
.short_notelen:
%endif
lodsb ; Length of longest note with this tone
mov edx, SUBSAMPLES_PER_TICK
mul edx
add eax, MAX_RELEASE_SUBSAMPLES
xchg ecx, eax
.sampleloop:
mov edx, layer_params
; Look up mod wave
fist dword [ebx]
mov eax, IP(modwave,INT)
mov ax, [ebx]
fld dword [waveforms + eax*4]
; Adjust by index
fmul qword [edx + 2*8] ; layer_index
fadd st0, st2
; Look up base wave
fistp dword [ebx]
mov eax, IP(basewave,INT)
mov ax, [ebx]
fld dword [waveforms + eax*4]
; Update phases
fld qword [edx] ; layer_bfreq
add edx, byte 8
fmul qword [edx + 2*8] ; layer_bpitch
faddp st3, st0
fld qword [edx] ; layer_mfreq
add edx, byte 8
fmul qword [edx + 2*8] ; layer_mpitch
faddp st2, st0
%if USES_INDEXDECAY
fld qword [edx] ; layer_index
%endif
add edx, byte 8
; Update pitches: p := (p-1)*d+1
fld1
fld qword [edx] ; layer_bpitch
fsub st0, st1
fmul qword [edx + 2*8] ; layer_bpitchd
faddp st1, st0
fstp qword [edx] ; layer_bpitch
add edx, byte 8
fld1
fld qword [edx] ; layer_mpitch
fsub st0, st1
fmul qword [edx + 2*8] ; layer_mpitchd
faddp st1, st0
fstp qword [edx] ; layer_mpitch
add edx, byte 8
%if USES_INDEXDECAY
fmul qword [edx + 2*8] ; layer_indexd
fstp qword [edx - 3*8] ; layer_index
%endif
; Add to existing layers
fadd dword [edi]
fstp dword [edi]
scasd
loop .sampleloop
fstp st0
fstp st0
ret
;; ********** Interpolate one section of amplitude envelope **********
section adsr text align=1
apply_adsr:
push eax
jg .integer_length
fimul dword [c_finalamp]
fistp dword [esp]
.integer_length:
fsub st0, st1
fild dword [esp]
pop eax
add eax, ecx
fdivp st1, st0
.adsrloop:
fld dword [_InstrumentBuffer + ecx*4]
fmul st0, st3 ; velocity / nlayers
fmul st0, st2 ; envelope value
%if USES_GAIN
fld1
fsubr qword [layer_gain]
fmul st0, st1
fmul st0, st1
fld1
faddp st1, st0
fdivr qword [layer_gain]
fsqrt
fmulp st1, st0
%endif
fstp dword [_InstrumentRender + ecx*4]
fadd st1, st0
inc ecx
cmp ecx, eax
jl .adsrloop
fstp st0
ret
;; ********** Main music generation **********
section genMus text align=1
Clinkster_GenerateMusic:
_Clinkster_GenerateMusic@0:
pusha
fninit
; Make waveforms
mov edi, waveforms
%if USES_SINE
fldz
mov ecx, WAVE_SIZE
.sineloop:
fadd dword [c_wavestep]
fld st0
fldpi
fmulp st1, st0
fsin
fstp dword [edi]
scasd
loop .sineloop
fstp st0
%endif
%if USES_SAWTOOTH
fld1
fchs
mov ecx, WAVE_SIZE
.sawtoothloop:
fadd dword [c_wavestep]
fst dword [edi]
scasd
loop .sawtoothloop
fstp st0
%endif
%if USES_SQUARE
fld1
fchs
mov ecx, WAVE_SIZE
.squareloop:
cmp ecx, WAVE_SIZE/2
jne .notflipsq
fabs
.notflipsq:
fst dword [edi]
scasd
loop .squareloop
fstp st0
%endif
%if USES_PARABOLA
fld1
fchs
mov ecx, WAVE_SIZE
.parabolaloop:
fadd dword [c_wavestep]
fld st0
fmul st0, st1
fadd st0, st0
fld1
fsubp st1, st0
fstp dword [edi]
scasd
loop .parabolaloop
fstp st0
%endif
%if USES_TRIANGLE
fld1
fchs
mov ecx, WAVE_SIZE
.triangleloop:
fadd dword [c_wavestep]
fld st0
fadd st0, st1
fabs
fld1
fsubp st1, st0
fstp dword [edi]
scasd
loop .triangleloop
fstp st0
%endif
%if USES_NOISE
fld1
fchs
mov ecx, WAVE_SIZE
.noiseloop:
fadd dword [c_wavestep]
fldpi
fmulp st1, st0
fsin
fst dword [edi]
scasd
loop .noiseloop
fstp st0
%endif
.stereoloop:
mov dword [noteposptr], _NotePositions
mov dword [notesamptr], _NoteSamples
xor eax, eax
mov edi, _MixingBuffer
mov ecx, TOTAL_SAMPLES
rep stosd
mov esi, _InstrumentData
%if USES_DELAY
jmp short .trackloop
.delay:
mov eax, dword [stereo]
mov edx, (LEFT_DELAY_LENGTH-RIGHT_DELAY_LENGTH)*4/2
mul edx
sub eax, LEFT_DELAY_LENGTH*4
mov edi, _MixingBuffer
mov ecx, TOTAL_SAMPLES
.delayloop:
fld dword [edi+eax]
fmul dword [c_delaystr]
fadd dword [edi]
fstp dword [edi]
scasd
loop .delayloop
%endif
.trackloop:
; ESI = instr data
mov edi, instrparams
mov ecx, instr_params_size/4
.ploop:
lodsb
movsx eax, al
push eax
fild dword [esp]
pop eax
fmul dword [param_weights-4+ecx*4]
fstp dword [edi]
scasd
loop .ploop
mov edi, instrparams+ip_bpitchd
mov ecx, 2+USES_INDEXDECAY
.cubeloop:
fld dword [edi]
fld st0
fmul st0, st0
fmulp st1, st0
fstp dword [edi]
scasd
loop .cubeloop
mov ebx, _InstrumentPointers
mov dword [ebx], _InstrumentStore ; store first instrument instance address
fld dword [c_basefreq]
; Loop over instrument tones
.toneloop:
xor eax, eax
lodsb ; Tone
.freqloop:
fmul dword [c_halfnote]
dec eax
jge .freqloop
; random seed for channel = RandomSeed * 16307 + channel * 12042
mov eax, dword [stereo]
mov edx, 12042/2
mul edx
add eax, IP(randomseed,INT)
xchg ecx, eax
mov dword [layer_random], ecx
xor eax, eax
mov edi, _InstrumentBuffer
mov ecx, MAX_INSTRUMENT_SUBSAMPLES
rep stosd
; Loop over layers
mov ecx, IP(layers,INT)
.layerloop:
pusha
call makelayer
popa
loop .layerloop
.lengthloop:
%if USES_VELOCITY
lodsb
movsx eax, al
push eax
fild dword [esp]
pop eax
fmul dword [c_velocityfac]
%else
fld1
%endif
fidiv IP(layers,INT)
xor ecx, ecx ; sample index
fldz ; amplitude level
fld1 ; attack amplitude target
fld qword [layer_attack]; attack length
call apply_adsr
fld IP(sustain,FLOAT) ; decay amplitude target
fld qword [layer_decay] ; decay length
call apply_adsr
xor eax, eax
%if USES_LONG_NOTES
cmp [esi], byte 0
jge near .short_notelen
lodsb
not al
shl eax, 8
.short_notelen:
%endif
lodsb ; note length in ticks
mov edx, SUBSAMPLES_PER_TICK
mul edx
sub eax, ecx ; note length exclusing attack and decay
jle .nosustain ; attack + decay overflows note length
fld IP(sustain,FLOAT) ; sustain amplitude target
call apply_adsr
.nosustain:
fldz ; release amplitude target
fld qword [layer_release];release length
call apply_adsr
fldz ; padding amplitude
fld1 ; padding length
call apply_adsr
fstp st0
fstp st0
; Resampling
mov edi, [ebx] ; instrument instance address
add ebx, byte 4
xchg edx, eax
mov ebp, _InstrumentRender - FILTER_SIZE*4
.resampleloop:
fldz
mov ecx, FILTER_SIZE
.filterloop:
movsx eax, byte [resamplefilter + ecx - 1]
push eax
fild dword [esp]
pop eax
fmul dword [ebp + ecx*4]
faddp st1, st0
loop .filterloop
%if USES_PANNING
mov eax, dword [stereo]
fmul IPI(volume,eax*2,FLOAT)
%else
fmul IP(volume,FLOAT)
%endif
fstp dword [edi]
scasd
add ebp, byte 4*4
sub edx, byte 4
jg .resampleloop
mov [ebx], edi ; store instrument instance address
cmp [esi], byte 0
jne near .lengthloop
lodsb
cmp [esi], byte 0
jge near .toneloop
lodsb
fstp st0
; Mixing
mov ebx, _InstrumentPointers
mov ebp, _MixingBuffer
xchg esi, dword [notesamptr]
.noteloop:
xchg esi, dword [noteposptr]
xor eax, eax
cmp [esi], byte 0
jge near .short_notepos
lodsb
not al
shl eax, 8
.short_notepos:
lodsb
mov edx, SUBSAMPLES_PER_TICK/4*4
mul edx
add ebp, eax
xchg esi, dword [noteposptr]
xor eax, eax
lodsb
mov edx, dword [ebx + eax*4] ; Instrument instance ptr
mov edi, ebp
.mixloop:
fld dword [edx]
fadd dword [edi]
fstp dword [edi]
scasd
add edx, byte 4
cmp edx, dword [ebx + eax*4 + 4]
jl .mixloop
cmp [esi], byte 0
jge near .noteloop
lodsb
xchg esi, dword [notesamptr]
cmp [esi], byte 0
jge near .trackloop
lodsb
%if USES_DELAY
cmp [esi], byte 0
jge near .delay
%endif
; Clamp and convert to shorts
fld1
mov edi, Clinkster_MusicBuffer
mov ecx, TOTAL_SAMPLES
add edi, dword [stereo]
.sloop:
fld dword [_MixingBuffer + ecx*4]
fcomi st0, st1
fcmovnb st0, st1
fchs
fcomi st0, st1
fcmovnb st0, st1
fchs
fimul dword [c_finalamp]
fistp word [edi + ecx*4]
loop .sloop
fstp st0
xor byte [stereo], 2
jne .stereoloop
popa
ret
;; ********** Start music **********
section startmus text align=1
Clinkster_StartMusic:
_Clinkster_StartMusic@0:
; Start music
push byte 0
push byte 0
push byte 0
push _WaveFormat
push byte -1
push _WaveOutHandle
call [__imp__waveOutOpen@24]
push byte 32 ; sizeof(WAVEHDR)
push _WaveHdr
push dword [_WaveOutHandle] ; waveOutHandle
call [__imp__waveOutPrepareHeader@12]
push byte 32 ; sizeof(WAVEHDR)
push _WaveHdr
push dword [_WaveOutHandle]
call [__imp__waveOutWrite@12]
ret