Bug fixed for unix error "readlink /proc/self/fd/0" on MacOS.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / src / avr.c
1 /*
2 ** Copyright (C) 2004-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 <string.h>
23
24 #include "sndfile.h"
25 #include "sfendian.h"
26 #include "common.h"
27
28 #define TWOBIT_MARKER (MAKE_MARKER ('2', 'B', 'I', 'T'))
29 #define AVR_HDR_SIZE 128
30
31 #define SFE_AVR_X 666
32
33 /*
34 ** From: hyc@hanauma.Jpl.Nasa.Gov (Howard Chu)
35 **
36 ** A lot of PD software exists to play Mac .snd files on the ST. One other
37 ** format that seems pretty popular (used by a number of commercial packages)
38 ** is the AVR format (from Audio Visual Research). This format has a 128 byte
39 ** header that looks like this (its actually packed, but thats not portable):
40 */
41
42 typedef struct
43 { int marker ; /* 2BIT */
44 char name [8] ; /* null-padded sample name */
45 short mono ; /* 0 = mono, 0xffff = stereo */
46 short rez ; /* 8 = 8 bit, 16 = 16 bit */
47 short sign ; /* 0 = unsigned, 0xffff = signed */
48
49 short loop ; /* 0 = no loop, 0xffff = looping sample */
50 short midi ; /* 0xffff = no MIDI note assigned, */
51 /* 0xffXX = single key note assignment */
52 /* 0xLLHH = key split, low/hi note */
53 int srate ; /* sample frequency in hertz */
54 int frames ; /* sample length in bytes or words (see rez) */
55 int lbeg ; /* offset to start of loop in bytes or words. */
56 /* set to zero if unused */
57 int lend ; /* offset to end of loop in bytes or words. */
58 /* set to sample length if unused */
59 short res1 ; /* Reserved, MIDI keyboard split */
60 short res2 ; /* Reserved, sample compression */
61 short res3 ; /* Reserved */
62 char ext [20] ; /* Additional filename space, used if (name[7] != 0) */
63 char user [64] ; /* User defined. Typically ASCII message */
64 } AVR_HEADER ;
65
66 /*------------------------------------------------------------------------------
67 ** Private static functions.
68 */
69
70 static int avr_close (SF_PRIVATE *psf) ;
71
72 static int avr_read_header (SF_PRIVATE *psf) ;
73 static int avr_write_header (SF_PRIVATE *psf, int calc_length) ;
74
75 /*------------------------------------------------------------------------------
76 ** Public function.
77 */
78
79 int
80 avr_open (SF_PRIVATE *psf)
81 { int error = 0 ;
82
83 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
84 { if ((error = avr_read_header (psf)))
85 return error ;
86 } ;
87
88 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_AVR)
89 return SFE_BAD_OPEN_FORMAT ;
90
91 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
92 { psf->endian = SF_ENDIAN (psf->sf.format) ;
93 psf->endian = SF_ENDIAN_BIG ;
94
95 if (avr_write_header (psf, SF_FALSE))
96 return psf->error ;
97
98 psf->write_header = avr_write_header ;
99 } ;
100
101 psf->container_close = avr_close ;
102
103 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
104
105 error = pcm_init (psf) ;
106
107 return error ;
108 } /* avr_open */
109
110 static int
111 avr_read_header (SF_PRIVATE *psf)
112 { AVR_HEADER hdr ;
113
114 memset (&hdr, 0, sizeof (hdr)) ;
115
116 psf_binheader_readf (psf, "pmb", 0, &hdr.marker, &hdr.name, sizeof (hdr.name)) ;
117 psf_log_printf (psf, "%M\n", hdr.marker) ;
118
119 if (hdr.marker != TWOBIT_MARKER)
120 return SFE_AVR_X ;
121
122 psf_log_printf (psf, " Name : %s\n", hdr.name) ;
123
124 psf_binheader_readf (psf, "E22222", &hdr.mono, &hdr.rez, &hdr.sign, &hdr.loop, &hdr.midi) ;
125
126 psf->sf.channels = (hdr.mono & 1) + 1 ;
127
128 psf_log_printf (psf, " Channels : %d\n Bit width : %d\n Signed : %s\n",
129 (hdr.mono & 1) + 1, hdr.rez, hdr.sign ? "yes" : "no") ;
130
131 switch ((hdr.rez << 16) + (hdr.sign & 1))
132 { case ((8 << 16) + 0) :
133 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_U8 ;
134 psf->bytewidth = 1 ;
135 break ;
136
137 case ((8 << 16) + 1) :
138 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_S8 ;
139 psf->bytewidth = 1 ;
140 break ;
141
142 case ((16 << 16) + 1) :
143 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_16 ;
144 psf->bytewidth = 2 ;
145 break ;
146
147 default :
148 psf_log_printf (psf, "Error : bad rez/sign combination.\n") ;
149 return SFE_AVR_X ;
150 } ;
151
152 psf_binheader_readf (psf, "E4444", &hdr.srate, &hdr.frames, &hdr.lbeg, &hdr.lend) ;
153
154 psf->sf.frames = hdr.frames ;
155 psf->sf.samplerate = hdr.srate ;
156
157 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
158 psf_log_printf (psf, " Sample rate : %d\n", psf->sf.samplerate) ;
159
160 psf_binheader_readf (psf, "E222", &hdr.res1, &hdr.res2, &hdr.res3) ;
161 psf_binheader_readf (psf, "bb", hdr.ext, sizeof (hdr.ext), hdr.user, sizeof (hdr.user)) ;
162
163 psf_log_printf (psf, " Ext : %s\n User : %s\n", hdr.ext, hdr.user) ;
164
165 psf->endian = SF_ENDIAN_BIG ;
166
167 psf->dataoffset = AVR_HDR_SIZE ;
168 psf->datalength = hdr.frames * (hdr.rez / 8) ;
169
170 if (psf->fileoffset > 0)
171 psf->filelength = AVR_HDR_SIZE + psf->datalength ;
172
173 if (psf_ftell (psf) != psf->dataoffset)
174 psf_binheader_readf (psf, "j", psf->dataoffset - psf_ftell (psf)) ;
175
176 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
177
178 if (psf->sf.frames == 0 && psf->blockwidth)
179 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
180
181 return 0 ;
182 } /* avr_read_header */
183
184 static int
185 avr_write_header (SF_PRIVATE *psf, int calc_length)
186 { sf_count_t current ;
187 int sign ;
188
189 if (psf->pipeoffset > 0)
190 return 0 ;
191
192 current = psf_ftell (psf) ;
193
194 if (calc_length)
195 { psf->filelength = psf_get_filelen (psf) ;
196
197 psf->datalength = psf->filelength - psf->dataoffset ;
198 if (psf->dataend)
199 psf->datalength -= psf->filelength - psf->dataend ;
200
201 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
202 } ;
203
204 /* Reset the current header length to zero. */
205 psf->header [0] = 0 ;
206 psf->headindex = 0 ;
207
208 /*
209 ** Only attempt to seek if we are not writng to a pipe. If we are
210 ** writing to a pipe we shouldn't be here anyway.
211 */
212 if (psf->is_pipe == SF_FALSE)
213 psf_fseek (psf, 0, SEEK_SET) ;
214
215 psf_binheader_writef (psf, "Emz22", TWOBIT_MARKER, make_size_t (8),
216 psf->sf.channels == 2 ? 0xFFFF : 0, psf->bytewidth * 8) ;
217
218 sign = ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_U8) ? 0 : 0xFFFF ;
219
220 psf_binheader_writef (psf, "E222", sign, 0, 0xFFFF) ;
221 psf_binheader_writef (psf, "E4444", psf->sf.samplerate, psf->sf.frames, 0, 0) ;
222
223 psf_binheader_writef (psf, "E222zz", 0, 0, 0, make_size_t (20), make_size_t (64)) ;
224
225 /* Header construction complete so write it out. */
226 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
227
228 if (psf->error)
229 return psf->error ;
230
231 psf->dataoffset = psf->headindex ;
232
233 if (current > 0)
234 psf_fseek (psf, current, SEEK_SET) ;
235
236 return psf->error ;
237 } /* avr_write_header */
238
239 static int
240 avr_close (SF_PRIVATE *psf)
241 {
242 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
243 avr_write_header (psf, SF_TRUE) ;
244
245 return 0 ;
246 } /* avr_close */
247