Debian Codegraph @

test/testsurround.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
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 /* Program to test surround sound audio channels */
14 #include "SDL_config.h"
15
16 #include "SDL.h"
17 #include "SDL_test.h"
18
19 static int total_channels;
20 static int active_channel;
21
22 #define SAMPLE_RATE_HZ 48000
23 #define QUICK_TEST_TIME_MSEC 100
24 #define CHANNEL_TEST_TIME_SEC 5
25 #define MAX_AMPLITUDE SDL_MAX_SINT16
26
27 #define SINE_FREQ_HZ 500
28 #define LFE_SINE_FREQ_HZ 50
29
30 /* The channel layout is defined in SDL_audio.h */
31 const char *
32 get_channel_name(int channel_index, int channel_count)
33 {
34 switch (channel_index) {
35 case 0:
36 return "Front Left";
37 case 1:
38 return "Front Right";
39 case 2:
40 switch (channel_count) {
41 case 3:
42 case 5:
43 return "Low Frequency Effects";
44 case 4:
45 return "Back Left";
46 default:
47 return "Front Center";
48 }
49 case 3:
50 switch (channel_count) {
51 case 4:
52 return "Back Right";
53 case 5:
54 return "Back Left";
55 default:
56 return "Low Frequency Effects";
57 }
58 case 4:
59 switch (channel_count) {
60 case 5:
61 return "Back Right";
62 case 6:
63 return "Side Left";
64 case 7:
65 return "Back Center";
66 case 8:
67 return "Back Left";
68 }
69 SDL_assert(0);
70 case 5:
71 switch (channel_count) {
72 case 6:
73 return "Side Right";
74 case 7:
75 return "Side Left";
76 case 8:
77 return "Back Right";
78 }
79 SDL_assert(0);
80 case 6:
81 switch (channel_count) {
82 case 7:
83 return "Side Right";
84 case 8:
85 return "Side Left";
86 }
87 SDL_assert(0);
88 case 7:
89 return "Side Right";
90 }
91
92 return NULL;
93 }
94
95 SDL_bool
96 is_lfe_channel(int channel_index, int channel_count)
97 {
98 return (channel_count == 3 && channel_index == 2) || (channel_count >= 6 && channel_index == 3);
99 }
100
101 void SDLCALL
102 fill_buffer(void *unused, Uint8 *stream, int len)
103 {
104 Sint16 *buffer = (Sint16 *)stream;
105 int samples = len / sizeof(Sint16);
106 static int total_samples = 0;
107 int i;
108
109 SDL_memset(stream, 0, len);
110
111 /* This can happen for a short time when switching devices */
112 if (active_channel == total_channels) {
113 return;
114 }
115
116 /* Play a sine wave on the active channel only */
117 for (i = active_channel; i < samples; i += total_channels) {
118 float time = (float)total_samples++ / SAMPLE_RATE_HZ;
119 int sine_freq = is_lfe_channel(active_channel, total_channels) ? LFE_SINE_FREQ_HZ : SINE_FREQ_HZ;
120 int amplitude;
121
122 /* Gradually ramp up and down to avoid audible pops when switching between channels */
123 if (total_samples < SAMPLE_RATE_HZ) {
124 amplitude = total_samples * MAX_AMPLITUDE / SAMPLE_RATE_HZ;
125 } else if (total_samples > (CHANNEL_TEST_TIME_SEC - 1) * SAMPLE_RATE_HZ) {
126 amplitude = (CHANNEL_TEST_TIME_SEC * SAMPLE_RATE_HZ - total_samples) * MAX_AMPLITUDE / SAMPLE_RATE_HZ;
127 } else {
128 amplitude = MAX_AMPLITUDE;
129 }
130
131 buffer[i] = (Sint16)(SDL_sin(6.283185f * sine_freq * time) * amplitude);
132
133 /* Reset our state for next callback if this channel test is finished */
134 if (total_samples == CHANNEL_TEST_TIME_SEC * SAMPLE_RATE_HZ) {
135 total_samples = 0;
136 active_channel++;
137 break;
138 }
139 }
140 }
141
142 int main(int argc, char *argv[])
143 {
144 int i;
145 SDLTest_CommonState *state;
146
147 state = SDLTest_CommonCreateState(argv, SDL_INIT_AUDIO);
148 if (!state) {
149 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
150 return 1;
151 }
152
153 /* Enable standard application logging */
154 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
155
156 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
157 return 1;
158 }
159
160 if (!SDLTest_CommonInit(state)) {
161 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
162 return 1;
163 }
164
165 /* Show the list of available drivers */
166 SDL_Log("Available audio drivers:");
167 for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
168 SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
169 }
170
171 SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
172
173 for (i = 0; i < SDL_GetNumAudioDevices(0); i++) {
174 const char *devname = SDL_GetAudioDeviceName(i, 0);
175 int j;
176 SDL_AudioSpec spec;
177 SDL_AudioDeviceID dev;
178
179 if (SDL_GetAudioDeviceSpec(i, 0, &spec) != 0) {
180 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GetAudioSpec() failed: %s\n", SDL_GetError());
181 continue;
182 }
183
184 spec.freq = SAMPLE_RATE_HZ;
185 spec.format = AUDIO_S16SYS;
186 spec.samples = 4096;
187 spec.callback = fill_buffer;
188
189 dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
190 if (dev == 0) {
191 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenAudioDevice() failed: %s\n", SDL_GetError());
192 continue;
193 }
194
195 SDL_Log("Testing audio device: %s (%d channels)\n", devname, spec.channels);
196
197 /* These are used by the fill_buffer callback */
198 total_channels = spec.channels;
199 active_channel = 0;
200
201 SDL_PauseAudioDevice(dev, 0);
202
203 for (j = 0; j < total_channels; j++) {
204 int sine_freq = is_lfe_channel(j, total_channels) ? LFE_SINE_FREQ_HZ : SINE_FREQ_HZ;
205
206 SDL_Log("Playing %d Hz test tone on channel: %s\n", sine_freq, get_channel_name(j, total_channels));
207
208 /* fill_buffer() will increment the active channel */
209 if (SDL_getenv("SDL_TESTS_QUICK") != NULL) {
210 SDL_Delay(QUICK_TEST_TIME_MSEC);
211 } else {
212 SDL_Delay(CHANNEL_TEST_TIME_SEC * 1000);
213 }
214 }
215
216 SDL_CloseAudioDevice(dev);
217 }
218
219 SDLTest_CommonQuit(state);
220 return 0;
221 }
222