-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoidos.asm
777 lines (628 loc) · 15 KB
/
oidos.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
; Oidos by Blueberry - https://github.com/askeksa/Oidos
; Offset applied by Oidos_GetPosition to compensate for graphics latency.
; Measured in samples (44100ths of a second).
; The default value of 2048 (corresponding to about 46 milliseconds) is
; appropriate for typical display latencies for high-framerate effects.
%define OIDOS_TIMER_OFFSET 2048
; Set to 0 if you don't care about Oidos_GenerateMusic preserving registers
%define OIDOS_SAVE_REGISTERS 1
%include "platform.inc"
%include "music.asm"
;; ********** Definitions **********
global PUBLIC_SYM(Oidos_GenerateMusic)
%ifdef WIN32
global PUBLIC_SYM(Oidos_StartMusic)
global PUBLIC_SYM(Oidos_GetPosition)
%endif
global PUBLIC_SYM(Oidos_MusicBuffer)
global PUBLIC_SYM(Oidos_TicksPerSecond)
global PUBLIC_SYM(Oidos_MusicLength)
global PUBLIC_SYM(Oidos_WavFileHeader)
%ifdef WIN32
extern __imp__waveOutOpen@24
extern __imp__waveOutPrepareHeader@12
extern __imp__waveOutWrite@12
extern __imp__waveOutGetPosition@12
%endif
extern PUBLIC_SYM(Oidos_RandomData)
%define SAMPLE_RATE 44100
%define BASE_FREQ 0.00232970791933 ; 440/((2^(1/12))^(9+12*4))/44100*(2*3.14159265358979)
;; ********** Public variables **********
SECTION_RDATA(tps) align=4
PUBLIC_SYM(Oidos_TicksPerSecond):
dd TICKS_PER_SECOND
SECTION_DATA(muslen) align=4
PUBLIC_SYM(Oidos_MusicLength):
dd MUSIC_LENGTH
SECTION_BSS(MusBuf) align=4
PUBLIC_SYM(Oidos_MusicBuffer):
.align24:
resw TOTAL_SAMPLES*2
SECTION_DATA(WavFile) align=4
PUBLIC_SYM(Oidos_WavFileHeader):
db "RIFF"
dd 36+TOTAL_SAMPLES*4
db "WAVE"
db "fmt "
dd 16
dw 1,2
dd SAMPLE_RATE
dd SAMPLE_RATE*4
dw 4,16
db "data"
dd TOTAL_SAMPLES*4
;; ********** System structures **********
SECTION_DATA(WaveForm) align=1
WaveFormat:
dw 1,2
dd SAMPLE_RATE
dd SAMPLE_RATE*4
dw 4,16,0
SECTION_DATA(WaveHdr) align=4
WaveHdr:
dd PUBLIC_SYM(Oidos_MusicBuffer)
dd (TOTAL_SAMPLES*4)
dd 0,0,0,0,0,0
SECTION_BSS(wavehand) align=4
WaveOutHandle:
.align16:
resd 1
SECTION_DATA(WaveTime) align=4
WaveTime:
dd 4,0,0,0,0,0,0,0
;; ********** Internal buffers **********
SECTION_BSS(freqarr) align=16
PartialArray:
.align16:
resq 10000*3*2
SECTION_BSS(sampbuf) align=16
SampleBuffer:
.align24:
reso MAX_TOTAL_INSTRUMENT_SAMPLES
SECTION_BSS(mixbuf) align=16
MixingBuffer:
.align24:
reso TOTAL_SAMPLES
%if NUM_TRACKS_WITH_REVERB > 0
SECTION_BSS(revbuf) align=16
ReverbBuffer:
.align24:
reso TOTAL_SAMPLES
SECTION_BSS(delbuf) align=8
DelayBuffer:
.align16:
resq 25600
%endif
;; ********** Internal structures **********
struc params
p_modes: resd 1
p_fat: resd 1
p_seed: resd 1
p_overtones: resd 1
p_decaydiff: resd 1
p_decaylow: resd 1
p_harmonicity: resd 1
p_sharpness: resd 1
p_width: resd 1
p_filterlow: resd 1
p_filterhigh: resd 1
p_fslopelow: resd 1
p_fslopehigh: resd 1
p_fsweeplow: resd 1
p_fsweephigh: resd 1
p_gain: resd 1
p_maxsamples: resd 1
p_release: resd 1
p_attack: resd 1
p_volume: resd 1
%ifdef USES_PANNING
p_panning: resd 1
%endif
;; ********** Internal constants and tables **********
SECTION_DATA(base) align=16
baseptr:
c_oneone: dq 1.0,1.0
c_twelve: dd 12
c_randscale: dd 0x30000000 ; 2^-31
c_ampmax: dd 32767.0
c_basefreq: dd BASE_FREQ
%if NUM_TRACKS_WITH_REVERB > 0
ReverbMaxDecay:
dd REVERB_MAX_DECAY
ReverbDecayMul:
dd REVERB_DECAY_MUL
ReverbParams:
dd REVERB_FILTER_HIGH, REVERB_FILTER_LOW, REVERB_DAMPEN_HIGH, REVERB_DAMPEN_LOW, REVERB_VOLUME_LEFT
%endif
ParamsPtr: dd InstrumentParams
TonesPtr: dd InstrumentTones
TovelPtr: dd TrackData
LengthPtr: dd NoteLengths
NotePtr: dd NoteSamples
MixingPtr: dd 0
%if NUM_TRACKS_WITH_REVERB > 0
ReverbState:
dq 0.0,0.0,0.0,0.0
%endif
SECTION_RDATA(offset) align=4
c_timeoffset:
dd OIDOS_TIMER_OFFSET*4
SECTION_RDATA(tempo) align=4
c_ticklength:
dd SAMPLES_PER_TICK*4
;; ********** Register definitions **********
%define RANDOM ebx
%define PARAMS edx
%define ARRAY esi
%define SAMPLE edi
%define TONE esi
%define LENGTH esi
%define NOTE esi
%define MIXING edi
%define TOVEL ebx
%define SAMPLESRC esi
%define BASE ebp - baseptr
;; ********** Helper functions **********
%macro TONE2FREQ 0
fidiv dword [BASE + c_twelve]
fld1
fld st1
fprem
f2xm1
faddp st1
fscale
fstp st1
%endmacro
%macro FREQ2TONE 0
fild dword [BASE + c_twelve]
fxch st1
fyl2x
%endmacro
%macro GETRANDOM 0
fild dword [RANDOM]
add RANDOM, byte 4
fmul dword [BASE + c_randscale]
%endmacro
;; ********** Instrument calculation **********
SECTION_TEXT(makeinst) align=1
MakeInstrument:
mov PARAMS, [BASE + ParamsPtr]
; tone
mov ARRAY, PartialArray
mov eax, [PARAMS] ; modes
.modeloop:
push PARAMS
mov RANDOM, [PARAMS]
add PARAMS, byte 4
mov ecx, [PARAMS] ; fat
add PARAMS, byte 4
sub RANDOM, eax
shl RANDOM, 8
add RANDOM, [PARAMS] ; seed
add PARAMS, byte 4
shl RANDOM, 2
add RANDOM, PUBLIC_SYM(Oidos_RandomData)
GETRANDOM ; subtone
fabs
fld st0
fimul dword [PARAMS] ; overtones
add PARAMS, byte 4
fxch st1
fmul dword [PARAMS] ; decayhigh - decaylow
add PARAMS, byte 4
fadd dword [PARAMS] ; decaylow
add PARAMS, byte 4
%rep 12
fsqrt
%endrep
; ampmul, reltone, tone
fxch st1
TONE2FREQ
fld st0
frndint
fsub st1
fmul dword [PARAMS] ; harmonicity
add PARAMS, byte 4
faddp st1
FREQ2TONE
; reltone2, ampmul, tone
fld st0
fmul dword [PARAMS] ; sharpness
add PARAMS, byte 4
TONE2FREQ
GETRANDOM
fmulp st1
; mamp, reltone2, ampmul, tone
.partialloop:
push PARAMS
GETRANDOM
fmul dword [PARAMS] ; width
add PARAMS, byte 4
fadd st0, st2
; ptone-tone, mamp, reltone2, ampmul, tone
; Step value
fld st0
fadd st0, st5
TONE2FREQ
fmul dword [BASE + c_basefreq]
fsincos
fmul st0, st5
fstp qword [ARRAY]
add ARRAY, byte 8
fmul st0, st4
fstp qword [ARRAY]
add ARRAY, byte 8
; State value
fldpi
GETRANDOM
fmulp st1
fsincos
fmul st0, st3
fstp qword [ARRAY]
add ARRAY, byte 8
fmul st0, st2
fstp qword [ARRAY]
add ARRAY, byte 8
; Filter value
fld st0
fsub dword [PARAMS] ; filterlow
add PARAMS, byte 8
fmul dword [PARAMS] ; fslopelow
fld1
faddp st1
fstp qword [ARRAY]
add ARRAY, byte 8
sub PARAMS, byte 4
fsub dword [PARAMS] ; filterhigh
add PARAMS, byte 8
fmul dword [PARAMS] ; fslopehigh
fld1
faddp st1
fstp qword [ARRAY]
add ARRAY, byte 8
pop PARAMS
loop .partialloop
fstp st0
fstp st0
fstp st0
pop PARAMS
dec eax
jne .modeloop
fstp st0
; Gain helper values
mov RANDOM, [PARAMS] ; modes
add PARAMS, byte 4
imul RANDOM, [PARAMS] ; fat
add PARAMS, byte p_fslopelow-p_fat
cvtsi2sd xmm5, RANDOM
cvtps2pd xmm7, [PARAMS] ; fslopelow, fslopehigh
add PARAMS, byte 8
cvtps2pd xmm4, [PARAMS] ; fsweeplow, fsweephigh
add PARAMS, byte 8
cvtss2sd xmm6, [PARAMS] ; gain
add PARAMS, byte 4
mulpd xmm7, xmm4
mov eax, [PARAMS] ; maxsamples
.sample:
xorpd xmm4, xmm4
mov ARRAY, PartialArray
mov ecx, RANDOM ; npartials
.decay:
movapd xmm0, [ARRAY] ; [y1, x1]
add ARRAY, byte 16
movapd xmm1, [ARRAY] ; [y2, x2]
pshufd xmm3, xmm1, 0xEE ; [y2, y2]
pshufd xmm2, xmm1, 0x44 ; [x2, x2]
pshufd xmm1, xmm0, 0x4E ; [x1, y1]
mulpd xmm0, xmm2 ; [y1*x2, x1*x2]
mulpd xmm1, xmm3 ; [x1*y2, y1*y2]
addsubpd xmm0, xmm1 ; [y1*x2+x1*y2, x1*x2-y1*y2]
movapd [ARRAY], xmm0
add ARRAY, byte 16
movapd xmm1, [ARRAY]
pshufd xmm3, xmm1, 0xEE
pshufd xmm2, xmm1, 0x44
minpd xmm2, xmm3
xorpd xmm3, xmm3
maxpd xmm2, xmm3
minpd xmm2, [BASE + c_oneone]
mulpd xmm0, xmm2
addpd xmm4, xmm0
addpd xmm1, xmm7
movapd [ARRAY], xmm1
add ARRAY, byte 16
loop .decay
; Gain
movsd xmm1, xmm6
subsd xmm1, [BASE + c_oneone]
mulsd xmm1, xmm4
mulsd xmm1, xmm4
addsd xmm1, xmm5
divsd xmm1, xmm6
sqrtsd xmm1, xmm1
divsd xmm4, xmm1
unpcklpd xmm4, xmm4
movapd [SAMPLE], xmm4
add SAMPLE, byte 16
dec eax
jne .sample
ret
;; ********** Track mixing **********
SECTION_TEXT(makechan) align=1
MakeChannel:
mov SAMPLE, SampleBuffer
push byte 0
.tonesloop:
mov TONE, [BASE + TonesPtr]
lodsb
mov [BASE + TonesPtr], TONE
movsx eax, al
add [esp], eax
js .tonesdone
fild dword [esp]
call MakeInstrument
jmp .tonesloop
.tonesdone:
pop eax
.column:
mov dword [BASE + MixingPtr], MixingBuffer
; Delta decode tones
mov TOVEL, [BASE + TovelPtr]
xor eax, eax
.toveldelta:
mov cl, [TOVEL+eax+1]
add eax, byte 2
add [TOVEL+eax+1], cl
jns .toveldelta
add eax, byte 2
add [BASE + TovelPtr], eax
.notesloop:
mov MIXING, [BASE + MixingPtr]
; Read note length
xor eax, eax
mov LENGTH, [BASE + LengthPtr]
cmp [LENGTH], al
jge .short
lodsb
not al
shl eax, 8
.short:
lodsb
mov [BASE + LengthPtr], LENGTH
; Scale by tick duration
imul eax, SAMPLES_PER_TICK
cvtsi2sd xmm2, eax ; For release
push eax ; For termination test
shl eax, 4
add [BASE + MixingPtr], eax ; Position of next note
; Read note index
mov NOTE, [BASE + NotePtr]
lodsb
mov [BASE + NotePtr], NOTE
movsx eax, al
dec eax
js near .nextnote ; OFF
; Note volume
movzx SAMPLESRC, byte [TOVEL+eax*2+2]
cvtsi2sd xmm0, SAMPLESRC
; Get sample length
mov PARAMS, [BASE + ParamsPtr]
add PARAMS, byte p_maxsamples
mov ecx, [PARAMS]
add PARAMS, byte 4
; Attack/release add
cvtps2pd xmm7, [PARAMS] ; release, attack
add PARAMS, byte 8
; Instrument volume
cvtss2sd xmm5, [PARAMS] ; volume
add PARAMS, byte 4
mulsd xmm5, xmm0
unpcklpd xmm5, xmm5
%ifdef USES_PANNING
; Instrument panning
cvtss2sd xmm4, [PARAMS] ; panning
add PARAMS, byte 4
unpcklpd xmm4, xmm4
movapd xmm1, [BASE + c_oneone]
addsubpd xmm1, xmm4
mulpd xmm5, xmm1
%endif
; Find sample
movzx SAMPLESRC, byte [TOVEL+eax*2+1]
imul SAMPLESRC, ecx
shl SAMPLESRC, 4
add SAMPLESRC, SampleBuffer
; Attack/release state
mov al, [TOVEL]
test eax, eax
je .until_next
imul eax, SAMPLES_PER_TICK
cvtsi2sd xmm2, eax ; Fixed note length
.until_next:
mulsd xmm2, xmm7
movsd xmm1, [BASE + c_oneone]
subsd xmm1, xmm2
; XMM1 = Attack/release state
; XMM5 = Volume
; XMM7 = Attack/release add
.mixingloop:
movapd xmm0, [SAMPLESRC]
add SAMPLESRC, byte 16
movapd xmm4, [MIXING]
mulpd xmm0, xmm5 ; Volume
pshufd xmm3, xmm1, 0xEE
pshufd xmm2, xmm1, 0x44
minpd xmm2, xmm3
xorpd xmm3, xmm3
maxpd xmm2, xmm3
minpd xmm2, [BASE + c_oneone]
mulpd xmm0, xmm2 ; Attack and release
addpd xmm4, xmm0
addpd xmm1, xmm7
movapd [MIXING], xmm4
add MIXING, byte 16
loop .mixingloop
.nextnote:
pop eax
dec eax
jns near .notesloop
.notesdone:
; More columns for instrument?
mov TONE, [BASE + TonesPtr]
dec byte [TONE-1]
js .column
mov [BASE + ParamsPtr], PARAMS
ret
;; ********** Main **********
SECTION_TEXT(synth) align=1
PUBLIC_SYM(Oidos_GenerateMusic):
%if OIDOS_SAVE_REGISTERS
pusha
%endif
fninit
mov ebp, baseptr
%if NUM_TRACKS_WITH_REVERB > 0
push byte NUM_TRACKS_WITH_REVERB
.loop1:
call MakeChannel
dec dword [esp]
jne .loop1
pop ebx
fld dword [BASE + ReverbMaxDecay]
mov esi, REVERB_NUM_DELAYS
mov ecx, REVERB_MAX_DELAY
.delayloop:
; Is this delay length included?
mov eax, ecx
%if REVERB_MIN_DELAY != 0
sub eax, REVERB_MIN_DELAY
%endif
mul dword [PUBLIC_SYM(Oidos_RandomData) + REVERB_RANDOMSEED*4 + ecx*4]
cmp edx, esi
jae short .skip
.feedbackloop:
; Index into delay buffer
xor edx, edx
mov eax, ebx
shr eax, 1
div ecx
lea eax, [DelayBuffer + edx*8]
lea PARAMS, [BASE + ReverbParams]
lea edi, [BASE + ReverbState]
; Filter input
fld qword [MixingBuffer + ebx*8]
call ReverbFilter
; Filter echo
fld qword [eax] ; delayed value
call ReverbFilter
; Extract delayed signal
fld qword [eax] ; delayed value
fmul dword [PARAMS] ; volume
fadd qword [ReverbBuffer + REVERB_ADD_DELAY*16 + ebx*8]
fstp qword [ReverbBuffer + REVERB_ADD_DELAY*16 + ebx*8]
; Attenuate echo and add to input
fmul st0, st2 ; feedback factor
faddp st1
fstp qword [eax]
add ebx, byte 2
cmp ebx, TOTAL_SAMPLES*2
jb .feedbackloop
%if REVERB_VOLUME_LEFT != REVERB_VOLUME_RIGHT
; Alternate between left and right volume
xor dword [PARAMS], REVERB_VOLUME_LEFT ^ REVERB_VOLUME_RIGHT
%endif
; Switch side
and ebx, byte 1
xor ebx, byte 1
dec esi
.skip:
fmul dword [BASE + ReverbDecayMul]
loop .delayloop
fstp st0
%endif
%if NUM_TRACKS_WITHOUT_REVERB > 0
push byte NUM_TRACKS_WITHOUT_REVERB
.loop2:
call MakeChannel
dec dword [esp]
jne .loop2
pop ebx
%endif
; Clamp and convert to shorts
fld dword [BASE + c_ampmax]
.sloop:
fld qword [MixingBuffer + ebx*8]
%if NUM_TRACKS_WITH_REVERB > 0
fadd qword [ReverbBuffer + ebx*8]
%endif
fcomi st0, st1
fcmovnb st0, st1
fchs
fcomi st0, st1
fcmovnb st0, st1
fchs
fistp word [PUBLIC_SYM(Oidos_MusicBuffer) + ebx*2]
add ebx, byte 1
cmp ebx, TOTAL_SAMPLES*2
jb .sloop
fstp st0
%if OIDOS_SAVE_REGISTERS
popa
%endif
ret
SECTION_TEXT(rfilter) align=1
ReverbFilter:
%macro FILTER 0
; Basic low-pass filter
fsub qword [edi] ; filter state
fmul dword [PARAMS] ; filter parameter
add PARAMS, byte 4
fadd qword [edi]
; Avoid denormals
fld1
fadd st1, st0
fsubp st1, st0
fst qword [edi]
add edi, byte 8
%endmacro
; Difference between two low-pass filters
fld st0
FILTER
fxch st1
FILTER
fsubp st1
ret
;; ********** Play **********
%ifdef WIN32
SECTION_TEXT(startsnd) align=1
PUBLIC_SYM(Oidos_StartMusic):
; 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]
call [__imp__waveOutPrepareHeader@12]
push byte 32 ; sizeof(WAVEHDR)
push WaveHdr
push dword [WaveOutHandle]
call [__imp__waveOutWrite@12]
ret
SECTION_TEXT(getpos) align=1
PUBLIC_SYM(Oidos_GetPosition):
push byte 32 ; sizeof(MMTIME)
push WaveTime
push dword [WaveOutHandle]
call [__imp__waveOutGetPosition@12]
fild dword [WaveTime+4]
%if OIDOS_TIMER_OFFSET>0
fiadd dword [c_timeoffset]
%endif
fidiv dword [c_ticklength]
ret
%endif