File: test_openal_capture_sanity.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (145 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright 2017 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

// This test attempts to open all possible capture devices, each one
// several times with a combination of "reasonable" parameters, and
// checks some basic conformance to expectations w.r.t the spec.
//
// Wishlist:
// - Any operation a closed device should fail; 
// - Trying to open multiple devices with the same name at the same time
//   and different settings should be fine;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <unistd.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#define ASSUME_AL_FLOAT32
#endif
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alext.h>

#define countof(x) (sizeof(x)/sizeof((x)[0]))

// OfflineAudioContexts are required to support sample rates ranging
// from 22050 to 96000.
// Should we test for purposefully-ugly sample rates within that range, too ?
static const ALCuint SAMPLE_RATES[] = {
  22050, 32000, 37800, 44100, 48000, 88200, 96000
};

static const ALenum FORMATS[] = {
  AL_FORMAT_MONO8, 
  AL_FORMAT_MONO16,
  AL_FORMAT_STEREO8,
  AL_FORMAT_STEREO16,
#ifdef ASSUME_AL_FLOAT32
  AL_FORMAT_MONO_FLOAT32,
  AL_FORMAT_STEREO_FLOAT32,
#endif
};

static const char* alformat_string(ALenum format) {
  switch(format) {
#define CASE(X) case X: return #X;
  CASE(AL_FORMAT_MONO8)
  CASE(AL_FORMAT_MONO16)
  CASE(AL_FORMAT_STEREO8)
  CASE(AL_FORMAT_STEREO16)
#ifdef ASSUME_AL_FLOAT32
  CASE(AL_FORMAT_MONO_FLOAT32)
  CASE(AL_FORMAT_STEREO_FLOAT32)
#endif
#undef CASE
  }
  return "<no_string_available>";
}

static void check_device_sanity_with_params(const char* name,
                                            ALCuint sample_rate,
                                            ALenum format,
                                            ALCsizei buffer_size) {
  printf(
    "Testing \"%s\" @%uHz with %u sample-frames (format: %s)...\n", 
    name, (unsigned)sample_rate, (unsigned) buffer_size,
    alformat_string(format)
  );

  ALCdevice* dev = alcCaptureOpenDevice(name, sample_rate, format, buffer_size);

  if (dev) {
    const char *claimed = alcGetString(dev, ALC_CAPTURE_DEVICE_SPECIFIER);
    if (strcmp(name, claimed)) {
      fprintf(stderr, "The device \"%s\" claims to be actually named \"%s\", which is not correct behavior.\n", name, claimed);
      exit(1);
    }
    ALCboolean could_close = alcCaptureCloseDevice(dev);
    if (!could_close) {
      fprintf(stderr, "alcCaptureCloseDevice() with \"%s\" failed!\n", name);
      exit(1);
    }
    return;
  }

  ALCenum err = alcGetError(dev);
  fprintf(stderr,
    "alcCaptureOpenDevice(\"%s\", sample_rate=%u, format=%s, "
    "buffer_size=%u) failed with ALC error %x (%s)\n", 
    name, (unsigned)sample_rate, alformat_string(format), 
    (unsigned) buffer_size,
    (unsigned) err, alcGetString(NULL, err)
  );
  exit(1);
}

static void check_device_sanity(const char *name) {
  for (int si=0 ; si<countof(SAMPLE_RATES) ; ++si) {
    for (int fi=0 ; fi<countof(FORMATS) ; ++fi) {
      // 8 seconds of data
      check_device_sanity_with_params(
        name, SAMPLE_RATES[si], FORMATS[fi], 8*SAMPLE_RATES[si]
      );
    }
  }
}

static bool is_defaultname_in_names(const char *dft, const char *names) {
  for (const char *name = names; *name ; name += 1+strlen(name)) {
    if (!strcmp(dft, name)) {
      return true;
    }
  }
  return false;
}

int main() {
  const char *dft = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
  const char *names = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);

  if (!is_defaultname_in_names(dft, names)) {
    printf(
      "The default device specifier (\"%s\") was not found amongst the device specifier list : ...\n"
      , dft
    );
    for (const char *name = names; *name ; name += 1+strlen(name)) {
      printf("- \"%s\";\n", name);
    }
    printf("... this is not an error, though.\n\n");
  }

  for (const char *name = names; *name ; name += 1+strlen(name)) {
    check_device_sanity(name);
  }

  return 0;
}