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