summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testresample.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testresample.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testresample.c154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testresample.c b/src/contrib/SDL-3.2.20/test/testresample.c
new file mode 100644
index 0000000..55db2f3
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testresample.c
@@ -0,0 +1,154 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12
13#include <SDL3/SDL.h>
14#include <SDL3/SDL_main.h>
15#include <SDL3/SDL_test.h>
16
17static void log_usage(char *progname, SDLTest_CommonState *state) {
18 static const char *options[] = { "in.wav", "out.wav", "newfreq", "newchan", NULL };
19 SDLTest_CommonLogUsage(state, progname, options);
20}
21
22int main(int argc, char **argv)
23{
24 SDL_AudioSpec spec;
25 SDL_AudioSpec cvtspec;
26 SDL_AudioStream *stream = NULL;
27 Uint8 *dst_buf = NULL;
28 Uint32 len = 0;
29 Uint8 *data = NULL;
30 int bitsize = 0;
31 int blockalign = 0;
32 int avgbytes = 0;
33 SDL_IOStream *io = NULL;
34 int dst_len;
35 int ret = 0;
36 int argpos = 0;
37 int i;
38 SDLTest_CommonState *state;
39 char *file_in = NULL;
40 char *file_out = NULL;
41
42 /* Initialize test framework */
43 state = SDLTest_CommonCreateState(argv, 0);
44 if (!state) {
45 return 1;
46 }
47
48 SDL_zero(cvtspec);
49
50 /* Parse commandline */
51 for (i = 1; i < argc;) {
52 int consumed;
53
54 consumed = SDLTest_CommonArg(state, i);
55 if (!consumed) {
56 if (argpos == 0) {
57 file_in = argv[i];
58 argpos++;
59 consumed = 1;
60 } else if (argpos == 1) {
61 file_out = argv[i];
62 argpos++;
63 consumed = 1;
64 } else if (argpos == 2) {
65 char *endp;
66 cvtspec.freq = (int)SDL_strtoul(argv[i], &endp, 0);
67 if (endp != argv[i] && *endp == '\0') {
68 argpos++;
69 consumed = 1;
70 }
71 } else if (argpos == 3) {
72 char *endp;
73 cvtspec.channels = (int)SDL_strtoul(argv[i], &endp, 0);
74 if (endp != argv[i] && *endp == '\0') {
75 argpos++;
76 consumed = 1;
77 }
78 }
79 }
80 if (consumed <= 0) {
81 log_usage(argv[0], state);
82 ret = 1;
83 goto end;
84 }
85
86 i += consumed;
87 }
88
89 if (argpos != 4) {
90 log_usage(argv[0], state);
91 ret = 1;
92 goto end;
93 }
94
95 if (!SDL_Init(SDL_INIT_AUDIO)) {
96 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s", SDL_GetError());
97 ret = 2;
98 goto end;
99 }
100
101 if (!SDL_LoadWAV(file_in, &spec, &data, &len)) {
102 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s", file_in, SDL_GetError());
103 ret = 3;
104 goto end;
105 }
106
107 cvtspec.format = spec.format;
108 if (!SDL_ConvertAudioSamples(&spec, data, len, &cvtspec, &dst_buf, &dst_len)) {
109 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to convert samples: %s", SDL_GetError());
110 ret = 4;
111 goto end;
112 }
113
114 /* write out a WAV header... */
115 io = SDL_IOFromFile(file_out, "wb");
116 if (!io) {
117 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "opening '%s' failed: %s", file_out, SDL_GetError());
118 ret = 5;
119 goto end;
120 }
121
122 bitsize = SDL_AUDIO_BITSIZE(spec.format);
123 blockalign = (bitsize / 8) * cvtspec.channels;
124 avgbytes = cvtspec.freq * blockalign;
125
126 SDL_WriteU32LE(io, 0x46464952); /* RIFF */
127 SDL_WriteU32LE(io, dst_len + 36);
128 SDL_WriteU32LE(io, 0x45564157); /* WAVE */
129 SDL_WriteU32LE(io, 0x20746D66); /* fmt */
130 SDL_WriteU32LE(io, 16); /* chunk size */
131 SDL_WriteU16LE(io, SDL_AUDIO_ISFLOAT(spec.format) ? 3 : 1); /* uncompressed */
132 SDL_WriteU16LE(io, (Uint16)cvtspec.channels); /* channels */
133 SDL_WriteU32LE(io, cvtspec.freq); /* sample rate */
134 SDL_WriteU32LE(io, avgbytes); /* average bytes per second */
135 SDL_WriteU16LE(io, (Uint16)blockalign); /* block align */
136 SDL_WriteU16LE(io, (Uint16)bitsize); /* significant bits per sample */
137 SDL_WriteU32LE(io, 0x61746164); /* data */
138 SDL_WriteU32LE(io, dst_len); /* size */
139 SDL_WriteIO(io, dst_buf, dst_len);
140
141 if (!SDL_CloseIO(io)) {
142 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "closing '%s' failed: %s", file_out, SDL_GetError());
143 ret = 6;
144 goto end;
145 }
146
147end:
148 SDL_free(dst_buf);
149 SDL_free(data);
150 SDL_DestroyAudioStream(stream);
151 SDL_Quit();
152 SDLTest_CommonDestroyState(state);
153 return ret;
154}