File: twentyfour2.c

package info (click to toggle)
audiofile 0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,192 kB
  • sloc: cpp: 31,691; sh: 11,006; ansic: 3,773; makefile: 271
file content (137 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
	Audio File Library

	Copyright (C) 2003, Michael Pruett <michael@68k.org>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License as
	published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be
	useful, but WITHOUT ANY WARRANTY; without even the implied
	warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
	PURPOSE.  See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public
	License along with this program; if not, write to the Free
	Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
	MA 02111-1307, USA.
*/

/*
	twentyfour2.c

	This program checks reading and writing a large amount of 24-bit
	audio data to an AIFF file.

	This program serves as a regression test for a bug in the Audio
	File Library in which requesting more than _AF_ATOMIC_NVFRAMES
	(1024 frames) from afReadFrames when reading a 24-bit audio file
	would result in corrupted audio data.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <audiofile.h>

#define TEST_FILE "/tmp/test.aiffXXXXXX"
char *real_test_file;
#define FRAME_COUNT 10000

void cleanup (void)
{
#ifndef DEBUG
	unlink(real_test_file);
#endif
}

void ensure (int condition, const char *message)
{
	if (!condition)
	{
		printf("%s.\n", message);
		cleanup();
		exit(EXIT_FAILURE);
	}
}

int main (void)
{
	AFfilehandle	file;
	AFfilesetup	setup;
	int32_t		*buffer, *readbuffer;
	int		i;
	AFframecount	frameswritten, framesread;

	setup = afNewFileSetup();
	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 24);

	real_test_file = malloc(sizeof(TEST_FILE));
	strcpy(real_test_file, TEST_FILE);
	int tmp = mkstemp(real_test_file);
	file = afOpenFile(real_test_file, "w", setup);
	ensure(file != NULL, "could not open test file for writing");

	afFreeFileSetup(setup);

	buffer = malloc(sizeof (int32_t) * FRAME_COUNT);
	ensure(buffer != NULL, "could not allocate buffer for audio data");

	readbuffer = malloc(sizeof (int32_t) * FRAME_COUNT);
	ensure(readbuffer != NULL, "could not allocate buffer for audio data");

	for (i=0; i<FRAME_COUNT; i++)
	{
		if ((i%3) == 0)
			buffer[i] = -i;
		else
			buffer[i] = i;
	}

	frameswritten = afWriteFrames(file, AF_DEFAULT_TRACK, buffer, FRAME_COUNT);
	ensure(frameswritten == FRAME_COUNT, "incorrect number of frames written");

	afCloseFile(file);

	/*
		Now open file for reading and ensure that the data read
		is equal to the data written.
	*/
	file = afOpenFile(real_test_file, "r", AF_NULL_FILESETUP);
	ensure(file != NULL, "could not open test file for reading");

	framesread = afReadFrames(file, AF_DEFAULT_TRACK, readbuffer, FRAME_COUNT);
	ensure(framesread == FRAME_COUNT, "incorrect number of frames read");

#ifdef DEBUG
	for (i=0; i<FRAME_COUNT; i++)
	{
		if (buffer[i] != readbuffer[i])
		{
			printf("buffer[%d] = %d, readbuffer[%d] = %d\n",
				i, buffer[i], i, readbuffer[i]);
		}
	}
#endif

	ensure(!memcmp(buffer, readbuffer, sizeof (int32_t) * FRAME_COUNT),
		"data read does not match data written");

	afCloseFile(file);

	free(buffer);
	free(readbuffer);

	cleanup();
	return 0;
}