Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * Copyright 2009-2011 Jörg Hermann Müller 00005 * 00006 * This file is part of AudaSpace. 00007 * 00008 * Audaspace is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * AudaSpace is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with Audaspace; if not, write to the Free Software Foundation, 00020 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00021 * 00022 * ***** END GPL LICENSE BLOCK ***** 00023 */ 00024 00030 #include "AUD_ConverterFunctions.h" 00031 #include "AUD_Buffer.h" 00032 00033 #define AUD_U8_0 0x80 00034 #define AUD_S16_MAX 0x7FFF 00035 #define AUD_S16_MIN 0x8000 00036 #define AUD_S16_FLT 32767.0f 00037 #define AUD_S32_MAX 0x7FFFFFFF 00038 #define AUD_S32_MIN 0x80000000 00039 #define AUD_S32_FLT 2147483647.0f 00040 #define AUD_FLT_MAX 1.0f 00041 #define AUD_FLT_MIN -1.0f 00042 00043 void AUD_convert_u8_s16(data_t* target, data_t* source, int length) 00044 { 00045 int16_t* t = (int16_t*) target; 00046 for(int i = length - 1; i >= 0; i--) 00047 t[i] = (((int16_t)source[i]) - AUD_U8_0) << 8; 00048 } 00049 00050 void AUD_convert_u8_s24_be(data_t* target, data_t* source, int length) 00051 { 00052 for(int i = length - 1; i >= 0; i--) 00053 { 00054 target[i*3] = source[i] - AUD_U8_0; 00055 target[i*3+1] = 0; 00056 target[i*3+2] = 0; 00057 } 00058 } 00059 00060 void AUD_convert_u8_s24_le(data_t* target, data_t* source, int length) 00061 { 00062 for(int i = length - 1; i >= 0; i--) 00063 { 00064 target[i*3+2] = source[i] - AUD_U8_0; 00065 target[i*3+1] = 0; 00066 target[i*3] = 0; 00067 } 00068 } 00069 00070 void AUD_convert_u8_s32(data_t* target, data_t* source, int length) 00071 { 00072 int32_t* t = (int32_t*) target; 00073 for(int i = length - 1; i >= 0; i--) 00074 t[i] = (((int32_t)source[i]) - AUD_U8_0) << 24; 00075 } 00076 00077 void AUD_convert_u8_float(data_t* target, data_t* source, int length) 00078 { 00079 float* t = (float*) target; 00080 for(int i = length - 1; i >= 0; i--) 00081 t[i] = (((int32_t)source[i]) - AUD_U8_0) / ((float)AUD_U8_0); 00082 } 00083 00084 void AUD_convert_u8_double(data_t* target, data_t* source, int length) 00085 { 00086 double* t = (double*) target; 00087 for(int i = length - 1; i >= 0; i--) 00088 t[i] = (((int32_t)source[i]) - AUD_U8_0) / ((double)AUD_U8_0); 00089 } 00090 00091 void AUD_convert_s16_u8(data_t* target, data_t* source, int length) 00092 { 00093 int16_t* s = (int16_t*) source; 00094 for(int i = 0; i < length; i++) 00095 target[i] = (unsigned char)((s[i] >> 8) + AUD_U8_0); 00096 } 00097 00098 void AUD_convert_s16_s24_be(data_t* target, data_t* source, int length) 00099 { 00100 int16_t* s = (int16_t*) source; 00101 int16_t t; 00102 for(int i = length - 1; i >= 0; i--) 00103 { 00104 t = s[i]; 00105 target[i*3] = t >> 8 & 0xFF; 00106 target[i*3+1] = t & 0xFF; 00107 target[i*3+2] = 0; 00108 } 00109 } 00110 00111 void AUD_convert_s16_s24_le(data_t* target, data_t* source, int length) 00112 { 00113 int16_t* s = (int16_t*) source; 00114 int16_t t; 00115 for(int i = length - 1; i >= 0; i--) 00116 { 00117 t = s[i]; 00118 target[i*3+2] = t >> 8 & 0xFF; 00119 target[i*3+1] = t & 0xFF; 00120 target[i*3] = 0; 00121 } 00122 } 00123 00124 void AUD_convert_s16_s32(data_t* target, data_t* source, int length) 00125 { 00126 int16_t* s = (int16_t*) source; 00127 int32_t* t = (int32_t*) target; 00128 for(int i = length - 1; i >= 0; i--) 00129 t[i] = ((int32_t)s[i]) << 16; 00130 } 00131 00132 void AUD_convert_s16_float(data_t* target, data_t* source, int length) 00133 { 00134 int16_t* s = (int16_t*) source; 00135 float* t = (float*) target; 00136 for(int i = length - 1; i >= 0; i--) 00137 t[i] = s[i] / AUD_S16_FLT; 00138 } 00139 00140 void AUD_convert_s16_double(data_t* target, data_t* source, int length) 00141 { 00142 int16_t* s = (int16_t*) source; 00143 double* t = (double*) target; 00144 for(int i = length - 1; i >= 0; i--) 00145 t[i] = s[i] / AUD_S16_FLT; 00146 } 00147 00148 void AUD_convert_s24_u8_be(data_t* target, data_t* source, int length) 00149 { 00150 for(int i = 0; i < length; i++) 00151 target[i] = source[i*3] ^ AUD_U8_0; 00152 } 00153 00154 void AUD_convert_s24_u8_le(data_t* target, data_t* source, int length) 00155 { 00156 for(int i = 0; i < length; i++) 00157 target[i] = source[i*3+2] ^ AUD_U8_0; 00158 } 00159 00160 void AUD_convert_s24_s16_be(data_t* target, data_t* source, int length) 00161 { 00162 int16_t* t = (int16_t*) target; 00163 for(int i = 0; i < length; i++) 00164 t[i] = source[i*3] << 8 | source[i*3+1]; 00165 } 00166 00167 void AUD_convert_s24_s16_le(data_t* target, data_t* source, int length) 00168 { 00169 int16_t* t = (int16_t*) target; 00170 for(int i = 0; i < length; i++) 00171 t[i] = source[i*3+2] << 8 | source[i*3+1]; 00172 } 00173 00174 void AUD_convert_s24_s24(data_t* target, data_t* source, int length) 00175 { 00176 memcpy(target, source, length * 3); 00177 } 00178 00179 void AUD_convert_s24_s32_be(data_t* target, data_t* source, int length) 00180 { 00181 int32_t* t = (int32_t*) target; 00182 for(int i = length - 1; i >= 0; i--) 00183 t[i] = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8; 00184 } 00185 00186 void AUD_convert_s24_s32_le(data_t* target, data_t* source, int length) 00187 { 00188 int32_t* t = (int32_t*) target; 00189 for(int i = length - 1; i >= 0; i--) 00190 t[i] = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8; 00191 } 00192 00193 void AUD_convert_s24_float_be(data_t* target, data_t* source, int length) 00194 { 00195 float* t = (float*) target; 00196 int32_t s; 00197 for(int i = length - 1; i >= 0; i--) 00198 { 00199 s = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8; 00200 t[i] = s / AUD_S32_FLT; 00201 } 00202 } 00203 00204 void AUD_convert_s24_float_le(data_t* target, data_t* source, int length) 00205 { 00206 float* t = (float*) target; 00207 int32_t s; 00208 for(int i = length - 1; i >= 0; i--) 00209 { 00210 s = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8; 00211 t[i] = s / AUD_S32_FLT; 00212 } 00213 } 00214 00215 void AUD_convert_s24_double_be(data_t* target, data_t* source, int length) 00216 { 00217 double* t = (double*) target; 00218 int32_t s; 00219 for(int i = length - 1; i >= 0; i--) 00220 { 00221 s = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8; 00222 t[i] = s / AUD_S32_FLT; 00223 } 00224 } 00225 00226 void AUD_convert_s24_double_le(data_t* target, data_t* source, int length) 00227 { 00228 double* t = (double*) target; 00229 int32_t s; 00230 for(int i = length - 1; i >= 0; i--) 00231 { 00232 s = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8; 00233 t[i] = s / AUD_S32_FLT; 00234 } 00235 } 00236 00237 void AUD_convert_s32_u8(data_t* target, data_t* source, int length) 00238 { 00239 int16_t* s = (int16_t*) source; 00240 for(int i = 0; i < length; i++) 00241 target[i] = (unsigned char)((s[i] >> 24) + AUD_U8_0); 00242 } 00243 00244 void AUD_convert_s32_s16(data_t* target, data_t* source, int length) 00245 { 00246 int16_t* t = (int16_t*) target; 00247 int32_t* s = (int32_t*) source; 00248 for(int i = 0; i < length; i++) 00249 t[i] = s[i] >> 16; 00250 } 00251 00252 void AUD_convert_s32_s24_be(data_t* target, data_t* source, int length) 00253 { 00254 int32_t* s = (int32_t*) source; 00255 int32_t t; 00256 for(int i = 0; i < length; i++) 00257 { 00258 t = s[i]; 00259 target[i*3] = t >> 24 & 0xFF; 00260 target[i*3+1] = t >> 16 & 0xFF; 00261 target[i*3+2] = t >> 8 & 0xFF; 00262 } 00263 } 00264 00265 void AUD_convert_s32_s24_le(data_t* target, data_t* source, int length) 00266 { 00267 int32_t* s = (int32_t*) source; 00268 int32_t t; 00269 for(int i = 0; i < length; i++) 00270 { 00271 t = s[i]; 00272 target[i*3+2] = t >> 24 & 0xFF; 00273 target[i*3+1] = t >> 16 & 0xFF; 00274 target[i*3] = t >> 8 & 0xFF; 00275 } 00276 } 00277 00278 void AUD_convert_s32_float(data_t* target, data_t* source, int length) 00279 { 00280 int32_t* s = (int32_t*) source; 00281 float* t = (float*) target; 00282 for(int i = 0; i < length; i++) 00283 t[i] = s[i] / AUD_S32_FLT; 00284 } 00285 00286 void AUD_convert_s32_double(data_t* target, data_t* source, int length) 00287 { 00288 int32_t* s = (int32_t*) source; 00289 double* t = (double*) target; 00290 for(int i = length - 1; i >= 0; i--) 00291 t[i] = s[i] / AUD_S32_FLT; 00292 } 00293 00294 void AUD_convert_float_u8(data_t* target, data_t* source, int length) 00295 { 00296 float* s = (float*) source; 00297 float t; 00298 for(int i = 0; i < length; i++) 00299 { 00300 t = s[i] + AUD_FLT_MAX; 00301 if(t <= 0.0f) 00302 target[i] = 0; 00303 else if(t >= 2.0f) 00304 target[i] = 255; 00305 else 00306 target[i] = (unsigned char)(t*127); 00307 } 00308 } 00309 00310 void AUD_convert_float_s16(data_t* target, data_t* source, int length) 00311 { 00312 int16_t* t = (int16_t*) target; 00313 float* s = (float*) source; 00314 for(int i = 0; i < length; i++) 00315 { 00316 if(s[i] <= AUD_FLT_MIN) 00317 t[i] = AUD_S16_MIN; 00318 else if(s[i] >= AUD_FLT_MAX) 00319 t[i] = AUD_S16_MAX; 00320 else 00321 t[i] = (int16_t)(s[i] * AUD_S16_MAX); 00322 } 00323 } 00324 00325 void AUD_convert_float_s24_be(data_t* target, data_t* source, int length) 00326 { 00327 int32_t t; 00328 float* s = (float*) source; 00329 for(int i = 0; i < length; i++) 00330 { 00331 if(s[i] <= AUD_FLT_MIN) 00332 t = AUD_S32_MIN; 00333 else if(s[i] >= AUD_FLT_MAX) 00334 t = AUD_S32_MAX; 00335 else 00336 t = (int32_t)(s[i]*AUD_S32_MAX); 00337 target[i*3] = t >> 24 & 0xFF; 00338 target[i*3+1] = t >> 16 & 0xFF; 00339 target[i*3+2] = t >> 8 & 0xFF; 00340 } 00341 } 00342 00343 void AUD_convert_float_s24_le(data_t* target, data_t* source, int length) 00344 { 00345 int32_t t; 00346 float* s = (float*) source; 00347 for(int i = 0; i < length; i++) 00348 { 00349 if(s[i] <= AUD_FLT_MIN) 00350 t = AUD_S32_MIN; 00351 else if(s[i] >= AUD_FLT_MAX) 00352 t = AUD_S32_MAX; 00353 else 00354 t = (int32_t)(s[i]*AUD_S32_MAX); 00355 target[i*3+2] = t >> 24 & 0xFF; 00356 target[i*3+1] = t >> 16 & 0xFF; 00357 target[i*3] = t >> 8 & 0xFF; 00358 } 00359 } 00360 00361 void AUD_convert_float_s32(data_t* target, data_t* source, int length) 00362 { 00363 int32_t* t = (int32_t*) target; 00364 float* s = (float*) source; 00365 for(int i = 0; i < length; i++) 00366 { 00367 if(s[i] <= AUD_FLT_MIN) 00368 t[i] = AUD_S32_MIN; 00369 else if(s[i] >= AUD_FLT_MAX) 00370 t[i] = AUD_S32_MAX; 00371 else 00372 t[i] = (int32_t)(s[i]*AUD_S32_MAX); 00373 } 00374 } 00375 00376 void AUD_convert_float_double(data_t* target, data_t* source, int length) 00377 { 00378 float* s = (float*) source; 00379 double* t = (double*) target; 00380 for(int i = length - 1; i >= 0; i--) 00381 t[i] = s[i]; 00382 } 00383 00384 void AUD_convert_double_u8(data_t* target, data_t* source, int length) 00385 { 00386 double* s = (double*) source; 00387 double t; 00388 for(int i = 0; i < length; i++) 00389 { 00390 t = s[i] + AUD_FLT_MAX; 00391 if(t <= 0.0) 00392 target[i] = 0; 00393 else if(t >= 2.0) 00394 target[i] = 255; 00395 else 00396 target[i] = (unsigned char)(t*127); 00397 } 00398 } 00399 00400 void AUD_convert_double_s16(data_t* target, data_t* source, int length) 00401 { 00402 int16_t* t = (int16_t*) target; 00403 double* s = (double*) source; 00404 for(int i = 0; i < length; i++) 00405 { 00406 if(s[i] <= AUD_FLT_MIN) 00407 t[i] = AUD_S16_MIN; 00408 else if(s[i] >= AUD_FLT_MAX) 00409 t[i] = AUD_S16_MAX; 00410 else 00411 t[i] = (int16_t)(s[i]*AUD_S16_MAX); 00412 } 00413 } 00414 00415 void AUD_convert_double_s24_be(data_t* target, data_t* source, int length) 00416 { 00417 int32_t t; 00418 double* s = (double*) source; 00419 for(int i = 0; i < length; i++) 00420 { 00421 if(s[i] <= AUD_FLT_MIN) 00422 t = AUD_S32_MIN; 00423 else if(s[i] >= AUD_FLT_MAX) 00424 t = AUD_S32_MAX; 00425 else 00426 t = (int32_t)(s[i]*AUD_S32_MAX); 00427 target[i*3] = t >> 24 & 0xFF; 00428 target[i*3+1] = t >> 16 & 0xFF; 00429 target[i*3+2] = t >> 8 & 0xFF; 00430 } 00431 } 00432 00433 void AUD_convert_double_s24_le(data_t* target, data_t* source, int length) 00434 { 00435 int32_t t; 00436 double* s = (double*) source; 00437 for(int i = 0; i < length; i++) 00438 { 00439 if(s[i] <= AUD_FLT_MIN) 00440 t = AUD_S32_MIN; 00441 else if(s[i] >= AUD_FLT_MAX) 00442 t = AUD_S32_MAX; 00443 else 00444 t = (int32_t)(s[i]*AUD_S32_MAX); 00445 target[i*3+2] = t >> 24 & 0xFF; 00446 target[i*3+1] = t >> 16 & 0xFF; 00447 target[i*3] = t >> 8 & 0xFF; 00448 } 00449 } 00450 00451 void AUD_convert_double_s32(data_t* target, data_t* source, int length) 00452 { 00453 int32_t* t = (int32_t*) target; 00454 double* s = (double*) source; 00455 for(int i = 0; i < length; i++) 00456 { 00457 if(s[i] <= AUD_FLT_MIN) 00458 t[i] = AUD_S32_MIN; 00459 else if(s[i] >= AUD_FLT_MAX) 00460 t[i] = AUD_S32_MAX; 00461 else 00462 t[i] = (int32_t)(s[i]*AUD_S32_MAX); 00463 } 00464 } 00465 00466 void AUD_convert_double_float(data_t* target, data_t* source, int length) 00467 { 00468 double* s = (double*) source; 00469 float* t = (float*) target; 00470 for(int i = 0; i < length; i++) 00471 t[i] = s[i]; 00472 }