1b815c7f3Sopenharmony_ci/*
2b815c7f3Sopenharmony_ci** Copyright (C) 2002-2014 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 <string.h>
24b815c7f3Sopenharmony_ci#include <math.h>
25b815c7f3Sopenharmony_ci
26b815c7f3Sopenharmony_ci#if HAVE_UNISTD_H
27b815c7f3Sopenharmony_ci#include <unistd.h>
28b815c7f3Sopenharmony_ci#else
29b815c7f3Sopenharmony_ci#include "sf_unistd.h"
30b815c7f3Sopenharmony_ci#endif
31b815c7f3Sopenharmony_ci
32b815c7f3Sopenharmony_ci#include <sndfile.h>
33b815c7f3Sopenharmony_ci
34b815c7f3Sopenharmony_ci#include "utils.h"
35b815c7f3Sopenharmony_ci
36b815c7f3Sopenharmony_ci#define	BUFFER_SIZE		(10000)
37b815c7f3Sopenharmony_ci
38b815c7f3Sopenharmony_ci#ifndef		M_PI
39b815c7f3Sopenharmony_ci#define		M_PI		3.14159265358979323846264338
40b815c7f3Sopenharmony_ci#endif
41b815c7f3Sopenharmony_ci
42b815c7f3Sopenharmony_cistatic	void	dwvw_test (const char *filename, int format, int bit_width) ;
43b815c7f3Sopenharmony_ci
44b815c7f3Sopenharmony_ciint
45b815c7f3Sopenharmony_cimain (void)
46b815c7f3Sopenharmony_ci{
47b815c7f3Sopenharmony_ci	dwvw_test ("dwvw12.raw", SF_FORMAT_RAW | SF_FORMAT_DWVW_12, 12) ;
48b815c7f3Sopenharmony_ci	dwvw_test ("dwvw16.raw", SF_FORMAT_RAW | SF_FORMAT_DWVW_16, 16) ;
49b815c7f3Sopenharmony_ci	dwvw_test ("dwvw24.raw", SF_FORMAT_RAW | SF_FORMAT_DWVW_24, 24) ;
50b815c7f3Sopenharmony_ci
51b815c7f3Sopenharmony_ci	return 0 ;
52b815c7f3Sopenharmony_ci} /* main */
53b815c7f3Sopenharmony_ci
54b815c7f3Sopenharmony_cistatic void
55b815c7f3Sopenharmony_cidwvw_test (const char *filename, int format, int bit_width)
56b815c7f3Sopenharmony_ci{	static	int		write_buf [BUFFER_SIZE] ;
57b815c7f3Sopenharmony_ci	static	int		read_buf [BUFFER_SIZE] ;
58b815c7f3Sopenharmony_ci
59b815c7f3Sopenharmony_ci	SNDFILE	*file ;
60b815c7f3Sopenharmony_ci	SF_INFO sfinfo ;
61b815c7f3Sopenharmony_ci	double 	value ;
62b815c7f3Sopenharmony_ci	int		k, bit_mask ;
63b815c7f3Sopenharmony_ci
64b815c7f3Sopenharmony_ci	srand (123456) ;
65b815c7f3Sopenharmony_ci
66b815c7f3Sopenharmony_ci	/* Only want to grab the top bit_width bits. */
67b815c7f3Sopenharmony_ci	bit_mask = arith_shift_left (-1, 32 - bit_width) ;
68b815c7f3Sopenharmony_ci
69b815c7f3Sopenharmony_ci	print_test_name ("dwvw_test", filename) ;
70b815c7f3Sopenharmony_ci
71b815c7f3Sopenharmony_ci	sf_info_setup (&sfinfo, format, 44100, 1) ;
72b815c7f3Sopenharmony_ci
73b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
74b815c7f3Sopenharmony_ci
75b815c7f3Sopenharmony_ci	/* Generate random.frames. */
76b815c7f3Sopenharmony_ci	for (k = 0 ; k < BUFFER_SIZE / 2 ; k++)
77b815c7f3Sopenharmony_ci	{	value = 0x7FFFFFFF * sin (123.0 / sfinfo.samplerate * 2 * k * M_PI) ;
78b815c7f3Sopenharmony_ci		write_buf [k] = bit_mask & lrint (value) ;
79b815c7f3Sopenharmony_ci		} ;
80b815c7f3Sopenharmony_ci
81b815c7f3Sopenharmony_ci	for ( ; k < BUFFER_SIZE ; k++)
82b815c7f3Sopenharmony_ci		write_buf [k] = bit_mask & (arith_shift_left (rand (), 11) ^ (rand () >> 11)) ;
83b815c7f3Sopenharmony_ci
84b815c7f3Sopenharmony_ci	sf_write_int (file, write_buf, BUFFER_SIZE) ;
85b815c7f3Sopenharmony_ci	sf_close (file) ;
86b815c7f3Sopenharmony_ci
87b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
88b815c7f3Sopenharmony_ci
89b815c7f3Sopenharmony_ci	if ((k = (int) sf_read_int (file, read_buf, BUFFER_SIZE)) != BUFFER_SIZE)
90b815c7f3Sopenharmony_ci	{	printf ("Error (line %d) : Only read %d/%d.frames.\n", __LINE__, k, BUFFER_SIZE) ;
91b815c7f3Sopenharmony_ci		exit (1) ;
92b815c7f3Sopenharmony_ci		}
93b815c7f3Sopenharmony_ci
94b815c7f3Sopenharmony_ci	for (k = 0 ; k < BUFFER_SIZE ; k++)
95b815c7f3Sopenharmony_ci	{	if (read_buf [k] != write_buf [k])
96b815c7f3Sopenharmony_ci		{	printf ("Error (line %d) : %d != %d at position %d/%d\n", __LINE__,
97b815c7f3Sopenharmony_ci				write_buf [k] >> (32 - bit_width), read_buf [k] >> (32 - bit_width),
98b815c7f3Sopenharmony_ci				k, BUFFER_SIZE) ;
99b815c7f3Sopenharmony_ci			oct_save_int (write_buf, read_buf, BUFFER_SIZE) ;
100b815c7f3Sopenharmony_ci			exit (1) ;
101b815c7f3Sopenharmony_ci			} ;
102b815c7f3Sopenharmony_ci		} ;
103b815c7f3Sopenharmony_ci
104b815c7f3Sopenharmony_ci	sf_close (file) ;
105b815c7f3Sopenharmony_ci
106b815c7f3Sopenharmony_ci	unlink (filename) ;
107b815c7f3Sopenharmony_ci	printf ("ok\n") ;
108b815c7f3Sopenharmony_ci
109b815c7f3Sopenharmony_ci	return ;
110b815c7f3Sopenharmony_ci} /* dwvw_test */
111b815c7f3Sopenharmony_ci
112