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

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/src/ircam.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) 2001-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 Lesser General Public License as published by
6
 
** the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
13
 
**
14
 
** You should have received a copy of the GNU Lesser 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        <fcntl.h>
23
 
#include        <string.h>
24
 
#include        <ctype.h>
25
 
 
26
 
#include        "sndfile.h"
27
 
#include        "sfendian.h"
28
 
#include        "common.h"
29
 
 
30
 
/*------------------------------------------------------------------------------
31
 
** Macros to handle big/little endian issues.
32
 
*/
33
 
 
34
 
/* The IRCAM magic number is weird in that one byte in the number can have
35
 
** values of 0x1, 0x2, 0x03 or 0x04. Hence the need for a marker and a mask.
36
 
*/
37
 
 
38
 
#define IRCAM_BE_MASK           (MAKE_MARKER (0xFF, 0xFF, 0x00, 0xFF))
39
 
#define IRCAM_BE_MARKER         (MAKE_MARKER (0x64, 0xA3, 0x00, 0x00))
40
 
 
41
 
#define IRCAM_LE_MASK           (MAKE_MARKER (0xFF, 0x00, 0xFF, 0xFF))
42
 
#define IRCAM_LE_MARKER         (MAKE_MARKER (0x00, 0x00, 0xA3, 0x64))
43
 
 
44
 
#define IRCAM_02B_MARKER        (MAKE_MARKER (0x64, 0xA3, 0x02, 0x00))
45
 
#define IRCAM_03L_MARKER        (MAKE_MARKER (0x64, 0xA3, 0x03, 0x00))
46
 
 
47
 
#define IRCAM_DATA_OFFSET       (1024)
48
 
 
49
 
/*------------------------------------------------------------------------------
50
 
** Typedefs.
51
 
*/
52
 
 
53
 
enum
54
 
{       IRCAM_PCM_16    = 0x00002,
55
 
        IRCAM_FLOAT             = 0x00004,
56
 
        IRCAM_ALAW              = 0x10001,
57
 
        IRCAM_ULAW              = 0x20001,
58
 
        IRCAM_PCM_32    = 0x40004
59
 
} ;
60
 
 
61
 
 
62
 
/*------------------------------------------------------------------------------
63
 
** Private static functions.
64
 
*/
65
 
 
66
 
static  int             ircam_close                     (SF_PRIVATE *psf) ;
67
 
static  int             ircam_write_header      (SF_PRIVATE *psf, int calc_length) ;
68
 
static  int             ircam_read_header       (SF_PRIVATE *psf) ;
69
 
 
70
 
static  int             get_encoding (int subformat) ;
71
 
 
72
 
static  const char*     get_encoding_str (int encoding) ;
73
 
 
74
 
/*------------------------------------------------------------------------------
75
 
** Public function.
76
 
*/
77
 
 
78
 
int
79
 
ircam_open      (SF_PRIVATE *psf)
80
 
{       int             subformat ;
81
 
        int             error = SFE_NO_ERROR ;
82
 
 
83
 
        if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
84
 
        {       if ((error = ircam_read_header (psf)))
85
 
                        return error ;
86
 
                } ;
87
 
 
88
 
        subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
89
 
 
90
 
        if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
91
 
        {       if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_IRCAM)
92
 
                        return  SFE_BAD_OPEN_FORMAT ;
93
 
 
94
 
                psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
95
 
                if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU)
96
 
                        psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
97
 
 
98
 
                psf->dataoffset = IRCAM_DATA_OFFSET ;
99
 
 
100
 
                if ((error = ircam_write_header (psf, SF_FALSE)))
101
 
                        return error ;
102
 
 
103
 
                psf->write_header = ircam_write_header ;
104
 
                } ;
105
 
 
106
 
        psf->container_close = ircam_close ;
107
 
 
108
 
        switch (subformat)
109
 
        {       case SF_FORMAT_ULAW :           /* 8-bit Ulaw encoding. */
110
 
                                error = ulaw_init (psf) ;
111
 
                                break ;
112
 
 
113
 
                case SF_FORMAT_ALAW :           /* 8-bit Alaw encoding. */
114
 
                                error = alaw_init (psf) ;
115
 
                                break ;
116
 
 
117
 
                case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
118
 
                case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
119
 
                                error = pcm_init (psf) ;
120
 
                                break ;
121
 
 
122
 
                case SF_FORMAT_FLOAT :  /* 32-bit linear PCM. */
123
 
                                error = float32_init (psf) ;
124
 
                                break ;
125
 
 
126
 
                default : break ;
127
 
                } ;
128
 
 
129
 
        return error ;
130
 
} /* ircam_open */
131
 
 
132
 
/*------------------------------------------------------------------------------
133
 
*/
134
 
 
135
 
static int
136
 
ircam_read_header       (SF_PRIVATE *psf)
137
 
{       unsigned int    marker, encoding ;
138
 
        float                   samplerate ;
139
 
        int                             error = SFE_NO_ERROR ;
140
 
 
141
 
        psf_binheader_readf (psf, "epmf44", 0, &marker, &samplerate, &(psf->sf.channels), &encoding) ;
142
 
 
143
 
        if (((marker & IRCAM_BE_MASK) != IRCAM_BE_MARKER) && ((marker & IRCAM_LE_MASK) != IRCAM_LE_MARKER))
144
 
        {       psf_log_printf (psf, "marker: 0x%X\n", marker) ;
145
 
                return SFE_IRCAM_NO_MARKER ;
146
 
                } ;
147
 
 
148
 
        psf->endian = SF_ENDIAN_LITTLE ;
149
 
 
150
 
        if (psf->sf.channels > 256)
151
 
        {       psf_binheader_readf (psf, "Epmf44", 0, &marker, &samplerate, &(psf->sf.channels), &encoding) ;
152
 
 
153
 
                /* Sanity checking for endian-ness detection. */
154
 
                if (psf->sf.channels > 256)
155
 
                {       psf_log_printf (psf, "marker: 0x%X\n", marker) ;
156
 
                        return SFE_IRCAM_BAD_CHANNELS ;
157
 
                        } ;
158
 
 
159
 
                psf->endian = SF_ENDIAN_BIG ;
160
 
                } ;
161
 
 
162
 
        psf_log_printf (psf, "marker: 0x%X\n", marker) ;
163
 
 
164
 
        psf->sf.samplerate = (int) samplerate ;
165
 
 
166
 
        psf_log_printf (psf, "  Sample Rate : %d\n"
167
 
                                                 "  Channels    : %d\n"
168
 
                                                 "  Encoding    : %X => %s\n", psf->sf.samplerate, psf->sf.channels, encoding, get_encoding_str (encoding)) ;
169
 
 
170
 
        switch (encoding)
171
 
        {       case IRCAM_PCM_16 :
172
 
                                psf->bytewidth = 2 ;
173
 
                                psf->blockwidth = psf->sf.channels * psf->bytewidth ;
174
 
 
175
 
                                psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_PCM_16 ;
176
 
                                break ;
177
 
 
178
 
                case IRCAM_PCM_32 :
179
 
                                psf->bytewidth = 4 ;
180
 
                                psf->blockwidth = psf->sf.channels * psf->bytewidth ;
181
 
 
182
 
                                psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_PCM_32 ;
183
 
                                break ;
184
 
 
185
 
                case IRCAM_FLOAT :
186
 
                                psf->bytewidth = 4 ;
187
 
                                psf->blockwidth = psf->sf.channels * psf->bytewidth ;
188
 
 
189
 
                                psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_FLOAT ;
190
 
                                break ;
191
 
 
192
 
                case IRCAM_ALAW :
193
 
                                psf->bytewidth = 1 ;
194
 
                                psf->blockwidth = psf->sf.channels * psf->bytewidth ;
195
 
 
196
 
                                psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_ALAW ;
197
 
                                break ;
198
 
 
199
 
                case IRCAM_ULAW :
200
 
                                psf->bytewidth = 1 ;
201
 
                                psf->blockwidth = psf->sf.channels * psf->bytewidth ;
202
 
 
203
 
                                psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_ULAW ;
204
 
                                break ;
205
 
 
206
 
                default :
207
 
                                error = SFE_IRCAM_UNKNOWN_FORMAT ;
208
 
                                break ;
209
 
                } ;
210
 
 
211
 
        if (psf->endian == SF_ENDIAN_BIG)
212
 
                psf->sf.format |= SF_ENDIAN_BIG ;
213
 
        else
214
 
                psf->sf.format |= SF_ENDIAN_LITTLE ;
215
 
 
216
 
        if (error)
217
 
                return error ;
218
 
 
219
 
        psf->dataoffset = IRCAM_DATA_OFFSET ;
220
 
        psf->datalength = psf->filelength - psf->dataoffset ;
221
 
 
222
 
        if (psf->sf.frames == 0 && psf->blockwidth)
223
 
                psf->sf.frames = psf->datalength / psf->blockwidth ;
224
 
 
225
 
        psf_log_printf (psf, "  Samples     : %d\n", psf->sf.frames) ;
226
 
 
227
 
        psf_binheader_readf (psf, "p", IRCAM_DATA_OFFSET) ;
228
 
 
229
 
        return 0 ;
230
 
} /* ircam_read_header */
231
 
 
232
 
static int
233
 
ircam_close     (SF_PRIVATE *psf)
234
 
{
235
 
        psf_log_printf (psf, "close\n") ;
236
 
 
237
 
        return 0 ;
238
 
} /* ircam_close */
239
 
 
240
 
static int
241
 
ircam_write_header (SF_PRIVATE *psf, int calc_length)
242
 
{       int                     encoding ;
243
 
        float           samplerate ;
244
 
        sf_count_t      current ;
245
 
 
246
 
        if (psf->pipeoffset > 0)
247
 
                return 0 ;
248
 
 
249
 
        current = psf_ftell (psf) ;
250
 
 
251
 
        calc_length = calc_length ;
252
 
 
253
 
        /* This also sets psf->endian. */
254
 
        encoding = get_encoding (psf->sf.format & SF_FORMAT_SUBMASK) ;
255
 
 
256
 
        if (encoding == 0)
257
 
                return SFE_BAD_OPEN_FORMAT ;
258
 
 
259
 
        /* Reset the current header length to zero. */
260
 
        psf->header [0] = 0 ;
261
 
        psf->headindex = 0 ;
262
 
 
263
 
        if (psf->is_pipe == SF_FALSE)
264
 
                psf_fseek (psf, 0, SEEK_SET) ;
265
 
 
266
 
        samplerate = psf->sf.samplerate ;
267
 
 
268
 
        switch (psf->endian)
269
 
        {       case SF_ENDIAN_BIG :
270
 
                        psf_binheader_writef (psf, "Emf", IRCAM_02B_MARKER, samplerate) ;
271
 
                        psf_binheader_writef (psf, "E44", psf->sf.channels, encoding) ;
272
 
                        break ;
273
 
 
274
 
                case SF_ENDIAN_LITTLE :
275
 
                        psf_binheader_writef (psf, "emf", IRCAM_03L_MARKER, samplerate) ;
276
 
                        psf_binheader_writef (psf, "e44", psf->sf.channels, encoding) ;
277
 
                        break ;
278
 
 
279
 
                default : return SFE_BAD_OPEN_FORMAT ;
280
 
                } ;
281
 
 
282
 
        psf_binheader_writef (psf, "z", (size_t) (IRCAM_DATA_OFFSET - psf->headindex)) ;
283
 
 
284
 
        /* Header construction complete so write it out. */
285
 
        psf_fwrite (psf->header, psf->headindex, 1, psf) ;
286
 
 
287
 
        if (psf->error)
288
 
                return psf->error ;
289
 
 
290
 
        if (current > 0)
291
 
                psf_fseek (psf, current, SEEK_SET) ;
292
 
 
293
 
        return psf->error ;
294
 
} /* ircam_write_header */
295
 
 
296
 
static int
297
 
get_encoding (int subformat)
298
 
{       switch (subformat)
299
 
        {       case SF_FORMAT_PCM_16 : return IRCAM_PCM_16 ;
300
 
                case SF_FORMAT_PCM_32 : return IRCAM_PCM_32 ;
301
 
 
302
 
                case SF_FORMAT_FLOAT :  return IRCAM_FLOAT ;
303
 
 
304
 
                case SF_FORMAT_ULAW :   return IRCAM_ULAW ;
305
 
                case SF_FORMAT_ALAW :   return IRCAM_ALAW ;
306
 
 
307
 
                default : break ;
308
 
                } ;
309
 
 
310
 
        return 0 ;
311
 
} /* get_encoding */
312
 
 
313
 
static const char*
314
 
get_encoding_str (int encoding)
315
 
{       switch (encoding)
316
 
        {       case IRCAM_PCM_16       : return "16 bit PCM" ;
317
 
                case IRCAM_FLOAT        : return "32 bit float" ;
318
 
                case IRCAM_ALAW         : return "A law" ;
319
 
                case IRCAM_ULAW         : return "u law" ;
320
 
                case IRCAM_PCM_32       : return "32 bit PCM" ;
321
 
                } ;
322
 
        return "Unknown encoding" ;
323
 
} /* get_encoding_str */
324
 
 
325
 
/*
326
 
** Do not edit or modify anything in this comment block.
327
 
** The arch-tag line is a file identity tag for the GNU Arch 
328
 
** revision control system.
329
 
**
330
 
** arch-tag: f2714ab8-f286-4c94-9740-edaf673a1c71
331
 
*/