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