-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathadt2opl3.pas
664 lines (570 loc) · 18.1 KB
/
adt2opl3.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
// 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 AdT2opl3;
{$S-,Q-,R-,V-,B-,X+}
{$PACKRECORDS 1}
interface
{$IFDEF GO32V2}
const
___OPL3OUT_UNIT_DATA_START___: Dword = 0;
{$ENDIF}
procedure opl2out(reg,data: Word);
procedure opl3out_proc(reg,data: Word);
procedure opl3exp(data: Word);
type
tOPL3OUT_proc = procedure(reg,data: Word);
const
opl3out: tOPL3OUT_proc = opl3out_proc;
{$IFDEF GO32V2}
const
opl3port: Word = 0;
opl_latency: Byte = 0;
function detect_OPL3: Boolean;
{$ELSE}
const
renew_wav_files_flag: Boolean = TRUE;
opl3_channel_recording_mode: Boolean = FALSE;
opl3_record_channel: array[1..20] of Boolean = (
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE);
procedure flush_WAV_data;
procedure opl3_init;
procedure opl3_done;
procedure snd_init;
procedure snd_done;
procedure snd_SetTimer(value: Longint);
procedure update_recorded_channels;
const
opl3_flushmode: Boolean = FALSE;
const
WAV_BUFFER_SIZE = 18*512*1024; // cache buffer size -> 512k per file
var
wav_buffer_len: Longint;
wav_buffer: array[0..18,0..PRED(WAV_BUFFER_SIZE)] of Byte;
{$ENDIF}
{$IFDEF GO32V2}
const
___OPL3OUT_UNIT_DATA_END___: Dword = 0;
{$ENDIF}
implementation
{$IFDEF GO32V2}
uses
GO32,
AdT2sys,
TxtScrIO;
procedure ___OPL3OUT_IRQ_CODE_START___; begin end;
var
_opl_regs_cache: array[WORD] of Word;
procedure opl2out(reg,data: Word);
begin
If (_opl_regs_cache[reg] <> data) then
_opl_regs_cache[reg] := data
else EXIT;
asm
mov ax,reg
mov dx,word ptr [opl3port]
or ah,ah
jz @@1
add dx,2
@@1: out dx,al
mov ecx,6
@@2: in al,dx
loop @@2
inc dl
mov ax,data
out dx,al
dec dl
mov ecx,36
@@3: in al,dx
loop @@3
end;
end;
procedure opl3out_proc(reg,data: Word);
begin
If (_opl_regs_cache[reg] <> data) then
_opl_regs_cache[reg] := data
else EXIT;
asm
mov ax,reg
mov dx,word ptr [opl3port]
or ah,ah
jz @@1
add dx,2
@@1: out dx,al
inc dl
mov ax,data
out dx,al
dec dl
mov ecx,26
@@2: in al,dx
loop @@2
end;
end;
procedure opl3exp(data: Word);
begin
if (_opl_regs_cache[(data AND $ff) OR $100] <> data SHR 8) then
_opl_regs_cache[(data AND $ff) OR $100] := data SHR 8
else EXIT;
asm
mov ax,data
mov dx,word ptr [opl3port]
add dx,2
out dx,al
mov ecx,6
@@1: in al,dx
loop @@1
inc dl
mov al,ah
out dx,al
mov ecx,36
@@2: in al,dx
loop @@2
end;
end;
procedure ___OPL3OUT_IRQ_CODE_END___; begin end;
function detect_OPL3: Boolean;
var
result: Boolean;
begin
_last_debug_str_ := _debug_str_;
_debug_str_ := 'ADT2OPL3.PAS:detect_OPL3';
asm
push dword 04h
push dword 80h
push dword 04h
push dword 60h
call opl2out
call WaitRetrace
call opl2out
call WaitRetrace
mov dx,opl3port
in al,dx
and al,0e0h
mov bl,al
push dword 04h
push dword 21h
push dword 02h
push dword 0ffh
call opl2out
call WaitRetrace
call opl2out
call WaitRetrace
mov dx,opl3port
in al,dx
and al,0e0h
mov bh,al
cmp bx,0c000h
jnz @@1
push dword 04h
push dword 80h
push dword 04h
push dword 60h
call opl2out
call WaitRetrace
call opl2out
call WaitRetrace
mov dx,opl3port
in al,dx
and al,6
or al,al
jnz @@1
mov result,TRUE
jmp @@2
@@1: mov result,FALSE
@@2:
end;
detect_OPL3 := result;
end;
{$ELSE}
uses
MATH,SysUtils,
AdT2unit,AdT2sys,TxtScrIO,StringIO,
SDL_Types,SDL_Audio,OPL3EMU;
const
opl3_sample_buffer_ptr: Pointer = NIL;
opl3_sample_buffer_chan_ptr: array[1..18] of Pointer = (
NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL,NIL);
var
sample_frame_size: Longint;
sdl_audio_spec: SDL_AudioSpec;
procedure flush_WAV_data;
type
tWAV_HEADER = Record
file_desc: array[0..3] of Char; // =="RIFF"
file_size: Dword; // ==filesize-8
wav_desc: array[0..3] of Char; // =="WAVE"
format_desc: array[0..3] of Char; // =="fmt "
wav_size: Dword; // ==16
wav_type: Word; // ==1 (PCM)
num_channels: Word; // ==2 (Stereo)
samples_sec: Dword; // sampling frequency
bytes_sec: Dword; // ==num_channels*samples_sec*bits_sample/8
block_align: Word; // ==num_channels*bits_sample/8
bits_sample: Word; // ==16
data_desc: array[0..3] of Char; // "data"
data_size: Dword; // size of data
end;
const
wav_header: tWAV_HEADER = (file_desc: 'RIFF';
file_size: SizeOf(tWAV_HEADER)-8;
wav_desc: 'WAVE';
format_desc: 'fmt ';
wav_size: 16;
wav_type: 1;
num_channels: 2;
samples_sec: 49716;
bytes_sec: 2*49716*16 DIV 8;
block_align: 2*16 DIV 8;
bits_sample: 16;
data_desc: 'data';
data_size: 0);
var
wav_file: File;
temp,bytes_to_write: Longint;
idx,from_ch,to_ch: Byte;
filename_suffix: String;
begin
bytes_to_write := wav_buffer_len;
// flush when at least 1 sec of recorded data
If (bytes_to_write < 2*sdl_sample_rate*16 DIV 8) then EXIT;
If NOT ((play_status = isPlaying) and (sdl_opl3_emulator <> 0)) then EXIT;
// prepare output directory
If NOT DirectoryExists(Copy(sdl_wav_directory,1,Length(sdl_wav_directory)-Length(NameOnly(sdl_wav_directory)))) then
If NOT CreateDir(Copy(sdl_wav_directory,1,Length(sdl_wav_directory)-Length(NameOnly(sdl_wav_directory)))) then
EXIT;
wav_buffer_len := 0;
If NOT opl3_channel_recording_mode then
begin
from_ch := 0;
to_ch := 0;
end
else
begin
from_ch := 1;
If NOT percussion_mode then to_ch := songdata.nm_tracks
else to_ch := 18;
end;
For idx := from_ch to to_ch do
begin
filename_suffix := '';
If (idx <> 0) then
If NOT (opl3_record_channel[idx]) or (is_4op_chan(idx) and NOT (idx in _4op_tracks_hi)) then CONTINUE
else If NOT is_4op_chan(idx) then
If percussion_mode then
Case idx of
16: filename_suffix := ' ('+ExpStrL(Num2str(idx,10),2,'0')+'_BD)';
17: filename_suffix := ' ('+ExpStrL(Num2str(idx,10),2,'0')+'_SD_HH)';
18: filename_suffix := ' ('+ExpStrL(Num2str(idx,10),2,'0')+'_TT_TC)';
else filename_suffix := ' ('+ExpStrL(Num2str(idx,10),2,'0')+')';
end
else filename_suffix := ' ('+ExpStrL(Num2str(idx,10),2,'0')+')'
else filename_suffix := ' ('+ExpStrL(Num2str(idx,10),2,'0')+'_'
+ExpStrL(Num2str(idx+1,10),2,'0')+'_4OP)';
If opl3_flushmode then
Assign(wav_file,Copy(sdl_wav_directory,1,Length(sdl_wav_directory)-Length(NameOnly(sdl_wav_directory)))+
BaseNameOnly(sdl_wav_directory)+filename_suffix+'.wav')
else Assign(wav_file,sdl_wav_directory+BaseNameOnly(songdata_title)+filename_suffix+'.wav');
// update WAV header
{$i-}
If renew_wav_files_flag then
begin
RewriteF(wav_file);
wav_header.file_size := 0;
wav_header.data_size := 0;
end
else ResetF(wav_file);
{$i+}
If renew_wav_files_flag or (IOresult <> 0) then
begin
{$i-}
RewriteF(wav_file);
{$i+}
If (IOresult <> 0) then
begin
Close(wav_file);
{$i-}
EraseF(wav_file);
{$i+}
If (IOresult <> 0) then ;
EXIT;
end;
wav_header.samples_sec := sdl_sample_rate;
wav_header.bytes_sec := 2*sdl_sample_rate*16 DIV 8;
wav_header.file_size := bytes_to_write;
wav_header.data_size := bytes_to_write;
{$i-}
BlockWriteF(wav_file,wav_header,SizeOf(wav_header),temp);
{$i+}
If (IOresult <> 0) or
(temp <> SizeOf(wav_header)) then
begin
CloseF(wav_file);
{$i-}
EraseF(wav_file);
{$i+}
If (IOresult <> 0) then ;
EXIT;
end;
end
else begin
{$i-}
BlockReadF(wav_file,wav_header,SizeOf(wav_header),temp);
{$i+}
If (IOresult <> 0) or
(temp <> SizeOf(wav_header)) then
begin
CloseF(wav_file);
{$i-}
EraseF(wav_file);
{$i+}
If (IOresult <> 0) then ;
EXIT;
end;
wav_header.file_size := wav_header.file_size+bytes_to_write;
wav_header.data_size := wav_header.data_size+bytes_to_write;
{$i-}
ResetF_RW(wav_file);
{$i+}
If (IOresult <> 0) then
begin
CloseF(wav_file);
{$i-}
EraseF(wav_file);
{$i+}
If (IOresult <> 0) then ;
EXIT;
end;
{$i-}
BlockWriteF(wav_file,wav_header,SizeOf(wav_header),temp);
{$i+}
If (IOresult <> 0) or
(temp <> SizeOf(wav_header)) then
begin
CloseF(wav_file);
{$i-}
EraseF(wav_file);
{$i+}
If (IOresult <> 0) then ;
EXIT;
end;
{$i-}
SeekF(wav_file,FileSize(wav_file));
{$i+}
If (IOresult <> 0) then
begin
CloseF(wav_file);
{$i-}
EraseF(wav_file);
{$i+}
If (IOresult <> 0) then ;
EXIT;
end;
end;
// write sample data
{$i-}
BlockWriteF(wav_file,wav_buffer[idx],bytes_to_write,temp);
{$i+}
If (IOresult <> 0) or (temp <> bytes_to_write) then
begin
CloseF(wav_file);
{$i-}
EraseF(wav_file);
{$i+}
If (IOresult <> 0) then ;
end
else
CloseF(wav_file);
end;
renew_wav_files_flag := FALSE;
end;
procedure opl2out(reg,data: Word);
begin
// relevant only for DOS version -> option opl_latency=1
opl3out_proc(reg,data);
end;
procedure opl3out_proc(reg,data: Word);
begin
OPL3EMU_WriteReg(reg,data);
end;
procedure opl3exp(data: Word);
begin
OPL3EMU_WriteReg((data AND $ff) OR $100,data SHR 8);
end;
procedure opl3_init;
begin
OPL3EMU_init;
end;
procedure opl3_done;
begin
SDL_PauseAudio(1);
end;
// value in Hz for timer
procedure snd_SetTimer(value: Longint);
begin
If (value < 18) then value := 18;
sample_frame_size := ROUND(sdl_sample_rate/value*(1+sdl_timer_slowdown/100));
end;
function get_num_files: Byte;
var
idx,result: Byte;
begin
result := 18;
For idx := 1 to 18 do
If NOT opl3_record_channel[idx] then Dec(result);
If (result <> 0) then get_num_files := result
else get_num_files := 1;
end;
procedure update_recorded_channels;
var
idx: Byte;
begin
For idx := 1 to 20 do
If channel_flag[idx] then opl3_record_channel[idx] := TRUE
else opl3_record_channel[idx] := FALSE;
For idx := SUCC(songdata.nm_tracks) to 20 do
opl3_record_channel[idx] := FALSE;
If percussion_mode then
begin
If NOT channel_flag[19] then opl3_record_channel[18] := FALSE;
If NOT channel_flag[20] then opl3_record_channel[17] := FALSE;
end;
end;
procedure playcallback(var userdata; stream: pByte; len: Longint); cdecl;
const
counter_idx: Longint = 0;
var
counter: Longint;
idx: Byte;
IRQ_freq_val: Longint;
buffer_ptr_table: array[1..18] of pDword;
buf_size: Longint;
begin
If NOT rewind then
IRQ_freq_val := IRQ_freq
else IRQ_freq_val := IRQ_freq * 20;
For counter := 0 to PRED(len DIV 4) do
begin
Inc(counter_idx);
If (counter_idx >= sample_frame_size) then
begin
counter_idx := 0;
If (ticklooper > 0) then
If (fast_forward or rewind) and NOT replay_forbidden then
poll_proc
else
else If NOT replay_forbidden then
poll_proc;
If (macro_ticklooper = 0) then
macro_poll_proc;
Inc(ticklooper);
If (ticklooper >= IRQ_freq_val DIV tempo) then
ticklooper := 0;
Inc(macro_ticklooper);
If (macro_ticklooper >= IRQ_freq_val DIV (tempo*macro_speedup)) then
macro_ticklooper := 0;
end;
// update partial channel sample buffer pointers
For idx := 1 to 18 do
buffer_ptr_table[idx] := opl3_sample_buffer_chan_ptr[idx]+counter*4;
// update one step
OPL3EMU_PollProc(opl3_sample_buffer_ptr+counter*4,buffer_ptr_table);
end;
// update SDL Audio sample buffer
Move(opl3_sample_buffer_ptr^,stream^,len);
If (play_status = isStopped) then
begin
wav_buffer_len := 0;
EXIT;
end;
// calculate cache buffer size
If opl3_channel_recording_mode then
buf_size := WAV_BUFFER_SIZE DIV 18 * get_num_files
else buf_size := WAV_BUFFER_SIZE DIV 18;
// WAV dumper
If (sdl_opl3_emulator <> 0) then
If (wav_buffer_len+len <= buf_size) then
begin
// update main sample buffer
Move(opl3_sample_buffer_ptr^,wav_buffer[0][wav_buffer_len],len);
// update partial channel sample buffers
For idx := 1 to 18 do
Move(opl3_sample_buffer_chan_ptr[idx]^,wav_buffer[idx][wav_buffer_len],len);
Inc(wav_buffer_len,len);
end
else
begin
// sample buffers full -> flush to disk!
flush_WAV_data;
// update main sample buffer
Move(opl3_sample_buffer_ptr^,wav_buffer[0][wav_buffer_len],len);
// update partial channel sample buffers
For idx := 1 to 18 do
Move(opl3_sample_buffer_chan_ptr[idx]^,wav_buffer[idx][wav_buffer_len],len);
Inc(wav_buffer_len,len);
end;
end;
procedure snd_init;
var
idx: Byte;
begin
GetMem(opl3_sample_buffer_ptr,sdl_sample_buffer*4);
For idx := 1 to 18 do GetMem(opl3_sample_buffer_chan_ptr[idx],sdl_sample_buffer*4);
sample_frame_size := ROUND(sdl_sample_rate/50*(1+sdl_timer_slowdown/100));;
opl3_init;
sdl_audio_spec.freq := sdl_sample_rate;
sdl_audio_spec.format := AUDIO_S16;
sdl_audio_spec.channels := 2;
sdl_audio_spec.samples := sdl_sample_buffer;
@sdl_audio_spec.callback := @playcallback;
sdl_audio_spec.userdata := NIL;
If (SDL_Openaudio(@sdl_audio_spec,NIL) < 0) then
begin
WriteLn('SDL: Audio initialization error');
HALT(1);
end;
WriteLn(' Sample buffer size: ',sdl_audio_spec.samples,' samples (requested ',sdl_sample_buffer,')');
WriteLn(' Sampling rate: ',sdl_audio_spec.freq,' Hz (requested ',sdl_sample_rate,')');
sdl_sample_rate := sdl_audio_spec.freq;
sdl_sample_buffer := sdl_audio_spec.samples;
SDL_PauseAudio(0);
end;
procedure snd_done;
var
idx: Byte;
begin
SDL_PauseAudio(1);
SDL_CloseAudio;
FreeMem(opl3_sample_buffer_ptr);
For idx := 1 to 18 do FreeMem(opl3_sample_buffer_chan_ptr[idx]);
opl3_sample_buffer_ptr := NIL;
end;
{$ENDIF}
{$IFDEF GO32V2}
var
old_exit_proc: procedure;
procedure new_exit_proc;
begin
Lock_Data(___OPL3OUT_UNIT_DATA_START___,DWORD(Addr(___OPL3OUT_UNIT_DATA_END___))-DWORD(Addr(___OPL3OUT_UNIT_DATA_START___)));
Lock_Code(@___OPL3OUT_IRQ_CODE_START___,DWORD(@___OPL3OUT_IRQ_CODE_END___)-DWORD(@___OPL3OUT_IRQ_CODE_START___));
ExitProc := @old_exit_proc;
end;
begin
FillWord(_opl_regs_cache,SizeOf(_opl_regs_cache) DIV SizeOf(WORD),NOT 0);
Lock_Data(___OPL3OUT_UNIT_DATA_START___,DWORD(Addr(___OPL3OUT_UNIT_DATA_END___))-DWORD(Addr(___OPL3OUT_UNIT_DATA_START___)));
Lock_Code(@___OPL3OUT_IRQ_CODE_START___,DWORD(@___OPL3OUT_IRQ_CODE_END___)-DWORD(@___OPL3OUT_IRQ_CODE_START___));
@old_exit_proc := ExitProc;
ExitProc := @new_exit_proc;
{$ENDIF}
end.