-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplayerunit.pas
3129 lines (2635 loc) · 91.5 KB
/
playerunit.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
unit playerunit;
{$mode objfpc}{$H+}
interface
//------------------------------------------------------------------------------
// A Winamp skinnable player for the Colors GUI/RPi
// v. 0.26 alpha - 20170619
// work in progress
// GPL 2.0
//------------------------------------------------------------------------------
uses
Classes, SysUtils, platform, retromalina, mwindows, blitter, threads, simpleaudio, retro, icons, retrokeyboard, unit6502,xmp,mp3;
type TPlayerThread=class (TThread)
private
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
TOscilloscope= class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create;
end;
TVisualization= class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create;
end;
TSettings= class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create;
end;
TPlaylistThread= class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create;
end;
TEqualizerThread= class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create;
end;
TCountThread= class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create;
end;
TPlaylistItem=class(TObject)
item:string;
name:string;
time:integer;
rep:integer;
x,y:integer;
selected:boolean;
granny:TWindow;
next,prev:TPlaylistItem;
constructor create(aitem:string);
procedure append(aitem:string);
procedure remove;
function checkall:boolean;
procedure draw;
procedure select;
end;
// File buffer thread
TFileBuffer= class(TThread)
private
buf:array[0..131071] of byte;
tempbuf:array[0..32767] of byte;
outbuf: array[0..8191] of byte;
pocz:integer;
koniec:integer;
il,fh,newfh:integer;
newfilename:string;
needclear:boolean;
seekamount:int64;
eof:boolean;
mp3:integer;
qq:integer;
maintenance:boolean;
reading:boolean;
protected
procedure Execute; override;
public
m:integer;
empty,full:boolean;
Constructor Create(CreateSuspended : boolean);
function getdata(b,ii:integer):integer;
procedure setfile(nfh:integer);
procedure clear;
procedure seek(amount:int64);
procedure setmp3(mp3b:integer);
end;
const
// CPU Affinity constants copied here
CPU_AFFINITY_0 = 1;
CPU_AFFINITY_1 = 2;
CPU_AFFINITY_2 = 4;
CPU_AFFINITY_3 = 8;
ver='RetAMP v. 0.28 (2017.06.27)';
var pl:TWindow=nil;
info:TWindow=nil;
sc:TWindow=nil;
vis:TWindow=nil;
fi:TWindow=nil;
list:TWindow=nil;
sett:TWindow=nil;
eq:TWindow=nil;
spritebutton:TButton;
oscilloscopebutton:TButton;
visarea:pointer=nil;
cbuttons:pointer=nil;
titlebar:pointer=nil;
baseskin:pointer=nil;
numbers:pointer=nil;
volume:pointer=nil;
balance:pointer=nil;
posbar:pointer=nil;
shufrep:pointer=nil;
monoster:pointer=nil;
playpaus:pointer=nil;
eqmain:pointer=nil;
pledit:pointer=nil;
playerthread:TPlayerthread=nil;
oscilloscope:TOscilloscope=nil;
visualization:TVisualization=nil;
playlistthread:Tplaylistthread=nil;
eqthread:TEqualizerThread=nil;
settings:TSettings=nil;
sel1:TFileselector=nil;
playfilename,pf2:string;
dir:string;
fileinfo:array[0..31,0..1] of string;
spr0x,spr0y,spr0dx,spr0dy:integer;
spr1x,spr1y,spr1dx,spr1dy:integer;
spr2x,spr2y,spr2dx,spr2dy:integer;
spr3x,spr3y,spr3dx,spr3dy:integer;
spr4x,spr4y,spr4dx,spr4dy:integer;
spr5x,spr5y,spr5dx,spr5dy:integer;
spr6x,spr6y,spr6dx,spr6dy:integer;
spr0,spr1,spr2,spr3,spr4,spr5,spr6:TAnimatedSprite;
pause1a:boolean=true;
song:word=0;
songs:word=0;
init:word;
mp3buf:array[0..4096] of byte;
atitle,author,copyright:string[32];
cia:integer;
a1base:integer=440;
ext:string;
av6502:int64=0;
s, fn:string;
buf:array[0..25] of byte;
songname:string;
key:integer;
playlistitem:TPlaylistitem=nil;
infofh:integer;
filebuffer:TFileBuffer=nil;
player_item:TPlaylistitem;
desired, obtained:TAudioSpec;
skintextcolor:integer=200;
skinbackcolor:integer=0;
maintextcolor:integer=200;
mainbackcolor:integer=0;
countfilename:string;
skindir:string;
sdir:string;
sliders:boolean=true;
volume_pos,balance_pos:integer;
speedtest:int64;
sidbuf:array[0..4799] of smallint;
procedure hide_sprites;
procedure start_sprites;
procedure vis_sprites;
procedure prepare_sprites;
procedure AudioCallback(userdata: Pointer; stream: PUInt8; len:Integer );
implementation
function mp3check(name:string):integer;
label p999;
var i,il,j,infofh,q,bitrate, freq, samplerate, padding,channels,fs,err:integer;
mp3buf:array[0..32767] of byte;
bitrates:array[0..15] of integer=(0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0);
bitrates2:array[0..15] of integer=(0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0);
bitrates22:array[0..15] of integer=(0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0);
freqs:array[0..3] of integer=(44100,48000,32000,0);
freqs2:array[0..3] of integer=(22050,24000,16000,0);
crc,lll,layer:integer;
skip,len: int64;
t:int64;
begin
t:=gettime;
infofh:=fileopen(name,$40);
fileread(infofh,mp3buf,10);
if (mp3buf[0]=ord('I')) and (mp3buf[1]=ord('D')) and (mp3buf[2]=ord('3')) then // Skip ID3
begin
skip:=(mp3buf[6] shl 21) + (mp3buf[7] shl 14) + (mp3buf[8] shl 7) + mp3buf[9]+10;
end
else skip:=0;
fileseek(infofh,skip,fsfrombeginning);
begin
repeat skip+=1; mp3buf[1]:=mp3buf[0]; il:=fileread(infofh,mp3buf,1) until
(il=0) or
((mp3buf[0]=$FB) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$F3) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FA) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$F2) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FC) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FD) and (mp3buf[1]=$FF));
fileseek(infofh,skip-2,fsfrombeginning);
end;
err:=0;
i:=0;
repeat
q:=0;
i:=i+1;
il:= fileread(infofh,mp3buf,1); mp3buf[1]:=mp3buf[0]; il:= fileread(infofh,mp3buf,1);
if (mp3buf[1]<>$FF) or ((mp3buf[0]<>$FB) and (mp3buf[0]<>$FA) and (mp3buf[0]<>$F3) and (mp3buf[0]<>$F2) and (mp3buf[0]<>$FC) and (mp3buf[0]<>$FD)) then
begin
err+=1;
repeat mp3buf[1]:=mp3buf[0]; il:=fileread(infofh,mp3buf,1); q+=1; until
(il=0) or
((mp3buf[0]=$FB) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$F3) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FA) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$F2) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FC) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FD) and (mp3buf[1]=$FF));
end;
if (mp3buf[0]=$FA) or (mp3buf[0]=$F2) or (mp3buf[0]=$FC) then crc:=2 else crc:=0;
if (mp3buf[0]=$F3) or (mp3buf[0]=$F2) then lll:=72 else lll:=144;
if (mp3buf[0]=$FC) or (mp3buf[0]=$FD) then layer:=2 else layer:=3;
if il=0 then goto p999;
il:=fileread(infofh,mp3buf,2);
if (layer=3) and (lll=144) then bitrate:=bitrates[mp3buf[0] shr 4]
else if (layer=3) and (lll=72) then bitrate:=bitrates2[mp3buf[0] shr 4]
else bitrate:=bitrates22[mp3buf[0] shr 4]; //layer 2
if lll=144 then samplerate:=freqs[(mp3buf[0] and $0C) shr 2] else samplerate:=freqs2[(mp3buf[0] and $0C) shr 2];
if (mp3buf[1] shr 6)=3 then channels:=1 else channels:=2;
if (mp3buf[0] and 2)=2 then padding:=1 else padding:=0;
len:=crc+padding+trunc((lll*bitrate*1000)/samplerate);
fs:=fileseek(infofh,len-4,fsfromcurrent);
until (il<>2) or (fs<0);
p999:
result:=i;
t:=gettime-t;
fileclose(infofh);
end;
procedure waveopen (var fh:integer);
label p999;
var
i,k:integer;
s:string;
head_datasize:int64;
samplenum:int64;
currentdatasize:int64;
begin
//ThreadSetpriority(ThreadGetCurrent,7);
//sleep(2000);
//fileseek(fh,0,0);
//speedtest:=gettime;
//fileread(fh,speedbuf,$1000000);
//speedtest:=gettime-speedtest;
//box(300,300,100,100,0);
//outtextxy(300,300,inttostr(speedtest), 15);
fileseek(fh,0,0);
fileread(fh,head,44);
if head.data<>1635017060 then
begin //non-standard header
i:=0;
repeat fileseek(fh,i,fsfrombeginning); fileread(fh,k,4); i+=1 until (k=1635017060) or (i>512);
if k=1635017060 then
begin
head.data:=k;
fileread(fh,k,4);
head.datasize:=k;
end
else
begin
goto p999;
end;
end;
// visualize wave data
//if fi<>nil then begin
//fi.box(0,0,600,600,15);
//fi.outtextxy(10,10, 'type: RIFF',178);
//fi.outtextxy (10,30 , 'size: '+inttostr(head.size),178);
//fi.outtextxy (10,50 , 'pcm type: '+inttostr(head.pcm),178);
//fi.outtextxy (10,70 , 'channels: '+inttostr(head.channels),178);
//fi.outtextxy (10,90 , 'sample rate: '+inttostr(head.srate),178);
//fi.outtextxy (10,110, 'bitrate: '+inttostr(head.brate),178);
//fi.outtextxy (10,130, 'bytes per sample: '+inttostr(head.bytesps),178);
//fi.outtextxy (10,150, 'bits per sample: '+inttostr(head.bps),178);
//fi.outtextxy (10,170, 'data size: '+inttostr(head.datasize),178);
//end;
if head.pcm=85 then goto p999;
head_datasize:=head.datasize ;
currentdatasize:=head.datasize;
// determine the number of samples
samplenum:=currentdatasize div (head.channels*head.bps div 8);
//fi.outtextxy (10,190, 'samples: '+inttostr(samplenum),178);
p999:
end;
function mp3open (var fh:integer):integer;
label p999;
var
il,il2:integer;
skip:integer;
q:int64;
bitrates:array[0..15] of integer=(0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0);
freqs:array[0..3] of integer=(44100,48000,32000,0);
samplerate,channels:integer;
begin
fileseek(fh,0,fsfrombeginning);
fileread(fh,mp3buf,10);
if (mp3buf[0]=ord('I')) and (mp3buf[1]=ord('D')) and (mp3buf[2]=ord('3')) then // Skip ID3
begin
skip:=(mp3buf[6] shl 21) + (mp3buf[7] shl 14) + (mp3buf[8] shl 7) + mp3buf[9]+10;
end
else skip:=0;
mp3buf[0]:=0; mp3buf[1]:=0;
q:=skip;
fileseek(fh,q,fsfrombeginning);
repeat q+=1; mp3buf[1]:=mp3buf[0]; il:=fileread(fh,mp3buf,1) until
(il=0) or
((mp3buf[0]=$FB) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$F3) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FA) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$F2) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FC) and (mp3buf[1]=$FF)) or
((mp3buf[0]=$FD) and (mp3buf[1]=$FF));
fileseek(fh,q-2,fsfrombeginning);
fileread(fh,mp3buf,4);
samplerate:=freqs[(mp3buf[2] and $0C) shr 2];
if (mp3buf[3] shr 6)=3 then channels:=1 else channels:=2;
if mp3buf[1]=$F3 then samplerate:=samplerate div 2;
q:=-4;
fileseek(fh,q,fsfromcurrent);
result:=samplerate;
end;
procedure sidopen (var fh:integer);
var i:integer;
speed:cardinal;
version,offset,load,startsong,flags:word;
dump:word;
il,b:byte;
begin
reset6502;
atitle:=' ';
author:=' ';
copyright:=' ';
fileread(fh,version,2); version:=(version shl 8) or (version shr 8);
fileread(fh,offset,2); offset:=(offset shl 8) or (offset shr 8);
fileread(fh,load,2); load:=(load shl 8) or (load shr 8);
fileread(fh,init,2); init:=(init shl 8) or (init shr 8);
fileread(fh,play,2); play:=(play shl 8) or (play shr 8);
fileread(fh,songs,2); songs:=(songs shl 8) or (songs shr 8);
fileread(fh,startsong,2); startsong:=(startsong shl 8) or (startsong shr 8);
fileread(fh,speed,4);
speed:=speed shr 24+((speed shr 8) and $0000FF00) + ((speed shl 8) and $00FF0000) + (speed shl 24);
fileread(fh,atitle[1],32);
fileread(fh,author[1],32);
fileread(fh,copyright[1],32);
if version>1 then begin
fileread(fh,flags,2); flags:=(flags shl 8) or (flags shr 8);
fileread(fh,dump,2);
fileread(fh,dump,2);
b:=0; if load=0 then begin b:=1; fileread(fh,load,2); end;
end;
for i:=1 to 32 do if byte(atitle[i])=$F1 then atitle[i]:=char(26);
for i:=1 to 32 do if byte(author[i])=$F1 then author[i]:=char(26);
//fi.box(0,0,600,600,15);
//fi.outtextxy (10,10,'type: PSID',178);
//fi.outtextxy (10,30 ,'version: '+inttostr(version),178);
//fi.outtextxy (10,50 ,'offset: '+inttohex(offset,4),178);
//fi.outtextxy (10,70 ,'load: '+inttohex(load,4),178-144*b);
//fi.outtextxy (10,90 ,'init: '+inttohex(init,4),178);
//fi.outtextxy (10,110,'play: '+inttohex(play,4),178);
//fi.outtextxy (10,130,'songs: '+inttostr(songs),178);
//fi.outtextxy (10,150,'startsong: '+inttostr(startsong),178);
//fi.outtextxy (10,170,'speed: '+inttohex(speed,8),178);
//fi.outtextxy (10,190,'title: '+atitle,178);
//fi.outtextxy (10,210,'author: '+author,178);
//fi.outtextxy (10,230,'copyright: '+copyright,178);
//fi.outtextxy (10,250,'flags: '+inttohex(flags,4),178);
song:=startsong-1;
for i:=0 to 65535 do write6502(i,0);
repeat
il:=fileread(fh,b,1);
write6502(load,b);
load+=1;
until il<>1;
fileseek(fh,0,fsfrombeginning);
CleanDataCacheRange(base,65536);
i:=lpeek(base+$60000);
repeat until lpeek(base+$60000)>(i+4);
jsr6502(song,init);
cia:=read6502($dc04)+256*read6502($dc05);
//fi.outtextxy (10,270,'cia: '+inttohex(read6502($dc04)+256*read6502($dc05),4),178);
end;
//------------------------------------------------------------------------------
// A helper procedure for displaying time with Winamp skin digits
//------------------------------------------------------------------------------
procedure displaytime(mm,ss:integer);
var mm1,mm2,ss1,ss2:integer;
begin
mm1:=mm div 10;
mm2:=mm mod 10;
ss1:=ss div 10;
ss2:=ss mod 10;
blit8(integer(numbers),180,0,integer(pl.canvas),96,52,18,26,216,550);
blit8(integer(numbers),mm1*18,0,integer(pl.canvas),96,52,18,26,216,550);
blit8(integer(numbers),180,0,integer(pl.canvas),120,52,18,26,216,550);
blit8(integer(numbers),mm2*18,0,integer(pl.canvas),120,52,18,26,216,550);
blit8(integer(numbers),180,0,integer(pl.canvas),156,52,18,26,216,550);
blit8(integer(numbers),ss1*18,0,integer(pl.canvas),156,52,18,26,216,550);
blit8(integer(numbers),180,0,integer(pl.canvas),180,52,18,26,216,550);
blit8(integer(numbers),ss2*18,0,integer(pl.canvas),180,52,18,26,216,550);
end;
// Old player code to be rewritten
procedure old_player;
label p102,p997;
var i,j,sr:integer;
begin
key:=readkey and $FF;
if key=ord('5') then begin siddelay:=20000; songfreq:=50; skip:=0; end
else if key=ord('1') then begin siddelay:=10000; songfreq:=100; skip:=0; end
else if key=ord('2') then begin siddelay:=5000; songfreq:=200; skip:=0;end
else if key=ord('3') then begin siddelay:=6666; songfreq:=150; skip:=0; end
else if key=ord('4') then begin siddelay:=2500; songfreq:=400; skip:=0; end
else if key=ord('p') then begin pause1a:=not pause1a; if pause1a then pauseaudio(1) else pauseaudio(0); end
else if key=key_f1 then begin if channel1on=0 then channel1on:=1 else channel1on:=0; end // F1 toggle channel 1 on/off
else if key=key_f2 then begin if channel2on=0 then channel2on:=1 else channel2on:=0; end // F2 toggle channel 1 on/off
else if key=key_f3 then begin if channel3on=0 then channel3on:=1 else channel3on:=0; end // F3 toggle channel 1 on/off
else if key=ord('q') then // volume up
begin
vol123-=1; if vol123<0 then vol123:=0;
setdbvolume(-vol123);
end
else if key=ord('a') then // volume down
begin
vol123+=1; if vol123>73 then vol123:=73;
setdbvolume(-vol123);
end
else if key=ord('+') then // next subsong
begin
if songs>0 then
begin
if song<songs-1 then
begin
song+=1;
jsr6502(song,init);
end;
end;
end
else if key=ord('-') then // previous subsong
begin
if songs>0 then
begin
if song>0 then
begin
song-=1;
jsr6502(song,init);
end;
end;
end
else if key=key_leftarrow then
begin
if abs(SA_GetCurrentFreq-44100)<200 then filebuffer.seek(-1760000)
else filebuffer.seek(-7680000);
end
else if key=key_rightarrow then
begin
if abs(SA_GetCurrentFreq-44100)<200 then filebuffer.seek(1760000)
else filebuffer.seek(7680000);
end
else if key=ord('f') then // set 432 Hz
begin
a1base:=432;
if abs(SA_GetCurrentFreq-44100)<200 then SA_ChangeParams(43298,0,0,0);
if abs(SA_GetCurrentFreq-48000)<200 then SA_ChangeParams(47127,0,0,0);
if abs(SA_GetCurrentFreq-961538)<2000 then SA_ChangeParams(944056,0,0,0);
if abs(SA_GetCurrentFreq-96000)<400 then SA_ChangeParams(94254,0,0,0);
if abs(SA_GetCurrentFreq-49152)<200 then SA_ChangeParams(48258,0,0,0);
end
else if key=ord('g') then // set 440 Hz
begin
a1base:=440;
if abs(SA_GetCurrentFreq-43298)<200 then SA_ChangeParams(44100,0,0,0);
if abs(SA_GetCurrentFreq-47127)<200 then SA_ChangeParams(48000,0,0,0);
if abs(SA_GetCurrentFreq-944056)<2000 then SA_ChangeParams(961538,0,0,0);
if abs(SA_GetCurrentFreq-94254)<400 then SA_ChangeParams(96000,0,0,0);
if abs(SA_GetCurrentFreq-48258)<200 then SA_ChangeParams(49152,0,0,0);
end
else if playfilename<>'' then // key=key_enter then
begin
i:=length(playfilename);
while (playfilename[i]<>'.') and (i>1) do i:=i-1;
ext:=lowercase(copy(playfilename,i+1,length(playfilename)-i+1));
if (ext<>'wav')
and (ext<>'mp2')
and (ext<>'mp3')
and (ext<>'s48')
and (ext<>'sid')
and (ext<>'dmp')
and (ext<>'mod')
and (ext<>'s3m')
and (ext<>'xm')
and (ext<>'it')
then begin playfilename:=''; goto p997; end;
av6502:=0;
pause1a:=true;
pauseaudio(1);
threadsleep(10);
for i:=$d400 to $d420 do poke(base+i,0);
if sfh>=0 then fileclose(sfh);
sfh:=-1;
threadsleep(10);
for i:=0 to $2F do siddata[i]:=0;
for i:=$50 to $7F do siddata[i]:=0;
siddata[$0e]:=$7FFFF8;
siddata[$1e]:=$7FFFF8;
siddata[$2e]:=$7FFFF8;
songtime:=0;
fn:= playfilename; // currentdir2+filenames[sel+selstart,0];
playfilename:='';
sfh:=fileopen(fn,$40);
s:=copy(fn,1,length(fn)-4);
for j:=1 to length(s) do if s[j]='_' then s[j]:=' ';
siddelay:=20000;
filetype:=0;
fileread(sfh,buf,4);
if (buf[0]=ord('S')) and (buf[1]=ord('D')) and (buf[2]=ord('M')) and (buf[3]=ord('P')) then
begin
for i:=0 to 15 do times6502[i]:=0;
// fi.box(0,0,600,600,15);
// fi.outtextxy(10,10,'type: SDMP',178);
songs:=0;
fileread(sfh,buf,4);
siddelay:=1000000 div buf[0];
// fi.outtextxy(10,30,'speed: '+inttostr(buf[0])+' Hz',178);
atitle:=' ';
fileread(sfh,atitle[1],16);
fileread(sfh,buf,1);
// fi.outtextxy (10,50,'title: '+atitle,178);
if a1base=432 then error:=SA_changeparams(944056,16,2,2400)
else error:=SA_changeparams(961538,16,2,2400);
songs:=0;
end
else if (buf[0]=ord('P')) and (buf[1]=ord('S')) and (buf[2]=ord('I')) and (buf[3]=ord('D')) then
begin
reset6502;
sidopen(sfh);
for i:=1 to 4 do waitvbl;
if cia>0 then siddelay:={985248}1000000 div (50*round(19652/cia));
filetype:=1;
if a1base=432 then error:=SA_changeparams(944056,16,2,4800)
else error:=SA_changeparams(961538,16,2,4800);
fileclose(sfh);
end
else if (buf[0]=ord('R')) and (buf[1]=ord('S')) and (buf[2]=ord('I')) and (buf[3]=ord('D')) then
begin
filetype:=2;
// fi.box(0,0,600,600,15);
// fi.outtextxy(10,10,'type: RSID, not yet supported',44);
fileclose(sfh);
end
else if (ext='mp3') or (ext='mp2') then
begin
filetype:=4;
sr:=mp3open(sfh);
threadsleep(20);
filebuffer.setmp3(1);
threadsleep(50);
for i:=0 to 15 do times6502[i]:=0;
filetype:=4;
filebuffer.setfile(sfh);
threadsleep(200);
songs:=0;
if sr>=44100 then
begin
if a1base=432 then error:=SA_changeparams(((sr*432) div 440),16,2,384)
else error:=SA_changeparams(sr,16,2,384);
end
else
begin
if a1base=432 then error:=SA_changeparams(((sr*432) div 440),16,2,160)
else error:=SA_changeparams(sr,16,2,160);
end;
if sr>=44100 then siddelay:=(8707*44100) div sr
else siddelay:=(160*8707*44100) div (384*sr);
pauseaudio(0);
end
else if (ext='mod')
or (ext='s3m')
or (ext='xm')
or (ext='it')
then
begin
fileclose(sfh);
i:=xmp_test_module(Pchar(fn),nil);
if i<>0 then goto p102;
if xmp_context<>nil then
begin
xmp_end_player(xmp_context);
xmp_release_module(xmp_context);
xmp_free_context(xmp_context);
end;
xmp_context:=xmp_create_context;
if a1base=432 then error:=SA_changeparams(48258,16,2,384)
else error:=SA_changeparams(49152,16,2,384);
siddelay:=7812;
filetype:=6;
i:=xmp_load_module(xmp_context,Pchar(fn));
if i<>0 then
begin
xmp_free_context(xmp_context);
goto p102;
end;
i:= xmp_start_player(xmp_context,49152,34);
xmp_set_player(xmp_context,xmp_player_interp,xmp_interp_spline);
xmp_set_player(xmp_context,xmp_player_mix,30);
if i<>0 then
begin
xmp_release_module(xmp_context);
xmp_free_context(xmp_context);
goto p102;
end;
threadsleep(50);
for i:=0 to 15 do times6502[i]:=0;
// fi.box(0,0,600,600,15);
// fi.outtextxy(10,10,'type: MOD',178);
pauseaudio(0);
p102:
end
else if ext='s48' then
begin
fileseek(sfh,$2800,fsfrombeginning);
filebuffer.clear;
threadsleep(50);
filetype:=5;
filebuffer.setmp3(2);
for i:=0 to 15 do times6502[i]:=0;
filebuffer.setfile(sfh);
threadsleep(200);
songs:=0;
siddelay:=8000;
error:=SA_changeparams(48000,16,2,384);
// fi.box(0,0,600,600,15);
// fi.outtextxy(10,10,'type: MP2',178);
pauseaudio(0);
end
else if (buf[0]=ord('R')) and (buf[1]=ord('I')) and (buf[2]=ord('F')) and (buf[3]=ord('F')) then
begin
pauseaudio(1);
for i:=0 to 15 do times6502[i]:=0;
waveopen(sfh);
if head.pcm=85 then
begin
filebuffer.setmp3(1);
filetype:=4;
end
else
begin
filebuffer.setmp3(0);
filetype:=3;
end;
filebuffer.clear;
// box(100,100,100,100,40);
filebuffer.setfile(sfh);
threadsleep(100);
songs:=0;
if head.srate=44100 then siddelay:=8707
else if head.srate=96000 then siddelay:=2000
else siddelay:=1000;
// box(100,100,100,100,120);
if head.pcm=3 then i:=32
else i:=16;
error:=SA_changeparams(head.srate,i,head.channels,384);
{
if head.srate=44100 then if a1base=432 then error:=SA_changeparams(43298,i,head.channels,384)
else error:=SA_changeparams(192000,i,head.channels,384);
if head.srate=96000 then if a1base=432 then error:=SA_changeparams(94254,i,2,192)
else error:=SA_changeparams(96000,i,2,192);
if head.srate=48000 then if a1base=432 then error:=SA_changeparams(94254 div 2,i,2,192)
else error:=SA_changeparams(96000 div 2,i,2,192);
if head.srate=192000 then if a1base=432 then error:=SA_changeparams(2*94254,i,2,192)
else error:=SA_changeparams(192000,i,2,192);
}
// box(100,100,100,100,250);
// outtextxy(100,100,inttostr(error),15);
pauseaudio(0);
end
else
begin
for i:=0 to 15 do times6502[i]:=0;
fileread(sfh,buf,21);
if a1base=432 then error:=SA_changeparams(944056,16,2,4800)
else error:=SA_changeparams(961538,16,2,4800);
songs:=0;
end;
i:=length(s);
while (copy(s,i,1)<>'\') and (i>1) do i:=i-1;
if i>1 then songname:=copy(s,i+1,length(s)-2) else songname:=s;
// songname:=s;
songtime:=0;
timer1:=-1;
if filetype<>2 then begin pause1a:=false; pauseaudio(0); end;
end;
p997:
end;
//------------------------------------------------------------------------------
// A main player thread
//------------------------------------------------------------------------------
constructor TPlayerThread.create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TPlayerThread.Execute;
label p999;
var fh,i,j,hh,mm,ss,sl1,sl2:integer;
s1,s2:string;
ext,mms,hhs,sss,ps:string;
qq,qqq:integer;
eject_down: boolean=false;
pause_down: boolean=false;
start_down: boolean=false;
stop_down: boolean=false;
prev_down: boolean=false;
next_down: boolean=false;
repeat_down: boolean=false;
shuffle_down: boolean=false;
playlist_down: boolean=false;
repeat_selected:boolean=false;
shuffle_selected:boolean=false;
playlist_selected:boolean=false;
equalizer_down:boolean=false;
equalizer_selected:boolean=false;
fh2:textfile;
q,vq:integer;
il:integer;
const clickcount:integer=0;
vbutton_x:integer=0;
vbutton_dx:integer=0;
bbutton_x:integer=0;
bbutton_dx:integer=0;
select:boolean=true;
cnt:integer=0;
begin
songtime:=0;
if playlistitem=nil then
begin
playlistitem:=Tplaylistitem.create('');
playlistitem.x:=28;
playlistitem.y:=30;
end;
player_item:=playlistitem;
if filebuffer=nil then
begin
filebuffer:=Tfilebuffer.create(true);
filebuffer.start;
end;
filetype:=-1;
filetype:=-1;
desired.callback:=@AudioCallback;
desired.channels:=2;
desired.format:=AUDIO_S16;
desired.freq:=480000;
desired.samples:=1200;
error:=openaudio(@desired,@obtained);
if error<>0 then
begin
pl:=Twindow.create(550,232,''); // no decoration, we will use the skin
pl.resizable:=false;
pl.move(400,500,550,232,0,0);
pl.cls(0);
pl.outtextxyz(16,16,'Error while opening audio: '+inttostr(error),120,1,1);
pl.outtextxyz(16,36,'Please close thee application which uses the audio driver ',120,1,1);
threadsleep(5000);
pl.destroy;
pl:=nil;
goto p999;
end;
//box(0,0,100,30,0); outtextxy(0,0,inttostr(error),15);
prepare_sprites;
hide_sprites;
dir:=drive;
ThreadSetAffinity(ThreadGetCurrent,CPU_AFFINITY_2);
// Create the player window
pl:=Twindow.create(550,232,''); // no decoration, we will use the skin
pl.resizable:=false;
pl.move(400,500,550,232,0,0);
// If the skin is not loaded, load it
skindir:=drive+'Colors\Bitmaps\Player\Base\';
if cbuttons=nil then
begin
cbuttons:=getmem(72*272);
fh:=fileopen(skindir+'cbuttons.rbm',$40);
fileread(fh,cbuttons^,72*272);
fileclose(fh);
end;
if posbar=nil then
begin
posbar:=getmem(614*20);
fh:=fileopen(skindir+'posbar.rbm',$40);
fileread(fh,posbar^,614*20);
fileclose(fh);
end;
if titlebar=nil then
begin
titlebar:=getmem(174*688);
fh:=fileopen(skindir+'titlebar.rbm',$40);
fileread(fh,titlebar^,174*688);
fileclose(fh);
end;
if baseskin=nil then
begin
baseskin:=getmem(127600);
fh:=fileopen(skindir+'main.rbm',$40);
fileread(fh,baseskin^,127600);
fileclose(fh);
end;
if numbers=nil then
begin
numbers:=getmem(26*216);
fh:=fileopen(skindir+'nums_ex.rbm',$40);
fileread(fh,numbers^,26*216);
fileclose(fh);
end;
if volume=nil then
begin
volume:=getmem(866*136);
fh:=fileopen(skindir+'volume.rbm',$40);
il:=fileread(fh,volume^,866*136);
if il<866*136 then sliders:=false;