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