1b815c7f3Sopenharmony_ci/*
2b815c7f3Sopenharmony_ci** Copyright (C) 2001-2015 Erik de Castro Lopo <erikd@mega-nerd.com>
3b815c7f3Sopenharmony_ci**
4b815c7f3Sopenharmony_ci** This program is free software; you can redistribute it and/or modify
5b815c7f3Sopenharmony_ci** it under the terms of the GNU General Public License as published by
6b815c7f3Sopenharmony_ci** the Free Software Foundation; either version 2 of the License, or
7b815c7f3Sopenharmony_ci** (at your option) any later version.
8b815c7f3Sopenharmony_ci**
9b815c7f3Sopenharmony_ci** This program is distributed in the hope that it will be useful,
10b815c7f3Sopenharmony_ci** but WITHOUT ANY WARRANTY; without even the implied warranty of
11b815c7f3Sopenharmony_ci** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12b815c7f3Sopenharmony_ci** GNU General Public License for more details.
13b815c7f3Sopenharmony_ci**
14b815c7f3Sopenharmony_ci** You should have received a copy of the GNU General Public License
15b815c7f3Sopenharmony_ci** along with this program; if not, write to the Free Software
16b815c7f3Sopenharmony_ci** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17b815c7f3Sopenharmony_ci*/
18b815c7f3Sopenharmony_ci
19b815c7f3Sopenharmony_ci#include "sfconfig.h"
20b815c7f3Sopenharmony_ci
21b815c7f3Sopenharmony_ci#include <stdio.h>
22b815c7f3Sopenharmony_ci#include <stdlib.h>
23b815c7f3Sopenharmony_ci#include <stdint.h>
24b815c7f3Sopenharmony_ci#include <inttypes.h>
25b815c7f3Sopenharmony_ci#include <string.h>
26b815c7f3Sopenharmony_ci#include <time.h>
27b815c7f3Sopenharmony_ci
28b815c7f3Sopenharmony_ci#if HAVE_UNISTD_H
29b815c7f3Sopenharmony_ci#include <unistd.h>
30b815c7f3Sopenharmony_ci#else
31b815c7f3Sopenharmony_ci#include "sf_unistd.h"
32b815c7f3Sopenharmony_ci#endif
33b815c7f3Sopenharmony_ci
34b815c7f3Sopenharmony_ci#include <math.h>
35b815c7f3Sopenharmony_ci
36b815c7f3Sopenharmony_ci#include <sndfile.h>
37b815c7f3Sopenharmony_ci
38b815c7f3Sopenharmony_ci#include "utils.h"
39b815c7f3Sopenharmony_ci
40b815c7f3Sopenharmony_ci#define	BUFFER_LEN		(1 << 10)
41b815c7f3Sopenharmony_ci#define LOG_BUFFER_SIZE	1024
42b815c7f3Sopenharmony_ci
43b815c7f3Sopenharmony_cistatic	void	channel_test			(void) ;
44b815c7f3Sopenharmony_cistatic	double	max_diff		(const float *a, const float *b, unsigned int len, unsigned int * position) ;
45b815c7f3Sopenharmony_ci
46b815c7f3Sopenharmony_ciint
47b815c7f3Sopenharmony_cimain (void) // int argc, char *argv [])
48b815c7f3Sopenharmony_ci{	channel_test () ;
49b815c7f3Sopenharmony_ci	return 0 ;
50b815c7f3Sopenharmony_ci} /* main */
51b815c7f3Sopenharmony_ci
52b815c7f3Sopenharmony_ci/*============================================================================================
53b815c7f3Sopenharmony_ci**	Here are the test functions.
54b815c7f3Sopenharmony_ci*/
55b815c7f3Sopenharmony_ci
56b815c7f3Sopenharmony_cistatic void
57b815c7f3Sopenharmony_cichannel_test (void)
58b815c7f3Sopenharmony_ci{	static float	float_data [1024] ;
59b815c7f3Sopenharmony_ci	static float	read_float [1024] ;
60b815c7f3Sopenharmony_ci	static int		read_int [1024] ;
61b815c7f3Sopenharmony_ci	static short	read_short [1024] ;
62b815c7f3Sopenharmony_ci	unsigned int	ch, k, position = 0 ;
63b815c7f3Sopenharmony_ci
64b815c7f3Sopenharmony_ci	gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9) ;
65b815c7f3Sopenharmony_ci
66b815c7f3Sopenharmony_ci	for (ch = 1 ; ch <= 8 ; ch++)
67b815c7f3Sopenharmony_ci	{	SNDFILE	*file ;
68b815c7f3Sopenharmony_ci		SF_INFO	wsfinfo, rsfinfo ;
69b815c7f3Sopenharmony_ci		sf_count_t wframes = ARRAY_LEN (float_data) / ch ;
70b815c7f3Sopenharmony_ci		double	maxdiff ;
71b815c7f3Sopenharmony_ci		char	filename [256] ;
72b815c7f3Sopenharmony_ci
73b815c7f3Sopenharmony_ci		snprintf (filename, sizeof (filename), "chan_%d.wav", ch) ;
74b815c7f3Sopenharmony_ci		print_test_name (__func__, filename) ;
75b815c7f3Sopenharmony_ci
76b815c7f3Sopenharmony_ci		sf_info_setup (&wsfinfo, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 48000, ch) ;
77b815c7f3Sopenharmony_ci		sf_info_clear (&rsfinfo) ;
78b815c7f3Sopenharmony_ci
79b815c7f3Sopenharmony_ci		/* Write the test file. */
80b815c7f3Sopenharmony_ci		file = test_open_file_or_die (filename, SFM_WRITE, &wsfinfo, SF_FALSE, __LINE__) ;
81b815c7f3Sopenharmony_ci		test_writef_float_or_die (file, 0, float_data, wframes, __LINE__) ;
82b815c7f3Sopenharmony_ci		sf_close (file) ;
83b815c7f3Sopenharmony_ci
84b815c7f3Sopenharmony_ci		/* Read it as float and test. */
85b815c7f3Sopenharmony_ci		file = test_open_file_or_die (filename, SFM_READ, &rsfinfo, SF_FALSE, __LINE__) ;
86b815c7f3Sopenharmony_ci		exit_if_true (rsfinfo.frames == 0,
87b815c7f3Sopenharmony_ci				"\n\nLine %d : Frames in file %" PRId64 ".\n\n", __LINE__, rsfinfo.frames) ;
88b815c7f3Sopenharmony_ci		exit_if_true (wframes != rsfinfo.frames,
89b815c7f3Sopenharmony_ci				"\n\nLine %d : Wrote %" PRId64 ", read %" PRId64 " frames.\n\n", __LINE__, wframes, rsfinfo.frames) ;
90b815c7f3Sopenharmony_ci
91b815c7f3Sopenharmony_ci		sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
92b815c7f3Sopenharmony_ci
93b815c7f3Sopenharmony_ci		test_readf_float_or_die (file, 0, read_float, rsfinfo.frames, __LINE__) ;
94b815c7f3Sopenharmony_ci		compare_float_or_die (float_data, read_float, ch * rsfinfo.frames, __LINE__) ;
95b815c7f3Sopenharmony_ci
96b815c7f3Sopenharmony_ci		/* Read it as short and test. */
97b815c7f3Sopenharmony_ci		test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
98b815c7f3Sopenharmony_ci		test_readf_short_or_die (file, 0, read_short, rsfinfo.frames, __LINE__) ;
99b815c7f3Sopenharmony_ci
100b815c7f3Sopenharmony_ci		for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
101b815c7f3Sopenharmony_ci			read_float [k] = read_short [k] * (0.9 / 0x8000) ;
102b815c7f3Sopenharmony_ci
103b815c7f3Sopenharmony_ci		maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ;
104b815c7f3Sopenharmony_ci		exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ;
105b815c7f3Sopenharmony_ci
106b815c7f3Sopenharmony_ci		/* Read it as int and test. */
107b815c7f3Sopenharmony_ci		test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
108b815c7f3Sopenharmony_ci		test_readf_int_or_die (file, 0, read_int, rsfinfo.frames, __LINE__) ;
109b815c7f3Sopenharmony_ci
110b815c7f3Sopenharmony_ci		for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
111b815c7f3Sopenharmony_ci			read_float [k] = read_int [k] * (0.9 / 0x80000000) ;
112b815c7f3Sopenharmony_ci
113b815c7f3Sopenharmony_ci		maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ;
114b815c7f3Sopenharmony_ci		exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ;
115b815c7f3Sopenharmony_ci
116b815c7f3Sopenharmony_ci		sf_close (file) ;
117b815c7f3Sopenharmony_ci		unlink (filename) ;
118b815c7f3Sopenharmony_ci		printf ("ok\n") ;
119b815c7f3Sopenharmony_ci		} ;
120b815c7f3Sopenharmony_ci
121b815c7f3Sopenharmony_ci	return ;
122b815c7f3Sopenharmony_ci} /* channel_test */
123b815c7f3Sopenharmony_ci
124b815c7f3Sopenharmony_cistatic double
125b815c7f3Sopenharmony_cimax_diff (const float *a, const float *b, unsigned int len, unsigned int * position)
126b815c7f3Sopenharmony_ci{	double mdiff = 0.0, diff ;
127b815c7f3Sopenharmony_ci	unsigned int k ;
128b815c7f3Sopenharmony_ci
129b815c7f3Sopenharmony_ci	for (k = 0 ; k < len ; k++)
130b815c7f3Sopenharmony_ci	{	diff = fabs (a [k] - b [k]) ;
131b815c7f3Sopenharmony_ci		if (diff > mdiff)
132b815c7f3Sopenharmony_ci		{ 	mdiff = diff ;
133b815c7f3Sopenharmony_ci			*position = k ;
134b815c7f3Sopenharmony_ci			} ;
135b815c7f3Sopenharmony_ci		} ;
136b815c7f3Sopenharmony_ci
137b815c7f3Sopenharmony_ci	return mdiff ;
138b815c7f3Sopenharmony_ci} /* max_diff */
139