00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <glib.h>
00021 #include <stdint.h>
00022
00023 #include "audio.h"
00024
00025 #define FROM_INT_LOOP(NAME, TYPE, SWAP, OFFSET, RANGE) \
00026 static void NAME (const TYPE * in, float * out, int samples) \
00027 { \
00028 const TYPE * end = in + samples; \
00029 while (in < end) \
00030 * out ++ = (TYPE) (SWAP (* in ++) - OFFSET) / (double) RANGE; \
00031 }
00032
00033 #define TO_INT_LOOP(NAME, TYPE, SWAP, OFFSET, RANGE) \
00034 static void NAME (const float * in, TYPE * out, int samples) \
00035 { \
00036 const float * end = in + samples; \
00037 while (in < end) \
00038 { \
00039 double f = * in ++; \
00040 * out ++ = SWAP (OFFSET + (TYPE) (CLAMP (f, -1, 1) * (double) RANGE)); \
00041 } \
00042 }
00043
00044 static inline int8_t noop8 (int8_t i) {return i;}
00045 static inline int16_t noop16 (int16_t i) {return i;}
00046 static inline int32_t noop32 (int32_t i) {return i;}
00047
00048 FROM_INT_LOOP (from_s8, int8_t, noop8, 0x00, 0x7f)
00049 FROM_INT_LOOP (from_u8, int8_t, noop8, 0x80, 0x7f)
00050 FROM_INT_LOOP (from_s16, int16_t, noop16, 0x0000, 0x7fff)
00051 FROM_INT_LOOP (from_u16, int16_t, noop16, 0x8000, 0x7fff)
00052 FROM_INT_LOOP (from_s24, int32_t, noop32, 0x000000, 0x7fffff)
00053 FROM_INT_LOOP (from_u24, int32_t, noop32, 0x800000, 0x7fffff)
00054 FROM_INT_LOOP (from_s32, int32_t, noop32, 0x00000000, 0x7fffffff)
00055 FROM_INT_LOOP (from_u32, int32_t, noop32, 0x80000000, 0x7fffffff)
00056
00057 TO_INT_LOOP (to_s8, int8_t, noop8, 0x00, 0x7f)
00058 TO_INT_LOOP (to_u8, int8_t, noop8, 0x80, 0x7f)
00059 TO_INT_LOOP (to_s16, int16_t, noop16, 0x0000, 0x7fff)
00060 TO_INT_LOOP (to_u16, int16_t, noop16, 0x8000, 0x7fff)
00061 TO_INT_LOOP (to_s24, int32_t, noop32, 0x000000, 0x7fffff)
00062 TO_INT_LOOP (to_u24, int32_t, noop32, 0x800000, 0x7fffff)
00063 TO_INT_LOOP (to_s32, int32_t, noop32, 0x00000000, 0x7fffffff)
00064 TO_INT_LOOP (to_u32, int32_t, noop32, 0x80000000, 0x7fffffff)
00065
00066 static inline int16_t swap16 (int16_t i) {return GUINT16_SWAP_LE_BE (i);}
00067 static inline int32_t swap32 (int32_t i) {return GUINT32_SWAP_LE_BE (i);}
00068
00069 FROM_INT_LOOP (from_s16_swap, int16_t, swap16, 0x0000, 0x7fff)
00070 FROM_INT_LOOP (from_u16_swap, int16_t, swap16, 0x8000, 0x7fff)
00071 FROM_INT_LOOP (from_s24_swap, int32_t, swap32, 0x000000, 0x7fffff)
00072 FROM_INT_LOOP (from_u24_swap, int32_t, swap32, 0x800000, 0x7fffff)
00073 FROM_INT_LOOP (from_s32_swap, int32_t, swap32, 0x00000000, 0x7fffffff)
00074 FROM_INT_LOOP (from_u32_swap, int32_t, swap32, 0x80000000, 0x7fffffff)
00075
00076 TO_INT_LOOP (to_s16_swap, int16_t, swap16, 0x0000, 0x7fff)
00077 TO_INT_LOOP (to_u16_swap, int16_t, swap16, 0x8000, 0x7fff)
00078 TO_INT_LOOP (to_s24_swap, int32_t, swap32, 0x000000, 0x7fffff)
00079 TO_INT_LOOP (to_u24_swap, int32_t, swap32, 0x800000, 0x7fffff)
00080 TO_INT_LOOP (to_s32_swap, int32_t, swap32, 0x00000000, 0x7fffffff)
00081 TO_INT_LOOP (to_u32_swap, int32_t, swap32, 0x80000000, 0x7fffffff)
00082
00083 typedef void (* FromFunc) (const void * in, float * out, int samples);
00084 typedef void (* ToFunc) (const float * in, void * out, int samples);
00085
00086 struct
00087 {
00088 int format;
00089 FromFunc from;
00090 ToFunc to;
00091 }
00092 convert_table [] =
00093 {
00094 {FMT_S8, (FromFunc) from_s8, (ToFunc) to_s8},
00095 {FMT_U8, (FromFunc) from_u8, (ToFunc) to_u8},
00096
00097 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
00098 {FMT_S16_LE, (FromFunc) from_s16, (ToFunc) to_s16},
00099 {FMT_U16_LE, (FromFunc) from_u16, (ToFunc) to_u16},
00100 {FMT_S24_LE, (FromFunc) from_s24, (ToFunc) to_s24},
00101 {FMT_U24_LE, (FromFunc) from_u24, (ToFunc) to_u24},
00102 {FMT_S32_LE, (FromFunc) from_s32, (ToFunc) to_s32},
00103 {FMT_U32_LE, (FromFunc) from_u32, (ToFunc) to_u32},
00104
00105 {FMT_S16_BE, (FromFunc) from_s16_swap, (ToFunc) to_s16_swap},
00106 {FMT_U16_BE, (FromFunc) from_u16_swap, (ToFunc) to_u16_swap},
00107 {FMT_S24_BE, (FromFunc) from_s24_swap, (ToFunc) to_s24_swap},
00108 {FMT_U24_BE, (FromFunc) from_u24_swap, (ToFunc) to_u24_swap},
00109 {FMT_S32_BE, (FromFunc) from_s32_swap, (ToFunc) to_s32_swap},
00110 {FMT_U32_BE, (FromFunc) from_u32_swap, (ToFunc) to_u32_swap},
00111 #else
00112 {FMT_S16_BE, (FromFunc) from_s16, (ToFunc) to_s16},
00113 {FMT_U16_BE, (FromFunc) from_u16, (ToFunc) to_u16},
00114 {FMT_S24_BE, (FromFunc) from_s24, (ToFunc) to_s24},
00115 {FMT_U24_BE, (FromFunc) from_u24, (ToFunc) to_u24},
00116 {FMT_S32_BE, (FromFunc) from_s32, (ToFunc) to_s32},
00117 {FMT_U32_BE, (FromFunc) from_u32, (ToFunc) to_u32},
00118
00119 {FMT_S16_LE, (FromFunc) from_s16_swap, (ToFunc) to_s16_swap},
00120 {FMT_U16_LE, (FromFunc) from_u16_swap, (ToFunc) to_u16_swap},
00121 {FMT_S24_LE, (FromFunc) from_s24_swap, (ToFunc) to_s24_swap},
00122 {FMT_U24_LE, (FromFunc) from_u24_swap, (ToFunc) to_u24_swap},
00123 {FMT_S32_LE, (FromFunc) from_s32_swap, (ToFunc) to_s32_swap},
00124 {FMT_U32_LE, (FromFunc) from_u32_swap, (ToFunc) to_u32_swap},
00125 #endif
00126 };
00127
00128 void audio_from_int (const void * in, int format, float * out, int samples)
00129 {
00130 int entry;
00131
00132 for (entry = 0; entry < G_N_ELEMENTS (convert_table); entry ++)
00133 {
00134 if (convert_table[entry].format == format)
00135 {
00136 convert_table[entry].from (in, out, samples);
00137 return;
00138 }
00139 }
00140 }
00141
00142 void audio_to_int (const float * in, void * out, int format, int samples)
00143 {
00144 int entry;
00145
00146 for (entry = 0; entry < G_N_ELEMENTS (convert_table); entry ++)
00147 {
00148 if (convert_table[entry].format == format)
00149 {
00150 convert_table[entry].to (in, out, samples);
00151 return;
00152 }
00153 }
00154 }
00155
00156 void audio_amplify (float * data, int channels, int frames, float * factors)
00157 {
00158 float * end = data + channels * frames;
00159 int channel;
00160
00161 while (data < end)
00162 {
00163 for (channel = 0; channel < channels; channel ++)
00164 {
00165 * data = * data * factors[channel];
00166 data ++;
00167 }
00168 }
00169 }