aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sfx/pcm_device/audbuf_test.cpp
blob: 08e8b2ad5ebf4149376da142c308e9157c93269a (plain)
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
/***************************************************************************
 audbuf_test.c  Copyright (C) 2002 Christoph Reichenbach


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

 It is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 merchantibility or fitness for a particular purpose. See the
 GNU General Public Licence for more details.

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


 Please contact the maintainer for any program-related bug reports or
 inquiries.

 Current Maintainer:

    Christoph Reichenbach (CR) <jameson@linuxgames.com>

***************************************************************************/

#include "audiobuf.h"
#if 0
sfx_audio_buf_t buf;

#define MIN_FRAMESIZE 1
#define MAX_FRAMESIZE 8


void
tester_write(unsigned char *data, int datalen, int framesize, int gran) {
	int i;

	for (i = 0; i < datalen; i += gran) {
		int size = (i + gran < datalen) ? gran : datalen - i;

		sfx_audbuf_write(&buf, data + (i * framesize), framesize, size);
	}
}


void
tester_read(unsigned char *data, int datalen, int framesize, int gran) {
	unsigned char *readdata = malloc(datalen * framesize);
	int i;

	for (i = 0; i < datalen; i += gran) {
		int size = (i + gran < datalen) ? gran : datalen - i;
		int j;

		sfx_audbuf_read(&buf, readdata + (i * framesize), framesize, size);
		for (j = 0; j < gran * framesize; j++) {
			int offset = i * framesize + j;

			if (data[i] != readdata[i]) {
				fprintf(stderr, "[ERROR] Mismatch at offset %08x (sample #%d): Expected %02x, got %02x\n",
				        offset, i,  readdata[i], data[i]);
			}
		}
	}

	free(readdata);
}


void
test1(unsigned char *data, int len)
/* Test the 'regular' case */
{
	int framesize;
	int stepsize;

	fprintf(stderr, "[Test-1] Commenced; len=%d.\n", len);

	for (framesize = MAX_FRAMESIZE; framesize >= MIN_FRAMESIZE; framesize >>= 1) {
		fprintf(stderr, "[Test-1] Writing frame size %d\n", framesize);
		for (stepsize = 1; stepsize <= len; stepsize++)
			tester_write(data, len / framesize, framesize, stepsize);
	}

	for (framesize = MAX_FRAMESIZE; framesize >= MIN_FRAMESIZE; framesize >>= 1) {
		fprintf(stderr, "[Test-1] Reading frame size %d\n", framesize);
		for (stepsize = len; stepsize >= 1; stepsize--)
			tester_read(data, len / framesize, framesize, stepsize);
	}

	fprintf(stderr, "[Test-1] Completed.\n");
}

#define TEST2_COUNT 10
#define TEST2_LEN 3

void
test2(unsigned char *data, int framesize)
/* Test whether buffer underrun repeats are handled correctly */
{
	int i;
	unsigned char *src;

	fprintf(stderr, "[Test-2] Commenced; framesize=%d.\n", framesize);

	sfx_audbuf_write(&buf, data, framesize, 1);
	src = malloc(framesize * TEST2_LEN);

	for (i = 0; i < TEST2_COUNT + 1; i++) {
		int inst;
		sfx_audbuf_read(&buf, src, framesize, TEST2_LEN);

		for (inst = 0; inst < TEST2_LEN; inst++) {
			int offset = inst * framesize;
			int j;

			for (j = 0; j < framesize; j++)
				if (src[j + offset] != data[j]) {
					fprintf(stderr, "[ERROR] At copy sample %d, frame %d, offset %d: Expected %02x, got %02x\n",
					        i, inst, j, data[j], src[j+offset]);
				}
		}
		memset(src, 0xbf, framesize * TEST2_LEN);
	}

	free(src);
	fprintf(stderr, "[Test-1] Completed.\n");
}


#define CHUNKS_NR 4

#define CHUNK_LEN_0 8
#define CHUNK_LEN_1 20
#define CHUNK_LEN_2 16
#define CHUNK_LEN_3 40

unsigned char test_data_0[CHUNK_LEN_0] = { 0x01, 0x02, 0x03, 0x04,  0x05, 0x06, 0x07, 0x08 };
unsigned char test_data_1[CHUNK_LEN_1] = { 0xff, 0xff, 0xff, 0xff,  0xff, 0xff, 0xff, 0xff,
        0x00, 0xff, 0x00, 0xff,  0x00, 0xff, 0x00, 0xff,
        0xff, 0x00, 0xff, 0x00
                                         };
unsigned char test_data_2[CHUNK_LEN_2] = { 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00
                                         };
unsigned char test_data_3[CHUNK_LEN_3] = { 0x01, 0x02, 0x03, 0x04,  0x05, 0x06, 0x07, 0x08,
        0x11, 0x12, 0x13, 0x14,  0x15, 0x16, 0x17, 0x18,
        0x21, 0x22, 0x23, 0x24,  0x25, 0x26, 0x27, 0x28,
        0x41, 0x42, 0x43, 0x44,  0x45, 0x46, 0x47, 0x48,
        0x8f, 0x8e, 0x8d, 0x8c,  0x8b, 0x8a, 0x89, 0x88
                                         };

struct {
	int len;
	unsigned char *data;
} test_chunks[CHUNKS_NR] = {
	{ CHUNK_LEN_0, test_data_0 },
	{ CHUNK_LEN_1, test_data_1 },
	{ CHUNK_LEN_2, test_data_2 },
	{ CHUNK_LEN_3, test_data_3 }
};

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

	sfx_audbuf_init(&buf);
	for (i = 0; i < CHUNKS_NR; i++) {
		int k;

		/*		for (k = MAX_FRAMESIZE; k >= MIN_FRAMESIZE; k >>= 1)
				test2(test_chunks[i].data, k);*/

		test1(test_chunks[i].data, test_chunks[i].len);
	}
	sfx_audbuf_exit(&buf);

	return 0;
}
#else
int main() {}
#endif