1 /*
2 ** Copyright (C) 2001-2016 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 #include	<math.h>
24 
25 #include	"sndfile.h"
26 #include	"common.h"
27 
28 static SF_FORMAT_INFO const simple_formats [] =
29 {
30 	{	SF_FORMAT_AIFF | SF_FORMAT_PCM_16,
31 		"AIFF (Apple/SGI 16 bit PCM)", "aiff"
32 		},
33 
34 	{	SF_FORMAT_AIFF | SF_FORMAT_FLOAT,
35 		"AIFF (Apple/SGI 32 bit float)", "aifc"
36 		},
37 
38 	{	SF_FORMAT_AIFF | SF_FORMAT_PCM_S8,
39 		"AIFF (Apple/SGI 8 bit PCM)", "aiff"
40 		},
41 
42 	{	SF_FORMAT_AU | SF_FORMAT_PCM_16,
43 		"AU (Sun/Next 16 bit PCM)", "au"
44 		},
45 
46 	{	SF_FORMAT_AU | SF_FORMAT_ULAW,
47 		"AU (Sun/Next 8-bit u-law)", "au"
48 		},
49 
50 	{	SF_FORMAT_CAF | SF_FORMAT_ALAC_16,
51 		"CAF (Apple 16 bit ALAC)", "caf"
52 		},
53 
54 	{	SF_FORMAT_CAF | SF_FORMAT_PCM_16,
55 		"CAF (Apple 16 bit PCM)", "caf"
56 		},
57 
58 #if HAVE_EXTERNAL_XIPH_LIBS
59 	{	SF_FORMAT_FLAC | SF_FORMAT_PCM_16,
60 		"FLAC 16 bit", "flac"
61 		},
62 #endif
63 
64 	{	SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM,
65 		"OKI Dialogic VOX ADPCM", "vox"
66 		},
67 
68 #if HAVE_EXTERNAL_XIPH_LIBS
69 	{	SF_FORMAT_OGG | SF_FORMAT_OPUS,
70 		"Ogg Opus (Xiph Foundation)", "opus"
71 		},
72 
73 	{	SF_FORMAT_OGG | SF_FORMAT_VORBIS,
74 		"Ogg Vorbis (Xiph Foundation)", "ogg"
75 		},
76 #endif
77 
78 	{	SF_FORMAT_WAV | SF_FORMAT_PCM_16,
79 		"WAV (Microsoft 16 bit PCM)", "wav"
80 		},
81 
82 	{	SF_FORMAT_WAV | SF_FORMAT_FLOAT,
83 		"WAV (Microsoft 32 bit float)", "wav"
84 		},
85 
86 	{	SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM,
87 		"WAV (Microsoft 4 bit IMA ADPCM)", "wav"
88 		},
89 
90 	{	SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM,
91 		"WAV (Microsoft 4 bit MS ADPCM)", "wav"
92 		},
93 
94 	{	SF_FORMAT_WAV | SF_FORMAT_PCM_U8,
95 		"WAV (Microsoft 8 bit PCM)", "wav"
96 		},
97 
98 } ; /* simple_formats */
99 
100 int
psf_get_format_simple_count(void)101 psf_get_format_simple_count	(void)
102 {	return (sizeof (simple_formats) / sizeof (SF_FORMAT_INFO)) ;
103 } /* psf_get_format_simple_count */
104 
105 int
psf_get_format_simple(SF_FORMAT_INFO * data)106 psf_get_format_simple (SF_FORMAT_INFO *data)
107 {	int indx ;
108 
109 	if (data->format < 0 || data->format >= (SIGNED_SIZEOF (simple_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
110 		return SFE_BAD_COMMAND_PARAM ;
111 
112 	indx = data->format ;
113 	memcpy (data, &(simple_formats [indx]), SIGNED_SIZEOF (SF_FORMAT_INFO)) ;
114 
115 	return 0 ;
116 } /* psf_get_format_simple */
117 
118 /*============================================================================
119 ** Major format info.
120 */
121 
122 static SF_FORMAT_INFO const major_formats [] =
123 {
124 	{	SF_FORMAT_AIFF,		"AIFF (Apple/SGI)",						"aiff" 	},
125 	{	SF_FORMAT_AU,		"AU (Sun/NeXT)", 						"au"	},
126 	{	SF_FORMAT_AVR,		"AVR (Audio Visual Research)",			"avr"	},
127 	{	SF_FORMAT_CAF,		"CAF (Apple Core Audio File)",			"caf"	},
128 #if HAVE_EXTERNAL_XIPH_LIBS
129 	{	SF_FORMAT_FLAC,		"FLAC (Free Lossless Audio Codec)",		"flac"	},
130 #endif
131 	{	SF_FORMAT_HTK,		"HTK (HMM Tool Kit)",					"htk"	},
132 	{	SF_FORMAT_SVX,		"IFF (Amiga IFF/SVX8/SV16)",			"iff"	},
133 	{	SF_FORMAT_MAT4,		"MAT4 (GNU Octave 2.0 / Matlab 4.2)",	"mat"	},
134 	{	SF_FORMAT_MAT5,		"MAT5 (GNU Octave 2.1 / Matlab 5.0)",	"mat"	},
135 	{	SF_FORMAT_MPC2K,	"MPC (Akai MPC 2k)",					"mpc"	},
136 #if HAVE_EXTERNAL_XIPH_LIBS
137 	{	SF_FORMAT_OGG,		"OGG (OGG Container format)",			"oga"	},
138 #endif
139 	{	SF_FORMAT_PAF,		"PAF (Ensoniq PARIS)", 					"paf"	},
140 	{	SF_FORMAT_PVF,		"PVF (Portable Voice Format)",			"pvf"	},
141 	{	SF_FORMAT_RAW,		"RAW (header-less)",					"raw"	},
142 	{	SF_FORMAT_RF64,		"RF64 (RIFF 64)",						"rf64"	},
143 	{	SF_FORMAT_SD2,		"SD2 (Sound Designer II)", 				"sd2"	},
144 	{	SF_FORMAT_SDS,		"SDS (Midi Sample Dump Standard)", 		"sds"	},
145 	{	SF_FORMAT_IRCAM,	"SF (Berkeley/IRCAM/CARL)",				"sf"	},
146 	{	SF_FORMAT_VOC,		"VOC (Creative Labs)",					"voc"	},
147 	{	SF_FORMAT_W64,		"W64 (SoundFoundry WAVE 64)",			"w64"	},
148 	{	SF_FORMAT_WAV,		"WAV (Microsoft)",						"wav"	},
149 	{	SF_FORMAT_NIST,		"WAV (NIST Sphere)",					"wav"	},
150 	{	SF_FORMAT_WAVEX,	"WAVEX (Microsoft)",					"wav"	},
151 	{	SF_FORMAT_WVE,		"WVE (Psion Series 3)",					"wve"	},
152 	{	SF_FORMAT_XI,		"XI (FastTracker 2)",					"xi"	},
153 
154 } ; /* major_formats */
155 
156 int
psf_get_format_major_count(void)157 psf_get_format_major_count	(void)
158 {	return (sizeof (major_formats) / sizeof (SF_FORMAT_INFO)) ;
159 } /* psf_get_format_major_count */
160 
161 int
psf_get_format_major(SF_FORMAT_INFO * data)162 psf_get_format_major (SF_FORMAT_INFO *data)
163 {	int indx ;
164 
165 	if (data->format < 0 || data->format >= (SIGNED_SIZEOF (major_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
166 		return SFE_BAD_COMMAND_PARAM ;
167 
168 	indx = data->format ;
169 	memcpy (data, &(major_formats [indx]), SIGNED_SIZEOF (SF_FORMAT_INFO)) ;
170 
171 	return 0 ;
172 } /* psf_get_format_major */
173 
174 /*============================================================================
175 ** Subtype format info.
176 */
177 
178 static SF_FORMAT_INFO subtype_formats [] =
179 {
180 	{	SF_FORMAT_PCM_S8,		"Signed 8 bit PCM",		NULL 	},
181 	{	SF_FORMAT_PCM_16,		"Signed 16 bit PCM",	NULL 	},
182 	{	SF_FORMAT_PCM_24,		"Signed 24 bit PCM",	NULL 	},
183 	{	SF_FORMAT_PCM_32,		"Signed 32 bit PCM",	NULL 	},
184 
185 	{	SF_FORMAT_PCM_U8,		"Unsigned 8 bit PCM",	NULL 	},
186 
187 	{	SF_FORMAT_FLOAT,		"32 bit float",			NULL 	},
188 	{	SF_FORMAT_DOUBLE,		"64 bit float",			NULL 	},
189 
190 	{	SF_FORMAT_ULAW,			"U-Law",				NULL 	},
191 	{	SF_FORMAT_ALAW,			"A-Law",				NULL 	},
192 	{	SF_FORMAT_IMA_ADPCM,	"IMA ADPCM",			NULL 	},
193 	{	SF_FORMAT_MS_ADPCM,		"Microsoft ADPCM",		NULL 	},
194 
195 	{	SF_FORMAT_GSM610,		"GSM 6.10",				NULL 	},
196 
197 	{	SF_FORMAT_G721_32,		"32kbs G721 ADPCM",		NULL 	},
198 	{	SF_FORMAT_G723_24,		"24kbs G723 ADPCM",		NULL 	},
199 	{	SF_FORMAT_G723_40,		"40kbs G723 ADPCM",		NULL	},
200 
201 	{	SF_FORMAT_DWVW_12,		"12 bit DWVW",			NULL 	},
202 	{	SF_FORMAT_DWVW_16,		"16 bit DWVW",			NULL 	},
203 	{	SF_FORMAT_DWVW_24,		"24 bit DWVW",			NULL 	},
204 	{	SF_FORMAT_VOX_ADPCM,	"VOX ADPCM",			"vox" 	},
205 
206 	{	SF_FORMAT_NMS_ADPCM_16,	"16kbs NMS ADPCM",		NULL	},
207 	{	SF_FORMAT_NMS_ADPCM_24,	"24kbs NMS ADPCM",		NULL	},
208 	{	SF_FORMAT_NMS_ADPCM_32,	"32kbs NMS ADPCM",		NULL	},
209 
210 	{	SF_FORMAT_DPCM_16,		"16 bit DPCM",			NULL 	},
211 	{	SF_FORMAT_DPCM_8,		"8 bit DPCM",			NULL 	},
212 
213 #if HAVE_EXTERNAL_XIPH_LIBS
214 	{	SF_FORMAT_VORBIS,		"Vorbis",				NULL 	},
215 	{	SF_FORMAT_OPUS,			"Opus",					NULL 	},
216 #endif
217 
218 	{	SF_FORMAT_ALAC_16,		"16 bit ALAC",			NULL	},
219 	{	SF_FORMAT_ALAC_20,		"20 bit ALAC",			NULL	},
220 	{	SF_FORMAT_ALAC_24,		"24 bit ALAC",			NULL	},
221 	{	SF_FORMAT_ALAC_32,		"32 bit ALAC",			NULL	},
222 } ; /* subtype_formats */
223 
224 int
psf_get_format_subtype_count(void)225 psf_get_format_subtype_count	(void)
226 {	return (sizeof (subtype_formats) / sizeof (SF_FORMAT_INFO)) ;
227 } /* psf_get_format_subtype_count */
228 
229 int
psf_get_format_subtype(SF_FORMAT_INFO * data)230 psf_get_format_subtype (SF_FORMAT_INFO *data)
231 {	int indx ;
232 
233 	if (data->format < 0 || data->format >= (SIGNED_SIZEOF (subtype_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
234 	{	data->format = 0 ;
235 		return SFE_BAD_COMMAND_PARAM ;
236 		} ;
237 
238 	indx = data->format ;
239 	memcpy (data, &(subtype_formats [indx]), sizeof (SF_FORMAT_INFO)) ;
240 
241 	return 0 ;
242 } /* psf_get_format_subtype */
243 
244 /*==============================================================================
245 */
246 
247 int
psf_get_format_info(SF_FORMAT_INFO * data)248 psf_get_format_info (SF_FORMAT_INFO *data)
249 {	int k, format ;
250 
251 	if (SF_CONTAINER (data->format))
252 	{	format = SF_CONTAINER (data->format) ;
253 
254 		for (k = 0 ; k < (SIGNED_SIZEOF (major_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)) ; k++)
255 		{	if (format == major_formats [k].format)
256 			{	memcpy (data, &(major_formats [k]), sizeof (SF_FORMAT_INFO)) ;
257 				return 0 ;
258 				} ;
259 			} ;
260 		}
261 	else if (SF_CODEC (data->format))
262 	{	format = SF_CODEC (data->format) ;
263 
264 		for (k = 0 ; k < (SIGNED_SIZEOF (subtype_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)) ; k++)
265 		{	if (format == subtype_formats [k].format)
266 			{	memcpy (data, &(subtype_formats [k]), sizeof (SF_FORMAT_INFO)) ;
267 				return 0 ;
268 				} ;
269 			} ;
270 		} ;
271 
272 	memset (data, 0, sizeof (SF_FORMAT_INFO)) ;
273 
274 	return SFE_BAD_COMMAND_PARAM ;
275 } /* psf_get_format_info */
276 
277 /*==============================================================================
278 */
279 
280 double
psf_calc_signal_max(SF_PRIVATE * psf,int normalize)281 psf_calc_signal_max (SF_PRIVATE *psf, int normalize)
282 {	BUF_UNION	ubuf ;
283 	sf_count_t	position ;
284 	double 		max_val, temp, *data ;
285 	int			k, len, readcount, save_state ;
286 
287 	/* If the file is not seekable, there is nothing we can do. */
288 	if (! psf->sf.seekable)
289 	{	psf->error = SFE_NOT_SEEKABLE ;
290 		return	0.0 ;
291 		} ;
292 
293 	if (! psf->read_double)
294 	{	psf->error = SFE_UNIMPLEMENTED ;
295 		return	0.0 ;
296 		} ;
297 
298 	save_state = sf_command ((SNDFILE*) psf, SFC_GET_NORM_DOUBLE, NULL, 0) ;
299 	sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, normalize) ;
300 
301 	/* Brute force. Read the whole file and find the biggest sample. */
302 	/* Get current position in file */
303 	position = sf_seek ((SNDFILE*) psf, 0, SEEK_CUR) ;
304 	/* Go to start of file. */
305 	sf_seek ((SNDFILE*) psf, 0, SEEK_SET) ;
306 
307 	data = ubuf.dbuf ;
308 	/* Make sure len is an integer multiple of the channel count. */
309 	len = ARRAY_LEN (ubuf.dbuf) - (ARRAY_LEN (ubuf.dbuf) % psf->sf.channels) ;
310 
311 	for (readcount = 1, max_val = 0.0 ; readcount > 0 ; /* nothing */)
312 	{	readcount = sf_read_double ((SNDFILE*) psf, data, len) ;
313 		for (k = 0 ; k < readcount ; k++)
314 		{	temp = fabs (data [k]) ;
315 			max_val = temp > max_val ? temp : max_val ;
316 			} ;
317 		} ;
318 
319 	/* Return to SNDFILE to original state. */
320 	sf_seek ((SNDFILE*) psf, position, SEEK_SET) ;
321 	sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, save_state) ;
322 
323 	return	max_val ;
324 } /* psf_calc_signal_max */
325 
326 int
psf_calc_max_all_channels(SF_PRIVATE * psf,double * peaks,int normalize)327 psf_calc_max_all_channels (SF_PRIVATE *psf, double *peaks, int normalize)
328 {	BUF_UNION	ubuf ;
329 	sf_count_t	position ;
330 	double 		temp, *data ;
331 	int			k, len, readcount, save_state ;
332 	int			chan ;
333 
334 	/* If the file is not seekable, there is nothing we can do. */
335 	if (! psf->sf.seekable)
336 		return (psf->error = SFE_NOT_SEEKABLE) ;
337 
338 	if (! psf->read_double)
339 		return (psf->error = SFE_UNIMPLEMENTED) ;
340 
341 	save_state = sf_command ((SNDFILE*) psf, SFC_GET_NORM_DOUBLE, NULL, 0) ;
342 	sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, normalize) ;
343 
344 	memset (peaks, 0, sizeof (double) * psf->sf.channels) ;
345 
346 	/* Brute force. Read the whole file and find the biggest sample for each channel. */
347 	position = sf_seek ((SNDFILE*) psf, 0, SEEK_CUR) ; /* Get current position in file */
348 	sf_seek ((SNDFILE*) psf, 0, SEEK_SET) ;			/* Go to start of file. */
349 
350 	len = ARRAY_LEN (ubuf.dbuf) - (ARRAY_LEN (ubuf.dbuf) % psf->sf.channels) ;
351 
352 	data = ubuf.dbuf ;
353 
354 	chan = 0 ;
355 	readcount = len ;
356 	while (readcount > 0)
357 	{	readcount = sf_read_double ((SNDFILE*) psf, data, len) ;
358 		for (k = 0 ; k < readcount ; k++)
359 		{	temp = fabs (data [k]) ;
360 			peaks [chan] = temp > peaks [chan] ? temp : peaks [chan] ;
361 			chan = (chan + 1) % psf->sf.channels ;
362 			} ;
363 		} ;
364 
365 	sf_seek ((SNDFILE*) psf, position, SEEK_SET) ;		/* Return to original position. */
366 
367 	sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, save_state) ;
368 
369 	return	0 ;
370 } /* psf_calc_max_all_channels */
371 
372 int
psf_get_signal_max(SF_PRIVATE * psf,double * peak)373 psf_get_signal_max (SF_PRIVATE *psf, double *peak)
374 {	int k ;
375 
376 	if (psf->peak_info == NULL)
377 		return SF_FALSE ;
378 
379 	peak [0] = psf->peak_info->peaks [0].value ;
380 
381 	for (k = 1 ; k < psf->sf.channels ; k++)
382 		peak [0] = SF_MAX (peak [0], psf->peak_info->peaks [k].value) ;
383 
384 	return SF_TRUE ;
385 } /* psf_get_signal_max */
386 
387 int
psf_get_max_all_channels(SF_PRIVATE * psf,double * peaks)388 psf_get_max_all_channels (SF_PRIVATE *psf, double *peaks)
389 {	int k ;
390 
391 	if (psf->peak_info == NULL)
392 		return SF_FALSE ;
393 
394 	for (k = 0 ; k < psf->sf.channels ; k++)
395 		peaks [k] = psf->peak_info->peaks [k].value ;
396 
397 	return SF_TRUE ;
398 } /* psf_get_max_all_channels */
399 
400 
401