-
Notifications
You must be signed in to change notification settings - Fork 11
/
adt2unit.pas
6233 lines (5383 loc) · 207 KB
/
adt2unit.pas
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of Adlib Tracker II (AT2).
//
// AT2 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// AT2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with AT2. If not, see <http://www.gnu.org/licenses/>.
unit AdT2unit;
{$S-,Q-,R-,V-,B-,X+}
{$PACKRECORDS 1}
interface
{$IFDEF GO32V2}
const
___UNIT_DATA_START___: Dword = 0;
{$ENDIF}
const
MIN_IRQ_FREQ = 50;
MAX_IRQ_FREQ = 1000;
{$i typcons1.inc}
{$i typcons2.inc}
const
IRQ_freq: Word = 50;
IRQ_freq_shift: Integer = 0;
tempo: Byte = 50;
speed: Byte = 6;
macro_speedup: Word = 1;
timer_initialized: Boolean = FALSE;
repeat_pattern: Boolean = FALSE;
fast_forward: Boolean = FALSE;
rewind: Boolean = FALSE;
pattern_break: Boolean = FALSE;
pattern_delay: Boolean = FALSE;
next_line: Byte = 0;
start_order: Byte = BYTE_NULL;
start_pattern: Byte = BYTE_NULL;
start_line: Byte = BYTE_NULL;
replay_forbidden: Boolean = TRUE;
single_play: Boolean = FALSE;
calibrating: Boolean = FALSE;
no_status_refresh: Boolean = FALSE;
do_synchronize: Boolean = FALSE;
space_pressed: Boolean = FALSE;
module_archived: Boolean = FALSE;
force_scrollbars: Boolean = FALSE;
no_sync_playing: Boolean = FALSE;
no_step_debugging: Boolean = FALSE;
play_single_patt: Boolean = FALSE;
no_trace_pattord: Boolean = FALSE;
skip_macro_flag: Boolean = FALSE;
max_patterns: Byte = 128;
jump_mark_mode: Boolean = FALSE;
force_macro_keyon: Boolean = FALSE;
ins_trailing_flag: Boolean = FALSE;
const
def_vibtrem_speed_factor: Byte = 1;
def_vibtrem_table_size: Byte = 32;
def_vibtrem_table: array[0..255] of Byte = (
0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253,255,
253,250,244,235,224,212,197,180,161,141,120,97,74,49,24,
0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253,255,
253,250,244,235,224,212,197,180,161,141,120,97,74,49,24,
0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253,255,
253,250,244,235,224,212,197,180,161,141,120,97,74,49,24,
0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253,255,
253,250,244,235,224,212,197,180,161,141,120,97,74,49,24,
0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253,255,
253,250,244,235,224,212,197,180,161,141,120,97,74,49,24,
0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253,255,
253,250,244,235,224,212,197,180,161,141,120,97,74,49,24,
0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253,255,
253,250,244,235,224,212,197,180,161,141,120,97,74,49,24,
0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253,255,
253,250,244,235,224,212,197,180,161,141,120,97,74,49,24);
var
vibtrem_speed_factor: Byte;
vibtrem_table_size: Byte;
vibtrem_table: array[0..255] of Byte;
type
tVIBRATO_TREMOLO_TABLE =
array[1..20] of Record
pos: Byte;
dir: Byte;
speed: Byte;
depth: Byte;
fine: Boolean;
end;
var
vibr_table: tVIBRATO_TREMOLO_TABLE;
vibr_table2: tVIBRATO_TREMOLO_TABLE;
trem_table: tVIBRATO_TREMOLO_TABLE;
trem_table2: tVIBRATO_TREMOLO_TABLE;
const
macro_preview_indic_proc: procedure(state: Byte) = NIL;
seconds_counter: Longint = 0;
hundereds_counter: Longint = 0;
really_no_status_refresh: Boolean = FALSE;
const
keyoff_flag = $080;
fixed_note_flag = $090;
pattern_loop_flag = $0e0;
pattern_break_flag = $0f0;
var
fmpar_table: array[1..20] of tFM_PARAMETER_TABLE;
volume_lock: array[1..20] of Boolean;
vol4op_lock: array[1..20] of Boolean;
volume_table: array[1..20] of Word;
vscale_table: array[1..20] of Word;
peak_lock: array[1..20] of Boolean;
pan_lock: array[1..20] of Boolean;
modulator_vol: array[1..20] of Byte;
carrier_vol: array[1..20] of Byte;
decay_bar: array[1..20] of tDECAY_BAR;
volum_bar: array[1..20] of tVOLUM_BAR;
channel_flag: array[1..20] of Boolean;
event_table: array[1..20] of tCHUNK;
voice_table: array[1..20] of Byte;
freq_table: array[1..20] of Word;
zero_fq_table: array[1..20] of Word;
effect_table: array[1..20] of Word;
effect_table2: array[1..20] of Word;
fslide_table: array[1..20] of Byte;
fslide_table2: array[1..20] of Byte;
glfsld_table: array[1..20] of Word;
glfsld_table2: array[1..20] of Word;
porta_table: array[1..20] of Record freq: Word; speed: Byte; end;
porta_table2: array[1..20] of Record freq: Word; speed: Byte; end;
portaFK_table: array[1..20] of Boolean;
arpgg_table: array[1..20] of Record state,note,add1,add2: Byte; end;
arpgg_table2: array[1..20] of Record state,note,add1,add2: Byte; end;
retrig_table: array[1..20] of Byte;
retrig_table2: array[1..20] of Byte;
tremor_table: array[1..20] of Record pos: Integer; volume: Word; end;
tremor_table2: array[1..20] of Record pos: Integer; volume: Word; end;
panning_table: array[1..20] of Byte;
last_effect: array[1..20] of Word;
last_effect2: array[1..20] of Word;
volslide_type: array[1..20] of Byte;
event_new: array[1..20] of Boolean;
freqtable2: array[1..20] of Word;
notedel_table: array[1..20] of Byte;
notecut_table: array[1..20] of Byte;
ftune_table: array[1..20] of Shortint;
keyoff_loop: array[1..20] of Boolean;
macro_table: array[1..20] of Record
fmreg_pos,arpg_pos,vib_pos: Word;
fmreg_count,fmreg_duration,arpg_count,
vib_count,vib_delay: Byte;
vib_paused: Boolean;
fmreg_table,arpg_table,vib_table: Byte;
arpg_note: Byte;
vib_freq: Word;
end;
loopbck_table: array[1..20] of Byte;
loop_table: array[1..20,0..255] of Byte;
misc_register: Byte;
ai_table: array[1..255] of Byte;
const
overall_volume: Byte = 63;
global_volume: Byte = 63;
fade_out_volume: Byte = 63;
playback_speed_shift: Longint = 0;
play_status: tPLAY_STATUS = isStopped;
chan_pos: Byte = 1;
chpos: Byte = 1;
transpos: Byte = 1;
track_chan_start: Byte = 1;
nm_track_chan: Byte = 1;
play_pos_buf: array[1..9] of Word = (0,0,0,0,0,0,0,0,0);
rec_correction: Byte = 0;
const
MACRO_NOTE_RETRIG_FLAG = $80;
MACRO_ENVELOPE_RESTART_FLAG = $40;
MACRO_ZERO_FREQ_FLAG = $20;
const
current_order: Byte = 0;
current_pattern: Byte = 0;
current_line: Byte = 0;
current_tremolo_depth: Byte = 0;
current_vibrato_depth: Byte = 0;
current_inst: Byte = 1;
current_octave: Byte = 4;
var
adt2_title: array[0..36] of String[18];
var
songdata_source: String;
instdata_source: String;
songdata_title: String;
var
songdata_crc,songdata_crc_ord: Longint;
temp_instrument: tADTRACK2_INS;
temp_instrument2: tADTRACK2_INS;
temp_instrument_macro: tREGISTER_TABLE;
temp_instrument_macro2: tREGISTER_TABLE;
temp_instrument_dis_fmreg_col: tDIS_FMREG_COL;
temp_instrument_dis_fmreg_col2: tDIS_FMREG_COL;
temp_ins_type: Byte;
pattord_page,pattord_hpos,pattord_vpos: Byte;
instrum_page: Byte;
pattern_patt,pattern_page,pattern_hpos: Byte;
limit_exceeded: Boolean;
load_flag,load_flag_alt: Byte;
reset_chan: array[1..20] of Boolean;
reset_adsrw: array[1..20] of Boolean;
ignore_note_once: array[1..20] of Boolean;
track_notes_ins: Boolean;
seek_pattern_break: Boolean;
var
speed_update,lockvol,panlock,lockVP: Boolean;
tremolo_depth,vibrato_depth: Byte;
volume_scaling,percussion_mode: Boolean;
last_order: Byte;
var
buf1: tGENERIC_IO_BUFFER;
buf2: tGENERIC_IO_BUFFER;
buf3: array[WORD] of Byte;
buf4: array[WORD] of Byte;
const
pattdata: ^tPATTERN_DATA = NIL;
var
old_hash_buffer: tOLD_VARIABLE_DATA1;
hash_buffer: tOLD_VARIABLE_DATA2;
old_songdata: tOLD_FIXED_SONGDATA;
dos_memavail: Word;
var
songdata: tFIXED_SONGDATA;
songdata_bak: tFIXED_SONGDATA;
temp_songdata: tFIXED_SONGDATA;
clipboard: tCLIPBOARD;
const
ptr_songdata: Pointer = Addr(songdata);
ptr_songdata_bak: Pointer = Addr(songdata_bak);
ptr_temp_songdata: Pointer = Addr(ptr_temp_songdata);
ptr_clipboard: Pointer = Addr(clipboard);
var
song_timer,timer_temp: Longint;
song_timer_tenths: Longint;
ticks,tick0,tickD,tickXF: Longint;
time_playing,time_playing0: Real;
const
{$IFDEF GO32V2}
timer_determinator: Longint = 1;
timer_det2: Longint = 1;
scr_scroll_x: Word = 0;
old_scr_scroll_x: Word = 0;
scr_scroll_y: Word = 0;
old_scr_scroll_y: Word = 0;
{$ELSE}
screen_scroll_offset: Word = 0;
{$ENDIF}
var
common_flag_backup: Byte;
volume_scaling_backup: Boolean;
event_table_backup: array[1..20] of tCHUNK;
freq_table_backup,freqtable2_backup: array[1..20] of Word;
keyoff_loop_backup: array[1..20] of Boolean;
channel_flag_backup: array[1..20] of Boolean;
fmpar_table_backup: array[1..20] of tFM_PARAMETER_TABLE;
volume_table_backup: array[1..20] of Word;
pan_lock_backup: array[1..20] of Boolean;
volume_lock_backup: array[1..20] of Boolean;
peak_lock_backup: array[1..20] of Boolean;
panning_table_backup: array[1..20] of Byte;
voice_table_backup: array[1..20] of Byte;
flag_4op_backup: Byte;
status_backup: Record
replay_forbidden: Boolean;
play_status: tPLAY_STATUS;
end;
function nFreq(note: Byte): Word;
procedure change_freq(chan: Byte; freq: Word);
function calc_pattern_pos(pattern: Byte): Byte;
function concw(lo,hi: Byte): Word;
function ins_parameter(ins,param: Byte): Byte;
function is_chan_adsr_data_empty(chan: Byte): Boolean;
function is_ins_adsr_data_empty(ins: Byte): Boolean;
function scale_volume(volume,scale_factor: Byte): Byte;
function _macro_speedup: Word;
procedure calibrate_player(order,line: Byte; status_filter: Boolean;
line_dependent: Boolean);
procedure update_timer(Hz: Word);
procedure key_on(chan: Byte);
procedure key_off(chan: Byte);
procedure release_sustaining_sound(chan: Byte);
procedure init_macro_table(chan,note,ins: Byte; freq: Word);
procedure set_ins_volume(modulator,carrier,chan: Byte);
procedure update_modulator_adsrw(chan: Byte);
procedure update_carrier_adsrw(chan: Byte);
procedure update_fmpar(chan: Byte);
procedure reset_chan_data(chan: Byte);
procedure poll_proc;
procedure macro_poll_proc;
procedure init_buffers;
procedure init_player;
procedure reset_player;
procedure start_playing;
procedure stop_playing;
procedure update_song_position;
procedure change_frequency(chan: Byte; freq: Word);
procedure set_global_volume;
procedure set_ins_data(ins,chan: Byte);
procedure init_timer_proc;
procedure done_timer_proc;
procedure realtime_gfx_poll_proc;
procedure decay_bars_refresh;
procedure status_refresh;
procedure trace_update_proc;
function hscroll_bar(x,y: Byte; size: Byte; len1,len2,pos: Word;
atr1,atr2: Byte): Byte;
function vscroll_bar(x,y: Byte; size: Byte; len1,len2,pos: Word;
atr1,atr2: Byte): Byte;
procedure centered_frame(var xstart,ystart: Byte; hsize,vsize: Byte;
name: String; atr1,atr2: Byte; border: String);
procedure get_chunk(pattern,line,channel: Byte; var chunk: tCHUNK);
procedure put_chunk(pattern,line,channel: Byte; chunk: tCHUNK);
function get_chanpos(var data; channels,scancode: Byte): Byte;
function get_chanpos2(var data; channels,scancode: Byte): Byte;
function count_channel(hpos: Byte): Byte;
function count_pos(hpos: Byte): Byte;
function calc_max_speedup(tempo: Byte): Word;
function calc_bpm_speed(tempo,speed,rows_per_beat: Byte): Real;
function calc_realtime_bpm_speed(tempo,speed,rows_per_beat: Byte): Real;
function calc_order_jump: Integer;
function calc_following_order(order: Byte): Integer;
function is_4op_chan(chan: Byte): Boolean;
procedure count_order(var entries: Byte);
procedure count_patterns(var patterns: Byte);
procedure count_instruments(var instruments: Byte);
procedure init_old_songdata;
procedure init_songdata;
procedure update_instr_data(ins: Byte);
procedure load_instrument(var data; chan: Byte);
procedure output_note(note,ins,chan: Byte;
restart_macro,restart_adsr: Boolean);
function min(value: Longint; minimum: Longint): Longint;
function max(value: Longint; maximum: Longint): Longint;
function asciiz_string(str: String): String;
procedure TimerSetup(Hz: Longint);
const
block_xstart: Byte = 1;
block_ystart: Byte = 0;
const
block_x0: Byte = 0;
block_y0: Byte = 1;
block_x1: Byte = 0;
block_y1: Byte = 1;
function is_in_block(x0,y0,x1,y1: Byte): Boolean;
procedure fade_out_playback(fade_screen: Boolean);
const
ticklooper: Longint = 0;
macro_ticklooper: Longint = 0;
const
{$IFDEF GO32V2}
MAX_NUM_BANK_POSITIONS = 1000;
{$ELSE}
MAX_NUM_BANK_POSITIONS = 500;
{$ENDIF}
const
bank_position_list_size: Longint = 0;
var
bank_position_list:
array[1..MAX_NUM_BANK_POSITIONS] of Record
{$IFDEF GO32V2}
bank_name: String[50];
{$ELSE}
bank_name: String;
{$ENDIF}
bank_size: Longint;
bank_position: Longint;
end;
function get_bank_position(bank_name: String; bank_size: Longint): Longint;
procedure add_bank_position(bank_name: String; bank_size: Longint; bank_position: Longint);
{$IFDEF GO32V2}
const
___UNIT_DATA_END___: Dword = 0;
{$ENDIF}
implementation
uses
{$IFDEF GO32V2}
CRT,DOS,GO32,ISS_Tim,
{$ELSE}
DOS,SDL_Types,SDL_Timer,
{$ENDIF}
AdT2opl3,AdT2sys,AdT2extn,AdT2ext2,AdT2keyb,
TxtScrIO,StringIO,DialogIO,ParserIO;
{$IFDEF GO32V2}
const
___IRQ_DATA_START___: Dword = 0;
procedure ___IRQ_CODE_START___; begin end;
{$ENDIF}
const
FreqStart = $156;
FreqEnd = $2ae;
FreqRange = FreqEnd-FreqStart;
function nFreq(note: Byte): Word;
const
Fnum: array[0..11] of Word = (
$157,$16b,$181,$198,$1b0,$1ca,$1e5,$202,$220,$241,$263,$287);
var
result: Word;
begin
asm
xor ebx,ebx
mov al,[note]
xor ah,ah
cmp ax,12*8
jae @@1
push eax
mov bl,12
div bl
mov bl,ah
xor bh,bh
shl bx,1
pop eax
mov cl,12
div cl
xor ah,ah
shl ax,10
add ax,word ptr [Fnum+ebx]
jmp @@2
@@1: mov ax,7
shl ax,10
add ax,FreqEnd
@@2: mov result,ax
end;
nFreq := result;
end;
function calc_freq_shift_up(freq,shift: Word): Word;
var
result: Word;
begin
asm
mov cx,freq
mov ax,shift
mov bx,cx
and bx,0000001111111111b
mov dx,cx
and dx,0001110000000000b
add bx,ax
and cx,1110000000000000b
shr dx,10
cmp bx,FreqEnd
jb @@2
cmp dx,7
jnz @@1
mov bx,FreqEnd
jmp @@2
@@1: sub bx,FreqRange
inc dx
@@2: mov ax,cx
shl dx,10
add ax,dx
add ax,bx
mov result,ax
end;
calc_freq_shift_up := result;
end;
function calc_freq_shift_down(freq,shift: Word): Word;
var
result: Word;
begin
asm
mov cx,freq
mov ax,shift
mov bx,cx
and bx,0000001111111111b
mov dx,cx
and dx,0001110000000000b
sub bx,ax
and cx,1110000000000000b
shr dx,10
cmp bx,FreqStart
ja @@2
or dx,dx
jnz @@1
mov bx,FreqStart
jmp @@2
@@1: add bx,FreqRange
dec dx
@@2: mov ax,cx
shl dx,10
add ax,dx
add ax,bx
mov result,ax
end;
calc_freq_shift_down := result;
end;
function calc_vibtrem_shift(chan: Byte; var table_data): Word;
var
pos,dir,depth: Byte;
result: Word;
begin
pos := tVIBRATO_TREMOLO_TABLE(table_data)[chan].pos;
depth := tVIBRATO_TREMOLO_TABLE(table_data)[chan].depth;
asm
xor ebx,ebx
mov al,depth
xor ah,ah
mov bl,pos
xor bh,bh
mov dh,bl
mov cl,byte ptr [vibtrem_table_size]
dec cl
and bl,cl
mov dl,byte ptr [vibtrem_table+ebx]
mul dl
rol ax,1
xchg ah,al
and ah,1
mov dir,1
mov dl,byte ptr [vibtrem_table_size]
test dh,dl
jne @@1
mov dir,0
@@1: mov result,ax
end;
tVIBRATO_TREMOLO_TABLE(table_data)[chan].dir := dir;
calc_vibtrem_shift := result;
end;
procedure change_freq(chan: Byte; freq: Word);
begin
If is_4op_chan(chan) and (chan in _4op_tracks_hi) then
begin
freq_table[SUCC(chan)] := freq_table[chan];
freqtable2[SUCC(chan)] := freqtable2[chan];
chan := SUCC(chan);
end;
asm
xor ebx,ebx
mov bl,chan
dec ebx
shl ebx,1
mov ax,freq
and ax,1fffh
mov dx,word ptr [freq_table+ebx]
and dx,NOT 1fffh
add ax,dx
mov word ptr [freq_table+ebx],ax
mov word ptr [freqtable2+ebx],ax
shr ebx,1
cmp byte ptr [channel_flag+ebx],TRUE
jnz @@1
shl ebx,1
xor edx,edx
mov dx,word ptr [_chan_n+ebx]
add dx,0a0h
push edx
xor edx,edx
mov dl,al
push edx
mov dx,word ptr [_chan_n+ebx]
add dx,0b0h
push edx
xor edx,edx
mov dl,ah
push edx
call opl3out
call opl3out
@@1:
end;
If is_4op_chan(chan) then
begin
freq_table[PRED(chan)] := freq_table[chan];
freqtable2[PRED(chan)] := freqtable2[chan];
end;
end;
function ins_parameter(ins,param: Byte): Byte;
var
result: Byte;
begin
asm
xor ebx,ebx
lea esi,[songdata.instr_data]
mov bl,ins
dec ebx
mov eax,INSTRUMENT_SIZE
mul ebx
add esi,eax
mov bl,param
add esi,ebx
lodsb
mov result,al
end;
ins_parameter := result;
end;
function is_chan_adsr_data_empty(chan: Byte): Boolean;
begin
is_chan_adsr_data_empty :=
(fmpar_table[chan].adsrw_car.attck = 0) and
(fmpar_table[chan].adsrw_mod.attck = 0) and
(fmpar_table[chan].adsrw_car.dec = 0) and
(fmpar_table[chan].adsrw_mod.dec = 0) and
(fmpar_table[chan].adsrw_car.sustn = 0) and
(fmpar_table[chan].adsrw_mod.sustn = 0) and
(fmpar_table[chan].adsrw_car.rel = 0) and
(fmpar_table[chan].adsrw_mod.rel = 0);
end;
function is_ins_adsr_data_empty(ins: Byte): Boolean;
begin
is_ins_adsr_data_empty :=
(ins_parameter(ins,5) SHR 4 = 0) and
(ins_parameter(ins,4) SHR 4 = 0) and
(ins_parameter(ins,5) AND $0f = 0) and
(ins_parameter(ins,4) AND $0f = 0) and
(ins_parameter(ins,7) SHR 4 = 0) and
(ins_parameter(ins,6) SHR 4 = 0) and
(ins_parameter(ins,7) AND $0f = 0) and
(ins_parameter(ins,6) AND $0f = 0);
end;
function is_data_empty(var buf; size: Longint): Boolean;
var
result: Boolean;
begin
asm
xor edx,edx
mov eax,size
cmp eax,16
jb @@3
mov ecx,4
div ecx
mov ecx,eax
jecxz @@1
mov edi,[buf]
xor eax,eax
repz scasd
jnz @@2
mov ecx,edx
jecxz @@1
repz scasb
jnz @@2
@@1: mov result,TRUE
jmp @@6
@@2: mov result,FALSE
jmp @@6
@@3: mov ecx,size
jecxz @@4
mov edi,[buf]
xor eax,eax
repz scasb
jnz @@5
@@4: mov result,TRUE
jmp @@6
@@5: mov result,FALSE
@@6:
end;
is_data_empty := result;
end;
function min(value: Longint; minimum: Longint): Longint;
begin
If (value > minimum) then min := value
else min := minimum;
end;
function max(value: Longint; maximum: Longint): Longint;
begin
If (value < maximum) then max := value
else max := maximum;
end;
function asciiz_string(str: String): String;
begin
If (Pos(#0,str) <> 0) then asciiz_string := Copy(str,1,Pos(#0,str)-1)
else asciiz_string := '';
end;
function concw(lo,hi: Byte): Word;
begin
concw := lo+(hi SHL 8);
end;
procedure synchronize_song_timer;
begin
song_timer := TRUNC(time_playing);
song_timer_tenths := TRUNC(time_playing*100) MOD 100;
timer_temp := song_timer_tenths;
end;
procedure change_frequency(chan: Byte; freq: Word);
begin
macro_table[chan].vib_paused := TRUE;
change_freq(chan,freq);
If is_4op_chan(chan) then
If (chan in _4op_tracks_hi) then
begin
macro_table[SUCC(chan)].vib_count := 1;
macro_table[SUCC(chan)].vib_pos := 0;
macro_table[SUCC(chan)].vib_freq := freq;
macro_table[SUCC(chan)].vib_paused := FALSE;
end
else
begin
macro_table[PRED(chan)].vib_count := 1;
macro_table[PRED(chan)].vib_pos := 0;
macro_table[PRED(chan)].vib_freq := freq;
macro_table[PRED(chan)].vib_paused := FALSE;
end;
macro_table[chan].vib_count := 1;
macro_table[chan].vib_pos := 0;
macro_table[chan].vib_freq := freq;
macro_table[chan].vib_paused := FALSE;
end;
procedure update_timer(Hz: Word);
begin
{$IFDEF GO32V2}
_last_debug_str_ := _debug_str_;
_debug_str_ := 'ADT2UNIT.PAS:update_timer';
{$ENDIF}
If (Hz = 0) then begin TimerSetup(18); EXIT end
else tempo := Hz;
If (tempo = 18) and timer_fix then IRQ_freq := TRUNC((tempo+0.2)*20)
else IRQ_freq := 250;
While (IRQ_freq MOD (tempo*_macro_speedup) <> 0) do Inc(IRQ_freq);
If (IRQ_freq > MAX_IRQ_FREQ) then IRQ_freq := MAX_IRQ_FREQ;
While (IRQ_freq+IRQ_freq_shift+playback_speed_shift > MAX_IRQ_FREQ) and
(playback_speed_shift > 0) do
Dec(playback_speed_shift);
While (IRQ_freq+IRQ_freq_shift+playback_speed_shift > MAX_IRQ_FREQ) and
(IRQ_freq_shift > 0) do
Dec(IRQ_freq_shift);
TimerSetup(max(IRQ_freq+IRQ_freq_shift+playback_speed_shift,MAX_IRQ_FREQ));
end;
procedure update_playback_speed(speed_shift: Longint);
begin
{$IFDEF GO32V2}
_last_debug_str_ := _debug_str_;
_debug_str_ := 'ADT2UNIT.PAS:update_playback_speed';
{$ENDIF}
If (speed_shift = 0) then EXIT
else If (speed_shift > 0) and (IRQ_freq+playback_speed_shift+speed_shift > MAX_IRQ_FREQ) then
While (IRQ_freq+IRQ_freq_shift+playback_speed_shift+speed_shift > MAX_IRQ_FREQ) do
Dec(speed_shift)
else If (speed_shift < 0) and (IRQ_freq+IRQ_freq_shift+playback_speed_shift+speed_shift < MIN_IRQ_FREQ) then
While (IRQ_freq+IRQ_freq_shift+playback_speed_shift+speed_shift < MIN_IRQ_FREQ) do
Inc(speed_shift);
playback_speed_shift := playback_speed_shift+speed_shift;
update_timer(tempo);
end;
procedure key_on(chan: Byte);
begin
If NOT (is_4op_chan(chan) and (chan in _4op_tracks_hi)) then
opl3out($0b0+_chan_n[chan],0)
else opl3out($0b0+_chan_n[SUCC(chan)],0);
end;
procedure key_off(chan: Byte);
begin
freq_table[chan] := LO(freq_table[chan])+
(HI(freq_table[chan]) AND NOT $20) SHL 8;
change_freq(chan,freq_table[chan]);
event_table[chan].note := event_table[chan].note OR keyoff_flag;
end;
procedure release_sustaining_sound(chan: Byte);
begin
opl3out(_instr[02]+_chan_m[chan],63);
opl3out(_instr[03]+_chan_c[chan],63);
FillChar(fmpar_table[chan].adsrw_car,
SizeOf(fmpar_table[chan].adsrw_car),0);
FillChar(fmpar_table[chan].adsrw_mod,
SizeOf(fmpar_table[chan].adsrw_mod),0);
key_on(chan);
opl3out(_instr[04]+_chan_m[chan],BYTE_NULL);
opl3out(_instr[05]+_chan_c[chan],BYTE_NULL);
opl3out(_instr[06]+_chan_m[chan],BYTE_NULL);
opl3out(_instr[07]+_chan_c[chan],BYTE_NULL);
key_off(chan);
event_table[chan].instr_def := 0;
reset_chan[chan] := TRUE;
end;
function scale_volume(volume,scale_factor: Byte): Byte;
begin
scale_volume := 63-Round((63-volume)/63*
(63-scale_factor));
end;
function _4op_data_flag(chan: Byte): Dword;
var
_4op_conn: Byte;
_4op_mode: Boolean;
_4op_ch1,_4op_ch2: Byte;
_4op_ins1,_4op_ins2: Byte;
begin
_4op_mode := FALSE;
If is_4op_chan(chan) then
begin
_4op_mode := TRUE;
If (chan in _4op_tracks_hi) then
begin
_4op_ch1 := chan;
_4op_ch2 := SUCC(chan);
end
else
begin
_4op_ch1 := PRED(chan);
_4op_ch2 := chan;
end;
_4op_ins1 := event_table[_4op_ch1].instr_def;
If (_4op_ins1 = 0) then _4op_ins1 := voice_table[_4op_ch1];
_4op_ins2 := event_table[_4op_ch2].instr_def;
If (_4op_ins2 = 0) then _4op_ins2 := voice_table[_4op_ch2];
If (_4op_ins1 <> 0) and (_4op_ins2 <> 0) then
begin
_4op_mode := TRUE;
_4op_conn := (pBYTE(@Addr(songdata.instr_data[_4op_ins1])^)[_instr_data_ofs[11]] AND 1) SHL 1 +
(pBYTE(@Addr(songdata.instr_data[_4op_ins2])^)[_instr_data_ofs[11]] AND 1);
end;
end;
{------+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+---}
{ BIT |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0 }
{------+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+---}
{ DATA |..|..|..|..|..|F7|F6|F5|F4|F3|F2|F1|F0|E7|E6|E5|E4|E3|E2|E1|E0|D3|D2|D1|D0|C3|C2|C1|C0|B1|B0|A0 }
{------+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+---}
_4op_data_flag := ORD(_4op_mode) + {1-bit: A0}
(_4op_conn AND 3) SHL 1 + {2-bit: B1-B0}
(_4op_ch1 AND 15) SHL 3 + {4-bit: C3-C0}
(_4op_ch2 AND 15) SHL 7 + {4-bit: D3-D0}
_4op_ins1 SHL 11 + {8-bit: E7-E0}
_4op_ins2 SHL 19; {8-bit: F7-F0}
end;
function _4op_vol_valid_chan(chan: Byte): Boolean;
var
_4op_flag: Dword;
begin
_4op_flag := _4op_data_flag(chan);
_4op_vol_valid_chan := BOOLEAN(_4op_flag AND 1) and vol4op_lock[chan] and
(BYTE(_4op_flag SHR 11) <> 0) and
(BYTE(_4op_flag SHR 19) <> 0);
end;
procedure set_ins_volume(modulator,carrier,chan: Byte);
var
temp: Byte;
begin
{$IFNDEF GO32V2}
// ** OPL3 emulation workaround **
// force muted instrument volume with missing channel ADSR data
// when there is additionally no FM-reg macro defined for this instrument
If is_chan_adsr_data_empty(chan) and
NOT (songdata.instr_macros[voice_table[chan]].length <> 0) then
begin
modulator := 63;
carrier := 63;
end;
{$ENDIF}
If (modulator <> BYTE_NULL) then
begin
temp := modulator;
If volume_scaling then
If (ins_parameter(voice_table[chan],10) AND 1 = 1) or
(percussion_mode and (chan in [17..20])) then
modulator := scale_volume(ins_parameter(voice_table[chan],2) AND $3f,modulator);
If (ins_parameter(voice_table[chan],10) AND 1 = 1) or
(percussion_mode and (chan in [17..20])) then
opl3out(_instr[02]+_chan_m[chan],
scale_volume(scale_volume(modulator,scale_volume(63-global_volume,63-fade_out_volume)),63-overall_volume)+LO(vscale_table[chan]))
else
opl3out(_instr[02]+_chan_m[chan],
temp+LO(vscale_table[chan]));
volume_table[chan] := concw(temp,HI(volume_table[chan]));
If (ins_parameter(voice_table[chan],10) AND 1 = 1) or
(percussion_mode and (chan in [17..20])) then
modulator_vol[chan] := 63-scale_volume(modulator,scale_volume(63-global_volume,63-fade_out_volume))
else modulator_vol[chan] := 63-modulator;
end;
If (carrier <> BYTE_NULL) then
begin
temp := carrier;
If volume_scaling then
carrier := scale_volume(ins_parameter(voice_table[chan],3) AND $3f,carrier);
opl3out(_instr[03]+_chan_c[chan],
scale_volume(scale_volume(carrier,scale_volume(63-global_volume,63-fade_out_volume)),63-overall_volume)+HI(vscale_table[chan]));
volume_table[chan] := concw(LO(volume_table[chan]),temp);
carrier_vol[chan] := 63-scale_volume(carrier,scale_volume(63-global_volume,63-fade_out_volume));
end;
end;
procedure set_ins_volume_4op(volume,chan: Byte);
var
_4op_flag: Dword;
_4op_conn: Byte;
_4op_ch1,_4op_ch2: Byte;
procedure set_volume(modulator,carrier,chan: Byte);
var
temp: Byte;
begin
{$IFNDEF GO32V2}
// OPL3 EMULATION WORKAROUND
// force muted instrument volume with missing channel ADSR data
// when there is additionally no FM-reg macro defined for this instrument
If is_chan_adsr_data_empty(chan) and
NOT (songdata.instr_macros[voice_table[chan]].length <> 0) then
begin
modulator := 63;
carrier := 63;
end;
{$ENDIF}