1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 */
29 #include "../fracmul.h"
30 #include "libspc/spc_codec.h"
31 #include "libspc/spc_profiler.h"
35 /**************** ID666 parsing ****************/
38 unsigned char isBinary
;
48 unsigned char emulator
;
51 static int LoadID666(unsigned char *buf
) {
52 unsigned char *ib
=buf
;
56 memcpy(ID666
.song
,ib
,32);
60 memcpy(ID666
.game
,ib
,32);
64 memcpy(ID666
.dumper
,ib
,16);
68 memcpy(ID666
.comments
,ib
,32);
72 /* Ok, now comes the fun part. */
75 if(ib
[2] == '/' && ib
[5] == '/' )
78 /* Reserved bytes check */
79 if(ib
[0xD2 - 0x2E - 112] >= '0' &&
80 ib
[0xD2 - 0x2E - 112] <= '9' &&
81 ib
[0xD3 - 0x2E - 112] == 0x00)
84 /* is length & fade only digits? */
86 (ib
[0xA9 - 0x2E - 112+i
]>='0'&&ib
[0xA9 - 0x2E - 112+i
]<='9') ||
87 ib
[0xA9 - 0x2E - 112+i
]=='\0');
91 ID666
.isBinary
= isbinary
;
94 DEBUGF("binary tag detected\n");
109 ID666
.length
|=*ib
<<8;
112 ID666
.length
|=*ib
<<16;
125 memcpy(ID666
.artist
,ib
,32);
138 DEBUGF("text tag detected\n");
143 for (i
=0;i
<2 && buf
[i
]>='0' && buf
[i
]<='9';i
++) tmp
=tmp
*10+buf
[i
]-'0';
150 for (i
=0;i
<2 && buf
[i
]>='0' && buf
[i
]<='9';i
++) tmp
=tmp
*10+buf
[i
]-'0';
157 for (i
=0;i
<4 && buf
[i
]>='0' && buf
[i
]<='9';i
++) tmp
=tmp
*10+buf
[i
]-'0';
164 for (i
=0;i
<3 && buf
[i
]>='0' && buf
[i
]<='9';i
++) tmp
=tmp
*10+buf
[i
]-'0';
165 ID666
.length
= tmp
* 1000;
171 for (i
=0;i
<5 && buf
[i
]>='0' && buf
[i
]<='9';i
++) tmp
=tmp
*10+buf
[i
]-'0';
175 memcpy(ID666
.artist
,ib
,32);
179 /*I have no idea if this is right or not.*/
191 /**************** Codec ****************/
192 enum {SAMPLE_RATE
= 32000};
193 static struct Spc_Emu spc_emu IDATA_ATTR CACHEALIGN_ATTR
;
196 /** Implementations for pipelined dual-core operation **/
197 static int spc_emu_thread_stack
[DEFAULT_STACK_SIZE
/sizeof(int)]
200 static const unsigned char * const spc_emu_thread_name
= "spc emu";
201 static unsigned int emu_thread_id
= 0;
217 #define WAV_NUM_CHUNKS 2
218 #define WAV_CHUNK_MASK (WAV_NUM_CHUNKS-1)
219 struct sample_queue_chunk
225 int32_t audio
[WAV_CHUNK_SIZE
*2];
232 struct semaphore emu_sem_head
;
233 struct semaphore emu_sem_tail
;
234 struct semaphore emu_evt_reply
;
236 struct sample_queue_chunk wav_chunk
[WAV_NUM_CHUNKS
];
237 } sample_queue SHAREDBSS_ATTR
;
239 static inline void samples_release_wrbuf(void)
242 ci
->semaphore_release(&sample_queue
.emu_sem_head
);
245 static inline struct sample_queue_chunk
* samples_get_wrbuf(void)
247 ci
->semaphore_wait(&sample_queue
.emu_sem_tail
);
248 return &sample_queue
.wav_chunk
[sample_queue
.tail
& WAV_CHUNK_MASK
];
251 static inline void samples_release_rdbuf(void)
253 if (sample_queue
.head
!= sample_queue
.tail
) {
257 ci
->semaphore_release(&sample_queue
.emu_sem_tail
);
260 static inline int32_t * samples_get_rdbuf(void)
262 ci
->semaphore_wait(&sample_queue
.emu_sem_head
);
264 if (ci
->stop_codec
|| ci
->new_track
)
266 /* Told to stop. Buffer must be released. */
267 samples_release_rdbuf();
271 return sample_queue
.wav_chunk
[sample_queue
.head
& WAV_CHUNK_MASK
].audio
;
274 static intptr_t emu_thread_send_msg(long id
, intptr_t data
)
276 struct sample_queue_chunk
*chunk
;
277 /* Grab an audio output buffer */
278 ci
->semaphore_wait(&sample_queue
.emu_sem_head
);
279 chunk
= &sample_queue
.wav_chunk
[sample_queue
.head
& WAV_CHUNK_MASK
];
280 /* Place a message in it instead of audio */
283 /* Release it to the emu thread */
284 samples_release_rdbuf();
286 if (id
!= SPC_EMU_QUIT
) {
287 /* Wait for a response */
288 ci
->semaphore_wait(&sample_queue
.emu_evt_reply
);
291 return sample_queue
.retval
;
294 /* thread function */
295 static bool emu_thread_process_msg(struct sample_queue_chunk
*chunk
)
298 bool ret
= id
!= SPC_EMU_QUIT
;
300 chunk
->id
= SPC_EMU_AUDIO
; /* Reset chunk type to audio */
301 sample_queue
.retval
= 0;
303 if (id
== SPC_EMU_LOAD
)
305 struct spc_load
*ld
= (struct spc_load
*)chunk
->data
;
306 ci
->cpucache_invalidate();
308 sample_queue
.retval
= SPC_load_spc(&spc_emu
, ld
->buf
, ld
->size
);
310 /* Empty the audio queue */
311 /* This is a dirty hack a timeout based wait would make unnescessary but
312 still safe because the other thread is known to be waiting for a reply
313 and is not using the objects. */
314 ci
->semaphore_init(&sample_queue
.emu_sem_tail
, 2, 2);
315 ci
->semaphore_init(&sample_queue
.emu_sem_head
, 2, 0);
316 sample_queue
.head
= sample_queue
.tail
= 0;
319 if (id
!= SPC_EMU_QUIT
) {
320 ci
->semaphore_release(&sample_queue
.emu_evt_reply
);
326 static void spc_emu_thread(void)
331 /* get a buffer for output */
332 struct sample_queue_chunk
*chunk
= samples_get_wrbuf();
334 if (chunk
->id
!= SPC_EMU_AUDIO
) {
335 /* This chunk doesn't contain audio but a command */
336 if (!emu_thread_process_msg(chunk
))
338 /* Have to re-get this pointer to keep semaphore counts correct */
343 /* fill samples buffer */
344 if ( SPC_play(&spc_emu
, WAV_CHUNK_SIZE
*2, chunk
->audio
) )
348 /* done so release it to output */
349 samples_release_wrbuf();
354 static bool spc_emu_start(void)
356 emu_thread_id
= ci
->create_thread(spc_emu_thread
, spc_emu_thread_stack
,
357 sizeof(spc_emu_thread_stack
), CREATE_THREAD_FROZEN
,
358 spc_emu_thread_name
IF_PRIO(, PRIORITY_PLAYBACK
), COP
);
360 if (emu_thread_id
== 0)
363 /* Initialize audio queue as full to prevent emu thread from trying to run the
364 emulator before loading something */
365 ci
->semaphore_init(&sample_queue
.emu_evt_reply
, 1, 0);
366 ci
->semaphore_init(&sample_queue
.emu_sem_tail
, 2, 0);
367 ci
->semaphore_init(&sample_queue
.emu_sem_head
, 2, 2);
368 sample_queue
.head
= 0;
369 sample_queue
.tail
= 2;
371 /* Start it running */
372 ci
->thread_thaw(emu_thread_id
);
376 /* load a new program on the emu thread */
377 static inline int load_spc_buffer(uint8_t *buf
, size_t size
)
379 struct spc_load ld
= { buf
, size
};
380 ci
->cpucache_flush();
381 return emu_thread_send_msg(SPC_EMU_LOAD
, (intptr_t)&ld
);
384 static inline void spc_emu_quit(void)
386 if (emu_thread_id
!= 0) {
387 emu_thread_send_msg(SPC_EMU_QUIT
, 0);
388 /* Wait for emu thread to be killed */
389 ci
->thread_wait(emu_thread_id
);
390 ci
->cpucache_invalidate();
394 static inline bool spc_play_get_samples(int32_t **samples
)
396 /* obtain filled samples buffer */
397 *samples
= samples_get_rdbuf();
398 return *samples
!= NULL
;
401 static inline void spc_play_send_samples(int32_t *samples
)
403 ci
->pcmbuf_insert(samples
, samples
+WAV_CHUNK_SIZE
, WAV_CHUNK_SIZE
);
404 /* done with chunk so release it to emu thread */
405 samples_release_rdbuf();
408 #else /* !SPC_DUAL_CORE */
409 /** Implementations for single-core operation **/
410 int32_t wav_chunk
[WAV_CHUNK_SIZE
*2] IBSS_ATTR
;
412 /* load a new program into emu */
413 static inline int load_spc_buffer(uint8_t *buf
, size_t size
)
416 return SPC_load_spc(&spc_emu
, buf
, size
);
419 static inline bool spc_emu_start(void)
422 /* signed integer mode with saturation */
423 coldfire_set_macsr(EMAC_SATURATE
);
429 static inline void spc_play_send_samples(int32_t *samples
)
431 ci
->pcmbuf_insert(samples
, samples
+WAV_CHUNK_SIZE
, WAV_CHUNK_SIZE
);
434 #define spc_emu_quit()
435 #define samples_release_rdbuf()
437 static inline bool spc_play_get_samples(int32_t **samples
)
440 /* fill samples buffer */
441 if ( SPC_play(&spc_emu
,WAV_CHUNK_SIZE
*2,wav_chunk
) )
444 *samples
= wav_chunk
;
447 #endif /* SPC_DUAL_CORE */
449 /* The main decoder loop */
450 static int play_track( void )
452 int sampleswritten
=0;
454 unsigned long fadestartsample
= ID666
.length
*(long long) SAMPLE_RATE
/1000;
455 unsigned long fadeendsample
= (ID666
.length
+ID666
.fade
)*(long long) SAMPLE_RATE
/1000;
457 int fadevol
= 0x7fffffffl
;
459 if (fadeendsample
>fadestartsample
)
460 fadedec
=0x7fffffffl
/(fadeendsample
-fadestartsample
)+1;
467 if (ci
->stop_codec
|| ci
->new_track
) {
472 int curtime
= sampleswritten
*1000LL/SAMPLE_RATE
;
473 DEBUGF("seek to %ld\ncurrently at %d\n",ci
->seek_time
,curtime
);
474 if (ci
->seek_time
< curtime
) {
475 DEBUGF("seek backwards = reset\n");
483 if (!spc_play_get_samples(&samples
))
486 sampleswritten
+= WAV_CHUNK_SIZE
;
488 /* is track timed? */
489 if (ci
->global_settings
->repeat_mode
!=REPEAT_ONE
&& ci
->id3
->length
) {
490 unsigned long curtime
= sampleswritten
*1000LL/SAMPLE_RATE
;
491 unsigned long lasttimesample
= (sampleswritten
-WAV_CHUNK_SIZE
);
494 if (curtime
>ID666
.length
)
497 /* Have to switch modes to do this */
498 long macsr
= coldfire_get_macsr();
499 coldfire_set_macsr(EMAC_SATURATE
| EMAC_FRACTIONAL
| EMAC_ROUND
);
502 for (i
=0;i
<WAV_CHUNK_SIZE
;i
++) {
503 if (lasttimesample
+i
>fadestartsample
) {
505 samples
[i
] = FRACMUL(samples
[i
], fadevol
);
506 samples
[i
+WAV_CHUNK_SIZE
] = FRACMUL(samples
[i
+WAV_CHUNK_SIZE
], fadevol
);
507 } else samples
[i
]=samples
[i
+WAV_CHUNK_SIZE
]=0;
512 coldfire_set_macsr(macsr
);
516 if (lasttimesample
>=fadeendsample
)
518 samples_release_rdbuf();
523 spc_play_send_samples(samples
);
525 if (ci
->global_settings
->repeat_mode
!=REPEAT_ONE
)
526 ci
->set_elapsed(sampleswritten
*1000LL/SAMPLE_RATE
);
535 /* this is the codec entry point */
536 enum codec_status
codec_main(void)
538 enum codec_status stat
= CODEC_ERROR
;
540 if (!spc_emu_start())
545 DEBUGF("SPC: next_track\n");
549 DEBUGF("SPC: after init\n");
551 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 24);
552 ci
->configure(DSP_SET_FREQUENCY
, SAMPLE_RATE
);
553 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
555 /* wait for track info to load */
556 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
559 codec_set_replaygain(ci
->id3
);
561 /* Read the entire file */
562 DEBUGF("SPC: request initial buffer\n");
565 uint8_t* buffer
= ci
->request_buffer(&buffersize
, ci
->filesize
);
570 DEBUGF("SPC: read size = 0x%lx\n",(unsigned long)buffersize
);
573 if (load_spc_buffer(buffer
, buffersize
)) {
574 DEBUGF("SPC load failure\n");
578 LoadID666(buffer
+0x2e);
580 if (ci
->global_settings
->repeat_mode
!=REPEAT_ONE
&& ID666
.length
==0) {
581 ID666
.length
=3*60*1000; /* 3 minutes */
582 ID666
.fade
=5*1000; /* 5 seconds */
585 reset_profile_timers();
587 while ( play_track() );
589 print_timers(ci
->id3
->path
);
591 while ( ci
->request_next_track() );