SDL

Форк
0
/
testmultiaudio.c 
204 строки · 6.2 Кб
1
/*
2
  Copyright (C) 1997-2024 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
#include <SDL3/SDL.h>
13
#include <SDL3/SDL_main.h>
14
#include <SDL3/SDL_test.h>
15

16
#include "testutils.h"
17

18
#ifdef SDL_PLATFORM_EMSCRIPTEN
19
#include <emscripten/emscripten.h>
20
#endif
21

22
#include <stdio.h> /* for fflush() and stdout */
23

24
static SDL_AudioSpec spec;
25
static Uint8 *sound = NULL; /* Pointer to wave data */
26
static Uint32 soundlen = 0; /* Length of wave data */
27

28
/* these have to be in globals so the Emscripten port can see them in the mainloop.  :/  */
29
static SDL_AudioStream *stream = NULL;
30

31

32
#ifdef SDL_PLATFORM_EMSCRIPTEN
33
static void loop(void)
34
{
35
    if (SDL_GetAudioStreamAvailable(stream) == 0) {
36
        SDL_Log("done.");
37
        SDL_DestroyAudioStream(stream);
38
        SDL_free(sound);
39
        SDL_Quit();
40
        emscripten_cancel_main_loop();
41
    }
42
}
43
#endif
44

45
static void
46
test_multi_audio(const SDL_AudioDeviceID *devices, int devcount)
47
{
48
    int keep_going = 1;
49
    SDL_AudioStream **streams = NULL;
50
    int i;
51

52
#ifdef SDL_PLATFORM_ANDROID  /* !!! FIXME: maybe always create a window, in the SDLTest layer, so these #ifdefs don't have to be here? */
53
    SDL_Event event;
54

55
    /* Create a Window to get fully initialized event processing for testing pause on Android. */
56
    SDL_CreateWindow("testmultiaudio", 320, 240, 0);
57
#endif
58

59
    for (i = 0; i < devcount; i++) {
60
        const char *devname = SDL_GetAudioDeviceName(devices[i]);
61

62
        SDL_Log("Playing on device #%d of %d: id=%u, name='%s'...", i, devcount, (unsigned int) devices[i], devname);
63

64
        if ((stream = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL)) == NULL) {
65
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed: %s", SDL_GetError());
66
        } else {
67
            SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
68
            SDL_PutAudioStreamData(stream, sound, soundlen);
69
            SDL_FlushAudioStream(stream);
70
#ifdef SDL_PLATFORM_EMSCRIPTEN
71
            emscripten_set_main_loop(loop, 0, 1);
72
#else
73
            while (SDL_GetAudioStreamAvailable(stream) > 0) {
74
#ifdef SDL_PLATFORM_ANDROID
75
                /* Empty queue, some application events would prevent pause. */
76
                while (SDL_PollEvent(&event)) {
77
                }
78
#endif
79
                SDL_Delay(100);
80
            }
81
#endif
82
            SDL_Log("done.");
83
            SDL_DestroyAudioStream(stream);
84
        }
85
        stream = NULL;
86
    }
87

88
    /* note that Emscripten currently doesn't run this part (but maybe only has a single audio device anyhow?) */
89
    SDL_Log("Playing on all devices...\n");
90
    streams = (SDL_AudioStream **) SDL_calloc(devcount, sizeof (SDL_AudioStream *));
91
    if (!streams) {
92
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
93
    } else {
94
        for (i = 0; i < devcount; i++) {
95
            streams[i] = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL);
96
            if (streams[i] == NULL) {
97
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed for device %d of %d: %s", i, devcount, SDL_GetError());
98
            } else {
99
                SDL_PutAudioStreamData(streams[i], sound, soundlen);
100
                SDL_FlushAudioStream(streams[i]);
101
            }
102
        }
103

104
        /* try to start all the devices about the same time. SDL does not guarantee sync across physical devices. */
105
        for (i = 0; i < devcount; i++) {
106
            if (streams[i]) {
107
                SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(streams[i]));
108
            }
109
        }
110

111
        while (keep_going) {
112
            keep_going = 0;
113
            for (i = 0; i < devcount; i++) {
114
                if (streams[i] && (SDL_GetAudioStreamAvailable(streams[i]) > 0)) {
115
                    keep_going = 1;
116
                }
117
            }
118
#ifdef SDL_PLATFORM_ANDROID
119
            /* Empty queue, some application events would prevent pause. */
120
            while (SDL_PollEvent(&event)) {}
121
#endif
122

123
            SDL_Delay(100);
124
        }
125

126
        for (i = 0; i < devcount; i++) {
127
            SDL_DestroyAudioStream(streams[i]);
128
        }
129

130
        SDL_free(streams);
131
    }
132

133
    SDL_Log("All done!\n");
134
}
135

136
int main(int argc, char **argv)
137
{
138
    SDL_AudioDeviceID *devices;
139
    int devcount = 0;
140
    int i;
141
    char *filename = NULL;
142
    SDLTest_CommonState *state;
143

144
    /* Initialize test framework */
145
    state = SDLTest_CommonCreateState(argv, 0);
146
    if (!state) {
147
        return 1;
148
    }
149

150
    /* Enable standard application logging */
151
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
152

153
    /* Parse commandline */
154
    for (i = 1; i < argc;) {
155
        int consumed;
156

157
        consumed = SDLTest_CommonArg(state, i);
158
        if (!consumed) {
159
            if (!filename) {
160
                filename = argv[i];
161
                consumed = 1;
162
            }
163
        }
164
        if (consumed <= 0) {
165
            static const char *options[] = { "[sample.wav]", NULL };
166
            SDLTest_CommonLogUsage(state, argv[0], options);
167
            return 1;
168
        }
169

170
        i += consumed;
171
    }
172

173
    /* Load the SDL library */
174
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
175
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
176
        return 1;
177
    }
178

179
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
180

181
    filename = GetResourceFilename(filename, "sample.wav");
182

183
    devices = SDL_GetAudioPlaybackDevices(&devcount);
184
    if (!devices) {
185
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio playback devices!");
186
    } else {
187
        /* Load the wave file into memory */
188
        if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) < 0) {
189
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename,
190
                         SDL_GetError());
191
        } else {
192
            test_multi_audio(devices, devcount);
193
            SDL_free(sound);
194
        }
195
        SDL_free(devices);
196
    }
197

198
    SDL_free(filename);
199

200
    SDL_Quit();
201
    SDLTest_CommonDestroyState(state);
202

203
    return 0;
204
}
205

206

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.