Import the current wip animation datatype and subclasses. further development will...
[AROS.git] / workbench / classes / datatypes / wav / wave_alaw.c
blob6b0b6cd970bcc6e4ef999f58153662f7527ed621
1 /*
2 * wave.datatype
3 * (c) Fredrik Wikstrom
4 */
6 #include "wave_alaw.h"
8 static WORD decode_alaw (UBYTE a_val);
9 static WORD decode_mulaw(UBYTE u_val);
11 DEC_SETUPPROTO(SetupALaw) {
12 struct WaveFormatEx * fmt;
13 fmt = data->fmt;
14 if (fmt->extraSize != 0)
15 return NOTOK;
16 if (fmt->bitsPerSample != 8)
17 return ERROR_NOT_IMPLEMENTED;
18 data->blockFrames = 1;
19 return OK;
22 DECODERPROTO(DecodeALaw) {
23 LONG frame,chan;
24 for (frame=0;frame<numFrames;frame++) {
25 for (chan=0;chan<fmt->numChannels;chan++) {
26 *Dst[chan]++ = decode_alaw(*Src++) >> 8;
29 return numFrames;
32 DECODERPROTO(DecodeMuLaw) {
33 LONG frame,chan;
34 for (frame=0;frame<numFrames;frame++) {
35 for (chan=0;chan<fmt->numChannels;chan++) {
36 *Dst[chan]++ = decode_mulaw(*Src++) >> 8;
39 return numFrames;
42 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
43 #define QUANT_MASK (0xf) /* Quantization field mask. */
44 #define SEG_SHIFT (4) /* Left shift for segment number. */
45 #define SEG_MASK (0x70) /* Segment field mask. */
46 #define BIAS (0x84) /* Bias for linear code. */
48 static WORD decode_alaw (UBYTE a_val) {
49 WORD t;
50 WORD seg;
52 a_val ^= 0x55;
54 t = (a_val & QUANT_MASK) << 4;
55 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
56 switch (seg) {
57 case 0:
58 t += 8;
59 break;
60 case 1:
61 t += 0x108;
62 break;
63 default:
64 t += 0x108;
65 t <<= seg - 1;
67 return ((a_val & SIGN_BIT) ? t : -t);
70 static WORD decode_mulaw(UBYTE u_val) {
71 WORD t;
73 /* Complement to obtain normal u-law value. */
74 u_val = ~u_val;
77 * Extract and bias the quantization bits. Then
78 * shift up by the segment number and subtract out the bias.
80 t = ((u_val & QUANT_MASK) << 3) + BIAS;
81 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
83 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));