#include <glib.h>
-#define ZEROTRAP /* turn on the trap as per the MIL-STD */
+#include "mulaw-conversion.h"
+
+#undef ZEROTRAP /* turn on the trap as per the MIL-STD */
#define BIAS 0x84 /* define the add-in bias for 16 bit samples */
#define CLIP 32635
void
mulaw_encode (gint16 * in, guint8 * out, gint numsamples)
{
- static gint16 exp_lut[256] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
+ static const gint16 exp_lut[256] = {
+ 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};
- gint16 sign, exponent, mantissa, i;
+ gint16 sign, exponent, mantissa;
gint16 sample;
guint8 ulawbyte;
+ gint i;
for (i = 0; i < numsamples; i++) {
sample = in[i];
/** get the sample into sign-magnitude **/
sign = (sample >> 8) & 0x80; /* set aside the sign */
- if (sign != 0)
+ if (sign != 0) {
sample = -sample; /* get magnitude */
- if (sample > CLIP)
+ }
+ /* sample can be zero because we can overflow in the inversion,
+ * checking against the unsigned version solves this */
+ if (((guint16) sample) > CLIP)
sample = CLIP; /* clip the magnitude */
+
/** convert from 16 bit linear to ulaw **/
sample = sample + BIAS;
exponent = exp_lut[(sample >> 7) & 0xFF];
void
mulaw_decode (guint8 * in, gint16 * out, gint numsamples)
{
- static gint16 exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
+ static const gint16 exp_lut[8] =
+ { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
gint16 sign, exponent, mantissa;
guint8 ulawbyte;
- gint16 linear, i;
+ gint16 linear;
+ gint i;
for (i = 0; i < numsamples; i++) {
ulawbyte = in[i];