File: test.c

package info (click to toggle)
praat 5.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 40,728 kB
  • sloc: cpp: 333,759; ansic: 237,947; makefile: 731; python: 340
file content (321 lines) | stat: -rw-r--r-- 7,969 bytes parent folder | download | duplicates (11)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <stdio.h>
#include <memory.h>
#include <malloc.h>
#include <time.h>
#include "mp3.h"
#include "mad_config.h"
#include "mad_decoder.h"

/*
 * This small program tests that my offset addition to libMAD actually works.
 * Erez Volk 2007-05-30
 */

static void dump_file( const char *filename );
static void test_file( const char *filename );
static void test_mad( FILE *f );
static void test_mp3f( FILE *f );

static void get_offsets( FILE *f );

static enum mad_flow cb_input( void *context, struct mad_stream *stream );
static enum mad_flow cb_header( void *context, struct mad_header const *header );
static enum mad_flow cb_accept_header( void *context, struct mad_header const *header );
static enum mad_flow cb_dump( void *context, struct mad_header const *header, struct mad_pcm *pcm );

static void cb_samples( const int *channels[MP3F_MAX_CHANNELS],
		unsigned num_samples,
		void *context );

#define BUFFER_SIZE 2048
static unsigned char the_buffer[BUFFER_SIZE];

#define MAX_OFFSETS 4096
static unsigned long offsets[MAX_OFFSETS];
static unsigned num_offsets;

int main( int argc, char *argv[] )
{
	int i;

	if ( argc <= 1 )
	{
		printf( "Usage: %s MP3_FILE [MP3_FILE...]\n", argv[0] );
		return -1;
	}

	if ( argc > 2 && !strcmp( argv[1], "-dump" ) )
		dump_file( argv[2] );
	else for ( i = 1; i < argc; ++ i )
		test_file( argv[i] );

	return 0;
}

typedef struct {
	FILE *f;
	unsigned done;
	unsigned left;
	FILE *f2;
} DUMP_CONTEXT;

static void dump_file( const char *filename )
{
	enum { MAX_FRAMES = 16 };
	FILE *f;
	unsigned i;

	printf( "Dumping file: \"%s\"...\n", filename );
	if ( (f = fopen( filename, "rb" )) == NULL ) {
		printf( "  Cannot open file.\n" );
		return;
	}

	get_offsets( f );

	printf( "  Header offsets: %lu, %lu, %lu, %lu, ... %lu\n",
		offsets[0], offsets[1], offsets[2], offsets[3],
		offsets[num_offsets - 1] );

	for ( i = 0; i < MAX_FRAMES; ++ i )
	{
		char dumpname[128];
		struct mad_decoder d;
		DUMP_CONTEXT c = { f, i, MAX_FRAMES - i };

		sprintf( dumpname, "%02u.dump", i );
		printf( "  Creating %s...\n", dumpname );
		c.f2 = fopen( dumpname, "w" );

		fseek( f, offsets[i], SEEK_SET );
		mad_decoder_init( &d, &c, cb_input, cb_accept_header, NULL, cb_dump, NULL, NULL );
		mad_decoder_run( &d, MAD_DECODER_MODE_SYNC );
		mad_decoder_finish( &d );

		fclose( c.f2 );
	}

	fclose( f );
}

static void test_file( const char *filename )
{
	FILE *f;

	printf( "Testing file: \"%s\"...\n", filename );

	if ( (f = fopen( filename, "rb" )) == NULL ) {
		printf( "  Cannot open file.\n" );
		return;
	}

	test_mad( f );
	test_mp3f( f );

	fclose( f );
}

static void test_mad( FILE *f )
{
	enum { MAX_BAD = 16 };
	unsigned i, bad;

	get_offsets( f );

	if ( num_offsets >= MAX_OFFSETS )
		printf( "  Reached maximum number of headers (%u)\n", MAX_OFFSETS );
	else
		printf( "  Number of headers found: %u\n", num_offsets );
	printf( "  Header offsets: %lu, %lu, %lu, %lu, ... %lu\n",
		offsets[0], offsets[1], offsets[2], offsets[3],
		offsets[num_offsets - 1] );
	printf( "  Checking header validity...\n" );
	for ( i = bad = 0; i < num_offsets && bad < MAX_BAD; ++ i )
	{
		unsigned char header[2] = { 0, 0 };
		if ( i > 0 && offsets[i] <= offsets[i - 1] )
		{
			printf( "  Invalid offset table.\n" );
			return;
		}
		fseek( f, offsets[i], SEEK_SET );
		fread( header, 1, sizeof(header), f );
		if ( (header[0] != 0xFF) || ((header[1] & 0xE0) != 0xE0) )
		{
			++ bad;
			printf( "  ERROR: No header at file offset %lu\n", offsets[i] );
		}
		else if ( bad > 0 )
			printf( "  Good header at file offset %lu\n", offsets[i] );
	}
	if ( bad == 0 )
		printf( "  All offsets have valid MP3 headers\n" );
}

static void get_offsets( FILE *f )
{
	struct mad_decoder d;

	/* Make sure there are no leftovers */
	memset( &d, time(NULL), sizeof(d) );

	num_offsets = 0;

	mad_decoder_init( &d, &f, cb_input, cb_header, NULL, NULL, NULL, NULL );
	if ( mad_decoder_run( &d, MAD_DECODER_MODE_SYNC ) != 0 )
	{
		printf( "  Error scanning file.\n" );
		return;
	}

	mad_decoder_finish( &d );
}

static enum mad_flow cb_input( void *context, struct mad_stream *stream )
{
	FILE *f = *(FILE **)context;
	unsigned char *data = NULL;
	unsigned nthrown = 0, ncopied = 0, size = 0;
	unsigned offset;
	size_t nread = 0;

	if (feof (f))
		return MAD_FLOW_STOP;

	if (stream -> next_frame) {
		nthrown = stream -> next_frame - the_buffer; 
		ncopied = BUFFER_SIZE - nthrown;
		memmove (the_buffer, stream -> next_frame, ncopied);
	}

	data = the_buffer + ncopied;
	size = BUFFER_SIZE - ncopied;

	offset = ftell (f) - ncopied;
	if (size > 0)
		nread = fread (data, 1, size, f);

	mad_stream_buffer_offset (stream, the_buffer, nread + ncopied, offset);
	stream -> this_offset = offset;
	return MAD_FLOW_CONTINUE;
}

static enum mad_flow cb_header( void *context, struct mad_header const *header )
{
	FILE *f = *(FILE **)context;
	unsigned long foff = ftell( f );
	if ( foff > header->offset + BUFFER_SIZE )
		printf( "  ??? %lu <-> %lu\n", foff, header->offset );
	offsets[num_offsets] = header->offset;
	if ( ++ num_offsets >= MAX_OFFSETS )
		return MAD_FLOW_STOP;
	return MAD_FLOW_CONTINUE;
}

static enum mad_flow cb_accept_header( void *context, struct mad_header const *header )
{
	(void)context;
	(void)header;
	return MAD_FLOW_CONTINUE;
}

static enum mad_flow cb_dump( void *context, struct mad_header const *header, struct mad_pcm *pcm )
{
	DUMP_CONTEXT *c = (DUMP_CONTEXT *)context;
	FILE *f = c->f2;
	unsigned i, j, length = pcm->length;
	const mad_fixed_t *samples = pcm->samples[0];
	(void)header;

	fprintf( f, "FRAME %u, OFFSET %lu (expected %lu)\n",
		       	c->done, header->offset, offsets[c->done] );
	for ( i = 0; i < length; i += 8 ) {
		for ( j = i; i < length && j < i + 8; ++ j )
			fprintf( f, "%9i ", samples[j] );
		fprintf( f, "\n" );
	}
	fprintf( f, "\n" );

	++ c->done;
	-- c->left;
	return c->left == 0 ? MAD_FLOW_STOP : MAD_FLOW_CONTINUE;
}

static void test_mp3f( FILE *f )
{
	enum { FIRST = 15000, SECOND = 2000, TOTAL = FIRST + SECOND };
	enum { MID_OFFSET = 16000, MID_SIZE = 800 };
	static int x[TOTAL], y[TOTAL], z[MID_SIZE];
	int *p;
	MP3_FILE mp3f = mp3f_new();

	mp3f_set_file( mp3f, f );
	if ( mp3f_analyze( mp3f ) )
	{
		printf( "  MP3F: Channels = %u\n", mp3f_channels( mp3f ) );
		printf( "  MP3F: Frequency = %u\n", mp3f_frequency( mp3f ) );
		printf( "  MP3F: Samples = %lu\n", (unsigned long)mp3f_samples( mp3f ) );
		printf( "  MP3F: Time => %.2lfs\n",
				(double)mp3f_samples(mp3f) / (double)mp3f_frequency(mp3f) );
	}
	else	
		printf( "  MP3F: Cannot analyze\n" );

	memset( x, 0x22, sizeof(x) );
	memset( y, 0x22, sizeof(y) );
	memset( z, 0x22, sizeof(z) );

	mp3f_set_file( mp3f, f );
	p = x;
	mp3f_set_callback( mp3f, cb_samples, &p );
	mp3f_read( mp3f, TOTAL );

	mp3f_set_file( mp3f, f );
	p = y;
	mp3f_set_callback( mp3f, cb_samples, &p );
	mp3f_read( mp3f, FIRST );
	mp3f_read( mp3f, SECOND );

	mp3f_set_file( mp3f, f );
	p = z;
	mp3f_set_callback( mp3f, cb_samples, &p );
	if ( mp3f_seek( mp3f, MID_OFFSET ) )
		mp3f_read( mp3f, MID_SIZE );
	else
		printf( "  MP3F: Seek failed\n" );

	if ( memcmp( x, y, FIRST * sizeof(int) ) != 0 )
		printf( "  MP3F: Difference in first samples!\n" );
	else
		printf( "  MP3F: First samples OK\n" );

	if ( memcmp( x + FIRST, y + FIRST, SECOND * sizeof(int) ) != 0 )
		printf( "  MP3F: Difference in later samples!\n" );
	else
		printf( "  MP3F: Later samples OK\n" );

	if ( memcmp( x + MID_OFFSET, z, MID_SIZE * sizeof(int) ) != 0 )
		printf( "  MP3F: Seek doesn't work\n" );
	else
		printf( "  MP3F: Seek works\n" );

	mp3f_delete( mp3f );
}

static void cb_samples( const int *channels[MP3F_MAX_CHANNELS],
		unsigned num_samples,
		void *context )
{
	int **pp = (int **)context;
	int *p = *pp;
	unsigned i;

	for ( i = 0; i < num_samples; ++ i )
		p[i] = channels[0][i];
	*pp += num_samples;
}

/* Needed for libMP3 */
void *Melder_malloc( unsigned size ) { return malloc( size ); }
void _Melder_free( void **p ) { free( *p ); }