-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3.c
1259 lines (1175 loc) · 26.4 KB
/
mp3.c
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
#include "config.h"
#ifdef ENABLE_MUSIC
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <pspkernel.h>
#include <pspaudiolib.h>
#include <pspaudio.h>
#include <mad.h>
#ifdef ENABLE_WMA
#include <wmadec.h>
#endif
#include "conf.h"
#include "ctrl.h"
#include "charsets.h"
#include "fat.h"
#include "scene.h"
#ifdef ENABLE_LYRIC
#include "lyric.h"
#endif
#include "mp3info.h"
#include "common/utils.h"
#include "mp3.h"
#define MAXVOLUME 0x8000
// resample from madplay, thanks to nj-zero
#define MAX_RESAMPLEFACTOR 6
#define MAX_NSAMPLES (1152 * MAX_RESAMPLEFACTOR)
struct resample_state {
mad_fixed_t ratio;
mad_fixed_t step;
mad_fixed_t last;
};
static mad_fixed_t (*Resampled)[2][MAX_NSAMPLES];
static struct resample_state Resample;
static struct OutputPCM{
unsigned int nsamples;
mad_fixed_t const * samples[2];
} OutputPCM;
__inline int resample_init(struct resample_state *state, unsigned int oldrate, unsigned int newrate)
{
mad_fixed_t ratio;
if (newrate == 0)
return -1;
ratio = mad_f_div(oldrate, newrate);
if (ratio <= 0 || ratio > MAX_RESAMPLEFACTOR * MAD_F_ONE)
return -1;
state->ratio = ratio;
state->step = 0;
state->last = 0;
return 0;
}
__inline unsigned int resample_block(struct resample_state *state, unsigned int nsamples, mad_fixed_t const *old0, mad_fixed_t const *old1, mad_fixed_t *new0, mad_fixed_t *new1)
{
mad_fixed_t const *end, *begin;
if (state->ratio == MAD_F_ONE) {
memcpy(new0, old0, nsamples * sizeof(mad_fixed_t));
memcpy(new1, old1, nsamples * sizeof(mad_fixed_t));
return nsamples;
}
end = old0 + nsamples;
begin = new0;
if (state->step < 0) {
state->step = mad_f_fracpart(-state->step);
while (state->step < MAD_F_ONE) {
if(state->step)
{
*new0++ = state->last + mad_f_mul(*old0 - state->last, state->step);
*new1++ = state->last + mad_f_mul(*old1 - state->last, state->step);
}
else
{
*new0++ = state->last;
*new1++ = state->last;
}
state->step += state->ratio;
if (((state->step + 0x00000080L) & 0x0fffff00L) == 0)
state->step = (state->step + 0x00000080L) & ~0x0fffffffL;
}
state->step -= MAD_F_ONE;
}
while (end - old0 > 1 + mad_f_intpart(state->step)) {
old0 += mad_f_intpart(state->step);
old1 += mad_f_intpart(state->step);
state->step = mad_f_fracpart(state->step);
if(state->step)
{
*new0++ = *old0 + mad_f_mul(old0[1] - old0[0], state->step);
*new1++ = *old1 + mad_f_mul(old1[1] - old1[0], state->step);
}
else
{
*new0++ = *old0;
*new1++ = *old1;
}
state->step += state->ratio;
if (((state->step + 0x00000080L) & 0x0fffff00L) == 0)
state->step = (state->step + 0x00000080L) & ~0x0fffffffL;
}
if (end - old0 == 1 + mad_f_intpart(state->step)) {
state->last = end[-1];
state->step = -state->step;
}
else
state->step -= mad_f_fromint(end - old0);
return new0 - begin;
}
// end of resample definitions & functions
static int mp3_handle = -1;
static int fd = -1;
static char mp3_tag[128], mp3_tag_encode[260], (* mp3_files)[512];
static int mp3_direct = -1, mp3_nfiles = 0, mp3_index = 0, mp3_jump = 0;
static t_conf_cycle mp3_cycle = conf_cycle_repeat;
static t_conf_encode mp3_encode = conf_encode_gbk;
static bool isPlaying = false, eos = true, isPause = true, manualSw = false;
#ifdef ENABLE_HPRM
static bool hprmEnabled = true;
#endif
#define INPUT_BUFFER_SIZE 8*1152
#define OUTPUT_BUFFER_SIZE 4*1152 /* Must be an integer multiple of 4. */
#ifdef ENABLE_WMA
#define WMA_BUFFER_SIZE WMA_MAX_BUF_SIZE
#endif
static mad_fixed_t Filter[32];
static int DoFilter = 0;
static SceUID mp3_thid = 0;
#ifdef ENABLE_WMA
static bool file_is_mp3 = false;
#endif
static struct mad_stream Stream;
static struct mad_frame Frame;
static struct mad_synth Synth;
static mad_timer_t Timer;
static signed short OutputBuffer[4][
#ifdef ENABLE_WMA
WMA_MAX_BUF_SIZE
#else
OUTPUT_BUFFER_SIZE / 2
#endif
];
static byte InputBuffer[INPUT_BUFFER_SIZE + MAD_BUFFER_GUARD],
* OutputPtr = (byte *)&OutputBuffer[0][0], * GuardPtr = NULL,
* OutputBufferEnd = (byte *)&OutputBuffer[0][0] + OUTPUT_BUFFER_SIZE;
static int OutputBuffer_Index = 0;
#ifdef ENABLE_WMA
static int OutputBuffer_Pos = 0;
static WmaFile * wma = NULL;
#endif
static t_mp3info mp3info;
#ifdef ENABLE_LYRIC
static t_lyric lyric;
#endif
extern int dup (int fd1)
{
return (fcntl (fd1, F_DUPFD, 0));
}
extern int dup2 (int fd1, int fd2)
{
close (fd2);
return (fcntl (fd1, F_DUPFD, fd2));
}
static void apply_filter(struct mad_frame *Frame)
{
int Channel, Sample, Samples, SubBand;
Samples = MAD_NSBSAMPLES(&Frame->header);
if (Frame->header.mode != MAD_MODE_SINGLE_CHANNEL)
for (Channel = 0; Channel < 2; Channel++)
for (Sample = 0; Sample < Samples; Sample++)
for (SubBand = 0; SubBand < 32; SubBand++)
Frame->sbsample[Channel][Sample][SubBand] =
mad_f_mul(Frame->sbsample[Channel][Sample][SubBand], Filter[SubBand]);
else
for (Sample = 0; Sample < Samples; Sample++)
for (SubBand = 0; SubBand < 32; SubBand++)
Frame->sbsample[0][Sample][SubBand] = mad_f_mul(Frame->sbsample[0][Sample][SubBand], Filter[SubBand]);
}
static signed short MadFixedToSshort(mad_fixed_t Fixed)
{
if (Fixed >= MAD_F_ONE)
return (SHRT_MAX);
if (Fixed <= -MAD_F_ONE)
return (-SHRT_MAX);
return ((signed short)(Fixed >> (MAD_F_FRACBITS - 15)));
}
static bool mp3_load()
{
#ifdef ENABLE_LYRIC
lyric_close(&lyric);
#endif
if(mp3_handle >= 0)
sceAudioChRelease(mp3_handle);
do {
while(isPlaying && (mp3_nfiles == 0 || mp3_files == NULL))
sceKernelDelayThread(500000);
if(!isPlaying)
return false;
if(manualSw || mp3_cycle == conf_cycle_repeat || mp3_cycle == conf_cycle_single)
{
if(mp3_direct == 0)
{
mp3_index --;
if(mp3_index < 0)
mp3_index = mp3_nfiles - 1;
}
else if(mp3_direct > 0)
{
mp3_index ++;
if(mp3_index >= mp3_nfiles)
{
if(manualSw || mp3_cycle == conf_cycle_repeat)
mp3_index = 0;
else
{
mp3_index = mp3_nfiles - 1;
isPause = true;
}
}
}
mp3_direct = 1;
}
else if(mp3_cycle == conf_cycle_random)
mp3_index = rand() % mp3_nfiles;
#ifdef ENABLE_WMA
if(file_is_mp3)
{
#endif
if(fd >= 0)
{
sceIoClose(fd);
fd = -1;
}
#ifdef ENABLE_WMA
}
else
{
if(wma != NULL)
{
wma_close(wma);
wma = NULL;
}
}
#endif
mad_timer_reset(&Timer);
while (mp3_nfiles > 0 && (fd = sceIoOpen(mp3_files[mp3_index], PSP_O_RDONLY, 0777)) < 0)
{
if(mp3_index < mp3_nfiles - 1)
memmove(&mp3_files[mp3_index], &mp3_files[mp3_index + 1], 512 * (mp3_nfiles - 1 - mp3_index));
mp3_nfiles --;
if(mp3_nfiles == 0)
{
mp3_tag_encode[0] = 0;
free((void *)mp3_files);
mp3_files = NULL;
}
}
} while(mp3_nfiles == 0);
#ifdef ENABLE_WMA
if(file_is_mp3)
{
#endif
mad_synth_finish(&Synth);
mad_frame_finish(&Frame);
mad_stream_finish(&Stream);
mp3info_free(&mp3info);
#ifdef ENABLE_WMA
}
file_is_mp3 = (stricmp(utils_fileext(mp3_files[mp3_index]), "mp3") == 0);
if(file_is_mp3)
{
#endif
mp3info_read(&mp3info, fd);
mad_stream_init(&Stream);
mad_frame_init(&Frame);
mad_synth_init(&Synth);
memset(&OutputBuffer[0][0], 0, 4 * OUTPUT_BUFFER_SIZE);
OutputPtr = (byte *)&OutputBuffer[0][0];
OutputBufferEnd = OutputPtr + OUTPUT_BUFFER_SIZE * 2;
if(mp3info.title[0] != 0)
{
if(mp3info.artist[0] != 0)
sprintf(mp3_tag, "%s - %s", (const char *)mp3info.artist, (const char *)mp3info.title);
else
sprintf(mp3_tag, "? - %s", (const char *)mp3info.title);
}
else
{
char * mp3_tag2 = strrchr(mp3_files[mp3_index], '/');
if(mp3_tag2 != NULL)
mp3_tag2 ++;
else
mp3_tag2 = "";
strcpy(mp3_tag, mp3_tag2);
}
mp3_handle = sceAudioChReserve( PSP_AUDIO_NEXT_CHANNEL, OUTPUT_BUFFER_SIZE / 4, 0 );
#ifdef ENABLE_WMA
}
else
{
sceIoClose(fd);
fd = -1;
wma = wma_open(mp3_files[mp3_index]);
if(wma == NULL)
return false;
charsets_ucs_conv((const byte *)wma->title, (byte *)wma->title);
charsets_ucs_conv((const byte *)wma->author, (byte *)wma->author);
if(wma->title[0] != 0)
{
if(wma->author[0] != 0)
sprintf(mp3_tag, "%s - %s", (const char *)wma->author, (const char *)wma->title);
else
sprintf(mp3_tag, "? - %s", (const char *)wma->title);
}
else
{
char * mp3_tag2 = strrchr(mp3_files[mp3_index], '/');
if(mp3_tag2 != NULL)
mp3_tag2 ++;
else
mp3_tag2 = "";
strcpy(mp3_tag, mp3_tag2);
}
mp3_handle = sceAudioChReserve( PSP_AUDIO_NEXT_CHANNEL, WMA_MAX_BUF_SIZE / 2 / wma->channels, (wma->channels == 2) ? 0 : 1 );
}
#endif
mp3_set_encode(mp3_encode);
eos = false;
manualSw = false;
mp3_jump = 0;
#ifdef ENABLE_LYRIC
char lyricname[256];
strcpy(lyricname, mp3_files[mp3_index]);
int lsize = strlen(lyricname);
lyricname[lsize - 3] = 'l';
lyricname[lsize - 2] = 'r';
lyricname[lsize - 1] = 'c';
lyric_open(&lyric, lyricname);
#endif
return true;
}
static int mp3_thread(unsigned int args, void * argp)
{
#ifdef ENABLE_HPRM
dword key;
#endif
while(isPlaying && (!eos || mp3_load()))
{
if(isPause)
{
while(isPause)
{
sceKernelDelayThread(500000);
#ifdef ENABLE_HPRM
if(!hprmEnabled)
continue;
key = ctrl_hprm();
switch(key)
{
case PSP_HPRM_PLAYPAUSE:
if(mp3_nfiles == 0 || mp3_files == NULL)
break;
isPause = false;
scene_power_save(false);
break;
case PSP_HPRM_FORWARD:
mp3_next();
break;
case PSP_HPRM_BACK:
mp3_prev();
break;
}
#endif
}
continue;
}
#ifdef ENABLE_WMA
if(file_is_mp3)
{
#endif
if (Stream.buffer == NULL || Stream.error == MAD_ERROR_BUFLEN)
{
size_t ReadSize, Remaining = 0, BufSize;
byte * ReadStart;
if(Stream.next_frame != NULL)
{
Remaining = Stream.bufend - Stream.next_frame;
memmove(InputBuffer, Stream.next_frame, Remaining);
ReadStart = InputBuffer + Remaining;
ReadSize = INPUT_BUFFER_SIZE - Remaining;
}
else
{
ReadSize = INPUT_BUFFER_SIZE;
ReadStart = InputBuffer;
Remaining = 0;
}
BufSize = sceIoRead(fd, ReadStart, ReadSize);
if(BufSize == 0)
{
eos = true;
manualSw = false;
continue;
}
if(BufSize < ReadSize)
{
GuardPtr = ReadStart + ReadSize;
memset(GuardPtr, 0, MAD_BUFFER_GUARD);
ReadSize += MAD_BUFFER_GUARD;
}
mad_stream_buffer(&Stream, InputBuffer, ReadSize + Remaining);
Stream.error = 0;
}
if (mad_frame_decode(&Frame, &Stream) == -1)
{
if(MAD_RECOVERABLE(Stream.error) || Stream.error == MAD_ERROR_BUFLEN)
continue;
else
{
eos = true;
manualSw = false;
continue;
}
}
mad_timer_add(&Timer, Frame.header.duration);
if(mp3_jump != 0)
{
if(mp3info.framecount > 0)
{
int pos = 0;
if(mp3_jump == -2)
{
pos = 0;
mp3_pause();
}
else if(mp3_jump == -1)
{
if(Timer.seconds > 5)
{
pos = (int)((double)(Timer.seconds - 5) / mp3info.framelen);
if(pos >= mp3info.framecount)
pos = mp3info.framecount - 1;
}
else
{
pos = 0;
}
}
else
{
pos = (int)((double)(Timer.seconds + 5) / mp3info.framelen);
if(pos >= mp3info.framecount)
{
eos = true;
pos = mp3info.framecount - 1;
}
}
sceIoLseek32(fd, mp3info.frameoff[pos], PSP_SEEK_SET);
Timer.seconds = (long)(mp3info.framelen * (double)pos);
Timer.fraction = (unsigned long)((mp3info.framelen * (double)pos - (double)Timer.seconds) * MAD_TIMER_RESOLUTION);
mad_synth_finish(&Synth);
mad_frame_finish(&Frame);
mad_stream_finish(&Stream);
mad_stream_init(&Stream);
mad_frame_init(&Frame);
mad_synth_init(&Synth);
mp3_jump = 0;
#ifdef ENABLE_LYRIC
lyric_update_pos(&lyric, (void *)&Timer);
#endif
continue;
}
mp3_jump = 0;
}
#ifdef ENABLE_LYRIC
lyric_update_pos(&lyric, (void *)&Timer);
#endif
if (DoFilter)
apply_filter(&Frame);
mad_synth_frame(&Synth, &Frame);
if(Synth.pcm.samplerate == 44100)
{
OutputPCM.nsamples = Synth.pcm.length;
OutputPCM.samples[0] = Synth.pcm.samples[0];
OutputPCM.samples[1] = Synth.pcm.samples[1];
}
else
{
if (resample_init(&Resample, Synth.pcm.samplerate, 44100) == -1)
continue;
OutputPCM.nsamples = resample_block(&Resample, Synth.pcm.length, Synth.pcm.samples[0], Synth.pcm.samples[1], (*Resampled)[0], (*Resampled)[1]);
OutputPCM.samples[0] = (*Resampled)[0];
OutputPCM.samples[1] = (*Resampled)[1];
}
if(OutputPCM.nsamples > 0)
{
int i;
if(MAD_NCHANNELS(&Frame.header) == 2)
for (i = 0; i < OutputPCM.nsamples; i++)
{
signed short Sample;
/* Left channel */
Sample = MadFixedToSshort(OutputPCM.samples[0][i]);
*(OutputPtr++) = Sample & 0xff;
*(OutputPtr++) = (Sample >> 8);
Sample = MadFixedToSshort(OutputPCM.samples[1][i]);
*(OutputPtr++) = Sample & 0xff;
*(OutputPtr++) = (Sample >> 8);
if(OutputPtr == OutputBufferEnd)
{
sceAudioOutputPannedBlocking(mp3_handle, MAXVOLUME, MAXVOLUME, (char *)OutputBuffer[OutputBuffer_Index]);
OutputBuffer_Index = (OutputBuffer_Index + 1) & 0x03;
OutputPtr = (byte *)&OutputBuffer[OutputBuffer_Index][0];
OutputBufferEnd = OutputPtr + OUTPUT_BUFFER_SIZE;
}
}
else
for (i = 0; i < OutputPCM.nsamples; i++)
{
signed short Sample;
/* Left channel */
Sample = MadFixedToSshort(OutputPCM.samples[0][i]);
*(OutputPtr++) = Sample & 0xff;
*(OutputPtr++) = (Sample >> 8);
*(OutputPtr++) = Sample & 0xff;
*(OutputPtr++) = (Sample >> 8);
if(OutputPtr >= OutputBufferEnd)
{
sceAudioOutputPannedBlocking(mp3_handle, MAXVOLUME, MAXVOLUME, (char *)OutputBuffer[OutputBuffer_Index]);
OutputBuffer_Index = (OutputBuffer_Index + 1) & 0x03;
OutputPtr = (byte *)&OutputBuffer[OutputBuffer_Index][0];
OutputBufferEnd = OutputPtr + OUTPUT_BUFFER_SIZE;
}
}
}
else
{
eos = true;
manualSw = false;
}
#ifdef ENABLE_HPRM
if(!hprmEnabled)
continue;
key = ctrl_hprm();
if(key > 0)
{
switch(key)
{
case PSP_HPRM_PLAYPAUSE:
isPause = true;
scene_power_save(true);
break;
case PSP_HPRM_FORWARD:
mp3_next();
break;
case PSP_HPRM_BACK:
mp3_prev();
break;
}
}
#endif
#ifdef ENABLE_WMA
}
else
{
mad_timer_t incr;
double tt = (double)(WMA_MAX_BUF_SIZE / 2 / wma->channels) / wma->samplerate;
incr.seconds = (long)tt;
incr.fraction = (unsigned long)((tt - (long)tt) * MAD_TIMER_RESOLUTION);
int size, p = 0;
if(mp3_jump != 0)
{
if(mp3_jump == -2)
{
OutputBuffer_Pos = 0;
eos = true;
mp3_direct = -1;
isPlaying = true;
mad_timer_reset(&Timer);
break;
}
else if(mp3_jump == -1)
{
if(Timer.seconds > 5)
{
wma_seek(wma, Timer.seconds - 5);
Timer.seconds -= 5;
Timer.fraction = 0;
}
else
{
wma_seek(wma, 0);
mad_timer_reset(&Timer);
}
mp3_jump = 0;
break;
}
else
{
int newp = Timer.seconds + 5;
if(newp < wma->duration)
{
wma_seek(wma, newp);
Timer.seconds = newp;
Timer.fraction = 0;
}
mp3_jump = 0;
break;
}
}
unsigned char * buf = (unsigned char *)wma_decode_frame(wma, &size);
if(buf == NULL)
{
eos = true;
manualSw = false;
continue;
}
while(size > 0)
{
if(OutputBuffer_Pos + size >= WMA_MAX_BUF_SIZE)
{
memcpy(&OutputBuffer[OutputBuffer_Index][OutputBuffer_Pos / 2], &buf[p], WMA_MAX_BUF_SIZE - OutputBuffer_Pos);
sceAudioOutputPannedBlocking(mp3_handle, MAXVOLUME, MAXVOLUME, OutputBuffer[OutputBuffer_Index]);
mad_timer_add(&Timer, incr);
#ifdef ENABLE_LYRIC
lyric_update_pos(&lyric, (void *)&Timer);
#endif
OutputBuffer_Index = (OutputBuffer_Index + 1) & 3;
memset(&OutputBuffer[OutputBuffer_Index][0], 0, WMA_MAX_BUF_SIZE);
p += WMA_MAX_BUF_SIZE - OutputBuffer_Pos;
size -= WMA_MAX_BUF_SIZE - OutputBuffer_Pos;
OutputBuffer_Pos = 0;
}
else
{
memcpy(&OutputBuffer[OutputBuffer_Index][OutputBuffer_Pos / 2], &buf[p], size);
OutputBuffer_Pos += size;
p += size;
size = 0;
}
#ifdef ENABLE_HPRM
if(!hprmEnabled)
continue;
key = ctrl_hprm();
if(key > 0)
{
switch(key)
{
case PSP_HPRM_PLAYPAUSE:
isPause = true;
scene_power_save(true);
break;
case PSP_HPRM_FORWARD:
mp3_next();
break;
case PSP_HPRM_BACK:
mp3_prev();
break;
}
}
#endif
}
}
#endif
}
return 0;
}
extern bool mp3_init()
{
srand((unsigned)time(NULL));
#ifdef ENABLE_LYRIC
lyric_init(&lyric);
#endif
mp3info_init(&mp3info);
#ifdef ENABLE_WMA
wma_init();
#endif
isPlaying = true;
isPause = true;
eos = true;
manualSw = false;
mp3_direct = -1;
Resampled = (mad_fixed_t (*)[2][MAX_NSAMPLES])malloc(sizeof(*Resampled));
mp3_thid = sceKernelCreateThread( "mp3 thread", mp3_thread, 0x14, 0x40000, PSP_THREAD_ATTR_USER | PSP_THREAD_ATTR_VFPU, NULL );
if(mp3_thid < 0)
return false;
strcpy(mp3_tag, "");
return true;
}
static void mp3_freetune()
{
mad_synth_finish(&Synth);
mad_frame_finish(&Frame);
mad_stream_finish(&Stream);
free((void *)Resampled);
}
extern void mp3_start()
{
mp3_direct = -1;
eos = true;
manualSw = false;
isPlaying = true;
scene_power_save(false);
sceKernelStartThread(mp3_thid, 0, NULL);
sceKernelWakeupThread(mp3_thid);
sceKernelResumeThread(mp3_thid);
}
extern void mp3_end()
{
mp3_stop();
#ifdef ENABLE_WMA
if(file_is_mp3)
{
#endif
if(fd >= 0)
{
sceIoClose(fd);
fd = -1;
}
#ifdef ENABLE_WMA
}
else
{
if(wma != NULL)
{
wma_close(wma);
wma = NULL;
}
}
#endif
#ifdef ENABLE_LYRIC
lyric_close(&lyric);
#endif
mp3info_free(&mp3info);
mp3_freetune();
sceAudioChRelease(mp3_handle);
}
extern void mp3_pause()
{
isPause = true;
scene_power_save(true);
}
extern void mp3_resume()
{
if(mp3_nfiles == 0 || mp3_files == NULL)
return;
scene_power_save(false);
isPause = false;
}
extern void mp3_stop()
{
isPlaying = false;
isPause = false;
eos = true;
manualSw = false;
mp3_direct = -1;
sceKernelWakeupThread(mp3_thid);
sceKernelResumeThread(mp3_thid);
sceKernelWaitThreadEnd(mp3_thid, NULL);
scene_power_save(true);
}
static bool lastpause;
static dword lastpos, lastindex;
extern void mp3_powerdown()
{
lastpause = isPause;
isPause = true;
#ifdef ENABLE_WMA
if(file_is_mp3)
{
#endif
lastpos = sceIoLseek32(fd, 0, PSP_SEEK_CUR);
if(fd >= 0)
{
sceIoClose(fd);
fd = -1;
}
#ifdef ENABLE_WMA
}
else
{
wma_close(wma);
wma = NULL;
}
#endif
lastindex = mp3_index;
scene_power_save(false);
}
extern void mp3_powerup()
{
mp3_index = lastindex;
#ifdef ENABLE_WMA
if(file_is_mp3)
{
#endif
if(mp3_nfiles == 0 || mp3_files == NULL || (fd = sceIoOpen(mp3_files[mp3_index], PSP_O_RDONLY, 0777)) < 0)
return;
sceIoLseek32(fd, lastpos, PSP_SEEK_SET);
#ifdef ENABLE_WMA
}
else
{
if(mp3_nfiles == 0 || mp3_files == NULL || (wma = wma_open(mp3_files[mp3_index])) == NULL)
return;
}
#endif
isPause = lastpause;
scene_power_save(isPause);
}
extern void mp3_list_add_dir(const char *comppath)
{
p_fat_info info;
char path[256];
dword i, count = fat_readdir(comppath, path, &info);
if(count == INVALID)
return;
for(i = 0; i < count; i ++)
{
if((info[i].attr & FAT_FILEATTR_DIRECTORY) > 0)
{
if(info[i].filename[0] == '.')
continue;
char subCompPath[260];
sprintf(subCompPath, "%s%s/", comppath, info[i].longname);
mp3_list_add_dir(subCompPath);
continue;
}
const char * ext = utils_fileext(info[i].filename);
if(ext == NULL || (stricmp(ext, "mp3") != 0
#ifdef ENABLE_WMA
&& stricmp(ext, "wma") != 0
#endif
))
continue;
if((mp3_nfiles % 256) == 0)
{
if(mp3_nfiles > 0)
mp3_files = (char(*)[512])realloc(mp3_files, 512 * (mp3_nfiles + 256));
else
mp3_files = (char(*)[512])malloc(512 * (mp3_nfiles + 256));
if(mp3_files == NULL)
{
mp3_nfiles = 0;
break;
}
}
sprintf(mp3_files[mp3_nfiles], "%s%s", path, info[i].filename);
sprintf(&mp3_files[mp3_nfiles][256], "%s%s", comppath, info[i].longname);
dword j;
for(j = 0; j < mp3_nfiles; j ++)
if(stricmp(mp3_files[j], mp3_files[mp3_nfiles]) == 0)
break;
if(j < mp3_nfiles)
continue;
mp3_nfiles ++;
}
free((void *)info);
}
extern void mp3_list_free()
{
if(mp3_files != NULL)
{
free((void *)mp3_files);
mp3_files = NULL;
}
mp3_nfiles = 0;
}
extern bool mp3_list_load(const char * filename)
{
FILE * fp = fopen(filename, "rt");
if(fp == NULL)
return false;
mp3_nfiles = 0;
char fname[256], cname[256];
while(fgets(fname, 256, fp) != NULL && fgets(cname, 256, fp) != NULL) {
dword len1 = strlen(fname), len2 = strlen(cname);
if(len1 > 1 && len2 > 1)
{
if(fname[len1 - 1] == '\n')
fname[len1 - 1] = 0;
if(cname[len2 - 1] == '\n')
cname[len2 - 1] = 0;
}
else
continue;
if(mp3_nfiles % 256 == 0)
{
if(mp3_nfiles == 0)
mp3_files = (char(*)[512])malloc(512 * 256);
else
mp3_files = (char(*)[512])realloc(mp3_files, 512 * (mp3_nfiles + 256));
if(mp3_files == NULL)
{
mp3_nfiles = 0;
return false;
}
}
strcpy(mp3_files[mp3_nfiles], fname);
strcpy(&mp3_files[mp3_nfiles][256], cname);
mp3_nfiles ++;
}
fclose(fp);
return true;
}
extern void mp3_list_save(const char * filename)
{
if(mp3_nfiles == 0 || mp3_files == NULL)
{
FILE * fp = fopen(filename, "wt");
if(fp == NULL)
return;
fclose(fp);
return;
}
FILE * fp = fopen(filename, "wt");
if(fp == NULL)
return;
dword i;
for (i = 0; i < mp3_nfiles; i ++)
{
fprintf(fp, "%s\n", mp3_files[i]);
fprintf(fp, "%s\n", &mp3_files[i][256]);
}
fclose(fp);
}
extern bool mp3_list_add(const char * filename, const char * longname)
{
dword i;
for(i = 0; i < mp3_nfiles; i ++)
if(stricmp(mp3_files[i], filename) == 0)
return false;
if(mp3_nfiles % 256 == 0)
{
if(mp3_nfiles > 0)