xref: /third_party/libsnd/tests/aiff_rw_test.c (revision b815c7f3)
1b815c7f3Sopenharmony_ci/*
2b815c7f3Sopenharmony_ci** Copyright (C) 2003-2017 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#if HAVE_UNISTD_H
25b815c7f3Sopenharmony_ci#include <unistd.h>
26b815c7f3Sopenharmony_ci#else
27b815c7f3Sopenharmony_ci#include "sf_unistd.h"
28b815c7f3Sopenharmony_ci#endif
29b815c7f3Sopenharmony_ci#include <math.h>
30b815c7f3Sopenharmony_ci#include <inttypes.h>
31b815c7f3Sopenharmony_ci
32b815c7f3Sopenharmony_ci
33b815c7f3Sopenharmony_ci#include <sndfile.h>
34b815c7f3Sopenharmony_ci
35b815c7f3Sopenharmony_ci#include "utils.h"
36b815c7f3Sopenharmony_ci
37b815c7f3Sopenharmony_ci
38b815c7f3Sopenharmony_cistatic unsigned char aifc_data [] =
39b815c7f3Sopenharmony_ci{	'F' , 'O' , 'R' , 'M' ,
40b815c7f3Sopenharmony_ci	0x00, 0x00, 0x01, 0xE8, /* FORM length */
41b815c7f3Sopenharmony_ci
42b815c7f3Sopenharmony_ci	'A' , 'I' , 'F' , 'C' ,
43b815c7f3Sopenharmony_ci	0x43, 0x4F, 0x4D, 0x4D, /* COMM */
44b815c7f3Sopenharmony_ci	0x00, 0x00, 0x00, 0x26, /* COMM length */
45b815c7f3Sopenharmony_ci	0x00, 0x01, 0x00, 0x00, 0x00, 0xAE, 0x00, 0x10, 0x40, 0x0D, 0xAC, 0x44,
46b815c7f3Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x4F, 0x4E, 0x45, 0x0D, 'N' ,
47b815c7f3Sopenharmony_ci	'o' , 't' , ' ' , 'c' , 'o' , 'm' , 'p' , 'r' , 'e' , 's' , 's' , 'e' ,
48b815c7f3Sopenharmony_ci	'd' , 0x00,
49b815c7f3Sopenharmony_ci
50b815c7f3Sopenharmony_ci	'F' , 'V' , 'E' , 'R' , 0x00, 0x00, 0x00, 0x04, 0xA2, 0x80, 0x51, 0x40,
51b815c7f3Sopenharmony_ci
52b815c7f3Sopenharmony_ci	/* A 'MARK' chunk. */
53b815c7f3Sopenharmony_ci	'M' , 'A' , 'R' , 'K' , 0x00, 0x00, 0x00, 0x36, 0x00, 0x05,
54b815c7f3Sopenharmony_ci	0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 'A' ,
55b815c7f3Sopenharmony_ci	0x00, 0x02, 0x00, 0x00, 0x11, 0x3A, 0x02, 'B' , 'C' , 0x00,
56b815c7f3Sopenharmony_ci	0x00, 0x03, 0x00, 0x00, 0x22, 0x74, 0x03, 'D' , 'E' , 'F',
57b815c7f3Sopenharmony_ci	0x00, 0x04, 0x00, 0x00, 0x33, 0xAE, 0x04, 'G' , 'H' , 'I', 'J' , 0x00,
58b815c7f3Sopenharmony_ci	0x00, 0x05, 0x00, 0x00, 0x44, 0xE8, 0x05, 'K' , 'L' , 'M', 'N' , 'O' ,
59b815c7f3Sopenharmony_ci
60b815c7f3Sopenharmony_ci	'S' , 'S' , 'N' , 'D' ,
61b815c7f3Sopenharmony_ci	0x00, 0x00, 0x01, 0x64, /* SSND length */
62b815c7f3Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63b815c7f3Sopenharmony_ci	0xFF, 0xE0, 0xFF, 0xDB, 0xFF, 0xD0, 0xFF, 0xD5, 0xFF, 0xD6, 0xFF, 0xD0,
64b815c7f3Sopenharmony_ci	0xFF, 0xBF, 0xFF, 0xBE, 0xFF, 0xB9, 0xFF, 0xC8, 0xFF, 0xBF, 0xFF, 0xD5,
65b815c7f3Sopenharmony_ci	0xFF, 0xC3, 0xFF, 0xBF, 0xFF, 0xB3, 0xFF, 0xBE, 0xFF, 0xB4, 0xFF, 0xAD,
66b815c7f3Sopenharmony_ci	0xFF, 0xAC, 0xFF, 0xAF, 0xFF, 0xB9, 0xFF, 0xB3, 0xFF, 0xA4, 0xFF, 0xA5,
67b815c7f3Sopenharmony_ci	0xFF, 0x93, 0xFF, 0x95, 0xFF, 0x97, 0xFF, 0x98, 0xFF, 0x99, 0xFF, 0x9E,
68b815c7f3Sopenharmony_ci	0xFF, 0x90, 0xFF, 0x80, 0xFF, 0x81, 0xFF, 0x7C, 0xFF, 0x80, 0xFF, 0x7C,
69b815c7f3Sopenharmony_ci	0xFF, 0x72, 0xFF, 0x72, 0xFF, 0x6C, 0xFF, 0x75, 0xFF, 0x6E, 0xFF, 0x6F,
70b815c7f3Sopenharmony_ci	0xFF, 0x66, 0xFF, 0x62, 0xFF, 0x5C, 0xFF, 0x64, 0xFF, 0x50, 0xFF, 0x56,
71b815c7f3Sopenharmony_ci	0xFF, 0x56, 0xFF, 0x4A, 0xFF, 0x4A, 0xFF, 0x49, 0xFF, 0x44, 0xFF, 0x49,
72b815c7f3Sopenharmony_ci	0xFF, 0x3B, 0xFF, 0x3F, 0xFF, 0x48, 0xFF, 0x46, 0xFF, 0x42, 0xFF, 0x49,
73b815c7f3Sopenharmony_ci	0xFF, 0x43, 0xFF, 0x36, 0xFF, 0x40, 0xFF, 0x35, 0xFF, 0x3F, 0xFF, 0x36,
74b815c7f3Sopenharmony_ci	0xFF, 0x37, 0xFF, 0x2E, 0xFF, 0x23, 0xFF, 0x23, 0xFF, 0x21, 0xFF, 0x1F,
75b815c7f3Sopenharmony_ci	0xFF, 0x25, 0xFF, 0x2C, 0xFF, 0x1E, 0xFF, 0x22, 0xFF, 0x24, 0xFF, 0x2B,
76b815c7f3Sopenharmony_ci	0xFF, 0x35, 0xFF, 0x27, 0xFF, 0x2E, 0xFF, 0x21, 0xFF, 0x18, 0xFF, 0x21,
77b815c7f3Sopenharmony_ci	0xFF, 0x20, 0xFF, 0x0F, 0xFF, 0x21, 0xFF, 0x1A, 0xFF, 0x10, 0xFF, 0x09,
78b815c7f3Sopenharmony_ci	0xFF, 0x1E, 0xFF, 0x19, 0xFF, 0x21, 0xFF, 0x13, 0xFF, 0x1B, 0xFF, 0x18,
79b815c7f3Sopenharmony_ci	0xFF, 0x21, 0xFF, 0x0F, 0xFF, 0x1A, 0xFF, 0x16, 0xFF, 0x21, 0xFF, 0x1B,
80b815c7f3Sopenharmony_ci	0xFF, 0x1B, 0xFF, 0x23, 0xFF, 0x1A, 0xFF, 0x21, 0xFF, 0x26, 0xFF, 0x23,
81b815c7f3Sopenharmony_ci	0xFF, 0x26, 0xFF, 0x27, 0xFF, 0x30, 0xFF, 0x27, 0xFF, 0x2F, 0xFF, 0x28,
82b815c7f3Sopenharmony_ci	0xFF, 0x2C, 0xFF, 0x27, 0xFF, 0x33, 0xFF, 0x29, 0xFF, 0x33, 0xFF, 0x3A,
83b815c7f3Sopenharmony_ci	0xFF, 0x42, 0xFF, 0x3B, 0xFF, 0x4D, 0xFF, 0x4B, 0xFF, 0x4D, 0xFF, 0x4A,
84b815c7f3Sopenharmony_ci	0xFF, 0x67, 0xFF, 0x77, 0xFF, 0x73, 0xFF, 0x7B, 0xFF, 0xDE, 0xFF, 0xAD,
85b815c7f3Sopenharmony_ci	0x00, 0x4A, 0x00, 0x63, 0xEC, 0x8C, 0x03, 0xBB, 0x0E, 0xE4, 0x08, 0xF2,
86b815c7f3Sopenharmony_ci	0x00, 0x70, 0xE3, 0xD1, 0xE5, 0xE4, 0x01, 0x6E, 0x0A, 0x67, 0x1C, 0x74,
87b815c7f3Sopenharmony_ci	0xF8, 0x8E, 0x10, 0x7B, 0xEA, 0x3C, 0x09, 0x87, 0x1B, 0x24, 0xEF, 0x05,
88b815c7f3Sopenharmony_ci	0x17, 0x76, 0x0D, 0x5B, 0x02, 0x43, 0xF5, 0xEF, 0x0C, 0x1D, 0xF7, 0x61,
89b815c7f3Sopenharmony_ci	0x05, 0x95, 0x0B, 0xC2, 0xF1, 0x69, 0x1A, 0xA1, 0xEC, 0x75, 0xF4, 0x11,
90b815c7f3Sopenharmony_ci	0x13, 0x4F, 0x13, 0x71, 0xFA, 0x33, 0xEC, 0x32, 0xC8, 0xCF, 0x05, 0xB0,
91b815c7f3Sopenharmony_ci	0x0B, 0x61, 0x33, 0x19, 0xCE, 0x37, 0xEF, 0xD4, 0x21, 0x9D, 0xFA, 0xAE,
92b815c7f3Sopenharmony_ci} ;
93b815c7f3Sopenharmony_ci
94b815c7f3Sopenharmony_cistatic void rw_test (const char *filename) ;
95b815c7f3Sopenharmony_ci
96b815c7f3Sopenharmony_ciint
97b815c7f3Sopenharmony_cimain (void)
98b815c7f3Sopenharmony_ci{	const char *filename = "rw.aifc" ;
99b815c7f3Sopenharmony_ci
100b815c7f3Sopenharmony_ci	print_test_name ("aiff_rw_test", filename) ;
101b815c7f3Sopenharmony_ci
102b815c7f3Sopenharmony_ci	dump_data_to_file (filename, aifc_data, sizeof (aifc_data)) ;
103b815c7f3Sopenharmony_ci
104b815c7f3Sopenharmony_ci	rw_test (filename) ;
105b815c7f3Sopenharmony_ci
106b815c7f3Sopenharmony_ci	unlink (filename) ;
107b815c7f3Sopenharmony_ci
108b815c7f3Sopenharmony_ci	puts ("ok") ;
109b815c7f3Sopenharmony_ci	return 0 ;
110b815c7f3Sopenharmony_ci} /* main */
111b815c7f3Sopenharmony_ci
112b815c7f3Sopenharmony_ci/*==============================================================================
113b815c7f3Sopenharmony_ci*/
114b815c7f3Sopenharmony_ci
115b815c7f3Sopenharmony_cistatic void
116b815c7f3Sopenharmony_cirw_test (const char *filename)
117b815c7f3Sopenharmony_ci{	SNDFILE *file ;
118b815c7f3Sopenharmony_ci	SF_INFO	sfinfo_rd, sfinfo_rw ;
119b815c7f3Sopenharmony_ci
120b815c7f3Sopenharmony_ci	memset (&sfinfo_rd, 0, sizeof (sfinfo_rd)) ;
121b815c7f3Sopenharmony_ci	memset (&sfinfo_rw, 0, sizeof (sfinfo_rw)) ;
122b815c7f3Sopenharmony_ci
123b815c7f3Sopenharmony_ci	/* Open the file in read only mode and fill in the SF_INFO struct. */
124b815c7f3Sopenharmony_ci	if ((file = sf_open (filename, SFM_READ, &sfinfo_rd)) == NULL)
125b815c7f3Sopenharmony_ci	{	printf ("\n\nLine %d : sf_open SFM_READ failed : %s\n\n", __LINE__, sf_strerror (NULL)) ;
126b815c7f3Sopenharmony_ci		exit (1) ;
127b815c7f3Sopenharmony_ci		} ;
128b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
129b815c7f3Sopenharmony_ci	sf_close (file) ;
130b815c7f3Sopenharmony_ci
131b815c7f3Sopenharmony_ci	/* Now open read/write and close the file. */
132b815c7f3Sopenharmony_ci	if ((file = sf_open (filename, SFM_RDWR, &sfinfo_rw)) == NULL)
133b815c7f3Sopenharmony_ci	{	printf ("\n\nLine %d : sf_open SFM_RDWR failed : %s\n\n", __LINE__, sf_strerror (NULL)) ;
134b815c7f3Sopenharmony_ci		exit (1) ;
135b815c7f3Sopenharmony_ci		} ;
136b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
137b815c7f3Sopenharmony_ci	sf_close (file) ;
138b815c7f3Sopenharmony_ci
139b815c7f3Sopenharmony_ci	/* Open again as read only again and fill in a new SF_INFO struct.  */
140b815c7f3Sopenharmony_ci	memset (&sfinfo_rw, 0, sizeof (sfinfo_rw)) ;
141b815c7f3Sopenharmony_ci	if ((file = sf_open (filename, SFM_READ, &sfinfo_rw)) == NULL)
142b815c7f3Sopenharmony_ci	{	printf ("\n\nLine %d : sf_open SFM_RDWR failed : %s\n\n", __LINE__, sf_strerror (NULL)) ;
143b815c7f3Sopenharmony_ci		exit (1) ;
144b815c7f3Sopenharmony_ci		} ;
145b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
146b815c7f3Sopenharmony_ci	sf_close (file) ;
147b815c7f3Sopenharmony_ci
148b815c7f3Sopenharmony_ci	/* Now compare the two. */
149b815c7f3Sopenharmony_ci	if (sfinfo_rd.format != sfinfo_rw.format)
150b815c7f3Sopenharmony_ci	{	printf ("\n\nLine %d : format mismatch (0x%08X != 0x%08X).\n\n", __LINE__,
151b815c7f3Sopenharmony_ci			sfinfo_rd.format, sfinfo_rw.format) ;
152b815c7f3Sopenharmony_ci		exit (1) ;
153b815c7f3Sopenharmony_ci		} ;
154b815c7f3Sopenharmony_ci
155b815c7f3Sopenharmony_ci	if (sfinfo_rd.channels != sfinfo_rw.channels)
156b815c7f3Sopenharmony_ci	{	printf ("\n\nLine %d : channel count mismatch (%d != %d).\n\n", __LINE__,
157b815c7f3Sopenharmony_ci			sfinfo_rd.channels, sfinfo_rw.channels) ;
158b815c7f3Sopenharmony_ci		exit (1) ;
159b815c7f3Sopenharmony_ci		} ;
160b815c7f3Sopenharmony_ci
161b815c7f3Sopenharmony_ci	if (sfinfo_rd.frames != sfinfo_rw.frames)
162b815c7f3Sopenharmony_ci	{	printf ("\n\nLine %d : frame count mismatch (rd %" PRId64 " != rw %" PRId64 ").\n\n", __LINE__,
163b815c7f3Sopenharmony_ci			sfinfo_rd.frames, sfinfo_rw.frames) ;
164b815c7f3Sopenharmony_ci		exit (1) ;
165b815c7f3Sopenharmony_ci		} ;
166b815c7f3Sopenharmony_ci
167b815c7f3Sopenharmony_ci	return ;
168b815c7f3Sopenharmony_ci} /* rw_test */
169b815c7f3Sopenharmony_ci
170