annotate test/testresample.c @ 3018:d706d3170d7d

testresample.c: Write out correct size for resampled buffer.
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 11 Jan 2009 04:29:36 +0000
parents 3272431eeee2
children 62d4992e5a92
rev   line source
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 #include <stdio.h>
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 #include "SDL.h"
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 int main(int argc, char **argv)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 SDL_AudioSpec spec;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 SDL_AudioCVT cvt;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 Uint32 len = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 Uint8 *data = NULL;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 int cvtfreq = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 int bitsize = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 int blockalign = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 int avgbytes = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 SDL_RWops *io = NULL;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 if (argc != 4)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19 return 1;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 cvtfreq = atoi(argv[3]);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 if (SDL_Init(SDL_INIT_AUDIO) == -1)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 return 2;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 return 3;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 spec.format, spec.channels, cvtfreq) == -1)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 return 4;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 cvt.len = len;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47 cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 if (cvt.buf == NULL)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50 fprintf(stderr, "Out of memory.\n");
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 return 5;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 memcpy(cvt.buf, data, len);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 if (SDL_ConvertAudio(&cvt) == -1)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 free(cvt.buf);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 return 6;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 /* write out a WAV header... */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 io = SDL_RWFromFile(argv[2], "wb");
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 if (io == NULL)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 free(cvt.buf);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 return 7;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 bitsize = SDL_AUDIO_BITSIZE(spec.format);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 blockalign = (bitsize / 8) * spec.channels;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 avgbytes = cvtfreq * blockalign;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 SDL_WriteLE32(io, 0x46464952); /* RIFF */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 SDL_WriteLE32(io, len * cvt.len_mult + 36);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 SDL_WriteLE32(io, 0x45564157); /* WAVE */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 SDL_WriteLE32(io, 0x20746D66); /* fmt */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 SDL_WriteLE32(io, 16); /* chunk size */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 SDL_WriteLE16(io, 1); /* uncompressed */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 SDL_WriteLE16(io, spec.channels); /* channels */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 SDL_WriteLE32(io, cvtfreq); /* sample rate */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 SDL_WriteLE32(io, avgbytes); /* average bytes per second */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 SDL_WriteLE16(io, blockalign); /* block align */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 SDL_WriteLE16(io, bitsize); /* significant bits per sample */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 SDL_WriteLE32(io, 0x61746164); /* data */
3018
d706d3170d7d testresample.c: Write out correct size for resampled buffer.
Ryan C. Gordon <icculus@icculus.org>
parents: 3017
diff changeset
93 SDL_WriteLE32(io, cvt.len_cvt); /* size */
d706d3170d7d testresample.c: Write out correct size for resampled buffer.
Ryan C. Gordon <icculus@icculus.org>
parents: 3017
diff changeset
94 SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 if (SDL_RWclose(io) == -1)
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 free(cvt.buf);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 return 8;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 } // if
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 free(cvt.buf);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 return 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 } // main
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 // end of resample_test.c ...
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112