Merged with trunk revision 600
[ardour.git] / libs / libsndfile / src / dwd.c
1 /*
2 ** Copyright (C) 2002-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 #if (ENABLE_EXPERIMENTAL_CODE == 0)
31
32 int
33 dwd_open        (SF_PRIVATE *psf)
34 {       if (psf)
35                 return SFE_UNIMPLEMENTED ;
36         return (psf && 0) ;
37 } /* dwd_open */
38
39 #else
40
41 /*------------------------------------------------------------------------------
42 ** Macros to handle big/little endian issues.
43 */
44
45 #define SFE_DWD_NO_DWD                  1666
46 #define SFE_DWD_BAND_BIT_WIDTH  1667
47 #define SFE_DWD_COMPRESSION             1668
48
49 #define DWD_IDENTIFIER          "DiamondWare Digitized\n\0\x1a"
50 #define DWD_IDENTIFIER_LEN      24
51
52 #define DWD_HEADER_LEN          57
53
54 /*------------------------------------------------------------------------------
55 ** Typedefs.
56 */
57
58 /*------------------------------------------------------------------------------
59 ** Private static functions.
60 */
61
62 static int      dwd_read_header (SF_PRIVATE *psf) ;
63
64 static int      dwd_close               (SF_PRIVATE *psf) ;
65
66 /*------------------------------------------------------------------------------
67 ** Public function.
68 */
69
70 int
71 dwd_open (SF_PRIVATE *psf)
72 {       int     subformat, error = 0 ;
73
74         if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
75         {       if ((error = dwd_read_header (psf)))
76                         return error ;
77                 } ;
78
79         if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_DWD)
80                 return  SFE_BAD_OPEN_FORMAT ;
81
82         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
83
84         if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
85         {
86                 /*-psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
87                 if (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU)
88                         psf->endian = SF_ENDIAN_LITTLE ;
89                 else if (psf->endian != SF_ENDIAN_LITTLE)
90                         psf->endian = SF_ENDIAN_BIG ;
91
92                 if (! (encoding = dwd_write_header (psf, SF_FALSE)))
93                         return psf->error ;
94
95                 psf->write_header = dwd_write_header ;
96                 -*/
97                 } ;
98
99         psf->container_close = dwd_close ;
100
101         /*-psf->blockwidth = psf->bytewidth * psf->sf.channels ;-*/
102
103         return error ;
104 } /* dwd_open */
105
106 /*------------------------------------------------------------------------------
107 */
108
109 static int
110 dwd_close       (SF_PRIVATE *psf)
111 {
112         psf = psf ;
113
114         return 0 ;
115 } /* dwd_close */
116
117 /* This struct contains all the fields of interest om the DWD header, but does not
118 ** do so in the same order and layout as the actual file, header.
119 ** No assumptions are made about the packing of this struct.
120 */
121 typedef struct
122 {       unsigned char major, minor, compression, channels, bitwidth ;
123         unsigned short srate, maxval ;
124         unsigned int id, datalen, frames, offset ;
125 } DWD_HEADER ;
126
127 static int
128 dwd_read_header (SF_PRIVATE *psf)
129 {       DWD_HEADER      dwdh ;
130
131         memset (psf->u.cbuf, 0, sizeof (psf->u.cbuf)) ;
132         /* Set position to start of file to begin reading header. */
133         psf_binheader_readf (psf, "pb", 0, psf->u.cbuf, DWD_IDENTIFIER_LEN) ;
134
135         if (memcmp (psf->u.cbuf, DWD_IDENTIFIER, DWD_IDENTIFIER_LEN) != 0)
136                 return SFE_DWD_NO_DWD ;
137
138         psf_log_printf (psf, "Read only : DiamondWare Digitized (.dwd)\n", psf->u.cbuf) ;
139
140         psf_binheader_readf (psf, "11", &dwdh.major, &dwdh.minor) ;
141         psf_binheader_readf (psf, "e4j1", &dwdh.id, 1, &dwdh.compression) ;
142         psf_binheader_readf (psf, "e211", &dwdh.srate, &dwdh.channels, &dwdh.bitwidth) ;
143         psf_binheader_readf (psf, "e24", &dwdh.maxval, &dwdh.datalen) ;
144         psf_binheader_readf (psf, "e44", &dwdh.frames, &dwdh.offset) ;
145
146         psf_log_printf (psf, "  Version Major : %d\n  Version Minor : %d\n  Unique ID     : %08X\n",
147                                                 dwdh.major, dwdh.minor, dwdh.id) ;
148         psf_log_printf (psf, "  Compression   : %d => ", dwdh.compression) ;
149
150         if (dwdh.compression != 0)
151         {       psf_log_printf (psf, "Unsupported compression\n") ;
152                 return SFE_DWD_COMPRESSION ;
153                 }
154         else
155                 psf_log_printf (psf, "None\n") ;
156
157         psf_log_printf (psf, "  Sample Rate   : %d\n  Channels      : %d\n"
158                                                  "  Bit Width     : %d\n",
159                                                  dwdh.srate, dwdh.channels, dwdh.bitwidth) ;
160
161         switch (dwdh.bitwidth)
162         {       case 8 :
163                                 psf->sf.format = SF_FORMAT_DWD | SF_FORMAT_PCM_S8 ;
164                                 psf->bytewidth = 1 ;
165                                 break ;
166
167                 case 16 :
168                                 psf->sf.format = SF_FORMAT_DWD | SF_FORMAT_PCM_16 ;
169                                 psf->bytewidth = 2 ;
170                                 break ;
171
172                 default :
173                                 psf_log_printf (psf, "*** Bad bit width %d\n", dwdh.bitwidth) ;
174                                 return SFE_DWD_BAND_BIT_WIDTH ;
175                                 } ;
176
177         if (psf->filelength != dwdh.offset + dwdh.datalen)
178         {       psf_log_printf (psf, "  Data Length   : %d (should be %D)\n", dwdh.datalen, psf->filelength - dwdh.offset) ;
179                 dwdh.datalen = (unsigned int) (psf->filelength - dwdh.offset) ;
180                 }
181         else
182                 psf_log_printf (psf, "  Data Length   : %d\n", dwdh.datalen) ;
183
184         psf_log_printf (psf, "  Max Value     : %d\n", dwdh.maxval) ;
185         psf_log_printf (psf, "  Frames        : %d\n", dwdh.frames) ;
186         psf_log_printf (psf, "  Data Offset   : %d\n", dwdh.offset) ;
187
188         psf->datalength = dwdh.datalen ;
189         psf->dataoffset = dwdh.offset ;
190
191         psf->endian = SF_ENDIAN_LITTLE ;
192
193         psf->sf.samplerate = dwdh.srate ;
194         psf->sf.channels = dwdh.channels ;
195         psf->sf.sections = 1 ;
196
197         return pcm_init (psf) ;
198 } /* dwd_read_header */
199
200 /*------------------------------------------------------------------------------
201 */
202
203 #endif
204 /*
205 ** Do not edit or modify anything in this comment block.
206 ** The arch-tag line is a file identity tag for the GNU Arch 
207 ** revision control system.
208 **
209 ** arch-tag: a5e1d2a6-a840-4039-a0e7-e1a43eb05a4f
210 */