-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMODULES.PAS
851 lines (623 loc) · 16.9 KB
/
MODULES.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
{$G-,N-,E-}
{**************************************************************
Modules.pas - Loading and saving songs
Author: Jan Knipperts
Program: TNDY-Tracker
Version: 1.15
***************************************************************
}
Unit Modules;
interface
uses global,play,tndy,tracker,textmode,keyboard,PIT,IE;
Procedure Proceed_Errors;
Procedure Save_Song;
Procedure LoadTNDY(filename : string);
implementation
Procedure Proceed_Errors;
begin
if (error > 0) and (error < 6) then
begin
CopyBuffer(buffer.blankscreen,buffer.screen2);
writeto(buffer.screen2);
box(10,7,70,12,palette.text_color1,palette.mark_color,false,true);
centeredtext(7,palette.text_color2,palette.mark_color,' Error! ');
case error of
1:
begin
centeredtext(9,palette.text_color1,palette.mark_color,'A disk error occured!');
end;
2:
begin
centeredtext(9,palette.text_color1,palette.mark_color,'File is corrupted or not a TNDY-Tracker module!');
end;
3:
begin
centeredtext(9,palette.text_color1,palette.mark_color,'File is corrupted or not a Protracker module!');
end;
4:
begin
centeredtext(9,palette.text_color1,palette.mark_color,'File is corrupted or no AGI 2.x music ressource!');
end;
5:
begin
centeredtext(9,palette.text_color1,palette.mark_color,'Not enough memory! ');
end;
end;
centeredtext(10,palette.text_color1,palette.mark_color,'Press any key to continue.');
displaybuffer(buffer.screen2);
Wait_for_Keypress;
Clear_Pattern_Data; {To make sure, there is no garbage loaded in our buffer}
Player.tempo := 50; {Default Player.tempo is 50 Hz}
Player.Speed := 6; {and a Player.Speed of 6 = 125 BPM}
set_timer(50);
Editor.orderview := -2;
Editor.Octave := 4;
Editor.chan := 1;
Player.Position := 0;
Player.row := 0;
Editor.curr_vol := 0;
Player.Songlength := 1;
Player.MaxPattern := 0;
fillchar(order,sizeof(order),0);
fillchar(msg_buffer,sizeof(msg_buffer),0);
msg_size := 0;
Editor.channels := 4;
writeto(buffer.screen1);
displaybuffer(buffer.screen1);
end;
end;
Procedure Compress_Pattern_Data;
var pc,tc,rc,erc,dummy,cv : byte;
Cevent : Comp_event;
begin
for pc := 0 to Player.MaxPattern do
begin
for tc := 0 to Editor.channels-1 do
begin
erc := 0;
for rc := 0 to 63 do
begin
fillchar(Cevent,sizeof(Cevent),0);
if (patterns[pc]^[rc][tc].note > 0) or
(patterns[pc]^[rc][tc].Octave > 0) or
(patterns[pc]^[rc][tc].volume > 0) or
(patterns[pc]^[rc][tc].effect > 0) or
(patterns[pc]^[rc][tc].ep > 0) then
begin
if (erc > 0) then
begin
dummy := $FF; {marker for empty row count}
{$I-}
blockwrite(f,dummy,1);
blockwrite(f,erc,1); {save number of empty rows}
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
erc := 0;
end;
if (tc < Editor.channels-1) then {normal voice data}
begin
cv := patterns[pc]^[rc][tc].volume;
if (cv = 0) and (patterns[pc]^[rc][tc].note = 0) then {if there is no volume, set note to 14}
Cevent.n_o := (14 shl 4) or patterns[pc]^[rc][tc].Octave
else
Cevent.n_o := (patterns[pc]^[rc][tc].note shl 4) or
patterns[pc]^[rc][tc].Octave; {if not, store the full information}
if (cv > 0) then dec(cv); {save volume as 0 to 15 in the file}
Cevent.v_e := (cv shl 4) or patterns[pc]^[rc][tc].effect;
Cevent.ep := patterns[pc]^[rc][tc].ep;
{$I-}
blockwrite(f,cevent,sizeof(cevent));
{$I+}
end
else
begin {Noise data}
cv := patterns[pc]^[rc][tc].volume;
if cv > 0 then dec(cv); {correct volume}
dummy := (patterns[pc]^[rc][tc].note shl 5) or cv;
if (patterns[pc]^[rc][tc].Octave = 2) then
dummy := dummy or $10; {set bit 4 for noise type: 0=Periodic, 1 = White noise}
{$I-}
blockwrite(f,dummy,1);
{$I+}
end;
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
end
else
begin
inc(erc);
if (rc = 63) then {complete track is empty}
begin
dummy := $FF; {marker for empty row count}
{$I-}
blockwrite(f,dummy,1);
blockwrite(f,erc,1); {save number of empty rows}
{$I+}
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
end;
end;
end; {row count}
end; {track count}
end; {pattern count}
end; {procedure}
Procedure Compress_Message;
var
mc,pc : word;
nc,c : word;
oldc : byte;
begin
fillchar(p_msg,sizeof(p_msg),0);
mc := 1;
pc := 1;
oldc := 1;
repeat;
if msg_buffer[mc] <> oldc then
begin
p_msg[pc] := msg_buffer[mc]; {put char in buffer}
oldc := msg_buffer[mc]; {save last charakter}
inc(pc);
inc(mc);
end
else
begin
nc := 1;
repeat
inc(nc);
inc(mc);
until (msg_buffer[mc] <> oldc) or
(mc > 400);
dec(nc);
if nc = 399 then {empty description?}
begin
msg_size := 0;
exit;
end;
if nc >= 2 then
begin
p_msg[pc] := 01;
if nc > $FF then
begin
p_msg[pc+1] := $FF;
p_msg[pc+2] := nc-$FF;
inc(pc,3);
end
else
begin
p_msg[pc+1] := nc;
inc(pc,2);
end;
end
else
begin
for c := 0 to nc-1 do
p_msg[pc+c] := oldc;
inc(pc,nc);
end;
end;
until mc > 400;
msg_size := pc;
end;
Procedure SaveTNDY(filename : string);
var
ncount : byte;
begin
if filename <> '.TND' then
begin
error := 0;
if fileexist(filename) then
begin
CopyBuffer(buffer.blankscreen,buffer.screen2);
writeto(buffer.screen2);
Box(16,7,64,12,palette.text_color1,palette.menu_color,false,true);
centeredtext(7,palette.text_color2,palette.menu_color,' Warning! ');
centeredtext(9,palette.text_color1,palette.menu_color,'File '+filename+' already exists!');
centeredtext(10,palette.text_color1,palette.menu_color,'Overwrite it? Y/N');
displaybuffer(buffer.screen2);
Clear_Keypress;
repeat
if is_key_pressed then Get_Key;
if (upcase(key_Ascii) = 'N') or (key_code = 1) then
begin
Clear_Keypress;
writeto(buffer.screen1);
displaybuffer(buffer.screen1);
error := 6;
exit;
end;
until (upcase(key_Ascii) = 'Y') or (upcase(key_Ascii) = 'Z') or (key_code = 27);
Clear_Keypress;
writeto(buffer.screen1);
displaybuffer(buffer.screen1);
end;
Header.ID := 'TNDY';
Header.Version := File_version;
Header.Nchan := Editor.channels;
Header.Length := Player.Songlength;
Header.loop := Player.Loop;
Header.NPatterns := Player.MaxPattern+1;
Header.Speed := Editor.D_Speed;
if (Editor.d_tempo <= 18) then
header.tempo := 0
else
header.tempo := editor.d_tempo;
{$I-}
assign(f,filename);
rewrite(f,1);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
{$I-}
blockwrite(f,header,sizeof(header));
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
close(f);
exit;
end;
Compress_Message;
{$I-}
blockwrite(f,msg_size,2);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
close(f);
exit;
end;
if msg_size > 0 then {only content will be saved}
begin
{$I-}
blockwrite(f,p_msg,msg_size);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
close(f);
exit;
end;
end;
{$I-}
blockwrite(f,order,Player.Songlength);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
close(f);
exit;
end;
Compress_Pattern_Data;
close(f);
end
else
error := 6;
end;
Procedure Save_Song;
var x : byte;
begin
move(buffer.blankscreen^,buffer.screen2^,4000);
writeto(buffer.screen2);
filename := '';
Box(20,7,60,12,15,1,false,true);
centeredtext(7,palette.text_color2,palette.menu_color,' Save song ');
centeredtext(9,palette.text_color1,palette.menu_color,' Please enter a filename:');
HLine(34,10,12,15,0,' ');
fastwrite(34,10,7,0,'.TND');
ox := GetX;
oy := GetY;
x := 34;
displaybuffer(buffer.screen2);
cursor(true);
change_cursor(6,7);
Clear_Keypress;
repeat
gotoxy(x,10);
if is_key_pressed then Get_Key;
if ((upcase(key_Ascii) in ['A'..'Z']) or (upcase(key_Ascii) in ['0'..'9'])) and (length(filename) < 8) then
begin
filename := filename+upcase(key_Ascii);
fastwrite(34,10,15,0,filename);
fastwrite(34+length(filename),10,7,0,'.TND');
inc(x);
Clear_Keypress;
displaybuffer(buffer.screen2);
end;
if key_code = 14 then {Backspace}
begin
if length(filename) > 0 then
begin
delete(filename,length(filename),1);
HLine(34,10,12,15,0,' ');
fastwrite(34,10,15,0,filename);
fastwrite(34+length(filename),10,7,0,'.TND');
dec(x);
end;
Clear_Keypress;
displaybuffer(buffer.screen2);
end;
if key_code = 28 then {Enter}
begin
filename := filename+'.TND';
SaveTNDY(filename);
Proceed_Errors;
Clear_Keypress;
writeto(buffer.screen1);
displaybuffer(buffer.screen1);
gotoxy(ox,oy);
change_cursor(0,7);
cursor(false);
exit;
end;
until key_code = 1;
error := 6;
Clear_Keypress;
writeto(buffer.screen1);
displaybuffer(buffer.screen1);
gotoxy(ox,oy);
change_cursor(0,7);
cursor(false);
end;
Procedure Uncompress_Pattern_Data;
var pc,tc,rc,dummy : byte;
Cevent : Comp_event;
begin
for pc := 0 to Player.MaxPattern do
begin
if maxavail < sizeof(TNDYPattern) then {Out of memory?}
begin
error := 5;
exit;
end;
if patterns[pc] = nil then new(patterns[pc]);
fillchar(patterns[pc]^,sizeof(TNDYPattern),0);
for tc := 0 to Editor.channels-1 do
begin
rc := 0;
repeat;
{$I-}
blockread(f,dummy,1);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
if dummy = $FF then
begin
{$I-}
blockread(f,dummy,1);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
inc(rc,(dummy-1)); {insert empty rows (-1 because we add 1 at the end of the loop)}
end
else
begin
seek(f,filepos(f)-1); {go one byte back}
fillchar(Cevent,sizeof(Cevent),0);
if (tc <> Editor.channels-1) then {normal voice data}
begin
{$I-}
blockread(f,cevent,sizeof(cevent)); {Read normal event}
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
patterns[pc]^[rc][tc].note := Cevent.n_o shr 4;
patterns[pc]^[rc][tc].Octave := Cevent.n_o and not $F0;
patterns[pc]^[rc][tc].volume := (Cevent.v_e shr 4);
patterns[pc]^[rc][tc].effect := Cevent.v_e and not $F0;
patterns[pc]^[rc][tc].ep := Cevent.ep;
if patterns[pc]^[rc][tc].note = 14 then {Just an effect without volume or note}
begin
patterns[pc]^[rc][tc].note := 0;
patterns[pc]^[rc][tc].Octave := 0;
patterns[pc]^[rc][tc].volume := 0;
end
else
inc(patterns[pc]^[rc][tc].volume); {make it 1 to 16 in pattern}
end
else
begin
{$I-}
blockread(f,dummy,1); {Read noise data}
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
patterns[pc]^[rc][tc].note := dummy shr 5; {Get frequency bit}
patterns[pc]^[rc][tc].volume := (dummy and not $F0);
asm
mov al,dummy
mov dummy,1
test al,00010000b {Get noise bit}
jz @1
mov dummy,2
@1:
end;
patterns[pc]^[rc][tc].Octave := dummy; {Save noise bit +1}
inc(patterns[pc]^[rc][tc].volume); {make it 1 to 16 in pattern}
end;
end;
inc(rc);
until rc >= 64; {row count}
end; {track count}
end; {pattern count}
end; {procedure}
Procedure Uncompress_Message;
var
mc,pc : word;
nc,c : word;
l,h,
last : byte;
begin
fillchar(msg_buffer,sizeof(msg_buffer),0);
mc := 1;
pc := 1;
repeat;
if p_msg[pc] = 1 then {repeat last char}
begin
last := p_msg[pc-1];
inc(pc);
if header.version = 1 then {Of course the old format is still supported :)}
begin
last := 0;
h := p_msg[pc];
inc(pc);
l := p_msg[pc];
asm
mov al,l
mov ah,h
mov nc,ax
end;
end
else
begin
nc := p_msg[pc];
if nc = $FF then
begin
inc(mc,255);
inc(pc);
nc := p_msg[pc];
end;
end;
for c := 1 to nc do
begin
msg_buffer[mc] := last;
inc(mc);
end;
inc(pc);
end
else
begin
msg_buffer[mc] := p_msg[pc];
inc(mc);
inc(pc);
end;
until pc > msg_size;
end;
Procedure LoadTNDY(filename : string);
begin
if filename <> '' then
begin
error := 0;
Clear_Pattern_Data;
writeto(buffer.screen2);
Box(16,8,64,12,palette.text_color1,palette.menu_color,false,true);
centeredtext(10,palette.text_color1,palette.menu_color,'Please wait...');
displaybuffer(buffer.screen2);
assign(f,filename);
{$I-}
reset(f,1);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
if filesize(f) < sizeof(header) then {File corrput}
begin
close(f);
error := 2;
exit;
end;
{$I-}
blockread(f,header,sizeof(header));
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
exit;
end;
if (header.id <> 'TNDY') or (header.version > File_version) then {Not a valid TNDY file}
begin
error := 2;
close(f);
exit;
end;
{$I-}
blockread(f,msg_size,2);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
close(f);
exit;
end;
if msg_size > 0 then
begin
{$I-}
blockread(f,p_msg,msg_size);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
close(f);
exit;
end;
Uncompress_Message;
end
else
fillchar(msg_buffer,sizeof(msg_buffer),0);
if Header.Version = File_version then {Version 1 saved it -1}
begin
dec(Header.NPatterns);
end;
Player.loop := header.loop;
Editor.channels := Header.Nchan;
Player.Speed := header.Speed;
Player.Songlength := Header.Length;
Player.MaxPattern := Header.NPatterns;
if header.tempo = 0 then
Player.tempo := 18
else
Player.tempo := header.tempo;
if Player.speed = 0 then Player.speed := 1; {Just to be save here...}
{$I-}
blockread(f,order,Player.Songlength);
{$I+}
if IOResult <> 0 then {IO Error}
begin
error := 1;
close(f);
exit;
end;
for z := 0 to Player.Songlength-1 do
begin
if order[z] > Player.MaxPattern then {is there a invalid pattern number in the order list?}
begin
error := 2;
close(f);
exit;
end;
end;
uncompress_pattern_data; {decrunsh pattern data}
close(f);
set_timer(Player.tempo); {Reset timer to new tempo}
end
else
error := 6; {Aborted by user}
end;
end.