-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3player.c
More file actions
356 lines (292 loc) · 9.39 KB
/
mp3player.c
File metadata and controls
356 lines (292 loc) · 9.39 KB
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
#include "mp3player.h"
#include <pspkernel.h>
#include <pspaudio.h>
#include <pspmp3.h>
#include <psputility.h>
#include <pspiofilemgr.h>
#include <string.h>
#include <stdio.h>
// Buffers (aligned for DMA)
static unsigned char mp3Buf[16 * 1024] __attribute__((aligned(64)));
static unsigned char pcmBuf[16 * (1152/2)] __attribute__((aligned(64)));
// Player state
static volatile int g_playing = 0;
static volatile int g_paused = 0;
static volatile int g_finished = 0;
static volatile int g_stopRequest = 0;
static volatile int g_numPlayed = 0;
static volatile int g_samplingRate = 44100;
static volatile int g_seekSkipSamples = 0; // >0 means skip this many samples silently
static volatile int g_seekBackRequest = 0; // seconds to seek back
static int g_volume = 80; // 0-100
static int g_modulesLoaded = 0;
static int g_resourceInited = 0;
static SceUID g_playerThread = -1;
static char g_currentPath[512];
// Read syncsafe integer from ID3v2 header
static int readSyncsafe(const unsigned char *b) {
return ((b[0] & 0x7F) << 21) | ((b[1] & 0x7F) << 14) |
((b[2] & 0x7F) << 7) | (b[3] & 0x7F);
}
// Detect ID3v2 tag and return the byte offset where MP3 audio data starts
static int getMP3DataStart(SceUID fd) {
unsigned char header[10];
sceIoLseek32(fd, 0, SEEK_SET);
if (sceIoRead(fd, header, 10) != 10) return 0;
if (header[0] == 'I' && header[1] == 'D' && header[2] == '3') {
int tagSize = readSyncsafe(header + 6);
return 10 + tagSize;
}
return 0;
}
// Fill the MP3 stream buffer from file
static int fillStreamBuffer(SceUID fd, int handle) {
unsigned char *dst;
SceInt32 write;
SceInt32 pos;
int status = sceMp3GetInfoToAddStreamData(handle, &dst, &write, &pos);
if (status < 0) return -1;
if (write <= 0) return 1; // nothing to write, but not an error
status = sceIoLseek32(fd, pos, SEEK_SET);
if (status < 0) return -1;
int readBytes = sceIoRead(fd, dst, write);
if (readBytes < 0) return -1;
if (readBytes == 0) return 0; // EOF
status = sceMp3NotifyAddStreamData(handle, readBytes);
if (status < 0) return -1;
return 1; // success
}
// Decode thread function
static int mp3DecodeThread(SceSize args, void *argp) {
SceUID fd = -1;
int channel = -1;
int lastDecoded = 0;
fd = sceIoOpen(g_currentPath, PSP_O_RDONLY, 0777);
if (fd < 0) {
g_playing = 0;
g_finished = 1;
return -1;
}
// Setup MP3 handle — skip ID3v2 tag so sceMp3 gets raw MP3 frames
int mp3Start = getMP3DataStart(fd);
int mp3End = sceIoLseek32(fd, 0, SEEK_END);
SceMp3InitArg initArg;
initArg.mp3StreamStart = mp3Start;
initArg.mp3StreamEnd = mp3End;
initArg.mp3Buf = mp3Buf;
initArg.mp3BufSize = sizeof(mp3Buf);
initArg.pcmBuf = pcmBuf;
initArg.pcmBufSize = sizeof(pcmBuf);
int handle = sceMp3ReserveMp3Handle(&initArg);
if (handle < 0) {
sceIoClose(fd);
g_playing = 0;
g_finished = 1;
return -1;
}
// Initial buffer fill
fillStreamBuffer(fd, handle);
int status = sceMp3Init(handle);
if (status < 0) {
sceMp3ReleaseMp3Handle(handle);
sceIoClose(fd);
g_playing = 0;
g_finished = 1;
return -1;
}
g_samplingRate = sceMp3GetSamplingRate(handle);
int numChannels = sceMp3GetMp3ChannelNum(handle);
g_numPlayed = 0;
// No looping
sceMp3SetLoopNum(handle, 0);
// Decode loop
while (!g_stopRequest) {
if (g_paused) {
sceKernelDelayThread(50000); // 50ms sleep while paused
continue;
}
// Handle seek backward: restart decode from beginning
if (g_seekBackRequest > 0) {
int targetTime = (g_numPlayed / g_samplingRate) - g_seekBackRequest;
if (targetTime < 0) targetTime = 0;
g_seekBackRequest = 0;
// Release current handle and reopen
if (channel >= 0) { sceAudioSRCChRelease(); channel = -1; }
sceMp3ReleaseMp3Handle(handle);
sceIoLseek32(fd, 0, SEEK_SET);
int restart_mp3Start = getMP3DataStart(fd);
int restart_mp3End = sceIoLseek32(fd, 0, SEEK_END);
SceMp3InitArg restartArg;
restartArg.mp3StreamStart = restart_mp3Start;
restartArg.mp3StreamEnd = restart_mp3End;
restartArg.mp3Buf = mp3Buf;
restartArg.mp3BufSize = sizeof(mp3Buf);
restartArg.pcmBuf = pcmBuf;
restartArg.pcmBufSize = sizeof(pcmBuf);
handle = sceMp3ReserveMp3Handle(&restartArg);
if (handle < 0) break;
fillStreamBuffer(fd, handle);
if (sceMp3Init(handle) < 0) break;
sceMp3SetLoopNum(handle, 0);
g_numPlayed = 0;
g_seekSkipSamples = targetTime * g_samplingRate;
lastDecoded = 0;
continue;
}
// Fill stream if needed
if (sceMp3CheckStreamDataNeeded(handle) > 0) {
fillStreamBuffer(fd, handle);
}
// Decode
short *buf;
int bytesDecoded = sceMp3Decode(handle, &buf);
if (bytesDecoded <= 0) {
// Check if stream needs more data
if (sceMp3CheckStreamDataNeeded(handle) > 0) {
int fillResult = fillStreamBuffer(fd, handle);
if (fillResult > 0) {
continue; // retry decode after refill
}
}
// EOF or unrecoverable error
break;
}
int samplesInFrame = bytesDecoded / (2 * numChannels);
// If seeking forward, skip audio output
if (g_seekSkipSamples > 0) {
g_numPlayed += samplesInFrame;
g_seekSkipSamples -= samplesInFrame;
continue;
}
// Reserve audio channel if needed
if (channel < 0 || lastDecoded != bytesDecoded) {
if (channel >= 0) sceAudioSRCChRelease();
channel = sceAudioSRCChReserve(
samplesInFrame,
g_samplingRate,
numChannels
);
lastDecoded = bytesDecoded;
}
// Calculate PSP volume from our 0-100 scale
int pspVol = (g_volume * PSP_AUDIO_VOLUME_MAX) / 100;
// Output audio
int samplesPlayed = sceAudioSRCOutputBlocking(pspVol, buf);
if (samplesPlayed > 0) {
g_numPlayed += samplesPlayed;
}
}
// Cleanup
if (channel >= 0) sceAudioSRCChRelease();
sceMp3ReleaseMp3Handle(handle);
sceIoClose(fd);
g_playing = 0;
if (!g_stopRequest) {
g_finished = 1; // natural end
}
return 0;
}
// ---- Public API ----
int mp3Init(void) {
if (!g_modulesLoaded) {
int status = sceUtilityLoadModule(PSP_MODULE_AV_AVCODEC);
if (status < 0) return status;
status = sceUtilityLoadModule(PSP_MODULE_AV_MP3);
if (status < 0) return status;
g_modulesLoaded = 1;
}
if (!g_resourceInited) {
int status = sceMp3InitResource();
if (status < 0) return status;
g_resourceInited = 1;
}
return 0;
}
int mp3Play(const char *filepath) {
// Stop any current playback
mp3Stop();
strncpy(g_currentPath, filepath, sizeof(g_currentPath) - 1);
g_currentPath[sizeof(g_currentPath) - 1] = '\0';
g_stopRequest = 0;
g_paused = 0;
g_finished = 0;
g_playing = 1;
g_numPlayed = 0;
g_playerThread = sceKernelCreateThread(
"mp3_decode",
mp3DecodeThread,
0x12, // priority (lower than main thread)
0x10000, // 64KB stack
PSP_THREAD_ATTR_USER,
NULL
);
if (g_playerThread < 0) {
g_playing = 0;
return -1;
}
sceKernelStartThread(g_playerThread, 0, NULL);
return 0;
}
void mp3Stop(void) {
if (g_playing || g_playerThread >= 0) {
g_stopRequest = 1;
g_paused = 0; // unpause so thread can exit
if (g_playerThread >= 0) {
sceKernelWaitThreadEnd(g_playerThread, NULL);
sceKernelDeleteThread(g_playerThread);
g_playerThread = -1;
}
g_playing = 0;
g_stopRequest = 0;
}
}
void mp3Pause(void) {
g_paused = 1;
}
void mp3Resume(void) {
g_paused = 0;
}
int mp3IsPaused(void) {
return g_paused;
}
int mp3IsPlaying(void) {
return g_playing;
}
int mp3GetPlayTime(void) {
if (g_samplingRate > 0) {
return g_numPlayed / g_samplingRate;
}
return 0;
}
int mp3HasFinished(void) {
if (g_finished) {
g_finished = 0; // consume flag — prevents cascading auto-advance
return 1;
}
return 0;
}
void mp3SetVolume(int vol) {
if (vol < 0) vol = 0;
if (vol > 100) vol = 100;
g_volume = vol;
}
int mp3GetVolume(void) {
return g_volume;
}
void mp3SeekForward(int seconds) {
if (g_playing && g_samplingRate > 0) {
g_seekSkipSamples = seconds * g_samplingRate;
}
}
void mp3SeekBackward(int seconds) {
if (g_playing && g_samplingRate > 0) {
g_seekBackRequest = seconds;
}
}
void mp3Shutdown(void) {
mp3Stop();
if (g_resourceInited) {
sceMp3TermResource();
g_resourceInited = 0;
}
}