libsndfile compiling.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / src / dwd.c
1 /*
2 ** Copyright (C) 2002-2011 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 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 error = 0 ;
73
74 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
75 { if ((error = dwd_read_header (psf)))
76 return error ;
77 } ;
78
79 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_DWD)
80 return SFE_BAD_OPEN_FORMAT ;
81
82 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
83 {
84 /*-psf->endian = SF_ENDIAN (psf->sf.format) ;
85 if (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU)
86 psf->endian = SF_ENDIAN_LITTLE ;
87 else if (psf->endian != SF_ENDIAN_LITTLE)
88 psf->endian = SF_ENDIAN_BIG ;
89
90 if (! (encoding = dwd_write_header (psf, SF_FALSE)))
91 return psf->error ;
92
93 psf->write_header = dwd_write_header ;
94 -*/
95 } ;
96
97 psf->container_close = dwd_close ;
98
99 /*-psf->blockwidth = psf->bytewidth * psf->sf.channels ;-*/
100
101 return error ;
102 } /* dwd_open */
103
104 /*------------------------------------------------------------------------------
105 */
106
107 static int
108 dwd_close (SF_PRIVATE * UNUSED (psf))
109 {
110 return 0 ;
111 } /* dwd_close */
112
113 /* This struct contains all the fields of interest om the DWD header, but does not
114 ** do so in the same order and layout as the actual file, header.
115 ** No assumptions are made about the packing of this struct.
116 */
117 typedef struct
118 { unsigned char major, minor, compression, channels, bitwidth ;
119 unsigned short srate, maxval ;
120 unsigned int id, datalen, frames, offset ;
121 } DWD_HEADER ;
122
123 static int
124 dwd_read_header (SF_PRIVATE *psf)
125 { DWD_HEADER dwdh ;
126
127 memset (psf->u.cbuf, 0, sizeof (psf->u.cbuf)) ;
128 /* Set position to start of file to begin reading header. */
129 psf_binheader_readf (psf, "pb", 0, psf->u.cbuf, DWD_IDENTIFIER_LEN) ;
130
131 if (memcmp (psf->u.cbuf, DWD_IDENTIFIER, DWD_IDENTIFIER_LEN) != 0)
132 return SFE_DWD_NO_DWD ;
133
134 psf_log_printf (psf, "Read only : DiamondWare Digitized (.dwd)\n", psf->u.cbuf) ;
135
136 psf_binheader_readf (psf, "11", &dwdh.major, &dwdh.minor) ;
137 psf_binheader_readf (psf, "e4j1", &dwdh.id, 1, &dwdh.compression) ;
138 psf_binheader_readf (psf, "e211", &dwdh.srate, &dwdh.channels, &dwdh.bitwidth) ;
139 psf_binheader_readf (psf, "e24", &dwdh.maxval, &dwdh.datalen) ;
140 psf_binheader_readf (psf, "e44", &dwdh.frames, &dwdh.offset) ;
141
142 psf_log_printf (psf, " Version Major : %d\n Version Minor : %d\n Unique ID : %08X\n",
143 dwdh.major, dwdh.minor, dwdh.id) ;
144 psf_log_printf (psf, " Compression : %d => ", dwdh.compression) ;
145
146 if (dwdh.compression != 0)
147 { psf_log_printf (psf, "Unsupported compression\n") ;
148 return SFE_DWD_COMPRESSION ;
149 }
150 else
151 psf_log_printf (psf, "None\n") ;
152
153 psf_log_printf (psf, " Sample Rate : %d\n Channels : %d\n"
154 " Bit Width : %d\n",
155 dwdh.srate, dwdh.channels, dwdh.bitwidth) ;
156
157 switch (dwdh.bitwidth)
158 { case 8 :
159 psf->sf.format = SF_FORMAT_DWD | SF_FORMAT_PCM_S8 ;
160 psf->bytewidth = 1 ;
161 break ;
162
163 case 16 :
164 psf->sf.format = SF_FORMAT_DWD | SF_FORMAT_PCM_16 ;
165 psf->bytewidth = 2 ;
166 break ;
167
168 default :
169 psf_log_printf (psf, "*** Bad bit width %d\n", dwdh.bitwidth) ;
170 return SFE_DWD_BAND_BIT_WIDTH ;
171 } ;
172
173 if (psf->filelength != dwdh.offset + dwdh.datalen)
174 { psf_log_printf (psf, " Data Length : %d (should be %D)\n", dwdh.datalen, psf->filelength - dwdh.offset) ;
175 dwdh.datalen = (unsigned int) (psf->filelength - dwdh.offset) ;
176 }
177 else
178 psf_log_printf (psf, " Data Length : %d\n", dwdh.datalen) ;
179
180 psf_log_printf (psf, " Max Value : %d\n", dwdh.maxval) ;
181 psf_log_printf (psf, " Frames : %d\n", dwdh.frames) ;
182 psf_log_printf (psf, " Data Offset : %d\n", dwdh.offset) ;
183
184 psf->datalength = dwdh.datalen ;
185 psf->dataoffset = dwdh.offset ;
186
187 psf->endian = SF_ENDIAN_LITTLE ;
188
189 psf->sf.samplerate = dwdh.srate ;
190 psf->sf.channels = dwdh.channels ;
191 psf->sf.sections = 1 ;
192
193 return pcm_init (psf) ;
194 } /* dwd_read_header */
195
196 /*------------------------------------------------------------------------------
197 */
198
199 #endif
200