annotate test/testresample.c @ 5159:307ccc9c135e

Made it possible to create a texture of any format, even if not supported by the renderer. This allows me to reduce the set of formats supported by the renderers to the most optimal set, for a nice speed boost.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 03 Feb 2011 00:19:40 -0800
parents 62d4992e5a92
children
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
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
4 int
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
5 main(int argc, char **argv)
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 {
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 SDL_AudioSpec spec;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 SDL_AudioCVT cvt;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 Uint32 len = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 Uint8 *data = NULL;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 int cvtfreq = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 int bitsize = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 int blockalign = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 int avgbytes = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 SDL_RWops *io = NULL;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
17 if (argc != 4) {
3017
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
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
24 if (SDL_Init(SDL_INIT_AUDIO) == -1) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 return 2;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
29 if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 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
31 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 return 3;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
36 spec.format, spec.channels, cvtfreq) == -1) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 return 4;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 cvt.len = len;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
45 if (cvt.buf == NULL) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 fprintf(stderr, "Out of memory.\n");
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 return 5;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 memcpy(cvt.buf, data, len);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
53 if (SDL_ConvertAudio(&cvt) == -1) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 free(cvt.buf);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 return 6;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 /* write out a WAV header... */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 io = SDL_RWFromFile(argv[2], "wb");
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
63 if (io == NULL) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 free(cvt.buf);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 return 7;
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
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 bitsize = SDL_AUDIO_BITSIZE(spec.format);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 blockalign = (bitsize / 8) * spec.channels;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 avgbytes = cvtfreq * blockalign;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
75 SDL_WriteLE32(io, 0x46464952); /* RIFF */
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 SDL_WriteLE32(io, len * cvt.len_mult + 36);
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
77 SDL_WriteLE32(io, 0x45564157); /* WAVE */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
78 SDL_WriteLE32(io, 0x20746D66); /* fmt */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
79 SDL_WriteLE32(io, 16); /* chunk size */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
80 SDL_WriteLE16(io, 1); /* uncompressed */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
81 SDL_WriteLE16(io, spec.channels); /* channels */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
82 SDL_WriteLE32(io, cvtfreq); /* sample rate */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
83 SDL_WriteLE32(io, avgbytes); /* average bytes per second */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
84 SDL_WriteLE16(io, blockalign); /* block align */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
85 SDL_WriteLE16(io, bitsize); /* significant bits per sample */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
86 SDL_WriteLE32(io, 0x61746164); /* data */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
87 SDL_WriteLE32(io, cvt.len_cvt); /* size */
3018
d706d3170d7d testresample.c: Write out correct size for resampled buffer.
Ryan C. Gordon <icculus@icculus.org>
parents: 3017
diff changeset
88 SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
90 if (SDL_RWclose(io) == -1) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 free(cvt.buf);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 return 8;
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
96 } // if
3017
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 free(cvt.buf);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 return 0;
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
102 } // main
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 // end of resample_test.c ...