~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/tests/ulaw_test.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3
 
**
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.
8
 
**
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.
13
 
**
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.
17
 
*/
18
 
 
19
 
#include "sfconfig.h"
20
 
 
21
 
#include <stdio.h>
22
 
#include <stdlib.h>
23
 
 
24
 
#if HAVE_UNISTD_H
25
 
#include <unistd.h>
26
 
#endif
27
 
 
28
 
#include <sndfile.h>
29
 
 
30
 
#include "utils.h"
31
 
 
32
 
#define BUFFER_SIZE             (65536)
33
 
 
34
 
static unsigned char    ulaw_encode (int sample) ;
35
 
static int                              ulaw_decode (unsigned int ulawbyte) ;
36
 
 
37
 
static  short                   short_buffer [BUFFER_SIZE] ;
38
 
static  unsigned char   ulaw_buffer [BUFFER_SIZE] ;
39
 
 
40
 
int
41
 
main (void)
42
 
{       SNDFILE         *file ;
43
 
        SF_INFO         sfinfo ;
44
 
        const char      *filename ;
45
 
        int                     k ;
46
 
 
47
 
        filename = "test.raw" ;
48
 
 
49
 
        sfinfo.format           = SF_FORMAT_RAW | SF_FORMAT_ULAW ;
50
 
        sfinfo.samplerate       = 44100 ;
51
 
        sfinfo.frames           = 123456789 ;
52
 
        sfinfo.channels         = 1 ;
53
 
 
54
 
        if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
55
 
        {       printf ("sf_open_write failed with error : ") ;
56
 
                fflush (stdout) ;
57
 
                puts (sf_strerror (NULL)) ;
58
 
                exit (1) ;
59
 
                } ;
60
 
 
61
 
        /* Generate a file containing all possible 16 bit sample values
62
 
        ** and write it to disk as ulaw encoded.frames.
63
 
        */
64
 
 
65
 
        for (k = 0 ; k < 0x10000 ; k++)
66
 
                short_buffer [k] = k & 0xFFFF ;
67
 
 
68
 
        sf_write_short (file, short_buffer, BUFFER_SIZE) ;
69
 
        sf_close (file) ;
70
 
 
71
 
        /* Now open that file and compare the ulaw encoded sample values
72
 
        ** with what they should be.
73
 
        */
74
 
 
75
 
        if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
76
 
        {       printf ("sf_open_write failed with error : ") ;
77
 
                puts (sf_strerror (NULL)) ;
78
 
                exit (1) ;
79
 
                } ;
80
 
 
81
 
        check_log_buffer_or_die (file, __LINE__) ;
82
 
 
83
 
        if (sf_read_raw (file, ulaw_buffer, BUFFER_SIZE) != BUFFER_SIZE)
84
 
        {       printf ("sf_read_raw : ") ;
85
 
                puts (sf_strerror (file)) ;
86
 
                exit (1) ;
87
 
                } ;
88
 
 
89
 
        for (k = 0 ; k < 0x10000 ; k++)
90
 
                if (ulaw_encode (short_buffer [k]) != ulaw_buffer [k])
91
 
                {       printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, ulaw_buffer [k], ulaw_encode (short_buffer [k])) ;
92
 
                        exit (1) ;
93
 
                        } ;
94
 
 
95
 
        sf_close (file) ;
96
 
 
97
 
        printf ("    ulaw_test : encoder ... ok\n") ;
98
 
 
99
 
        /* Now generate a file containing all possible 8 bit encoded
100
 
        ** sample values and write it to disk as ulaw encoded.frames.
101
 
        */
102
 
 
103
 
        if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
104
 
        {       printf ("sf_open_write failed with error : ") ;
105
 
                puts (sf_strerror (NULL)) ;
106
 
                exit (1) ;
107
 
                } ;
108
 
 
109
 
        for (k = 0 ; k < 256 ; k++)
110
 
                ulaw_buffer [k] = k & 0xFF ;
111
 
 
112
 
        sf_write_raw (file, ulaw_buffer, 256) ;
113
 
        sf_close (file) ;
114
 
 
115
 
        /* Now open that file and compare the ulaw decoded sample values
116
 
        ** with what they should be.
117
 
        */
118
 
 
119
 
        if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
120
 
        {       printf ("sf_open_write failed with error : ") ;
121
 
                puts (sf_strerror (NULL)) ;
122
 
                exit (1) ;
123
 
                } ;
124
 
 
125
 
        check_log_buffer_or_die (file, __LINE__) ;
126
 
 
127
 
        if (sf_read_short (file, short_buffer, 256) != 256)
128
 
        {       printf ("sf_read_short : ") ;
129
 
                puts (sf_strerror (file)) ;
130
 
                exit (1) ;
131
 
                } ;
132
 
 
133
 
 
134
 
        for (k = 0 ; k < 256 ; k++)
135
 
                if (short_buffer [k] != ulaw_decode (ulaw_buffer [k]))
136
 
                {       printf ("Decoder error : sample #%d (0x%04X should be 0x%04X)\n", k, short_buffer [k], ulaw_decode (ulaw_buffer [k])) ;
137
 
                        exit (1) ;
138
 
                        } ;
139
 
 
140
 
        sf_close (file) ;
141
 
 
142
 
        printf ("    ulaw_test : decoder ... ok\n") ;
143
 
 
144
 
        unlink (filename) ;
145
 
 
146
 
        return 0 ;
147
 
} /* main */
148
 
 
149
 
 
150
 
/*=================================================================================
151
 
**      The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
152
 
**
153
 
**      This code is not compiled into libsndfile. It is only used to test the
154
 
**      libsndfile lookup tables for correctness.
155
 
**
156
 
**      I have included the original authors comments.
157
 
*/
158
 
 
159
 
/*
160
 
** This routine converts from linear to ulaw.
161
 
**
162
 
** Craig Reese: IDA/Supercomputing Research Center
163
 
** Joe Campbell: Department of Defense
164
 
** 29 September 1989
165
 
**
166
 
** References:
167
 
** 1) CCITT Recommendation G.711  (very difficult to follow)
168
 
** 2) "A New Digital Technique for Implementation of Any
169
 
**     Continuous PCM Companding Law," Villeret, Michel,
170
 
**     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
171
 
**     1973, pg. 11.12-11.17
172
 
** 3) MIL-STD-188-113,"Interoperability and Performance Standards
173
 
**     for Analog-to_Digital Conversion Techniques,"
174
 
**     17 February 1987
175
 
**
176
 
** Input: Signed 16 bit linear sample
177
 
** Output: 8 bit ulaw sample
178
 
*/
179
 
 
180
 
#define uBIAS 0x84              /* define the add-in bias for 16 bit.frames */
181
 
#define uCLIP 32635
182
 
 
183
 
static
184
 
unsigned char ulaw_encode (int sample)
185
 
{       static int exp_lut [256] =
186
 
        {       0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
187
 
                4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
188
 
                5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
189
 
                5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
190
 
                6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
191
 
                6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
192
 
                6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
193
 
                6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
194
 
                7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
195
 
                7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
196
 
                7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
197
 
                7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
198
 
                7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
199
 
                7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
200
 
                7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
201
 
                7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
202
 
                } ;
203
 
 
204
 
    int sign, exponent, mantissa ;
205
 
    unsigned char ulawbyte ;
206
 
 
207
 
    /* Get the sample into sign-magnitude. */
208
 
    sign = (sample >> 8) & 0x80 ;                                       /* set aside the sign */
209
 
    if ( sign != 0 )
210
 
                sample = -sample ;                                                      /* get magnitude */
211
 
    if ( sample > uCLIP )
212
 
                sample = uCLIP ;                                                        /* clip the magnitude */
213
 
 
214
 
    /* Convert from 16 bit linear to ulaw. */
215
 
    sample = sample + uBIAS ;
216
 
    exponent = exp_lut [( sample >> 7 ) & 0xFF] ;
217
 
    mantissa = (sample >> ( exponent + 3 ) ) & 0x0F ;
218
 
    ulawbyte = ~ (sign | ( exponent << 4 ) | mantissa) ;
219
 
 
220
 
        return ulawbyte ;
221
 
} /* ulaw_encode */
222
 
 
223
 
 
224
 
/*
225
 
** This routine converts from ulaw to 16 bit linear.
226
 
**
227
 
** Craig Reese: IDA/Supercomputing Research Center
228
 
** 29 September 1989
229
 
**
230
 
** References:
231
 
** 1) CCITT Recommendation G.711  (very difficult to follow)
232
 
** 2) MIL-STD-188-113,"Interoperability and Performance Standards
233
 
**     for Analog-to_Digital Conversion Techniques,"
234
 
**     17 February 1987
235
 
**
236
 
** Input: 8 bit ulaw sample
237
 
** Output: signed 16 bit linear sample
238
 
*/
239
 
 
240
 
static
241
 
int ulaw_decode (unsigned int ulawbyte)
242
 
{       static int exp_lut [8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 } ;
243
 
    int sign, exponent, mantissa, sample ;
244
 
 
245
 
    ulawbyte = ~ ulawbyte ;
246
 
    sign = (ulawbyte & 0x80) ;
247
 
    exponent = (ulawbyte >> 4) & 0x07 ;
248
 
    mantissa = ulawbyte & 0x0F ;
249
 
    sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ;
250
 
    if (sign != 0)
251
 
                sample = -sample ;
252
 
 
253
 
    return sample ;
254
 
} /* ulaw_decode */
255
 
 
256
 
/*
257
 
** Do not edit or modify anything in this comment block.
258
 
** The arch-tag line is a file identity tag for the GNU Arch 
259
 
** revision control system.
260
 
**
261
 
** arch-tag: e764cee1-5a9a-480b-a4ca-34d9b57dea6f
262
 
*/