Make basic cache functions into calls, and get rid of CACHE_FUNCTION_WRAPPERS and...
[kugel-rb.git] / apps / codecs / spc.c
blob6ceb704c7c0f870e546c81b5cee3901067585c37
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007-2008 Michael Sevakis (jhMikeS)
11 * Copyright (C) 2006-2007 Adam Gashlin (hcs)
12 * Copyright (C) 2004-2007 Shay Green (blargg)
13 * Copyright (C) 2002 Brad Martin
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
25 /* lovingly ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
26 /* DSP Based on Brad Martin's OpenSPC DSP emulator */
27 /* tag reading from sexyspc by John Brawn (John_Brawn@yahoo.com) and others */
28 #include "codeclib.h"
29 #include "libspc/spc_codec.h"
30 #include "libspc/spc_profiler.h"
32 CODEC_HEADER
34 /**************** ID666 parsing ****************/
36 struct {
37 unsigned char isBinary;
38 char song[32];
39 char game[32];
40 char dumper[16];
41 char comments[32];
42 int day,month,year;
43 unsigned long length;
44 unsigned long fade;
45 char artist[32];
46 unsigned char muted;
47 unsigned char emulator;
48 } ID666;
50 static int LoadID666(unsigned char *buf) {
51 unsigned char *ib=buf;
52 int isbinary = 1;
53 int i;
55 memcpy(ID666.song,ib,32);
56 ID666.song[31]=0;
57 ib+=32;
59 memcpy(ID666.game,ib,32);
60 ID666.game[31]=0;
61 ib+=32;
63 memcpy(ID666.dumper,ib,16);
64 ID666.dumper[15]=0;
65 ib+=16;
67 memcpy(ID666.comments,ib,32);
68 ID666.comments[31]=0;
69 ib+=32;
71 /* Ok, now comes the fun part. */
73 /* Date check */
74 if(ib[2] == '/' && ib[5] == '/' )
75 isbinary = 0;
77 /* Reserved bytes check */
78 if(ib[0xD2 - 0x2E - 112] >= '0' &&
79 ib[0xD2 - 0x2E - 112] <= '9' &&
80 ib[0xD3 - 0x2E - 112] == 0x00)
81 isbinary = 0;
83 /* is length & fade only digits? */
84 for (i=0;i<8 && (
85 (ib[0xA9 - 0x2E - 112+i]>='0'&&ib[0xA9 - 0x2E - 112+i]<='9') ||
86 ib[0xA9 - 0x2E - 112+i]=='\0');
87 i++);
88 if (i==8) isbinary=0;
90 ID666.isBinary = isbinary;
92 if(isbinary) {
93 DEBUGF("binary tag detected\n");
94 ID666.year=*ib;
95 ib++;
96 ID666.year|=*ib<<8;
97 ib++;
98 ID666.month=*ib;
99 ib++;
100 ID666.day=*ib;
101 ib++;
103 ib+=7;
105 ID666.length=*ib;
106 ib++;
108 ID666.length|=*ib<<8;
109 ib++;
111 ID666.length|=*ib<<16;
112 ID666.length*=1000;
113 ib++;
115 ID666.fade=*ib;
116 ib++;
117 ID666.fade|=*ib<<8;
118 ib++;
119 ID666.fade|=*ib<<16;
120 ib++;
121 ID666.fade|=*ib<<24;
122 ib++;
124 memcpy(ID666.artist,ib,32);
125 ID666.artist[31]=0;
126 ib+=32;
128 ID666.muted=*ib;
129 ib++;
131 ID666.emulator=*ib;
132 ib++;
133 } else {
134 unsigned long tmp;
135 char buf[64];
137 DEBUGF("text tag detected\n");
139 memcpy(buf, ib, 2);
140 buf[2] = 0;
141 tmp = 0;
142 for (i=0;i<2 && buf[i]>='0' && buf[i]<='9';i++) tmp=tmp*10+buf[i]-'0';
143 ID666.month = tmp;
144 ib+=3;
146 memcpy(buf, ib, 2);
147 buf[2] = 0;
148 tmp = 0;
149 for (i=0;i<2 && buf[i]>='0' && buf[i]<='9';i++) tmp=tmp*10+buf[i]-'0';
150 ID666.day = tmp;
151 ib+=3;
153 memcpy(buf, ib, 4);
154 buf[4] = 0;
155 tmp = 0;
156 for (i=0;i<4 && buf[i]>='0' && buf[i]<='9';i++) tmp=tmp*10+buf[i]-'0';
157 ID666.year = tmp;
158 ib+=5;
160 memcpy(buf, ib, 3);
161 buf[3] = 0;
162 tmp = 0;
163 for (i=0;i<3 && buf[i]>='0' && buf[i]<='9';i++) tmp=tmp*10+buf[i]-'0';
164 ID666.length = tmp * 1000;
165 ib+=3;
167 memcpy(buf, ib, 5);
168 buf[5] = 0;
169 tmp = 0;
170 for (i=0;i<5 && buf[i]>='0' && buf[i]<='9';i++) tmp=tmp*10+buf[i]-'0';
171 ID666.fade = tmp;
172 ib+=5;
174 memcpy(ID666.artist,ib,32);
175 ID666.artist[31]=0;
176 ib+=32;
178 /*I have no idea if this is right or not.*/
179 ID666.muted=*ib;
180 ib++;
182 memcpy(buf, ib, 1);
183 buf[1] = 0;
184 tmp = 0;
185 ib++;
187 return 1;
190 /**************** Codec ****************/
191 enum {SAMPLE_RATE = 32000};
192 static struct Spc_Emu spc_emu IDATA_ATTR CACHEALIGN_ATTR;
194 #if SPC_DUAL_CORE
195 /** Implementations for pipelined dual-core operation **/
196 static int spc_emu_thread_stack[DEFAULT_STACK_SIZE/sizeof(int)]
197 CACHEALIGN_ATTR;
199 static const unsigned char * const spc_emu_thread_name = "spc emu";
200 static unsigned int emu_thread_id = 0;
202 enum
204 SPC_EMU_AUDIO = 0,
205 SPC_EMU_LOAD,
206 SPC_EMU_QUIT,
209 struct spc_load
211 uint8_t *buf;
212 size_t size;
215 /* sample queue */
216 #define WAV_NUM_CHUNKS 2
217 #define WAV_CHUNK_MASK (WAV_NUM_CHUNKS-1)
218 struct sample_queue_chunk
220 long id;
221 union
223 intptr_t data;
224 int32_t audio[WAV_CHUNK_SIZE*2];
228 static struct
230 int head, tail;
231 struct semaphore emu_sem_head;
232 struct semaphore emu_sem_tail;
233 struct semaphore emu_evt_reply;
234 intptr_t retval;
235 struct sample_queue_chunk wav_chunk[WAV_NUM_CHUNKS];
236 } sample_queue SHAREDBSS_ATTR;
238 static inline void samples_release_wrbuf(void)
240 sample_queue.tail++;
241 ci->semaphore_release(&sample_queue.emu_sem_head);
244 static inline struct sample_queue_chunk * samples_get_wrbuf(void)
246 ci->semaphore_wait(&sample_queue.emu_sem_tail);
247 return &sample_queue.wav_chunk[sample_queue.tail & WAV_CHUNK_MASK];
250 static inline void samples_release_rdbuf(void)
252 if (sample_queue.head != sample_queue.tail) {
253 sample_queue.head++;
256 ci->semaphore_release(&sample_queue.emu_sem_tail);
259 static inline int32_t * samples_get_rdbuf(void)
261 ci->semaphore_wait(&sample_queue.emu_sem_head);
263 if (ci->stop_codec || ci->new_track)
265 /* Told to stop. Buffer must be released. */
266 samples_release_rdbuf();
267 return NULL;
270 return sample_queue.wav_chunk[sample_queue.head & WAV_CHUNK_MASK].audio;
273 static intptr_t emu_thread_send_msg(long id, intptr_t data)
275 struct sample_queue_chunk *chunk;
276 /* Grab an audio output buffer */
277 ci->semaphore_wait(&sample_queue.emu_sem_head);
278 chunk = &sample_queue.wav_chunk[sample_queue.head & WAV_CHUNK_MASK];
279 /* Place a message in it instead of audio */
280 chunk->id = id;
281 chunk->data = data;
282 /* Release it to the emu thread */
283 samples_release_rdbuf();
285 if (id != SPC_EMU_QUIT) {
286 /* Wait for a response */
287 ci->semaphore_wait(&sample_queue.emu_evt_reply);
290 return sample_queue.retval;
293 /* thread function */
294 static bool emu_thread_process_msg(struct sample_queue_chunk *chunk)
296 long id = chunk->id;
297 bool ret = id != SPC_EMU_QUIT;
299 chunk->id = SPC_EMU_AUDIO; /* Reset chunk type to audio */
300 sample_queue.retval = 0;
302 if (id == SPC_EMU_LOAD)
304 struct spc_load *ld = (struct spc_load *)chunk->data;
305 ci->cpucache_invalidate();
306 SPC_Init(&spc_emu);
307 sample_queue.retval = SPC_load_spc(&spc_emu, ld->buf, ld->size);
309 /* Empty the audio queue */
310 /* This is a dirty hack a timeout based wait would make unnescessary but
311 still safe because the other thread is known to be waiting for a reply
312 and is not using the objects. */
313 ci->semaphore_init(&sample_queue.emu_sem_tail, 2, 2);
314 ci->semaphore_init(&sample_queue.emu_sem_head, 2, 0);
315 sample_queue.head = sample_queue.tail = 0;
318 if (id != SPC_EMU_QUIT) {
319 ci->semaphore_release(&sample_queue.emu_evt_reply);
322 return ret;
325 static void spc_emu_thread(void)
327 CPU_Init(&spc_emu);
329 while (1) {
330 /* get a buffer for output */
331 struct sample_queue_chunk *chunk = samples_get_wrbuf();
333 if (chunk->id != SPC_EMU_AUDIO) {
334 /* This chunk doesn't contain audio but a command */
335 if (!emu_thread_process_msg(chunk))
336 break;
337 /* Have to re-get this pointer to keep semaphore counts correct */
338 continue;
341 ENTER_TIMER(render);
342 /* fill samples buffer */
343 if ( SPC_play(&spc_emu, WAV_CHUNK_SIZE*2, chunk->audio) )
344 assert( false );
345 EXIT_TIMER(render);
347 /* done so release it to output */
348 samples_release_wrbuf();
349 ci->yield();
353 static bool spc_emu_start(void)
355 emu_thread_id = ci->create_thread(spc_emu_thread, spc_emu_thread_stack,
356 sizeof(spc_emu_thread_stack), CREATE_THREAD_FROZEN,
357 spc_emu_thread_name IF_PRIO(, PRIORITY_PLAYBACK), COP);
359 if (emu_thread_id == 0)
360 return false;
362 /* Initialize audio queue as full to prevent emu thread from trying to run the
363 emulator before loading something */
364 ci->semaphore_init(&sample_queue.emu_evt_reply, 1, 0);
365 ci->semaphore_init(&sample_queue.emu_sem_tail, 2, 0);
366 ci->semaphore_init(&sample_queue.emu_sem_head, 2, 2);
367 sample_queue.head = 0;
368 sample_queue.tail = 2;
370 /* Start it running */
371 ci->thread_thaw(emu_thread_id);
372 return true;
375 /* load a new program on the emu thread */
376 static inline int load_spc_buffer(uint8_t *buf, size_t size)
378 struct spc_load ld = { buf, size };
379 ci->cpucache_flush();
380 return emu_thread_send_msg(SPC_EMU_LOAD, (intptr_t)&ld);
383 static inline void spc_emu_quit(void)
385 if (emu_thread_id != 0) {
386 emu_thread_send_msg(SPC_EMU_QUIT, 0);
387 /* Wait for emu thread to be killed */
388 ci->thread_wait(emu_thread_id);
389 ci->cpucache_invalidate();
393 static inline bool spc_play_get_samples(int32_t **samples)
395 /* obtain filled samples buffer */
396 *samples = samples_get_rdbuf();
397 return *samples != NULL;
400 static inline void spc_play_send_samples(int32_t *samples)
402 ci->pcmbuf_insert(samples, samples+WAV_CHUNK_SIZE, WAV_CHUNK_SIZE);
403 /* done with chunk so release it to emu thread */
404 samples_release_rdbuf();
407 #else /* !SPC_DUAL_CORE */
408 /** Implementations for single-core operation **/
409 int32_t wav_chunk[WAV_CHUNK_SIZE*2] IBSS_ATTR;
411 /* load a new program into emu */
412 static inline int load_spc_buffer(uint8_t *buf, size_t size)
414 SPC_Init(&spc_emu);
415 return SPC_load_spc(&spc_emu, buf, size);
418 static inline bool spc_emu_start(void)
420 #ifdef CPU_COLDFIRE
421 /* signed integer mode with saturation */
422 coldfire_set_macsr(EMAC_SATURATE);
423 #endif
424 CPU_Init(&spc_emu);
425 return true;
428 static inline void spc_play_send_samples(int32_t *samples)
430 ci->pcmbuf_insert(samples, samples+WAV_CHUNK_SIZE, WAV_CHUNK_SIZE);
433 #define spc_emu_quit()
434 #define samples_release_rdbuf()
436 static inline bool spc_play_get_samples(int32_t **samples)
438 ENTER_TIMER(render);
439 /* fill samples buffer */
440 if ( SPC_play(&spc_emu,WAV_CHUNK_SIZE*2,wav_chunk) )
441 assert( false );
442 EXIT_TIMER(render);
443 *samples = wav_chunk;
444 return true;
446 #endif /* SPC_DUAL_CORE */
448 /* The main decoder loop */
449 static int play_track( void )
451 int sampleswritten=0;
453 unsigned long fadestartsample = ID666.length*(long long) SAMPLE_RATE/1000;
454 unsigned long fadeendsample = (ID666.length+ID666.fade)*(long long) SAMPLE_RATE/1000;
455 int fadedec = 0;
456 int fadevol = 0x7fffffffl;
458 if (fadeendsample>fadestartsample)
459 fadedec=0x7fffffffl/(fadeendsample-fadestartsample)+1;
461 ENTER_TIMER(total);
463 while ( 1 )
465 ci->yield();
466 if (ci->stop_codec || ci->new_track) {
467 break;
470 if (ci->seek_time) {
471 int curtime = sampleswritten*1000LL/SAMPLE_RATE;
472 DEBUGF("seek to %ld\ncurrently at %d\n",ci->seek_time,curtime);
473 if (ci->seek_time < curtime) {
474 DEBUGF("seek backwards = reset\n");
475 ci->seek_complete();
476 return 1;
478 ci->seek_complete();
481 int32_t *samples;
482 if (!spc_play_get_samples(&samples))
483 break;
485 sampleswritten += WAV_CHUNK_SIZE;
487 /* is track timed? */
488 if (ci->global_settings->repeat_mode!=REPEAT_ONE && ci->id3->length) {
489 unsigned long curtime = sampleswritten*1000LL/SAMPLE_RATE;
490 unsigned long lasttimesample = (sampleswritten-WAV_CHUNK_SIZE);
492 /* fade? */
493 if (curtime>ID666.length)
495 #ifdef CPU_COLDFIRE
496 /* Have to switch modes to do this */
497 long macsr = coldfire_get_macsr();
498 coldfire_set_macsr(EMAC_SATURATE | EMAC_FRACTIONAL | EMAC_ROUND);
499 #endif
500 int i;
501 for (i=0;i<WAV_CHUNK_SIZE;i++) {
502 if (lasttimesample+i>fadestartsample) {
503 if (fadevol>0) {
504 samples[i] = FRACMUL(samples[i], fadevol);
505 samples[i+WAV_CHUNK_SIZE] = FRACMUL(samples[i+WAV_CHUNK_SIZE], fadevol);
506 } else samples[i]=samples[i+WAV_CHUNK_SIZE]=0;
507 fadevol-=fadedec;
510 #ifdef CPU_COLDFIRE
511 coldfire_set_macsr(macsr);
512 #endif
514 /* end? */
515 if (lasttimesample>=fadeendsample)
517 samples_release_rdbuf();
518 break;
522 spc_play_send_samples(samples);
524 if (ci->global_settings->repeat_mode!=REPEAT_ONE)
525 ci->set_elapsed(sampleswritten*1000LL/SAMPLE_RATE);
526 else
527 ci->set_elapsed(0);
530 EXIT_TIMER(total);
531 return 0;
534 /* this is the codec entry point */
535 enum codec_status codec_main(void)
537 enum codec_status stat = CODEC_ERROR;
539 if (!spc_emu_start())
540 goto codec_quit;
544 DEBUGF("SPC: next_track\n");
545 if (codec_init()) {
546 goto codec_quit;
548 DEBUGF("SPC: after init\n");
550 ci->configure(DSP_SET_SAMPLE_DEPTH, 24);
551 ci->configure(DSP_SET_FREQUENCY, SAMPLE_RATE);
552 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
554 /* wait for track info to load */
555 while (!*ci->taginfo_ready && !ci->stop_codec)
556 ci->yield();
558 codec_set_replaygain(ci->id3);
560 /* Read the entire file */
561 DEBUGF("SPC: request initial buffer\n");
562 ci->seek_buffer(0);
563 size_t buffersize;
564 uint8_t* buffer = ci->request_buffer(&buffersize, ci->filesize);
565 if (!buffer) {
566 goto codec_quit;
569 DEBUGF("SPC: read size = 0x%lx\n",(unsigned long)buffersize);
572 if (load_spc_buffer(buffer, buffersize)) {
573 DEBUGF("SPC load failure\n");
574 goto codec_quit;
577 LoadID666(buffer+0x2e);
579 if (ci->global_settings->repeat_mode!=REPEAT_ONE && ID666.length==0) {
580 ID666.length=3*60*1000; /* 3 minutes */
581 ID666.fade=5*1000; /* 5 seconds */
584 reset_profile_timers();
586 while ( play_track() );
588 print_timers(ci->id3->path);
590 while ( ci->request_next_track() );
592 stat = CODEC_OK;
594 codec_quit:
595 spc_emu_quit();
597 return stat;