e25101d9d82f6c06b17571fe14690deb9b620859
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #define BUFFER_SIZE (65536)
35 static unsigned char alaw_encode (int sample
) ;
36 static int alaw_decode (unsigned int alawbyte
) ;
38 static short short_buffer
[BUFFER_SIZE
] ;
39 static unsigned char alaw_buffer
[BUFFER_SIZE
] ;
45 const char *filename
;
48 print_test_name ("alaw_test", "encoder") ;
50 filename
= "test.raw" ;
52 sf_info_setup (&sfinfo
, SF_FORMAT_RAW
| SF_FORMAT_ALAW
, 44100, 1) ;
54 if ((file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)) == NULL
)
55 { printf ("sf_open_write failed with error : ") ;
57 puts (sf_strerror (NULL
)) ;
61 /* Generate a file containing all possible 16 bit sample values
62 ** and write it to disk as alaw encoded.frames.
65 for (k
= 0 ; k
< 0x10000 ; k
++)
66 short_buffer
[k
] = k
& 0xFFFF ;
68 sf_write_short (file
, short_buffer
, BUFFER_SIZE
) ;
71 /* Now open that file and compare the alaw encoded sample values
72 ** with what they should be.
75 if ((file
= sf_open (filename
, SFM_READ
, &sfinfo
)) == NULL
)
76 { printf ("sf_open_write failed with error : ") ;
77 puts (sf_strerror (NULL
)) ;
81 check_log_buffer_or_die (file
, __LINE__
) ;
83 if (sf_read_raw (file
, alaw_buffer
, BUFFER_SIZE
) != BUFFER_SIZE
)
84 { printf ("sf_read_raw : ") ;
85 puts (sf_strerror (file
)) ;
89 for (k
= 0 ; k
< 0x10000 ; k
++)
90 if (alaw_encode (short_buffer
[k
]) != alaw_buffer
[k
])
91 { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k
, alaw_buffer
[k
], alaw_encode (short_buffer
[k
])) ;
99 print_test_name ("alaw_test", "decoder") ;
100 /* Now generate a file containing all possible 8 bit encoded
101 ** sample values and write it to disk as alaw encoded.frames.
104 if (! (file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)))
105 { printf ("sf_open_write failed with error : ") ;
106 puts (sf_strerror (NULL
)) ;
110 for (k
= 0 ; k
< 256 ; k
++)
111 alaw_buffer
[k
] = k
& 0xFF ;
113 sf_write_raw (file
, alaw_buffer
, 256) ;
116 /* Now open that file and compare the alaw decoded sample values
117 ** with what they should be.
120 if (! (file
= sf_open (filename
, SFM_READ
, &sfinfo
)))
121 { printf ("sf_open_write failed with error : ") ;
122 puts (sf_strerror (NULL
)) ;
126 check_log_buffer_or_die (file
, __LINE__
) ;
128 if (sf_read_short (file
, short_buffer
, 256) != 256)
129 { printf ("sf_read_short : ") ;
130 puts (sf_strerror (file
)) ;
135 for (k
= 0 ; k
< 256 ; k
++)
136 if (short_buffer
[k
] != alaw_decode (alaw_buffer
[k
]))
137 { printf ("Decoder error : sample #%d (0x%02X should be 0x%02X)\n", k
, short_buffer
[k
], alaw_decode (alaw_buffer
[k
])) ;
151 /*=================================================================================
152 ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
154 ** This code is not compiled into libsndfile. It is only used to test the
155 ** libsndfile lookup tables for correctness.
157 ** I have included the original authors comments.
161 ** A-law routines by Graeme W. Gill.
165 ** 1) CCITT Recommendation G.711
172 unsigned char alaw_encode (int sample
)
173 { static int exp_lut
[128] =
174 { 1, 1, 2, 2, 3, 3, 3, 3,
175 4, 4, 4, 4, 4, 4, 4, 4,
176 5, 5, 5, 5, 5, 5, 5, 5,
177 5, 5, 5, 5, 5, 5, 5, 5,
178 6, 6, 6, 6, 6, 6, 6, 6,
179 6, 6, 6, 6, 6, 6, 6, 6,
180 6, 6, 6, 6, 6, 6, 6, 6,
181 6, 6, 6, 6, 6, 6, 6, 6,
182 7, 7, 7, 7, 7, 7, 7, 7,
183 7, 7, 7, 7, 7, 7, 7, 7,
184 7, 7, 7, 7, 7, 7, 7, 7,
185 7, 7, 7, 7, 7, 7, 7, 7,
186 7, 7, 7, 7, 7, 7, 7, 7,
187 7, 7, 7, 7, 7, 7, 7, 7,
188 7, 7, 7, 7, 7, 7, 7, 7,
189 7, 7, 7, 7, 7, 7, 7, 7
192 int sign
, exponent
, mantissa
;
193 unsigned char Alawbyte
;
195 /* Get the sample into sign-magnitude. */
196 sign
= ((~sample
) >> 8) & 0x80 ; /* set aside the sign */
198 sample
= -sample
; /* get magnitude */
200 sample
= ACLIP
; /* clip the magnitude */
202 /* Convert from 16 bit linear to ulaw. */
204 { exponent
= exp_lut
[(sample
>> 8) & 0x7F] ;
205 mantissa
= ( sample
>> ( exponent
+ 3 ) ) & 0x0F ;
206 Alawbyte
= ((exponent
<< 4) | mantissa
) ;
209 Alawbyte
= (sample
>> 4) ;
211 Alawbyte
^= (sign
^ 0x55) ;
217 int alaw_decode (unsigned int Alawbyte
)
218 { static int exp_lut
[8] = { 0, 264, 528, 1056, 2112, 4224, 8448, 16896 } ;
219 int sign
, exponent
, mantissa
, sample
;
222 sign
= (Alawbyte
& 0x80) ;
223 Alawbyte
&= 0x7f ; /* get magnitude */
225 { exponent
= (Alawbyte
>> 4 ) & 0x07 ;
226 mantissa
= Alawbyte
& 0x0F ;
227 sample
= exp_lut
[exponent
] + (mantissa
<< ( exponent
+ 3 )) ;
230 sample
= (Alawbyte
<< 4) + 8 ;