1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2006-2008 Adam Gashlin (hcs)
10 * Copyright (C) 2006 Jens Arnold
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
26 #include "lib/fixedpoint.h"
30 /* Maximum number of bytes to process in one iteration */
31 #define WAV_CHUNK_SIZE (1024*2)
33 /* Number of times to loop looped tracks when repeat is disabled */
36 /* Length of fade-out for looped tracks (milliseconds) */
37 #define FADE_LENGTH 10000L
39 /* Default high pass filter cutoff frequency is 500 Hz.
40 * Others can be set, but the default is nearly always used,
41 * and there is no way to determine if another was used, anyway.
43 static const long cutoff
= 500;
45 static int16_t samples
[WAV_CHUNK_SIZE
] IBSS_ATTR
;
47 /* this is the codec entry point */
48 enum codec_status
codec_main(enum codec_entry_call_reason reason
)
50 if (reason
== CODEC_LOAD
) {
51 /* Generic codec initialisation */
52 /* we only render 16 bits */
53 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 16);
59 /* this is called for each file to process */
60 enum codec_status
codec_run(void)
63 int sampleswritten
, i
;
65 int32_t ch1_1
, ch1_2
, ch2_1
, ch2_2
; /* ADPCM history */
67 int endofstream
; /* end of stream flag */
68 uint32_t avgbytespersec
;
69 int looping
; /* looping flag */
70 int loop_count
; /* number of loops done so far */
71 int fade_count
; /* countdown for fadeout */
72 int fade_frames
; /* length of fade in frames */
73 off_t start_adr
, end_adr
; /* loop points */
74 off_t chanstart
, bufoff
;
75 /*long coef1=0x7298L,coef2=-0x3350L;*/
79 DEBUGF("ADX: next_track\n");
83 DEBUGF("ADX: after init\n");
86 ch1_1
=ch1_2
=ch2_1
=ch2_2
=0;
88 codec_set_replaygain(ci
->id3
);
91 DEBUGF("ADX: request initial buffer\n");
93 buf
= ci
->request_buffer(&n
, 0x38);
94 if (!buf
|| n
< 0x38) {
98 DEBUGF("ADX: read size = %lx\n",(unsigned long)n
);
100 /* Get file header for starting offset, channel count */
102 chanstart
= ((buf
[2] << 8) | buf
[3]) + 4;
105 /* useful for seeking and reporting current playback position */
106 avgbytespersec
= ci
->id3
->frequency
* 18 * channels
/ 32;
107 DEBUGF("avgbytespersec=%ld\n",(unsigned long)avgbytespersec
);
109 /* calculate filter coefficients */
112 * A simple table of these coefficients would be nice, but
113 * some very odd frequencies are used and if I'm going to
114 * interpolate I might as well just go all the way and
115 * calclate them precisely.
116 * Speed is not an issue as this only needs to be done once per file.
119 const int64_t big28
= 0x10000000LL
;
120 const int64_t big32
= 0x100000000LL
;
121 int64_t frequency
= ci
->id3
->frequency
;
122 int64_t phasemultiple
= cutoff
*big32
/frequency
;
126 const int64_t b
= (M_SQRT2
*big28
)-big28
;
130 fp_sincos((unsigned long)phasemultiple
,&z
);
132 a
= (M_SQRT2
*big28
) - (z
>> 3);
135 * In the long passed to fsqrt there are only 4 nonfractional bits,
136 * which is sufficient here, but this is the only reason why I don't
137 * use 32 fractional bits everywhere.
139 d
= fp_sqrt((a
+b
)*(a
-b
)/big28
,28);
142 coef1
= (c
*8192) >> 28;
143 coef2
= (c
*c
/big28
*-4096) >> 28;
144 DEBUGF("ADX: samprate=%ld ",(long)frequency
);
145 DEBUGF("coef1 %04x ",(unsigned int)(coef1
*4));
146 DEBUGF("coef2 %04x\n",(unsigned int)(coef2
*-4));
151 looping
= 0; start_adr
= 0; end_adr
= 0;
152 if (!memcmp(buf
+0x10,"\x01\xF4\x03\x00",4)) {
153 /* Soul Calibur 2 style (type 03) */
154 DEBUGF("ADX: type 03 found\n");
155 /* check if header is too small for loop data */
156 if (chanstart
-6 < 0x2c) looping
=0;
158 looping
= (buf
[0x18]) ||
162 end_adr
= (buf
[0x28]<<24) |
172 )/32*channels
*18+chanstart
;
174 } else if (!memcmp(buf
+0x10,"\x01\xF4\x04\x00",4)) {
175 /* Standard (type 04) */
176 DEBUGF("ADX: type 04 found\n");
177 /* check if header is too small for loop data */
178 if (chanstart
-6 < 0x38) looping
=0;
180 looping
= (buf
[0x24]) ||
184 end_adr
= (buf
[0x34]<<24) |
193 )/32*channels
*18+chanstart
;
196 DEBUGF("ADX: error, couldn't determine ADX type\n");
201 DEBUGF("ADX: looped, start: %lx end: %lx\n",start_adr
,end_adr
);
203 DEBUGF("ADX: not looped\n");
206 /* advance to first frame */
207 DEBUGF("ADX: first frame at %lx\n",chanstart
);
210 /* get in position */
211 ci
->seek_buffer(bufoff
);
214 /* setup pcm buffer format */
215 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
217 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_INTERLEAVED
);
218 } else if (channels
== 1) {
219 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_MONO
);
221 DEBUGF("ADX CODEC_ERROR: more than 2 channels\n");
227 fade_count
= -1; /* disable fade */
230 /* The main decoder loop */
232 while (!endofstream
) {
233 enum codec_command_action action
= ci
->get_command(¶m
);
235 if (action
== CODEC_ACTION_HALT
)
238 /* do we need to loop? */
239 if (bufoff
> end_adr
-18*channels
&& looping
) {
240 DEBUGF("ADX: loop!\n");
241 /* check for endless looping */
242 if (ci
->global_settings
->repeat_mode
==REPEAT_ONE
) {
244 fade_count
= -1; /* disable fade */
246 /* otherwise start fade after LOOP_TIMES loops */
248 if (loop_count
>= LOOP_TIMES
&& fade_count
< 0) {
249 /* frames to fade over */
250 fade_frames
= FADE_LENGTH
*ci
->id3
->frequency
/32/1000;
251 /* volume relative to fade_frames */
252 fade_count
= fade_frames
;
253 DEBUGF("ADX: fade_frames = %d\n",fade_frames
);
257 ci
->seek_buffer(bufoff
);
260 /* do we need to seek? */
261 if (action
== CODEC_ACTION_SEEK_TIME
) {
264 DEBUGF("ADX: seek to %ldms\n", (long)param
);
268 fade_count
= -1; /* disable fade */
271 newpos
= (((uint64_t)avgbytespersec
*param
)
272 / (1000LL*18*channels
))*(18*channels
);
273 bufoff
= chanstart
+ newpos
;
274 while (bufoff
> end_adr
-18*channels
) {
275 bufoff
-=end_adr
-start_adr
;
278 ci
->seek_buffer(bufoff
);
282 if (bufoff
>ci
->filesize
-channels
*18) break; /* End of stream */
287 /* Is there data left in the file? */
288 (bufoff
<= ci
->filesize
-(18*channels
)) &&
289 /* Is there space in the output buffer? */
290 (sampleswritten
<= WAV_CHUNK_SIZE
-(32*channels
)) &&
291 /* Should we be looping? */
292 ((!looping
) || bufoff
<= end_adr
-18*channels
))
294 /* decode first/only channel */
299 buf
= ci
->request_buffer(&n
, 18);
302 DEBUGF("ADX: couldn't get buffer at %lx\n",
307 scale
= ((buf
[0] << 8) | (buf
[1])) +1;
309 for (i
= 2; i
< 18; i
++)
311 d
= (buf
[i
] >> 4) & 15;
313 ch1_0
= d
*scale
+ ((coef1
*ch1_1
+ coef2
*ch1_2
) >> 12);
314 if (ch1_0
> 32767) ch1_0
= 32767;
315 else if (ch1_0
< -32768) ch1_0
= -32768;
316 samples
[sampleswritten
] = ch1_0
;
317 sampleswritten
+=channels
;
318 ch1_2
= ch1_1
; ch1_1
= ch1_0
;
322 ch1_0
= d
*scale
+ ((coef1
*ch1_1
+ coef2
*ch1_2
) >> 12);
323 if (ch1_0
> 32767) ch1_0
= 32767;
324 else if (ch1_0
< -32768) ch1_0
= -32768;
325 samples
[sampleswritten
] = ch1_0
;
326 sampleswritten
+=channels
;
327 ch1_2
= ch1_1
; ch1_1
= ch1_0
;
330 ci
->advance_buffer(18);
333 /* decode second channel */
337 buf
= ci
->request_buffer(&n
, 18);
340 DEBUGF("ADX: couldn't get buffer at %lx\n",
345 scale
= ((buf
[0] << 8)|(buf
[1]))+1;
349 for (i
= 2; i
< 18; i
++)
351 d
= (buf
[i
] >> 4) & 15;
353 ch2_0
= d
*scale
+ ((coef1
*ch2_1
+ coef2
*ch2_2
) >> 12);
354 if (ch2_0
> 32767) ch2_0
= 32767;
355 else if (ch2_0
< -32768) ch2_0
= -32768;
356 samples
[sampleswritten
] = ch2_0
;
358 ch2_2
= ch2_1
; ch2_1
= ch2_0
;
362 ch2_0
= d
*scale
+ ((coef1
*ch2_1
+ coef2
*ch2_2
) >> 12);
363 if (ch2_0
> 32767) ch2_0
= 32767;
364 else if (ch2_0
< -32768) ch2_0
= -32768;
365 samples
[sampleswritten
] = ch2_0
;
367 ch2_2
= ch2_1
; ch2_1
= ch2_0
;
370 ci
->advance_buffer(18);
371 sampleswritten
--; /* go back to first channel's next sample */
376 for (i
=0;i
<(channels
==1?32:64);i
++) samples
[sampleswritten
-i
-1]=
377 ((int32_t)samples
[sampleswritten
-i
-1])*fade_count
/fade_frames
;
378 if (fade_count
==0) {endofstream
=1; break;}
383 sampleswritten
>>= 1; /* make samples/channel */
385 ci
->pcmbuf_insert(samples
, NULL
, sampleswritten
);
388 ((end_adr
-start_adr
)*loop_count
+ bufoff
-chanstart
)*
389 1000LL/avgbytespersec
);