Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 9 | * Copyright (C) 2006-2008 Adam Gashlin (hcs) |
| 10 | * Copyright (C) 2006 Jens Arnold |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 21 | #include "codeclib.h" |
| 22 | #include "inttypes.h" |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 23 | #include "math.h" |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 24 | |
| 25 | CODEC_HEADER |
| 26 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 27 | /* Maximum number of bytes to process in one iteration */ |
| 28 | #define WAV_CHUNK_SIZE (1024*2) |
| 29 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 30 | /* Number of times to loop looped tracks when repeat is disabled */ |
| 31 | #define LOOP_TIMES 2 |
| 32 | |
| 33 | /* Length of fade-out for looped tracks (milliseconds) */ |
| 34 | #define FADE_LENGTH 10000L |
| 35 | |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 36 | /* Default high pass filter cutoff frequency is 500 Hz. |
| 37 | * Others can be set, but the default is nearly always used, |
| 38 | * and there is no way to determine if another was used, anyway. |
| 39 | */ |
| 40 | const long cutoff = 500; |
| 41 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 42 | static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR; |
| 43 | |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 44 | /* fixed point stuff from apps/plugins/lib/fixedpoint.c */ |
| 45 | |
| 46 | /* Inverse gain of circular cordic rotation in s0.31 format. */ |
| 47 | static const long cordic_circular_gain = 0xb2458939; /* 0.607252929 */ |
| 48 | |
| 49 | /* Table of values of atan(2^-i) in 0.32 format fractions of pi where pi = 0xffffffff / 2 */ |
| 50 | static const unsigned long atan_table[] = { |
| 51 | 0x1fffffff, /* +0.785398163 (or pi/4) */ |
| 52 | 0x12e4051d, /* +0.463647609 */ |
| 53 | 0x09fb385b, /* +0.244978663 */ |
| 54 | 0x051111d4, /* +0.124354995 */ |
| 55 | 0x028b0d43, /* +0.062418810 */ |
| 56 | 0x0145d7e1, /* +0.031239833 */ |
| 57 | 0x00a2f61e, /* +0.015623729 */ |
| 58 | 0x00517c55, /* +0.007812341 */ |
| 59 | 0x0028be53, /* +0.003906230 */ |
| 60 | 0x00145f2e, /* +0.001953123 */ |
| 61 | 0x000a2f98, /* +0.000976562 */ |
| 62 | 0x000517cc, /* +0.000488281 */ |
| 63 | 0x00028be6, /* +0.000244141 */ |
| 64 | 0x000145f3, /* +0.000122070 */ |
| 65 | 0x0000a2f9, /* +0.000061035 */ |
| 66 | 0x0000517c, /* +0.000030518 */ |
| 67 | 0x000028be, /* +0.000015259 */ |
| 68 | 0x0000145f, /* +0.000007629 */ |
| 69 | 0x00000a2f, /* +0.000003815 */ |
| 70 | 0x00000517, /* +0.000001907 */ |
| 71 | 0x0000028b, /* +0.000000954 */ |
| 72 | 0x00000145, /* +0.000000477 */ |
| 73 | 0x000000a2, /* +0.000000238 */ |
| 74 | 0x00000051, /* +0.000000119 */ |
| 75 | 0x00000028, /* +0.000000060 */ |
| 76 | 0x00000014, /* +0.000000030 */ |
| 77 | 0x0000000a, /* +0.000000015 */ |
| 78 | 0x00000005, /* +0.000000007 */ |
| 79 | 0x00000002, /* +0.000000004 */ |
| 80 | 0x00000001, /* +0.000000002 */ |
| 81 | 0x00000000, /* +0.000000001 */ |
| 82 | 0x00000000, /* +0.000000000 */ |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Implements sin and cos using CORDIC rotation. |
| 87 | * |
| 88 | * @param phase has range from 0 to 0xffffffff, representing 0 and |
| 89 | * 2*pi respectively. |
| 90 | * @param cos return address for cos |
| 91 | * @return sin of phase, value is a signed value from LONG_MIN to LONG_MAX, |
| 92 | * representing -1 and 1 respectively. |
| 93 | */ |
| 94 | static long fsincos(unsigned long phase, long *cos) |
| 95 | { |
| 96 | int32_t x, x1, y, y1; |
| 97 | unsigned long z, z1; |
| 98 | int i; |
| 99 | |
| 100 | /* Setup initial vector */ |
| 101 | x = cordic_circular_gain; |
| 102 | y = 0; |
| 103 | z = phase; |
| 104 | |
| 105 | /* The phase has to be somewhere between 0..pi for this to work right */ |
| 106 | if (z < 0xffffffff / 4) { |
| 107 | /* z in first quadrant, z += pi/2 to correct */ |
| 108 | x = -x; |
| 109 | z += 0xffffffff / 4; |
| 110 | } else if (z < 3 * (0xffffffff / 4)) { |
| 111 | /* z in third quadrant, z -= pi/2 to correct */ |
| 112 | z -= 0xffffffff / 4; |
| 113 | } else { |
| 114 | /* z in fourth quadrant, z -= 3pi/2 to correct */ |
| 115 | x = -x; |
| 116 | z -= 3 * (0xffffffff / 4); |
| 117 | } |
| 118 | |
| 119 | /* Each iteration adds roughly 1-bit of extra precision */ |
| 120 | for (i = 0; i < 31; i++) { |
| 121 | x1 = x >> i; |
| 122 | y1 = y >> i; |
| 123 | z1 = atan_table[i]; |
| 124 | |
| 125 | /* Decided which direction to rotate vector. Pivot point is pi/2 */ |
| 126 | if (z >= 0xffffffff / 4) { |
| 127 | x -= y1; |
| 128 | y += x1; |
| 129 | z -= z1; |
| 130 | } else { |
| 131 | x += y1; |
| 132 | y -= x1; |
| 133 | z += z1; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (cos) |
| 138 | *cos = x; |
| 139 | |
| 140 | return y; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Fixed point square root via Newton-Raphson. |
| 145 | * @param a square root argument. |
| 146 | * @param fracbits specifies number of fractional bits in argument. |
| 147 | * @return Square root of argument in same fixed point format as input. |
| 148 | */ |
| 149 | static long fsqrt(long a, unsigned int fracbits) |
| 150 | { |
| 151 | long b = a/2 + (1 << fracbits); /* initial approximation */ |
| 152 | unsigned n; |
| 153 | const unsigned iterations = 8; /* bumped up from 4 as it wasn't |
| 154 | nearly enough for 28 fractional bits */ |
| 155 | |
| 156 | for (n = 0; n < iterations; ++n) |
| 157 | b = (b + (long)(((long long)(a) << fracbits)/b))/2; |
| 158 | |
| 159 | return b; |
| 160 | } |
| 161 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 162 | /* this is the codec entry point */ |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 163 | enum codec_status codec_main(void) |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 164 | { |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 165 | int channels; |
| 166 | int sampleswritten, i; |
| 167 | uint8_t *buf; |
| 168 | int32_t ch1_1, ch1_2, ch2_1, ch2_2; /* ADPCM history */ |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 169 | size_t n; |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 170 | int endofstream; /* end of stream flag */ |
| 171 | uint32_t avgbytespersec; |
| 172 | int looping; /* looping flag */ |
| 173 | int loop_count; /* number of loops done so far */ |
| 174 | int fade_count; /* countdown for fadeout */ |
| 175 | int fade_frames; /* length of fade in frames */ |
| 176 | off_t start_adr, end_adr; /* loop points */ |
| 177 | off_t chanstart, bufoff; |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 178 | /*long coef1=0x7298L,coef2=-0x3350L;*/ |
| 179 | long coef1, coef2; |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 180 | |
| 181 | /* Generic codec initialisation */ |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 182 | /* we only render 16 bits */ |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 183 | ci->configure(DSP_SET_SAMPLE_DEPTH, 16); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 184 | |
| 185 | next_track: |
| 186 | DEBUGF("ADX: next_track\n"); |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 187 | if (codec_init()) { |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 188 | return CODEC_ERROR; |
| 189 | } |
| 190 | DEBUGF("ADX: after init\n"); |
| 191 | |
| 192 | /* init history */ |
| 193 | ch1_1=ch1_2=ch2_1=ch2_2=0; |
| 194 | |
| 195 | /* wait for track info to load */ |
| 196 | while (!*ci->taginfo_ready && !ci->stop_codec) |
| 197 | ci->sleep(1); |
Michael Sevakis | 9b9e227 | 2007-02-26 17:15:04 +0000 | [diff] [blame] | 198 | |
| 199 | codec_set_replaygain(ci->id3); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 200 | |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 201 | /* Get header */ |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 202 | DEBUGF("ADX: request initial buffer\n"); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 203 | ci->seek_buffer(0); |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 204 | buf = ci->request_buffer(&n, 0x38); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 205 | if (!buf || n < 0x38) { |
| 206 | return CODEC_ERROR; |
| 207 | } |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 208 | bufoff = 0; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 209 | DEBUGF("ADX: read size = %lx\n",(unsigned long)n); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 210 | |
| 211 | /* Get file header for starting offset, channel count */ |
| 212 | |
| 213 | chanstart = ((buf[2] << 8) | buf[3]) + 4; |
| 214 | channels = buf[7]; |
| 215 | |
| 216 | /* useful for seeking and reporting current playback position */ |
| 217 | avgbytespersec = ci->id3->frequency * 18 * channels / 32; |
Jens Arnold | bd5c0ad | 2007-03-17 10:50:58 +0000 | [diff] [blame] | 218 | DEBUGF("avgbytespersec=%ld\n",(unsigned long)avgbytespersec); |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 219 | |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 220 | /* calculate filter coefficients */ |
| 221 | |
| 222 | /** |
| 223 | * A simple table of these coefficients would be nice, but |
| 224 | * some very odd frequencies are used and if I'm going to |
| 225 | * interpolate I might as well just go all the way and |
| 226 | * calclate them precisely. |
| 227 | * Speed is not an issue as this only needs to be done once per file. |
| 228 | */ |
| 229 | { |
| 230 | const int64_t big28 = 0x10000000LL; |
| 231 | const int64_t big32 = 0x100000000LL; |
| 232 | int64_t frequency = ci->id3->frequency; |
| 233 | int64_t phasemultiple = cutoff*big32/frequency; |
| 234 | |
| 235 | long z; |
| 236 | int64_t a; |
| 237 | const int64_t b = (M_SQRT2*big28)-big28; |
| 238 | int64_t c; |
| 239 | int64_t d; |
| 240 | |
| 241 | fsincos((unsigned long)phasemultiple,&z); |
| 242 | |
| 243 | a = (M_SQRT2*big28)-(z*big28/LONG_MAX); |
| 244 | |
| 245 | /** |
| 246 | * In the long passed to fsqrt there are only 4 nonfractional bits, |
| 247 | * which is sufficient here, but this is the only reason why I don't |
| 248 | * use 32 fractional bits everywhere. |
| 249 | */ |
| 250 | d = fsqrt((a+b)*(a-b)/big28,28); |
| 251 | c = (a-d)*big28/b; |
| 252 | |
| 253 | coef1 = (c*8192) >> 28; |
| 254 | coef2 = (c*c/big28*-4096) >> 28; |
Adam Gashlin | 460d6d2 | 2008-02-25 21:57:09 +0000 | [diff] [blame] | 255 | DEBUGF("ADX: samprate=%ld ",(long)frequency); |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 256 | DEBUGF("coef1 %04x ",(unsigned int)(coef1*4)); |
| 257 | DEBUGF("coef2 %04x\n",(unsigned int)(coef2*-4)); |
| 258 | } |
| 259 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 260 | /* Get loop data */ |
| 261 | |
| 262 | looping = 0; start_adr = 0; end_adr = 0; |
| 263 | if (!memcmp(buf+0x10,"\x01\xF4\x03\x00",4)) { |
| 264 | /* Soul Calibur 2 style (type 03) */ |
| 265 | DEBUGF("ADX: type 03 found\n"); |
| 266 | /* check if header is too small for loop data */ |
| 267 | if (chanstart-6 < 0x2c) looping=0; |
| 268 | else { |
| 269 | looping = (buf[0x18]) || |
| 270 | (buf[0x19]) || |
| 271 | (buf[0x1a]) || |
| 272 | (buf[0x1b]); |
| 273 | end_adr = (buf[0x28]<<24) | |
| 274 | (buf[0x29]<<16) | |
| 275 | (buf[0x2a]<<8) | |
| 276 | (buf[0x2b]); |
| 277 | |
| 278 | start_adr = ( |
| 279 | (buf[0x1c]<<24) | |
| 280 | (buf[0x1d]<<16) | |
| 281 | (buf[0x1e]<<8) | |
| 282 | (buf[0x1f]) |
| 283 | )/32*channels*18+chanstart; |
| 284 | } |
| 285 | } else if (!memcmp(buf+0x10,"\x01\xF4\x04\x00",4)) { |
| 286 | /* Standard (type 04) */ |
| 287 | DEBUGF("ADX: type 04 found\n"); |
| 288 | /* check if header is too small for loop data */ |
| 289 | if (chanstart-6 < 0x38) looping=0; |
| 290 | else { |
| 291 | looping = (buf[0x24]) || |
| 292 | (buf[0x25]) || |
| 293 | (buf[0x26]) || |
| 294 | (buf[0x27]); |
| 295 | end_adr = (buf[0x34]<<24) | |
| 296 | (buf[0x35]<<16) | |
| 297 | (buf[0x36]<<8) | |
| 298 | buf[0x37]; |
| 299 | start_adr = ( |
| 300 | (buf[0x28]<<24) | |
| 301 | (buf[0x29]<<16) | |
| 302 | (buf[0x2a]<<8) | |
| 303 | (buf[0x2b]) |
| 304 | )/32*channels*18+chanstart; |
| 305 | } |
| 306 | } else { |
| 307 | DEBUGF("ADX: error, couldn't determine ADX type\n"); |
| 308 | return CODEC_ERROR; |
| 309 | } |
| 310 | |
| 311 | if (looping) { |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 312 | DEBUGF("ADX: looped, start: %lx end: %lx\n",start_adr,end_adr); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 313 | } else { |
| 314 | DEBUGF("ADX: not looped\n"); |
| 315 | } |
| 316 | |
| 317 | /* advance to first frame */ |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 318 | DEBUGF("ADX: first frame at %lx\n",chanstart); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 319 | bufoff = chanstart; |
| 320 | |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 321 | /* get in position */ |
| 322 | ci->seek_buffer(bufoff); |
| 323 | |
| 324 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 325 | /* setup pcm buffer format */ |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 326 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 327 | if (channels == 2) { |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 328 | ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 329 | } else if (channels == 1) { |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 330 | ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 331 | } else { |
| 332 | DEBUGF("ADX CODEC_ERROR: more than 2 channels\n"); |
| 333 | return CODEC_ERROR; |
| 334 | } |
| 335 | |
| 336 | endofstream = 0; |
| 337 | loop_count = 0; |
| 338 | fade_count = -1; /* disable fade */ |
| 339 | fade_frames = 1; |
| 340 | |
| 341 | /* The main decoder loop */ |
| 342 | |
| 343 | while (!endofstream) { |
| 344 | ci->yield(); |
| 345 | if (ci->stop_codec || ci->new_track) { |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | /* do we need to loop? */ |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 350 | if (bufoff > end_adr-18*channels && looping) { |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 351 | DEBUGF("ADX: loop!\n"); |
| 352 | /* check for endless looping */ |
| 353 | if (ci->global_settings->repeat_mode==REPEAT_ONE) { |
| 354 | loop_count=0; |
| 355 | fade_count = -1; /* disable fade */ |
| 356 | } else { |
| 357 | /* otherwise start fade after LOOP_TIMES loops */ |
| 358 | loop_count++; |
| 359 | if (loop_count >= LOOP_TIMES && fade_count < 0) { |
| 360 | /* frames to fade over */ |
| 361 | fade_frames = FADE_LENGTH*ci->id3->frequency/32/1000; |
| 362 | /* volume relative to fade_frames */ |
| 363 | fade_count = fade_frames; |
| 364 | DEBUGF("ADX: fade_frames = %d\n",fade_frames); |
| 365 | } |
| 366 | } |
| 367 | bufoff = start_adr; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 368 | ci->seek_buffer(bufoff); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* do we need to seek? */ |
| 372 | if (ci->seek_time) { |
| 373 | uint32_t newpos; |
| 374 | |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 375 | DEBUGF("ADX: seek to %ldms\n",ci->seek_time); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 376 | |
| 377 | endofstream = 0; |
| 378 | loop_count = 0; |
| 379 | fade_count = -1; /* disable fade */ |
| 380 | fade_frames = 1; |
| 381 | |
| 382 | newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1)) |
| 383 | / (1000LL*18*channels))*(18*channels); |
| 384 | bufoff = chanstart + newpos; |
| 385 | while (bufoff > end_adr-18*channels) { |
| 386 | bufoff-=end_adr-start_adr; |
| 387 | loop_count++; |
| 388 | } |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 389 | ci->seek_buffer(bufoff); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 390 | ci->seek_complete(); |
| 391 | } |
| 392 | |
| 393 | if (bufoff>ci->filesize-channels*18) break; /* End of stream */ |
| 394 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 395 | sampleswritten=0; |
| 396 | |
| 397 | while ( |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 398 | /* Is there data left in the file? */ |
| 399 | (bufoff <= ci->filesize-(18*channels)) && |
| 400 | /* Is there space in the output buffer? */ |
| 401 | (sampleswritten <= WAV_CHUNK_SIZE-(32*channels)) && |
| 402 | /* Should we be looping? */ |
| 403 | ((!looping) || bufoff <= end_adr-18*channels)) |
| 404 | { |
| 405 | /* decode first/only channel */ |
| 406 | int32_t scale; |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 407 | int32_t ch1_0, d; |
| 408 | |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 409 | /* fetch a frame */ |
| 410 | buf = ci->request_buffer(&n, 18); |
| 411 | |
| 412 | if (!buf || n!=18) { |
| 413 | DEBUGF("ADX: couldn't get buffer at %lx\n", |
| 414 | bufoff); |
| 415 | return CODEC_ERROR; |
| 416 | } |
| 417 | |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 418 | scale = ((buf[0] << 8) | (buf[1])) +1; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 419 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 420 | for (i = 2; i < 18; i++) |
| 421 | { |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 422 | d = (buf[i] >> 4) & 15; |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 423 | if (d & 8) d-= 16; |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 424 | ch1_0 = d*scale + ((coef1*ch1_1 + coef2*ch1_2) >> 12); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 425 | if (ch1_0 > 32767) ch1_0 = 32767; |
| 426 | else if (ch1_0 < -32768) ch1_0 = -32768; |
| 427 | samples[sampleswritten] = ch1_0; |
| 428 | sampleswritten+=channels; |
| 429 | ch1_2 = ch1_1; ch1_1 = ch1_0; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 430 | |
| 431 | d = buf[i] & 15; |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 432 | if (d & 8) d -= 16; |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 433 | ch1_0 = d*scale + ((coef1*ch1_1 + coef2*ch1_2) >> 12); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 434 | if (ch1_0 > 32767) ch1_0 = 32767; |
| 435 | else if (ch1_0 < -32768) ch1_0 = -32768; |
| 436 | samples[sampleswritten] = ch1_0; |
| 437 | sampleswritten+=channels; |
| 438 | ch1_2 = ch1_1; ch1_1 = ch1_0; |
| 439 | } |
| 440 | bufoff+=18; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 441 | ci->advance_buffer(18); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 442 | |
| 443 | if (channels == 2) { |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 444 | /* decode second channel */ |
| 445 | int32_t scale; |
Adam Gashlin | 1c108df | 2007-11-07 19:01:58 +0000 | [diff] [blame] | 446 | int32_t ch2_0, d; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 447 | |
| 448 | buf = ci->request_buffer(&n, 18); |
| 449 | |
| 450 | if (!buf || n!=18) { |
| 451 | DEBUGF("ADX: couldn't get buffer at %lx\n", |
| 452 | bufoff); |
| 453 | return CODEC_ERROR; |
| 454 | } |
| 455 | |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 456 | scale = ((buf[0] << 8)|(buf[1]))+1; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 457 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 458 | sampleswritten-=63; |
| 459 | |
| 460 | for (i = 2; i < 18; i++) |
| 461 | { |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 462 | d = (buf[i] >> 4) & 15; |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 463 | if (d & 8) d-= 16; |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 464 | ch2_0 = d*scale + ((coef1*ch2_1 + coef2*ch2_2) >> 12); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 465 | if (ch2_0 > 32767) ch2_0 = 32767; |
| 466 | else if (ch2_0 < -32768) ch2_0 = -32768; |
| 467 | samples[sampleswritten] = ch2_0; |
| 468 | sampleswritten+=2; |
| 469 | ch2_2 = ch2_1; ch2_1 = ch2_0; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 470 | |
| 471 | d = buf[i] & 15; |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 472 | if (d & 8) d -= 16; |
Adam Gashlin | 2668547 | 2008-02-25 21:47:56 +0000 | [diff] [blame] | 473 | ch2_0 = d*scale + ((coef1*ch2_1 + coef2*ch2_2) >> 12); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 474 | if (ch2_0 > 32767) ch2_0 = 32767; |
| 475 | else if (ch2_0 < -32768) ch2_0 = -32768; |
| 476 | samples[sampleswritten] = ch2_0; |
| 477 | sampleswritten+=2; |
| 478 | ch2_2 = ch2_1; ch2_1 = ch2_0; |
| 479 | } |
| 480 | bufoff+=18; |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 481 | ci->advance_buffer(18); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 482 | sampleswritten--; /* go back to first channel's next sample */ |
| 483 | } |
Adam Gashlin | b9dba4f | 2007-11-09 02:42:39 +0000 | [diff] [blame] | 484 | |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 485 | if (fade_count>0) { |
| 486 | fade_count--; |
| 487 | for (i=0;i<(channels==1?32:64);i++) samples[sampleswritten-i-1]= |
| 488 | ((int32_t)samples[sampleswritten-i-1])*fade_count/fade_frames; |
| 489 | if (fade_count==0) {endofstream=1; break;} |
| 490 | } |
| 491 | } |
| 492 | |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 493 | if (channels == 2) |
| 494 | sampleswritten >>= 1; /* make samples/channel */ |
| 495 | |
| 496 | ci->pcmbuf_insert(samples, NULL, sampleswritten); |
Rani Hod | d542930 | 2006-09-25 16:35:03 +0000 | [diff] [blame] | 497 | |
| 498 | ci->set_elapsed( |
| 499 | ((end_adr-start_adr)*loop_count + bufoff-chanstart)* |
| 500 | 1000LL/avgbytespersec); |
| 501 | } |
| 502 | |
| 503 | if (ci->request_next_track()) |
| 504 | goto next_track; |
| 505 | |
| 506 | return CODEC_OK; |
| 507 | } |