• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/audio/src/util/alutCodec.cpp

Go to the documentation of this file.
00001 #include "alutInternal.h"
00002 
00003 
00004 ALvoid* _alutCodecLinear(ALvoid *data, size_t length, ALint numChannels,
00005                 ALint bitsPerSample, ALfloat sampleFrequency)
00006 {
00007         return _alutBufferDataConstruct (data, length, numChannels, bitsPerSample,
00008                         sampleFrequency);
00009 }
00010 
00011 ALvoid* _alutCodecPCM8s(ALvoid *data, size_t length, ALint numChannels,
00012                 ALint bitsPerSample, ALfloat sampleFrequency)
00013 {
00014         int8_t *d = (int8_t*)data;
00015         size_t i;
00016         for (i = 0; i < length; i++)
00017                 d[i] += (int8_t) 128;
00018 
00019         return _alutBufferDataConstruct(data, length, numChannels, bitsPerSample,
00020                         sampleFrequency);
00021 }
00022 
00023 ALvoid* _alutCodecPCM16(ALvoid *data, size_t length, ALint numChannels,
00024                 ALint bitsPerSample, ALfloat sampleFrequency)
00025 {
00026         int16_t *d = (int16_t *) data;
00027         size_t i, l = length / 2;
00028         for (i = 0; i < l; i++)
00029         {
00030                 int16_t x = d[i];
00031                 d[i] = ((x << 8) & 0xFF00) | ((x >> 8) & 0x00FF);
00032         }
00033         return _alutBufferDataConstruct(data, length, numChannels, bitsPerSample,
00034                 sampleFrequency);
00035 }
00036 
00037 static int16_t mulaw2linear(uint8_t mulawbyte)
00038 {
00039         static const int16_t exp_lut[8] = {
00040                 0, 132, 396, 924, 1980, 4092, 8316, 16764
00041         };
00042         int16_t sign, exponent, mantissa, sample;
00043         mulawbyte = ~mulawbyte;
00044         sign = (mulawbyte & 0x80);
00045         exponent = (mulawbyte >> 4) & 0x07;
00046         mantissa = mulawbyte & 0x0F;
00047         sample = exp_lut[exponent] + (mantissa << (exponent + 3));
00048         if (sign != 0)
00049         {
00050                 sample = -sample;
00051         }
00052         return sample;
00053 }
00054 
00055 ALvoid* _alutCodecULaw(ALvoid *data, size_t length, ALint numChannels,
00056                 ALint bitsPerSample, ALfloat sampleFrequency)
00057 {
00058         uint8_t *d = (uint8_t*)data;
00059         int16_t *buf = (int16_t*)memalloc(length * 2);
00060         size_t i;
00061         if(buf == NULL)
00062                 return NULL;
00063 
00064         for (i = 0; i < length; i++)
00065                 buf[i] = mulaw2linear (d[i]);
00066 
00067         memfree(data);
00068         return _alutBufferDataConstruct(buf, length * 2, numChannels,
00069                         bitsPerSample, sampleFrequency);
00070 }
00071 
00072 /*
00073  * From: http://www.multimedia.cx/simpleaudio.html#tth_sEc6.1
00074  */
00075 #define SIGN_BIT (0x80)         /* Sign bit for a A-law byte. */
00076 #define QUANT_MASK (0xf)        /* Quantization field mask. */
00077 #define SEG_SHIFT (4)           /* Left shift for segment number. */
00078 #define SEG_MASK (0x70)         /* Segment field mask. */
00079 
00080 static int16_t alaw2linear(uint8_t a_val)
00081 {
00082         int16_t t, seg;
00083         a_val ^= 0x55;
00084         t = (a_val & QUANT_MASK) << 4;
00085         seg = ((int16_t) a_val & SEG_MASK) >> SEG_SHIFT;
00086         switch (seg)
00087         {
00088         case 0:
00089                 t += 8;
00090                 break;
00091         case 1:
00092                 t += 0x108;
00093                 break;
00094         default:
00095                 t += 0x108;
00096                 t <<= seg - 1;
00097                 break;
00098         }
00099         return (a_val & SIGN_BIT) ? t : -t;
00100 }
00101 
00102 ALvoid* _alutCodecALaw (ALvoid *data, size_t length, ALint numChannels,
00103                 ALint bitsPerSample, ALfloat sampleFrequency)
00104 {
00105         uint8_t *d = (uint8_t*) data;
00106         int16_t *buf = (int16_t*) memalloc(length * 2);
00107         size_t i;
00108         if(buf == NULL)
00109                 return NULL;
00110 
00111         for(i = 0; i < length; i++)
00112                 buf[i] = alaw2linear(d[i]);
00113 
00114         memfree(data);
00115         return _alutBufferDataConstruct(buf, length * 2, numChannels,
00116                         bitsPerSample, sampleFrequency);
00117 }

Generated on Fri Jun 18 2010 17:48:39 for Cannonball by  doxygen 1.7.0