1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2006-2007 Adam Gashlin (hcs)
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
23 /* Maximum number of bytes to process in one iteration */
24 #define WAV_CHUNK_SIZE (1024*2)
26 /* Volume for ADX decoder */
27 #define BASE_VOL 0x2000
29 /* Number of times to loop looped tracks when repeat is disabled */
32 /* Length of fade-out for looped tracks (milliseconds) */
33 #define FADE_LENGTH 10000L
35 static int16_t samples
[WAV_CHUNK_SIZE
] IBSS_ATTR
;
37 /* this is the codec entry point */
38 enum codec_status
codec_main(void)
41 int sampleswritten
, i
;
43 int32_t ch1_1
, ch1_2
, ch2_1
, ch2_2
; /* ADPCM history */
45 int endofstream
; /* end of stream flag */
46 uint32_t avgbytespersec
;
47 int looping
; /* looping flag */
48 int loop_count
; /* number of loops done so far */
49 int fade_count
; /* countdown for fadeout */
50 int fade_frames
; /* length of fade in frames */
51 off_t start_adr
, end_adr
; /* loop points */
52 off_t chanstart
, bufoff
;
54 /* Generic codec initialisation */
55 /* we only render 16 bits */
56 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 16);
59 DEBUGF("ADX: next_track\n");
63 DEBUGF("ADX: after init\n");
66 ch1_1
=ch1_2
=ch2_1
=ch2_2
=0;
68 /* wait for track info to load */
69 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
72 codec_set_replaygain(ci
->id3
);
75 DEBUGF("ADX: request initial buffer\n");
77 buf
= ci
->request_buffer(&n
, 0x38);
78 if (!buf
|| n
< 0x38) {
82 DEBUGF("ADX: read size = %lx\n",(unsigned long)n
);
84 /* Get file header for starting offset, channel count */
86 chanstart
= ((buf
[2] << 8) | buf
[3]) + 4;
89 /* useful for seeking and reporting current playback position */
90 avgbytespersec
= ci
->id3
->frequency
* 18 * channels
/ 32;
91 DEBUGF("avgbytespersec=%ld\n",(unsigned long)avgbytespersec
);
95 looping
= 0; start_adr
= 0; end_adr
= 0;
96 if (!memcmp(buf
+0x10,"\x01\xF4\x03\x00",4)) {
97 /* Soul Calibur 2 style (type 03) */
98 DEBUGF("ADX: type 03 found\n");
99 /* check if header is too small for loop data */
100 if (chanstart
-6 < 0x2c) looping
=0;
102 looping
= (buf
[0x18]) ||
106 end_adr
= (buf
[0x28]<<24) |
116 )/32*channels
*18+chanstart
;
118 } else if (!memcmp(buf
+0x10,"\x01\xF4\x04\x00",4)) {
119 /* Standard (type 04) */
120 DEBUGF("ADX: type 04 found\n");
121 /* check if header is too small for loop data */
122 if (chanstart
-6 < 0x38) looping
=0;
124 looping
= (buf
[0x24]) ||
128 end_adr
= (buf
[0x34]<<24) |
137 )/32*channels
*18+chanstart
;
140 DEBUGF("ADX: error, couldn't determine ADX type\n");
145 DEBUGF("ADX: looped, start: %lx end: %lx\n",start_adr
,end_adr
);
147 DEBUGF("ADX: not looped\n");
150 /* advance to first frame */
151 DEBUGF("ADX: first frame at %lx\n",chanstart
);
154 /* get in position */
155 ci
->seek_buffer(bufoff
);
158 /* setup pcm buffer format */
159 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
161 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_INTERLEAVED
);
162 } else if (channels
== 1) {
163 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_MONO
);
165 DEBUGF("ADX CODEC_ERROR: more than 2 channels\n");
171 fade_count
= -1; /* disable fade */
174 /* The main decoder loop */
176 while (!endofstream
) {
178 if (ci
->stop_codec
|| ci
->new_track
) {
182 /* do we need to loop? */
183 if (bufoff
> end_adr
-18*channels
&& looping
) {
184 DEBUGF("ADX: loop!\n");
185 /* check for endless looping */
186 if (ci
->global_settings
->repeat_mode
==REPEAT_ONE
) {
188 fade_count
= -1; /* disable fade */
190 /* otherwise start fade after LOOP_TIMES loops */
192 if (loop_count
>= LOOP_TIMES
&& fade_count
< 0) {
193 /* frames to fade over */
194 fade_frames
= FADE_LENGTH
*ci
->id3
->frequency
/32/1000;
195 /* volume relative to fade_frames */
196 fade_count
= fade_frames
;
197 DEBUGF("ADX: fade_frames = %d\n",fade_frames
);
201 ci
->seek_buffer(bufoff
);
204 /* do we need to seek? */
208 DEBUGF("ADX: seek to %ldms\n",ci
->seek_time
);
212 fade_count
= -1; /* disable fade */
215 newpos
= (((uint64_t)avgbytespersec
*(ci
->seek_time
- 1))
216 / (1000LL*18*channels
))*(18*channels
);
217 bufoff
= chanstart
+ newpos
;
218 while (bufoff
> end_adr
-18*channels
) {
219 bufoff
-=end_adr
-start_adr
;
222 ci
->seek_buffer(bufoff
);
226 if (bufoff
>ci
->filesize
-channels
*18) break; /* End of stream */
231 /* Is there data left in the file? */
232 (bufoff
<= ci
->filesize
-(18*channels
)) &&
233 /* Is there space in the output buffer? */
234 (sampleswritten
<= WAV_CHUNK_SIZE
-(32*channels
)) &&
235 /* Should we be looping? */
236 ((!looping
) || bufoff
<= end_adr
-18*channels
))
238 /* decode first/only channel */
243 buf
= ci
->request_buffer(&n
, 18);
246 DEBUGF("ADX: couldn't get buffer at %lx\n",
251 scale
= (((buf
[0] << 8) | (buf
[1])) +1) * BASE_VOL
;
253 for (i
= 2; i
< 18; i
++)
255 d
= (buf
[i
] >> 4) & 15;
257 ch1_0
= (d
*scale
+ 0x7298L
*ch1_1
- 0x3350L
*ch1_2
) >> 14;
258 if (ch1_0
> 32767) ch1_0
= 32767;
259 else if (ch1_0
< -32768) ch1_0
= -32768;
260 samples
[sampleswritten
] = ch1_0
;
261 sampleswritten
+=channels
;
262 ch1_2
= ch1_1
; ch1_1
= ch1_0
;
266 ch1_0
= (d
*scale
+ 0x7298L
*ch1_1
- 0x3350L
*ch1_2
) >> 14;
267 if (ch1_0
> 32767) ch1_0
= 32767;
268 else if (ch1_0
< -32768) ch1_0
= -32768;
269 samples
[sampleswritten
] = ch1_0
;
270 sampleswritten
+=channels
;
271 ch1_2
= ch1_1
; ch1_1
= ch1_0
;
274 ci
->advance_buffer(18);
277 /* decode second channel */
281 buf
= ci
->request_buffer(&n
, 18);
284 DEBUGF("ADX: couldn't get buffer at %lx\n",
289 scale
= (((buf
[0] << 8)|(buf
[1]))+1)*BASE_VOL
;
293 for (i
= 2; i
< 18; i
++)
295 d
= (buf
[i
] >> 4) & 15;
297 ch2_0
= (d
*scale
+ 0x7298L
*ch2_1
- 0x3350L
*ch2_2
) >> 14;
298 if (ch2_0
> 32767) ch2_0
= 32767;
299 else if (ch2_0
< -32768) ch2_0
= -32768;
300 samples
[sampleswritten
] = ch2_0
;
302 ch2_2
= ch2_1
; ch2_1
= ch2_0
;
306 ch2_0
= (d
*scale
+ 0x7298L
*ch2_1
- 0x3350L
*ch2_2
) >> 14;
307 if (ch2_0
> 32767) ch2_0
= 32767;
308 else if (ch2_0
< -32768) ch2_0
= -32768;
309 samples
[sampleswritten
] = ch2_0
;
311 ch2_2
= ch2_1
; ch2_1
= ch2_0
;
314 ci
->advance_buffer(18);
315 sampleswritten
--; /* go back to first channel's next sample */
320 for (i
=0;i
<(channels
==1?32:64);i
++) samples
[sampleswritten
-i
-1]=
321 ((int32_t)samples
[sampleswritten
-i
-1])*fade_count
/fade_frames
;
322 if (fade_count
==0) {endofstream
=1; break;}
327 sampleswritten
>>= 1; /* make samples/channel */
329 ci
->pcmbuf_insert(samples
, NULL
, sampleswritten
);
332 ((end_adr
-start_adr
)*loop_count
+ bufoff
-chanstart
)*
333 1000LL/avgbytespersec
);
336 if (ci
->request_next_track())