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