6 #define STB_VORBIS_HEADER_ONLY
7 #include "stb_vorbis.c"
9 #define SAMPLES_TO_TEST 3000
11 int test_count
[5] = { 5000, 3000, 2000, 50000, 50000 };
12 int test_spacing
[5] = { 1, 111, 3337, 7779, 72717 };
14 int try_seeking(stb_vorbis
*v
, unsigned int pos
, short *output
, unsigned int num_samples
)
17 short samples
[SAMPLES_TO_TEST
*2];
18 assert(pos
<= num_samples
);
20 if (!stb_vorbis_seek(v
, pos
)) {
21 fprintf(stderr
, "Seek to %u returned error from stb_vorbis\n", pos
);
25 count
= stb_vorbis_get_samples_short_interleaved(v
, 2, samples
, SAMPLES_TO_TEST
*2);
27 if (count
> (int) (num_samples
- pos
)) {
28 fprintf(stderr
, "Seek to %u allowed decoding %d samples when only %d should have been valid.\n",
29 pos
, count
, (int) (num_samples
- pos
));
33 if (count
< SAMPLES_TO_TEST
&& count
< (int) (num_samples
- pos
)) {
34 fprintf(stderr
, "Seek to %u only decoded %d samples of %d attempted when at least %d should have been valid.\n",
35 pos
, count
, SAMPLES_TO_TEST
, num_samples
- pos
);
39 if (0 != memcmp(samples
, output
+ pos
*2, count
*2)) {
41 for (k
=0; k
< SAMPLES_TO_TEST
*2; ++k
) {
42 if (samples
[k
] != output
[k
]) {
43 fprintf(stderr
, "Seek to %u produced incorrect samples starting at sample %u (short #%d in buffer).\n",
48 assert(k
!= SAMPLES_TO_TEST
*2);
55 int main(int argc
, char **argv
)
57 int num_chan
, samprate
;
58 int i
, j
, test
, phase
;
62 fprintf(stderr
, "Usage: vorbseek {vorbisfile} [{vorbisfile]*]\n");
63 fprintf(stderr
, "Tests various seek offsets to make sure they're sample exact.\n");
69 // check that outofmem occurs correctly
71 va
.alloc_buffer
= malloc(1024*1024);
72 for (i
=0; i
< 1024*1024; i
+= 10) {
75 va
.alloc_buffer_length_in_bytes
= i
;
76 v
= stb_vorbis_open_filename(argv
[1], &error
, &va
);
79 printf("Error %d at %d\n", error
, i
);
84 for (j
=1; j
< argc
; ++j
) {
85 unsigned int successes
=0, attempts
= 0;
86 unsigned int num_samples
= stb_vorbis_decode_filename(argv
[j
], &num_chan
, &samprate
, &output
);
90 if (num_samples
== 0xffffffff) {
91 fprintf(stderr
, "Error: couldn't open file or not vorbis file: %s\n", argv
[j
]);
96 fprintf(stderr
, "vorbseek testing only works with files with 2 channels, %s has %d\n", argv
[j
], num_chan
);
100 for (test
=0; test
< 5; ++test
) {
102 stb_vorbis
*v
= stb_vorbis_open_filename(argv
[j
], &error
, NULL
);
104 fprintf(stderr
, "Couldn't re-open %s for test #%d\n", argv
[j
], test
);
107 for (phase
=0; phase
< 3; ++phase
) {
108 unsigned int base
= phase
== 0 ? 0 : phase
== 1 ? num_samples
- test_count
[test
]*test_spacing
[test
] : num_samples
/3;
109 for (i
=0; i
< test_count
[test
]; ++i
) {
110 unsigned int pos
= base
+ i
*test_spacing
[test
];
111 if (pos
> num_samples
) // this also catches underflows
113 successes
+= try_seeking(v
, pos
, output
, num_samples
);
119 printf("%d of %d seeks failed in %s (%d samples)\n", attempts
-successes
, attempts
, argv
[j
], num_samples
);