1 /*
2 ** Copyright (C) 2004-2017 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
avr_open(SF_PRIVATE * psf)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_BIG ;
93
94 if (avr_write_header (psf, SF_FALSE))
95 return psf->error ;
96
97 psf->write_header = avr_write_header ;
98 } ;
99
100 psf->container_close = avr_close ;
101
102 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
103
104 error = pcm_init (psf) ;
105
106 return error ;
107 } /* avr_open */
108
109 static int
avr_read_header(SF_PRIVATE * psf)110 avr_read_header (SF_PRIVATE *psf)
111 { AVR_HEADER hdr ;
112
113 memset (&hdr, 0, sizeof (hdr)) ;
114
115 psf_binheader_readf (psf, "pmb", 0, &hdr.marker, &hdr.name, sizeof (hdr.name)) ;
116 psf_log_printf (psf, "%M\n", hdr.marker) ;
117
118 if (hdr.marker != TWOBIT_MARKER)
119 return SFE_AVR_X ;
120
121 psf_log_printf (psf, " Name : %s\n", hdr.name) ;
122
123 psf_binheader_readf (psf, "E22222", &hdr.mono, &hdr.rez, &hdr.sign, &hdr.loop, &hdr.midi) ;
124
125 psf->sf.channels = (hdr.mono & 1) + 1 ;
126
127 psf_log_printf (psf, " Channels : %d\n Bit width : %d\n Signed : %s\n",
128 (hdr.mono & 1) + 1, hdr.rez, hdr.sign ? "yes" : "no") ;
129
130 switch (arith_shift_left (hdr.rez, 16) + (hdr.sign & 1))
131 { case ((8 << 16) + 0) :
132 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_U8 ;
133 psf->bytewidth = 1 ;
134 break ;
135
136 case ((8 << 16) + 1) :
137 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_S8 ;
138 psf->bytewidth = 1 ;
139 break ;
140
141 case ((16 << 16) + 1) :
142 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_16 ;
143 psf->bytewidth = 2 ;
144 break ;
145
146 default :
147 psf_log_printf (psf, "Error : bad rez/sign combination.\n") ;
148 return SFE_AVR_X ;
149 } ;
150
151 psf_binheader_readf (psf, "E4444", &hdr.srate, &hdr.frames, &hdr.lbeg, &hdr.lend) ;
152
153 psf->sf.frames = hdr.frames ;
154 psf->sf.samplerate = hdr.srate ;
155
156 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
157 psf_log_printf (psf, " Sample rate : %d\n", psf->sf.samplerate) ;
158
159 psf_binheader_readf (psf, "E222", &hdr.res1, &hdr.res2, &hdr.res3) ;
160 psf_binheader_readf (psf, "bb", hdr.ext, sizeof (hdr.ext), hdr.user, sizeof (hdr.user)) ;
161
162 psf_log_printf (psf, " Ext : %s\n User : %s\n", hdr.ext, hdr.user) ;
163
164 psf->endian = SF_ENDIAN_BIG ;
165
166 psf->dataoffset = AVR_HDR_SIZE ;
167 psf->datalength = hdr.frames * (hdr.rez / 8) ;
168
169 if (psf->fileoffset > 0)
170 psf->filelength = AVR_HDR_SIZE + psf->datalength ;
171
172 if (psf_ftell (psf) != psf->dataoffset)
173 psf_binheader_readf (psf, "j", psf->dataoffset - psf_ftell (psf)) ;
174
175 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
176
177 if (psf->sf.frames == 0 && psf->blockwidth)
178 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
179
180 return 0 ;
181 } /* avr_read_header */
182
183 static int
avr_write_header(SF_PRIVATE * psf,int calc_length)184 avr_write_header (SF_PRIVATE *psf, int calc_length)
185 { sf_count_t current ;
186 int sign ;
187
188 if (psf->pipeoffset > 0)
189 return 0 ;
190
191 current = psf_ftell (psf) ;
192
193 if (calc_length)
194 { psf->filelength = psf_get_filelen (psf) ;
195
196 psf->datalength = psf->filelength - psf->dataoffset ;
197 if (psf->dataend)
198 psf->datalength -= psf->filelength - psf->dataend ;
199
200 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
201 } ;
202
203 /* Reset the current header length to zero. */
204 psf->header.ptr [0] = 0 ;
205 psf->header.indx = 0 ;
206
207 /*
208 ** Only attempt to seek if we are not writng to a pipe. If we are
209 ** writing to a pipe we shouldn't be here anyway.
210 */
211 if (psf->is_pipe == SF_FALSE)
212 psf_fseek (psf, 0, SEEK_SET) ;
213
214 psf_binheader_writef (psf, "Emz22", BHWm (TWOBIT_MARKER), BHWz (8),
215 BHW2 (psf->sf.channels == 2 ? 0xFFFF : 0), BHW2 (psf->bytewidth * 8)) ;
216
217 sign = ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_U8) ? 0 : 0xFFFF ;
218
219 psf_binheader_writef (psf, "E222", BHW2 (sign), BHW2 (0), BHW2 (0xFFFF)) ;
220 psf_binheader_writef (psf, "E4444", BHW4 (psf->sf.samplerate), BHW4 (psf->sf.frames), BHW4 (0), BHW4 (0)) ;
221
222 psf_binheader_writef (psf, "E222zz", BHW2 (0), BHW2 (0), BHW2 (0), BHWz (20), BHWz (64)) ;
223
224 /* Header construction complete so write it out. */
225 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
226
227 if (psf->error)
228 return psf->error ;
229
230 psf->dataoffset = psf->header.indx ;
231
232 if (current > 0)
233 psf_fseek (psf, current, SEEK_SET) ;
234
235 return psf->error ;
236 } /* avr_write_header */
237
238 static int
avr_close(SF_PRIVATE * psf)239 avr_close (SF_PRIVATE *psf)
240 {
241 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
242 avr_write_header (psf, SF_TRUE) ;
243
244 return 0 ;
245 } /* avr_close */
246
247