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