test/testautomation_audio.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 11272 d60ede5a0c2e
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
aschiffler@6772
     1
/**
aschiffler@6772
     2
 * Original code: automated SDL audio test written by Edgar Simo "bobbens"
aschiffler@6772
     3
 * New/updated tests: aschiffler at ferzkopp dot net
aschiffler@6772
     4
 */
aschiffler@6772
     5
aschiffler@8994
     6
/* quiet windows compiler warnings */
aschiffler@8994
     7
#define _CRT_SECURE_NO_WARNINGS
aschiffler@8994
     8
aschiffler@6772
     9
#include <stdio.h>
aschiffler@6819
    10
#include <string.h>
aschiffler@6772
    11
aschiffler@6772
    12
#include "SDL.h"
aschiffler@6772
    13
#include "SDL_test.h"
aschiffler@6772
    14
aschiffler@6772
    15
/* ================= Test Case Implementation ================== */
aschiffler@6772
    16
aschiffler@6772
    17
/* Fixture */
aschiffler@6772
    18
aschiffler@6772
    19
void
aschiffler@6772
    20
_audioSetUp(void *arg)
aschiffler@6772
    21
{
slouken@7191
    22
    /* Start SDL audio subsystem */
slouken@7191
    23
    int ret = SDL_InitSubSystem( SDL_INIT_AUDIO );
aschiffler@6772
    24
        SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO)");
slouken@7191
    25
    SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_AUDIO)");
slouken@7191
    26
    if (ret != 0) {
aschiffler@6772
    27
           SDLTest_LogError("%s", SDL_GetError());
aschiffler@6772
    28
        }
aschiffler@6772
    29
}
aschiffler@6772
    30
aschiffler@8994
    31
void
aschiffler@8994
    32
_audioTearDown(void *arg)
aschiffler@8994
    33
{
aschiffler@8994
    34
    /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
aschiffler@8994
    35
    remove("sdlaudio.raw");
aschiffler@8994
    36
aschiffler@8994
    37
    SDLTest_AssertPass("Cleanup of test files completed");
aschiffler@8994
    38
}
aschiffler@8994
    39
aschiffler@8994
    40
aschiffler@8994
    41
/* Global counter for callback invocation */
aschiffler@8994
    42
int _audio_testCallbackCounter;
aschiffler@8994
    43
aschiffler@8994
    44
/* Global accumulator for total callback length */
aschiffler@8994
    45
int _audio_testCallbackLength;
aschiffler@8994
    46
aschiffler@6772
    47
aschiffler@7093
    48
/* Test callback function */
slouken@11272
    49
void SDLCALL _audio_testCallback(void *userdata, Uint8 *stream, int len)
aschiffler@7093
    50
{
aschiffler@8994
    51
   /* track that callback was called */
aschiffler@8994
    52
   _audio_testCallbackCounter++;
aschiffler@8994
    53
   _audio_testCallbackLength += len;
aschiffler@7093
    54
}
aschiffler@7093
    55
aschiffler@7093
    56
aschiffler@6772
    57
/* Test case functions */
aschiffler@6772
    58
aschiffler@6772
    59
/**
aschiffler@7093
    60
 * \brief Stop and restart audio subsystem
slouken@7191
    61
 *
aschiffler@8994
    62
 * \sa https://wiki.libsdl.org/SDL_QuitSubSystem
aschiffler@8994
    63
 * \sa https://wiki.libsdl.org/SDL_InitSubSystem
aschiffler@7093
    64
 */
aschiffler@7093
    65
int audio_quitInitAudioSubSystem()
aschiffler@7093
    66
{
slouken@7191
    67
    /* Stop SDL audio subsystem */
slouken@7191
    68
    SDL_QuitSubSystem( SDL_INIT_AUDIO );
aschiffler@7093
    69
        SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
aschiffler@7093
    70
aschiffler@7093
    71
        /* Restart audio again */
aschiffler@7093
    72
        _audioSetUp(NULL);
philipp@7126
    73
philipp@7126
    74
    return TEST_COMPLETED;
aschiffler@7093
    75
}
aschiffler@7093
    76
aschiffler@7093
    77
/**
aschiffler@7093
    78
 * \brief Start and stop audio directly
slouken@7191
    79
 *
aschiffler@8994
    80
 * \sa https://wiki.libsdl.org/SDL_InitAudio
aschiffler@8994
    81
 * \sa https://wiki.libsdl.org/SDL_QuitAudio
aschiffler@7093
    82
 */
aschiffler@7093
    83
int audio_initQuitAudio()
aschiffler@7093
    84
{
aschiffler@7093
    85
        int result;
slouken@7191
    86
    int i, iMax;
slouken@7191
    87
    const char* audioDriver;
slouken@7191
    88
slouken@7191
    89
    /* Stop SDL audio subsystem */
slouken@7191
    90
    SDL_QuitSubSystem( SDL_INIT_AUDIO );
aschiffler@7093
    91
        SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
aschiffler@7093
    92
aschiffler@7093
    93
        /* Loop over all available audio drivers */
aschiffler@7093
    94
        iMax = SDL_GetNumAudioDrivers();
philipp@9219
    95
        SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
aschiffler@7093
    96
        SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
aschiffler@7093
    97
        for (i = 0; i < iMax; i++) {
slouken@7191
    98
            audioDriver = SDL_GetAudioDriver(i);
slouken@7191
    99
            SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
slouken@7191
   100
            SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
slouken@7721
   101
            SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
slouken@7191
   102
slouken@7191
   103
            /* Call Init */
slouken@7191
   104
            result = SDL_AudioInit(audioDriver);
slouken@7191
   105
            SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
slouken@7191
   106
            SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
slouken@7191
   107
slouken@7191
   108
            /* Call Quit */
slouken@7191
   109
            SDL_AudioQuit();
slouken@7191
   110
            SDLTest_AssertPass("Call to SDL_AudioQuit()");
slouken@7191
   111
    }
slouken@7191
   112
slouken@7191
   113
    /* NULL driver specification */
slouken@7191
   114
    audioDriver = NULL;
slouken@7191
   115
slouken@7191
   116
    /* Call Init */
slouken@7191
   117
    result = SDL_AudioInit(audioDriver);
slouken@7191
   118
    SDLTest_AssertPass("Call to SDL_AudioInit(NULL)");
slouken@7191
   119
    SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
slouken@7191
   120
slouken@7191
   121
    /* Call Quit */
slouken@7191
   122
    SDL_AudioQuit();
slouken@7191
   123
    SDLTest_AssertPass("Call to SDL_AudioQuit()");
slouken@7191
   124
aschiffler@7093
   125
        /* Restart audio again */
aschiffler@7093
   126
        _audioSetUp(NULL);
philipp@7126
   127
philipp@7126
   128
    return TEST_COMPLETED;
aschiffler@7093
   129
}
aschiffler@7093
   130
aschiffler@7093
   131
/**
aschiffler@7093
   132
 * \brief Start, open, close and stop audio
slouken@7191
   133
 *
aschiffler@8994
   134
 * \sa https://wiki.libsdl.org/SDL_InitAudio
aschiffler@8994
   135
 * \sa https://wiki.libsdl.org/SDL_OpenAudio
aschiffler@8994
   136
 * \sa https://wiki.libsdl.org/SDL_CloseAudio
aschiffler@8994
   137
 * \sa https://wiki.libsdl.org/SDL_QuitAudio
aschiffler@7093
   138
 */
aschiffler@7093
   139
int audio_initOpenCloseQuitAudio()
aschiffler@7093
   140
{
aschiffler@8994
   141
    int result, expectedResult;
aschiffler@8994
   142
    int i, iMax, j, k;
aschiffler@8994
   143
    const char* audioDriver;
aschiffler@8994
   144
    SDL_AudioSpec desired;
aschiffler@8994
   145
aschiffler@8994
   146
    /* Stop SDL audio subsystem */
aschiffler@8994
   147
    SDL_QuitSubSystem( SDL_INIT_AUDIO );
aschiffler@8994
   148
        SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
aschiffler@8994
   149
aschiffler@8994
   150
        /* Loop over all available audio drivers */
aschiffler@8994
   151
        iMax = SDL_GetNumAudioDrivers();
philipp@9219
   152
        SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
aschiffler@8994
   153
        SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
aschiffler@8994
   154
        for (i = 0; i < iMax; i++) {
aschiffler@8994
   155
            audioDriver = SDL_GetAudioDriver(i);
aschiffler@8994
   156
            SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
aschiffler@8994
   157
            SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
aschiffler@8994
   158
            SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
aschiffler@8994
   159
aschiffler@8994
   160
            /* Change specs */
aschiffler@8994
   161
            for (j = 0; j < 2; j++) {
aschiffler@8994
   162
aschiffler@8994
   163
                /* Call Init */
aschiffler@8994
   164
                result = SDL_AudioInit(audioDriver);
aschiffler@8994
   165
                SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
aschiffler@8994
   166
                SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
aschiffler@8994
   167
aschiffler@8994
   168
                /* Set spec */
aschiffler@8994
   169
                SDL_memset(&desired, 0, sizeof(desired));
aschiffler@8994
   170
                switch (j) {
aschiffler@8994
   171
                    case 0:
aschiffler@8994
   172
                    /* Set standard desired spec */
aschiffler@8994
   173
                    desired.freq = 22050;
aschiffler@8994
   174
                    desired.format = AUDIO_S16SYS;
aschiffler@8994
   175
                    desired.channels = 2;
aschiffler@8994
   176
                    desired.samples = 4096;
aschiffler@8994
   177
                    desired.callback = _audio_testCallback;
aschiffler@8994
   178
                    desired.userdata = NULL;
aschiffler@8994
   179
aschiffler@8994
   180
                    case 1:
aschiffler@8994
   181
                    /* Set custom desired spec */
aschiffler@8994
   182
                    desired.freq = 48000;
aschiffler@8994
   183
                    desired.format = AUDIO_F32SYS;
aschiffler@8994
   184
                    desired.channels = 2;
aschiffler@8994
   185
                    desired.samples = 2048;
aschiffler@8994
   186
                    desired.callback = _audio_testCallback;
aschiffler@8994
   187
                    desired.userdata = NULL;
aschiffler@8994
   188
                    break;
aschiffler@8994
   189
            }
aschiffler@8994
   190
aschiffler@8994
   191
            /* Call Open (maybe multiple times) */
aschiffler@8994
   192
            for (k=0; k <= j; k++) {
aschiffler@8994
   193
                result = SDL_OpenAudio(&desired, NULL);
aschiffler@8994
   194
                SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL), call %d", j, k+1);
aschiffler@8994
   195
                expectedResult = (k==0) ? 0 : -1;
aschiffler@8994
   196
                SDLTest_AssertCheck(result == expectedResult, "Verify return value; expected: %d, got: %d", expectedResult, result);
aschiffler@8994
   197
            }
aschiffler@8994
   198
aschiffler@8994
   199
            /* Call Close (maybe multiple times) */
aschiffler@8994
   200
            for (k=0; k <= j; k++) {
aschiffler@8994
   201
                SDL_CloseAudio();
aschiffler@8994
   202
                SDLTest_AssertPass("Call to SDL_CloseAudio(), call %d", k+1);
aschiffler@8994
   203
            }
aschiffler@8994
   204
aschiffler@8994
   205
            /* Call Quit (maybe multiple times) */
aschiffler@8994
   206
            for (k=0; k <= j; k++) {
aschiffler@8994
   207
                SDL_AudioQuit();
aschiffler@8994
   208
                SDLTest_AssertPass("Call to SDL_AudioQuit(), call %d", k+1);
aschiffler@8994
   209
            }
aschiffler@8994
   210
aschiffler@8994
   211
        } /* spec loop */
aschiffler@8994
   212
    } /* driver loop */
aschiffler@8994
   213
aschiffler@8994
   214
        /* Restart audio again */
aschiffler@8994
   215
        _audioSetUp(NULL);
aschiffler@8994
   216
aschiffler@8994
   217
    return TEST_COMPLETED;
aschiffler@8994
   218
}
aschiffler@8994
   219
aschiffler@8994
   220
/**
aschiffler@8994
   221
 * \brief Pause and unpause audio
aschiffler@8994
   222
 *
aschiffler@8994
   223
 * \sa https://wiki.libsdl.org/SDL_PauseAudio
aschiffler@8994
   224
 */
aschiffler@8994
   225
int audio_pauseUnpauseAudio()
aschiffler@8994
   226
{
aschiffler@7134
   227
    int result;
aschiffler@8994
   228
    int i, iMax, j, k, l;
aschiffler@8994
   229
    int totalDelay;
aschiffler@8994
   230
    int pause_on;
aschiffler@8994
   231
    int originalCounter;
slouken@7191
   232
    const char* audioDriver;
slouken@7191
   233
    SDL_AudioSpec desired;
slouken@7191
   234
slouken@7191
   235
    /* Stop SDL audio subsystem */
slouken@7191
   236
    SDL_QuitSubSystem( SDL_INIT_AUDIO );
aschiffler@7093
   237
        SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
aschiffler@7093
   238
aschiffler@7093
   239
        /* Loop over all available audio drivers */
aschiffler@7093
   240
        iMax = SDL_GetNumAudioDrivers();
philipp@9219
   241
        SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
aschiffler@7093
   242
        SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
aschiffler@7093
   243
        for (i = 0; i < iMax; i++) {
slouken@7191
   244
            audioDriver = SDL_GetAudioDriver(i);
slouken@7191
   245
            SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
slouken@7191
   246
            SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
slouken@7721
   247
            SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
slouken@7191
   248
slouken@7191
   249
            /* Change specs */
slouken@7191
   250
            for (j = 0; j < 2; j++) {
slouken@7191
   251
slouken@7191
   252
                /* Call Init */
slouken@7191
   253
                result = SDL_AudioInit(audioDriver);
slouken@7191
   254
                SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
slouken@7191
   255
                SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
slouken@7191
   256
slouken@7191
   257
                /* Set spec */
slouken@7191
   258
                SDL_memset(&desired, 0, sizeof(desired));
slouken@7191
   259
                switch (j) {
slouken@7191
   260
                    case 0:
slouken@7191
   261
                    /* Set standard desired spec */
slouken@7191
   262
                    desired.freq = 22050;
slouken@7191
   263
                    desired.format = AUDIO_S16SYS;
slouken@7191
   264
                    desired.channels = 2;
slouken@7191
   265
                    desired.samples = 4096;
slouken@7191
   266
                    desired.callback = _audio_testCallback;
slouken@7191
   267
                    desired.userdata = NULL;
aschiffler@7093
   268
slouken@7191
   269
                    case 1:
slouken@7191
   270
                    /* Set custom desired spec */
slouken@7191
   271
                    desired.freq = 48000;
slouken@7191
   272
                    desired.format = AUDIO_F32SYS;
slouken@7191
   273
                    desired.channels = 2;
slouken@7191
   274
                    desired.samples = 2048;
slouken@7191
   275
                    desired.callback = _audio_testCallback;
slouken@7191
   276
                    desired.userdata = NULL;
slouken@7191
   277
                    break;
slouken@7191
   278
            }
slouken@7191
   279
slouken@7191
   280
            /* Call Open */
slouken@7191
   281
            result = SDL_OpenAudio(&desired, NULL);
slouken@7191
   282
            SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL)", j);
slouken@7191
   283
            SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0 got: %d", result);
slouken@7191
   284
aschiffler@8994
   285
            /* Start and stop audio multiple times */
aschiffler@8994
   286
            for (l=0; l<3; l++) {
aschiffler@8994
   287
                SDLTest_Log("Pause/Unpause iteration: %d", l+1);
aschiffler@8994
   288
            
aschiffler@8994
   289
                /* Reset callback counters */
aschiffler@8994
   290
                _audio_testCallbackCounter = 0;
aschiffler@8994
   291
                _audio_testCallbackLength = 0;
aschiffler@8994
   292
aschiffler@8994
   293
                /* Un-pause audio to start playing (maybe multiple times) */
aschiffler@8994
   294
                pause_on = 0;
aschiffler@8994
   295
                for (k=0; k <= j; k++) {
aschiffler@8994
   296
                    SDL_PauseAudio(pause_on);
aschiffler@8994
   297
                    SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
aschiffler@8994
   298
                }
aschiffler@8994
   299
            
aschiffler@8994
   300
                /* Wait for callback */
aschiffler@8994
   301
                totalDelay = 0;
aschiffler@8994
   302
                do {
aschiffler@8994
   303
                    SDL_Delay(10);
aschiffler@8994
   304
                    totalDelay += 10;
aschiffler@8994
   305
                } 
aschiffler@8994
   306
                while (_audio_testCallbackCounter == 0 && totalDelay < 1000);
aschiffler@8994
   307
                SDLTest_AssertCheck(_audio_testCallbackCounter > 0, "Verify callback counter; expected: >0 got: %d", _audio_testCallbackCounter);
aschiffler@8994
   308
                SDLTest_AssertCheck(_audio_testCallbackLength > 0, "Verify callback length; expected: >0 got: %d", _audio_testCallbackLength);
aschiffler@8994
   309
aschiffler@8994
   310
                /* Pause audio to stop playing (maybe multiple times) */
aschiffler@8994
   311
                for (k=0; k <= j; k++) {
aschiffler@8994
   312
                    pause_on = (k==0) ? 1 : SDLTest_RandomIntegerInRange(99, 9999);
aschiffler@8994
   313
                    SDL_PauseAudio(pause_on);
aschiffler@8994
   314
                    SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
aschiffler@8994
   315
                }
aschiffler@8994
   316
            
aschiffler@8994
   317
                /* Ensure callback is not called again */
aschiffler@8994
   318
                originalCounter = _audio_testCallbackCounter;
aschiffler@8994
   319
                SDL_Delay(totalDelay + 10);
aschiffler@8994
   320
                SDLTest_AssertCheck(originalCounter == _audio_testCallbackCounter, "Verify callback counter; expected: %d, got: %d", originalCounter, _audio_testCallbackCounter);
aschiffler@8994
   321
            }
aschiffler@8994
   322
slouken@7191
   323
            /* Call Close */
aschiffler@8994
   324
            SDL_CloseAudio();
aschiffler@8994
   325
            SDLTest_AssertPass("Call to SDL_CloseAudio()");
slouken@7191
   326
slouken@7191
   327
            /* Call Quit */
slouken@7191
   328
            SDL_AudioQuit();
slouken@7191
   329
            SDLTest_AssertPass("Call to SDL_AudioQuit()");
slouken@7191
   330
slouken@7191
   331
        } /* spec loop */
slouken@7191
   332
    } /* driver loop */
slouken@7191
   333
aschiffler@8994
   334
    /* Restart audio again */
aschiffler@8994
   335
    _audioSetUp(NULL);
philipp@7126
   336
philipp@7126
   337
    return TEST_COMPLETED;
aschiffler@7093
   338
}
aschiffler@7093
   339
aschiffler@7093
   340
/**
aschiffler@6772
   341
 * \brief Enumerate and name available audio devices (output and capture).
slouken@7191
   342
 *
aschiffler@8994
   343
 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
aschiffler@8994
   344
 * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
aschiffler@6772
   345
 */
aschiffler@6772
   346
int audio_enumerateAndNameAudioDevices()
aschiffler@6772
   347
{
aschiffler@6772
   348
   int t, tt;
aschiffler@6772
   349
   int i, n, nn;
aschiffler@6772
   350
   const char *name, *nameAgain;
aschiffler@6772
   351
aschiffler@6772
   352
   /* Iterate over types: t=0 output device, t=1 input/capture device */
aschiffler@6772
   353
   for (t=0; t<2; t++) {
slouken@7191
   354
aschiffler@6772
   355
      /* Get number of devices. */
aschiffler@6772
   356
      n = SDL_GetNumAudioDevices(t);
aschiffler@6772
   357
      SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(%i)", t);
aschiffler@6897
   358
      SDLTest_Log("Number of %s devices < 0, reported as %i", (t) ? "capture" : "output", n);
aschiffler@6772
   359
      SDLTest_AssertCheck(n >= 0, "Validate result is >= 0, got: %i", n);
aschiffler@6772
   360
aschiffler@6772
   361
      /* Variation of non-zero type */
aschiffler@6772
   362
      if (t==1) {
aschiffler@6772
   363
         tt = t + SDLTest_RandomIntegerInRange(1,10);
aschiffler@6772
   364
         nn = SDL_GetNumAudioDevices(tt);
aschiffler@6772
   365
         SDLTest_AssertCheck(n==nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", tt, n, nn);
aschiffler@6772
   366
         nn = SDL_GetNumAudioDevices(-tt);
aschiffler@6772
   367
         SDLTest_AssertCheck(n==nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", -tt, n, nn);
slouken@7191
   368
      }
slouken@7191
   369
aschiffler@6772
   370
      /* List devices. */
aschiffler@6772
   371
      if (n>0) {
aschiffler@6772
   372
         for (i=0; i<n; i++) {
aschiffler@6772
   373
            name = SDL_GetAudioDeviceName(i, t);
aschiffler@6772
   374
            SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
aschiffler@6772
   375
            SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, t);
aschiffler@6772
   376
            if (name != NULL) {
slouken@7721
   377
              SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name);
aschiffler@6772
   378
              if (t==1) {
aschiffler@6772
   379
                  /* Also try non-zero type */
aschiffler@6772
   380
                  tt = t + SDLTest_RandomIntegerInRange(1,10);
aschiffler@6772
   381
                  nameAgain = SDL_GetAudioDeviceName(i, tt);
aschiffler@6772
   382
                  SDLTest_AssertCheck(nameAgain != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, tt);
aschiffler@6772
   383
                  if (nameAgain != NULL) {
slouken@7721
   384
                    SDLTest_AssertCheck(nameAgain[0] != '\0', "Verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, tt, nameAgain);
slouken@7191
   385
                    SDLTest_AssertCheck(SDL_strcmp(name, nameAgain)==0,
slouken@7191
   386
                      "Verify SDL_GetAudioDeviceName(%i, %i) and SDL_GetAudioDeviceName(%i %i) return the same string",
aschiffler@6772
   387
                      i, t, i, tt);
aschiffler@6772
   388
                  }
aschiffler@6772
   389
               }
aschiffler@6772
   390
            }
aschiffler@6772
   391
         }
aschiffler@6772
   392
      }
aschiffler@6772
   393
   }
slouken@7191
   394
aschiffler@6772
   395
   return TEST_COMPLETED;
aschiffler@6772
   396
}
aschiffler@6772
   397
aschiffler@6772
   398
/**
aschiffler@6772
   399
 * \brief Negative tests around enumeration and naming of audio devices.
slouken@7191
   400
 *
aschiffler@8994
   401
 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
aschiffler@8994
   402
 * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
aschiffler@6772
   403
 */
aschiffler@6772
   404
int audio_enumerateAndNameAudioDevicesNegativeTests()
aschiffler@6772
   405
{
aschiffler@6772
   406
   int t;
aschiffler@6772
   407
   int i, j, no, nc;
aschiffler@6772
   408
   const char *name;
slouken@7191
   409
aschiffler@6772
   410
   /* Get number of devices. */
aschiffler@6772
   411
   no = SDL_GetNumAudioDevices(0);
aschiffler@6772
   412
   SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
aschiffler@6772
   413
   nc = SDL_GetNumAudioDevices(1);
aschiffler@6772
   414
   SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(1)");
slouken@7191
   415
aschiffler@6772
   416
   /* Invalid device index when getting name */
aschiffler@6772
   417
   for (t=0; t<2; t++) {
aschiffler@6772
   418
      /* Negative device index */
aschiffler@6772
   419
      i = SDLTest_RandomIntegerInRange(-10,-1);
aschiffler@6772
   420
      name = SDL_GetAudioDeviceName(i, t);
aschiffler@6772
   421
      SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
aschiffler@6772
   422
      SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result NULL, expected NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
slouken@7191
   423
aschiffler@6772
   424
      /* Device index past range */
aschiffler@6772
   425
      for (j=0; j<3; j++) {
aschiffler@6772
   426
         i = (t) ? nc+j : no+j;
aschiffler@6772
   427
         name = SDL_GetAudioDeviceName(i, t);
aschiffler@6772
   428
         SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
aschiffler@6772
   429
         SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
aschiffler@6772
   430
      }
slouken@7191
   431
aschiffler@6772
   432
      /* Capture index past capture range but within output range */
aschiffler@6772
   433
      if ((no>0) && (no>nc) && (t==1)) {
aschiffler@6772
   434
         i = no-1;
aschiffler@6772
   435
         name = SDL_GetAudioDeviceName(i, t);
aschiffler@6772
   436
         SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
aschiffler@6772
   437
         SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
aschiffler@6772
   438
      }
aschiffler@6772
   439
   }
aschiffler@6772
   440
aschiffler@6772
   441
   return TEST_COMPLETED;
aschiffler@6772
   442
}
aschiffler@6772
   443
aschiffler@6772
   444
aschiffler@6772
   445
/**
aschiffler@6819
   446
 * \brief Checks available audio driver names.
aschiffler@6819
   447
 *
aschiffler@8994
   448
 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDrivers
aschiffler@8994
   449
 * \sa https://wiki.libsdl.org/SDL_GetAudioDriver
aschiffler@6772
   450
 */
aschiffler@6772
   451
int audio_printAudioDrivers()
aschiffler@6772
   452
{
aschiffler@6772
   453
   int i, n;
aschiffler@6772
   454
   const char *name;
aschiffler@6772
   455
aschiffler@6772
   456
   /* Get number of drivers */
aschiffler@6772
   457
   n = SDL_GetNumAudioDrivers();
aschiffler@6772
   458
   SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
aschiffler@6772
   459
   SDLTest_AssertCheck(n>=0, "Verify number of audio drivers >= 0, got: %i", n);
slouken@7191
   460
aschiffler@6772
   461
   /* List drivers. */
aschiffler@6772
   462
   if (n>0)
aschiffler@6772
   463
   {
aschiffler@6772
   464
      for (i=0; i<n; i++) {
aschiffler@6772
   465
         name = SDL_GetAudioDriver(i);
aschiffler@6772
   466
         SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i);
aschiffler@6772
   467
         SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
aschiffler@6772
   468
         if (name != NULL) {
slouken@7721
   469
            SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
aschiffler@6772
   470
         }
aschiffler@6772
   471
      }
aschiffler@6772
   472
   }
aschiffler@6772
   473
aschiffler@6772
   474
   return TEST_COMPLETED;
aschiffler@6772
   475
}
aschiffler@6772
   476
aschiffler@6772
   477
aschiffler@6772
   478
/**
aschiffler@6819
   479
 * \brief Checks current audio driver name with initialized audio.
aschiffler@6819
   480
 *
aschiffler@8994
   481
 * \sa https://wiki.libsdl.org/SDL_GetCurrentAudioDriver
aschiffler@6772
   482
 */
aschiffler@6772
   483
int audio_printCurrentAudioDriver()
aschiffler@6772
   484
{
aschiffler@6772
   485
   /* Check current audio driver */
aschiffler@6830
   486
   const char *name = SDL_GetCurrentAudioDriver();
aschiffler@6772
   487
   SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()");
aschiffler@6772
   488
   SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
aschiffler@6772
   489
   if (name != NULL) {
slouken@7721
   490
      SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
aschiffler@6772
   491
   }
aschiffler@6772
   492
aschiffler@6772
   493
   return TEST_COMPLETED;
aschiffler@6772
   494
}
aschiffler@6772
   495
aschiffler@6830
   496
/* Definition of all formats, channels, and frequencies used to test audio conversions */
aschiffler@7134
   497
const int _numAudioFormats = 18;
slouken@7191
   498
SDL_AudioFormat _audioFormats[] = { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16SYS, AUDIO_S16, AUDIO_U16LSB,
slouken@7191
   499
                AUDIO_U16MSB, AUDIO_U16SYS, AUDIO_U16, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32SYS, AUDIO_S32,
aschiffler@6830
   500
                                AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32SYS, AUDIO_F32 };
slouken@7191
   501
char *_audioFormatsVerbose[] = { "AUDIO_S8", "AUDIO_U8", "AUDIO_S16LSB", "AUDIO_S16MSB", "AUDIO_S16SYS", "AUDIO_S16", "AUDIO_U16LSB",
slouken@7191
   502
                "AUDIO_U16MSB", "AUDIO_U16SYS", "AUDIO_U16", "AUDIO_S32LSB", "AUDIO_S32MSB", "AUDIO_S32SYS", "AUDIO_S32",
aschiffler@6830
   503
                                "AUDIO_F32LSB", "AUDIO_F32MSB", "AUDIO_F32SYS", "AUDIO_F32" };
aschiffler@7134
   504
const int _numAudioChannels = 4;
aschiffler@7134
   505
Uint8 _audioChannels[] = { 1, 2, 4, 6 };
aschiffler@7134
   506
const int _numAudioFrequencies = 4;
aschiffler@7134
   507
int _audioFrequencies[] = { 11025, 22050, 44100, 48000 };
aschiffler@6830
   508
aschiffler@6819
   509
aschiffler@6819
   510
/**
aschiffler@6819
   511
 * \brief Builds various audio conversion structures
aschiffler@6819
   512
 *
aschiffler@8994
   513
 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
aschiffler@6819
   514
 */
aschiffler@6819
   515
int audio_buildAudioCVT()
aschiffler@6819
   516
{
aschiffler@6819
   517
  int result;
aschiffler@6819
   518
  SDL_AudioCVT  cvt;
aschiffler@6819
   519
  SDL_AudioSpec spec1;
aschiffler@6819
   520
  SDL_AudioSpec spec2;
aschiffler@6830
   521
  int i, ii, j, jj, k, kk;
slouken@7191
   522
aschiffler@6819
   523
  /* No conversion needed */
aschiffler@6819
   524
  spec1.format = AUDIO_S16LSB;
aschiffler@6819
   525
  spec1.channels = 2;
aschiffler@6819
   526
  spec1.freq = 22050;
aschiffler@6819
   527
  result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
aschiffler@6819
   528
                                   spec1.format, spec1.channels, spec1.freq);
aschiffler@6819
   529
  SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec1)");
aschiffler@6819
   530
  SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0, got: %i", result);
aschiffler@6819
   531
aschiffler@6819
   532
  /* Typical conversion */
aschiffler@6819
   533
  spec1.format = AUDIO_S8;
aschiffler@6819
   534
  spec1.channels = 1;
aschiffler@6819
   535
  spec1.freq = 22050;
aschiffler@6819
   536
  spec2.format = AUDIO_S16LSB;
aschiffler@6819
   537
  spec2.channels = 2;
slouken@7191
   538
  spec2.freq = 44100;
aschiffler@6819
   539
  result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
aschiffler@6819
   540
                                   spec2.format, spec2.channels, spec2.freq);
aschiffler@6819
   541
  SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
aschiffler@6819
   542
  SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
aschiffler@6819
   543
aschiffler@6830
   544
  /* All source conversions with random conversion targets, allow 'null' conversions */
aschiffler@7134
   545
  for (i = 0; i < _numAudioFormats; i++) {
aschiffler@7134
   546
    for (j = 0; j < _numAudioChannels; j++) {
aschiffler@7134
   547
      for (k = 0; k < _numAudioFrequencies; k++) {
aschiffler@7134
   548
        spec1.format = _audioFormats[i];
aschiffler@7134
   549
        spec1.channels = _audioChannels[j];
aschiffler@7134
   550
        spec1.freq = _audioFrequencies[k];
aschiffler@7134
   551
        ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
aschiffler@7134
   552
        jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
aschiffler@7134
   553
        kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
aschiffler@7134
   554
        spec2.format = _audioFormats[ii];
aschiffler@7134
   555
        spec2.channels = _audioChannels[jj];
aschiffler@7134
   556
        spec2.freq = _audioFrequencies[kk];
aschiffler@6819
   557
        result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
aschiffler@6830
   558
                                         spec2.format, spec2.channels, spec2.freq);
slouken@7191
   559
        SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
aschiffler@7134
   560
            i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
aschiffler@6819
   561
        SDLTest_AssertCheck(result == 0 || result == 1, "Verify result value; expected: 0 or 1, got: %i", result);
aschiffler@6819
   562
        if (result<0) {
philipp@8853
   563
          SDLTest_LogError("%s", SDL_GetError());
aschiffler@6830
   564
        } else {
aschiffler@6830
   565
          SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
aschiffler@6819
   566
        }
aschiffler@6819
   567
      }
aschiffler@6819
   568
    }
aschiffler@6819
   569
  }
aschiffler@6819
   570
aschiffler@6819
   571
   return TEST_COMPLETED;
aschiffler@6819
   572
}
aschiffler@6819
   573
aschiffler@6819
   574
/**
aschiffler@6819
   575
 * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
aschiffler@6819
   576
 *
aschiffler@8994
   577
 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
aschiffler@6819
   578
 */
aschiffler@6819
   579
int audio_buildAudioCVTNegative()
aschiffler@6819
   580
{
aschiffler@6819
   581
  const char *expectedError = "Parameter 'cvt' is invalid";
slouken@7191
   582
  const char *error;
aschiffler@6819
   583
  int result;
aschiffler@6819
   584
  SDL_AudioCVT  cvt;
aschiffler@6819
   585
  SDL_AudioSpec spec1;
aschiffler@6819
   586
  SDL_AudioSpec spec2;
aschiffler@6819
   587
  int i;
aschiffler@6819
   588
  char message[256];
aschiffler@6819
   589
slouken@7191
   590
  /* Valid format */
aschiffler@6819
   591
  spec1.format = AUDIO_S8;
aschiffler@6819
   592
  spec1.channels = 1;
aschiffler@6819
   593
  spec1.freq = 22050;
aschiffler@6819
   594
  spec2.format = AUDIO_S16LSB;
aschiffler@6819
   595
  spec2.channels = 2;
slouken@7191
   596
  spec2.freq = 44100;
aschiffler@6819
   597
aschiffler@6819
   598
  SDL_ClearError();
aschiffler@6819
   599
  SDLTest_AssertPass("Call to SDL_ClearError()");
aschiffler@6819
   600
aschiffler@6819
   601
  /* NULL input for CVT buffer */
aschiffler@6819
   602
  result = SDL_BuildAudioCVT((SDL_AudioCVT *)NULL, spec1.format, spec1.channels, spec1.freq,
aschiffler@6819
   603
                                                   spec2.format, spec2.channels, spec2.freq);
aschiffler@6819
   604
  SDLTest_AssertPass("Call to SDL_BuildAudioCVT(NULL,...)");
slouken@7191
   605
  SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
aschiffler@6819
   606
  error = SDL_GetError();
aschiffler@6819
   607
  SDLTest_AssertPass("Call to SDL_GetError()");
aschiffler@6819
   608
  SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
aschiffler@6819
   609
  if (error != NULL) {
slouken@7191
   610
      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
aschiffler@6819
   611
          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
aschiffler@6819
   612
  }
aschiffler@6819
   613
aschiffler@6819
   614
  /* Invalid conversions */
aschiffler@6819
   615
  for (i = 1; i < 64; i++) {
slouken@7191
   616
    /* Valid format to start with */
aschiffler@6819
   617
    spec1.format = AUDIO_S8;
aschiffler@6819
   618
    spec1.channels = 1;
aschiffler@6819
   619
    spec1.freq = 22050;
aschiffler@6819
   620
    spec2.format = AUDIO_S16LSB;
aschiffler@6819
   621
    spec2.channels = 2;
slouken@7191
   622
    spec2.freq = 44100;
aschiffler@6819
   623
aschiffler@6819
   624
    SDL_ClearError();
aschiffler@6819
   625
    SDLTest_AssertPass("Call to SDL_ClearError()");
slouken@7191
   626
aschiffler@6819
   627
    /* Set various invalid format inputs */
aschiffler@6822
   628
    SDL_strlcpy(message, "Invalid: ", 256);
aschiffler@6819
   629
    if (i & 1) {
aschiffler@6822
   630
        SDL_strlcat(message, " spec1.format", 256);
aschiffler@6819
   631
        spec1.format = 0;
aschiffler@6819
   632
    }
slouken@7191
   633
    if (i & 2) {
aschiffler@6822
   634
        SDL_strlcat(message, " spec1.channels", 256);
aschiffler@6819
   635
        spec1.channels = 0;
aschiffler@6819
   636
    }
slouken@7191
   637
    if (i & 4) {
aschiffler@6822
   638
        SDL_strlcat(message, " spec1.freq", 256);
aschiffler@6819
   639
        spec1.freq = 0;
aschiffler@6819
   640
    }
slouken@7191
   641
    if (i & 8) {
aschiffler@6822
   642
        SDL_strlcat(message, " spec2.format", 256);
aschiffler@6819
   643
        spec2.format = 0;
aschiffler@6819
   644
    }
slouken@7191
   645
    if (i & 16) {
aschiffler@6822
   646
        SDL_strlcat(message, " spec2.channels", 256);
aschiffler@6819
   647
        spec2.channels = 0;
aschiffler@6819
   648
    }
slouken@7191
   649
    if (i & 32) {
aschiffler@6822
   650
        SDL_strlcat(message, " spec2.freq", 256);
aschiffler@6819
   651
        spec2.freq = 0;
aschiffler@6819
   652
    }
philipp@8853
   653
    SDLTest_Log("%s", message);
aschiffler@6819
   654
    result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
aschiffler@6819
   655
                                   spec2.format, spec2.channels, spec2.freq);
aschiffler@6819
   656
    SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
aschiffler@6819
   657
    SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
aschiffler@6819
   658
    error = SDL_GetError();
aschiffler@6819
   659
    SDLTest_AssertPass("Call to SDL_GetError()");
slouken@7721
   660
    SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty");
aschiffler@6819
   661
  }
aschiffler@6819
   662
aschiffler@6819
   663
  SDL_ClearError();
aschiffler@6819
   664
  SDLTest_AssertPass("Call to SDL_ClearError()");
slouken@7191
   665
aschiffler@6819
   666
  return TEST_COMPLETED;
aschiffler@6819
   667
}
aschiffler@6819
   668
aschiffler@6819
   669
/**
aschiffler@6819
   670
 * \brief Checks current audio status.
aschiffler@6819
   671
 *
aschiffler@8994
   672
 * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
aschiffler@6819
   673
 */
aschiffler@6819
   674
int audio_getAudioStatus()
aschiffler@6819
   675
{
aschiffler@6819
   676
   SDL_AudioStatus result;
aschiffler@6819
   677
aschiffler@6819
   678
   /* Check current audio status */
aschiffler@6819
   679
   result = SDL_GetAudioStatus();
aschiffler@6819
   680
   SDLTest_AssertPass("Call to SDL_GetAudioStatus()");
aschiffler@6819
   681
   SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
slouken@7191
   682
        "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
aschiffler@6819
   683
        SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
aschiffler@6819
   684
aschiffler@6819
   685
   return TEST_COMPLETED;
aschiffler@6819
   686
}
aschiffler@6819
   687
aschiffler@6819
   688
aschiffler@6819
   689
aschiffler@6819
   690
/**
aschiffler@6819
   691
 * \brief Opens, checks current audio status, and closes a device.
aschiffler@6819
   692
 *
aschiffler@8994
   693
 * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
aschiffler@6819
   694
 */
aschiffler@6819
   695
int audio_openCloseAndGetAudioStatus()
aschiffler@6819
   696
{
aschiffler@6819
   697
   SDL_AudioStatus result;
aschiffler@6819
   698
   int i;
aschiffler@6819
   699
   int count;
slouken@7191
   700
   char *device;
aschiffler@6819
   701
   SDL_AudioDeviceID id;
aschiffler@6819
   702
   SDL_AudioSpec desired, obtained;
slouken@7191
   703
aschiffler@6819
   704
   /* Get number of devices. */
aschiffler@6819
   705
   count = SDL_GetNumAudioDevices(0);
aschiffler@6819
   706
   SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
aschiffler@6830
   707
   if (count > 0) {
aschiffler@6830
   708
     for (i = 0; i < count; i++) {
aschiffler@6819
   709
       /* Get device name */
aschiffler@6819
   710
       device = (char *)SDL_GetAudioDeviceName(i, 0);
aschiffler@6819
   711
       SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
aschiffler@6819
   712
       SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
aschiffler@6819
   713
       if (device == NULL) return TEST_ABORTED;
aschiffler@6819
   714
aschiffler@6819
   715
       /* Set standard desired spec */
aschiffler@6819
   716
       desired.freq=22050;
aschiffler@6819
   717
       desired.format=AUDIO_S16SYS;
aschiffler@6819
   718
       desired.channels=2;
aschiffler@6819
   719
       desired.samples=4096;
aschiffler@6819
   720
       desired.callback=_audio_testCallback;
aschiffler@6819
   721
       desired.userdata=NULL;
slouken@7191
   722
aschiffler@6819
   723
       /* Open device */
aschiffler@6819
   724
       id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
aschiffler@6819
   725
       SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
aschiffler@6819
   726
       SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
aschiffler@6819
   727
       if (id > 1) {
slouken@7191
   728
aschiffler@6819
   729
         /* Check device audio status */
aschiffler@6819
   730
         result = SDL_GetAudioDeviceStatus(id);
aschiffler@6819
   731
         SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()");
aschiffler@6819
   732
         SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
slouken@7191
   733
            "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
slouken@7191
   734
            SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
slouken@7191
   735
aschiffler@6819
   736
         /* Close device again */
aschiffler@6819
   737
         SDL_CloseAudioDevice(id);
aschiffler@6819
   738
         SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
aschiffler@6819
   739
       }
aschiffler@6819
   740
     }
aschiffler@6819
   741
   } else {
aschiffler@6819
   742
     SDLTest_Log("No devices to test with");
aschiffler@6819
   743
   }
slouken@7191
   744
aschiffler@6819
   745
   return TEST_COMPLETED;
aschiffler@6819
   746
}
aschiffler@6819
   747
aschiffler@6819
   748
/**
aschiffler@6819
   749
 * \brief Locks and unlocks open audio device.
aschiffler@6819
   750
 *
aschiffler@8994
   751
 * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
aschiffler@8994
   752
 * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
aschiffler@6819
   753
 */
aschiffler@6819
   754
int audio_lockUnlockOpenAudioDevice()
aschiffler@6819
   755
{
aschiffler@6819
   756
   int i;
aschiffler@6819
   757
   int count;
slouken@7191
   758
   char *device;
aschiffler@6819
   759
   SDL_AudioDeviceID id;
aschiffler@6819
   760
   SDL_AudioSpec desired, obtained;
slouken@7191
   761
aschiffler@6819
   762
   /* Get number of devices. */
aschiffler@6819
   763
   count = SDL_GetNumAudioDevices(0);
aschiffler@6819
   764
   SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
aschiffler@6830
   765
   if (count > 0) {
aschiffler@6830
   766
     for (i = 0; i < count; i++) {
aschiffler@6819
   767
       /* Get device name */
aschiffler@6819
   768
       device = (char *)SDL_GetAudioDeviceName(i, 0);
aschiffler@6819
   769
       SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
aschiffler@6819
   770
       SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
aschiffler@6819
   771
       if (device == NULL) return TEST_ABORTED;
aschiffler@6819
   772
aschiffler@6819
   773
       /* Set standard desired spec */
aschiffler@6819
   774
       desired.freq=22050;
aschiffler@6819
   775
       desired.format=AUDIO_S16SYS;
aschiffler@6819
   776
       desired.channels=2;
aschiffler@6819
   777
       desired.samples=4096;
aschiffler@6819
   778
       desired.callback=_audio_testCallback;
aschiffler@6819
   779
       desired.userdata=NULL;
slouken@7191
   780
aschiffler@6819
   781
       /* Open device */
aschiffler@6819
   782
       id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
aschiffler@6819
   783
       SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
aschiffler@6819
   784
       SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
slouken@7191
   785
       if (id > 1) {
aschiffler@6819
   786
         /* Lock to protect callback */
aschiffler@6819
   787
         SDL_LockAudioDevice(id);
aschiffler@6819
   788
         SDLTest_AssertPass("SDL_LockAudioDevice(%i)", id);
slouken@7191
   789
aschiffler@6819
   790
         /* Simulate callback processing */
aschiffler@6819
   791
         SDL_Delay(10);
aschiffler@6819
   792
         SDLTest_Log("Simulate callback processing - delay");
slouken@7191
   793
gabomdq@7677
   794
         /* Unlock again */
aschiffler@6819
   795
         SDL_UnlockAudioDevice(id);
slouken@7191
   796
         SDLTest_AssertPass("SDL_UnlockAudioDevice(%i)", id);
slouken@7191
   797
aschiffler@6819
   798
         /* Close device again */
aschiffler@6819
   799
         SDL_CloseAudioDevice(id);
aschiffler@6819
   800
         SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
aschiffler@6819
   801
       }
aschiffler@6819
   802
     }
aschiffler@6819
   803
   } else {
aschiffler@6819
   804
     SDLTest_Log("No devices to test with");
aschiffler@6819
   805
   }
slouken@7191
   806
aschiffler@6819
   807
   return TEST_COMPLETED;
aschiffler@6819
   808
}
aschiffler@6819
   809
aschiffler@6819
   810
aschiffler@6830
   811
/**
aschiffler@6830
   812
 * \brief Convert audio using various conversion structures
aschiffler@6830
   813
 *
aschiffler@8994
   814
 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
aschiffler@8994
   815
 * \sa https://wiki.libsdl.org/SDL_ConvertAudio
aschiffler@6830
   816
 */
aschiffler@6830
   817
int audio_convertAudio()
aschiffler@6830
   818
{
aschiffler@6830
   819
  int result;
aschiffler@6830
   820
  SDL_AudioCVT  cvt;
aschiffler@6830
   821
  SDL_AudioSpec spec1;
aschiffler@6830
   822
  SDL_AudioSpec spec2;
aschiffler@6830
   823
  int c;
aschiffler@6830
   824
  char message[128];
aschiffler@6830
   825
  int i, ii, j, jj, k, kk, l, ll;
aschiffler@6830
   826
aschiffler@6830
   827
  /* Iterate over bitmask that determines which parameters are modified in the conversion */
aschiffler@6830
   828
  for (c = 1; c < 8; c++) {
aschiffler@6830
   829
    SDL_strlcpy(message, "Changing:", 128);
aschiffler@6830
   830
    if (c & 1) {
aschiffler@6830
   831
      SDL_strlcat(message, " Format", 128);
aschiffler@6830
   832
    }
aschiffler@6830
   833
    if (c & 2) {
aschiffler@6830
   834
      SDL_strlcat(message, " Channels", 128);
aschiffler@6830
   835
    }
aschiffler@6830
   836
    if (c & 4) {
aschiffler@6830
   837
      SDL_strlcat(message, " Frequencies", 128);
aschiffler@6830
   838
    }
philipp@8853
   839
    SDLTest_Log("%s", message);
aschiffler@6830
   840
    /* All source conversions with random conversion targets */
aschiffler@7134
   841
    for (i = 0; i < _numAudioFormats; i++) {
aschiffler@7134
   842
      for (j = 0; j < _numAudioChannels; j++) {
slouken@7191
   843
        for (k = 0; k < _numAudioFrequencies; k++) {
aschiffler@7134
   844
          spec1.format = _audioFormats[i];
aschiffler@7134
   845
          spec1.channels = _audioChannels[j];
aschiffler@7134
   846
          spec1.freq = _audioFrequencies[k];
slouken@7191
   847
aschiffler@6830
   848
          /* Ensure we have a different target format */
aschiffler@6830
   849
          do {
aschiffler@6830
   850
            if (c & 1) {
aschiffler@7134
   851
              ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
aschiffler@6830
   852
            } else {
aschiffler@6830
   853
              ii = 1;
aschiffler@6830
   854
            }
aschiffler@6830
   855
            if (c & 2) {
aschiffler@7134
   856
              jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
aschiffler@6830
   857
            } else {
aschiffler@6830
   858
              jj= j;
aschiffler@6830
   859
            }
aschiffler@6830
   860
            if (c & 4) {
aschiffler@7134
   861
              kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
aschiffler@6830
   862
            } else {
aschiffler@6830
   863
              kk = k;
aschiffler@6830
   864
            }
aschiffler@6830
   865
          } while ((i == ii) && (j == jj) && (k == kk));
aschiffler@7134
   866
          spec2.format = _audioFormats[ii];
aschiffler@7134
   867
          spec2.channels = _audioChannels[jj];
aschiffler@7134
   868
          spec2.freq = _audioFrequencies[kk];
slouken@7191
   869
aschiffler@6830
   870
          result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
aschiffler@6830
   871
                                           spec2.format, spec2.channels, spec2.freq);
slouken@7191
   872
          SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
aschiffler@7134
   873
            i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
aschiffler@6830
   874
          SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
aschiffler@6830
   875
          if (result != 1) {
philipp@8853
   876
            SDLTest_LogError("%s", SDL_GetError());
aschiffler@6830
   877
          } else {
aschiffler@6830
   878
            SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
aschiffler@6830
   879
            if (cvt.len_mult < 1) return TEST_ABORTED;
slouken@7191
   880
aschiffler@6830
   881
            /* Create some random data to convert */
aschiffler@6830
   882
            l = 64;
aschiffler@6830
   883
            ll = l * cvt.len_mult;
aschiffler@6830
   884
            SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, ll);
aschiffler@6830
   885
            cvt.len = l;
aschiffler@6830
   886
            cvt.buf = (Uint8 *)SDL_malloc(ll);
aschiffler@6830
   887
            SDLTest_AssertCheck(cvt.buf != NULL, "Check data buffer to convert is not NULL");
aschiffler@6830
   888
            if (cvt.buf == NULL) return TEST_ABORTED;
slouken@7191
   889
aschiffler@6830
   890
            /* Convert the data */
aschiffler@6830
   891
            result = SDL_ConvertAudio(&cvt);
aschiffler@6830
   892
            SDLTest_AssertPass("Call to SDL_ConvertAudio()");
aschiffler@6830
   893
            SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0; got: %i", result);
aschiffler@6830
   894
            SDLTest_AssertCheck(cvt.buf != NULL, "Verify conversion buffer is not NULL");
aschiffler@6830
   895
            SDLTest_AssertCheck(cvt.len_ratio > 0.0, "Verify conversion length ratio; expected: >0; got: %f", cvt.len_ratio);
slouken@7191
   896
aschiffler@6830
   897
            /* Free converted buffer */
slouken@7191
   898
            SDL_free(cvt.buf);
slouken@7191
   899
            cvt.buf = NULL;
slouken@7719
   900
          }
aschiffler@6830
   901
        }
aschiffler@6830
   902
      }
aschiffler@6830
   903
    }
aschiffler@6830
   904
  }
aschiffler@6830
   905
aschiffler@6830
   906
   return TEST_COMPLETED;
aschiffler@6830
   907
}
aschiffler@6830
   908
aschiffler@6830
   909
aschiffler@6830
   910
/**
aschiffler@6830
   911
 * \brief Opens, checks current connected status, and closes a device.
aschiffler@6830
   912
 *
aschiffler@8994
   913
 * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
aschiffler@6830
   914
 */
aschiffler@6830
   915
int audio_openCloseAudioDeviceConnected()
aschiffler@6830
   916
{
aschiffler@6839
   917
   int result = -1;
aschiffler@6830
   918
   int i;
aschiffler@6830
   919
   int count;
slouken@7191
   920
   char *device;
aschiffler@6830
   921
   SDL_AudioDeviceID id;
aschiffler@6830
   922
   SDL_AudioSpec desired, obtained;
slouken@7191
   923
aschiffler@6830
   924
   /* Get number of devices. */
aschiffler@6830
   925
   count = SDL_GetNumAudioDevices(0);
aschiffler@6830
   926
   SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
aschiffler@6830
   927
   if (count > 0) {
aschiffler@6830
   928
     for (i = 0; i < count; i++) {
aschiffler@6830
   929
       /* Get device name */
aschiffler@6830
   930
       device = (char *)SDL_GetAudioDeviceName(i, 0);
aschiffler@6830
   931
       SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
aschiffler@6830
   932
       SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
aschiffler@6830
   933
       if (device == NULL) return TEST_ABORTED;
aschiffler@6830
   934
aschiffler@6830
   935
       /* Set standard desired spec */
aschiffler@6830
   936
       desired.freq=22050;
aschiffler@6830
   937
       desired.format=AUDIO_S16SYS;
aschiffler@6830
   938
       desired.channels=2;
aschiffler@6830
   939
       desired.samples=4096;
aschiffler@6830
   940
       desired.callback=_audio_testCallback;
aschiffler@6830
   941
       desired.userdata=NULL;
slouken@7191
   942
aschiffler@6830
   943
       /* Open device */
aschiffler@6830
   944
       id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
aschiffler@6830
   945
       SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
aschiffler@6839
   946
       SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >1, got: %i", id);
aschiffler@6830
   947
       if (id > 1) {
aschiffler@6830
   948
aschiffler@6830
   949
/* TODO: enable test code when function is available in SDL2 */
aschiffler@6830
   950
slouken@7191
   951
#ifdef AUDIODEVICECONNECTED_DEFINED
aschiffler@6830
   952
         /* Get connected status */
aschiffler@6830
   953
         result = SDL_AudioDeviceConnected(id);
aschiffler@6830
   954
         SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()");
aschiffler@6830
   955
#endif
aschiffler@6839
   956
         SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result);
slouken@7191
   957
aschiffler@6830
   958
         /* Close device again */
aschiffler@6830
   959
         SDL_CloseAudioDevice(id);
aschiffler@6830
   960
         SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
aschiffler@6830
   961
       }
aschiffler@6830
   962
     }
aschiffler@6830
   963
   } else {
aschiffler@6830
   964
     SDLTest_Log("No devices to test with");
aschiffler@6830
   965
   }
slouken@7191
   966
aschiffler@6830
   967
   return TEST_COMPLETED;
aschiffler@6830
   968
}
aschiffler@6830
   969
aschiffler@6830
   970
aschiffler@6819
   971
aschiffler@6772
   972
/* ================= Test Case References ================== */
aschiffler@6772
   973
aschiffler@6772
   974
/* Audio test cases */
aschiffler@6772
   975
static const SDLTest_TestCaseReference audioTest1 =
slouken@7191
   976
        { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED };
aschiffler@6772
   977
aschiffler@6772
   978
static const SDLTest_TestCaseReference audioTest2 =
slouken@7191
   979
        { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED };
aschiffler@6772
   980
aschiffler@6772
   981
static const SDLTest_TestCaseReference audioTest3 =
slouken@7191
   982
        { (SDLTest_TestCaseFp)audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED };
aschiffler@6772
   983
aschiffler@6772
   984
static const SDLTest_TestCaseReference audioTest4 =
slouken@7191
   985
        { (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED };
aschiffler@6772
   986
aschiffler@6819
   987
static const SDLTest_TestCaseReference audioTest5 =
slouken@7191
   988
        { (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED };
aschiffler@6819
   989
aschiffler@6819
   990
static const SDLTest_TestCaseReference audioTest6 =
slouken@7191
   991
        { (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED };
aschiffler@6819
   992
aschiffler@6819
   993
static const SDLTest_TestCaseReference audioTest7 =
slouken@7191
   994
        { (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED };
aschiffler@6819
   995
aschiffler@6819
   996
static const SDLTest_TestCaseReference audioTest8 =
slouken@7191
   997
        { (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED };
aschiffler@6819
   998
aschiffler@6819
   999
static const SDLTest_TestCaseReference audioTest9 =
slouken@7191
  1000
        { (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED };
aschiffler@6819
  1001
aschiffler@6830
  1002
/* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed.    */
aschiffler@6830
  1003
/* For debugging, test case can be run manually using --filter audio_convertAudio  */
aschiffler@6830
  1004
aschiffler@6830
  1005
static const SDLTest_TestCaseReference audioTest10 =
slouken@7191
  1006
        { (SDLTest_TestCaseFp)audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED };
aschiffler@6830
  1007
aschiffler@6830
  1008
/* TODO: enable test when SDL_AudioDeviceConnected has been implemented.           */
aschiffler@6830
  1009
aschiffler@6830
  1010
static const SDLTest_TestCaseReference audioTest11 =
slouken@7191
  1011
        { (SDLTest_TestCaseFp)audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED };
aschiffler@6830
  1012
aschiffler@7093
  1013
static const SDLTest_TestCaseReference audioTest12 =
slouken@7191
  1014
        { (SDLTest_TestCaseFp)audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED };
aschiffler@7093
  1015
aschiffler@7093
  1016
static const SDLTest_TestCaseReference audioTest13 =
aschiffler@8989
  1017
        { (SDLTest_TestCaseFp)audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED };
aschiffler@7093
  1018
aschiffler@7093
  1019
static const SDLTest_TestCaseReference audioTest14 =
aschiffler@8989
  1020
        { (SDLTest_TestCaseFp)audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED };
aschiffler@7093
  1021
aschiffler@8994
  1022
static const SDLTest_TestCaseReference audioTest15 =
aschiffler@8994
  1023
        { (SDLTest_TestCaseFp)audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED };
aschiffler@8994
  1024
aschiffler@6772
  1025
/* Sequence of Audio test cases */
aschiffler@6772
  1026
static const SDLTest_TestCaseReference *audioTests[] =  {
slouken@7191
  1027
    &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
slouken@7191
  1028
    &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
aschiffler@8994
  1029
    &audioTest12, &audioTest13, &audioTest14, &audioTest15, NULL
aschiffler@6772
  1030
};
aschiffler@6772
  1031
aschiffler@6772
  1032
/* Audio test suite (global) */
aschiffler@6772
  1033
SDLTest_TestSuiteReference audioTestSuite = {
slouken@7191
  1034
    "Audio",
slouken@7191
  1035
    _audioSetUp,
slouken@7191
  1036
    audioTests,
aschiffler@8994
  1037
    _audioTearDown
aschiffler@6772
  1038
};