media/libcubeb/tests/test_audio.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libcubeb/tests/test_audio.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,199 @@
     1.4 +/*
     1.5 + * Copyright © 2013 Sebastien Alaiwan <sebastien.alaiwan@gmail.com>
     1.6 + *
     1.7 + * This program is made available under an ISC-style license.  See the
     1.8 + * accompanying file LICENSE for details.
     1.9 + */
    1.10 +
    1.11 +/* libcubeb api/function exhaustive test. Plays a series of tones in different
    1.12 + * conditions. */
    1.13 +#ifdef NDEBUG
    1.14 +#undef NDEBUG
    1.15 +#endif
    1.16 +#define _XOPEN_SOURCE 500
    1.17 +#include <stdio.h>
    1.18 +#include <stdlib.h>
    1.19 +#include <math.h>
    1.20 +#include <assert.h>
    1.21 +#include <string.h>
    1.22 +
    1.23 +#include "cubeb/cubeb.h"
    1.24 +#include "common.h"
    1.25 +
    1.26 +#define MAX_NUM_CHANNELS 32
    1.27 +
    1.28 +#if !defined(M_PI)
    1.29 +#define M_PI 3.14159265358979323846
    1.30 +#endif
    1.31 +
    1.32 +#define NELEMS(x) ((int) (sizeof(x) / sizeof(x[0])))
    1.33 +#define VOLUME 0.2
    1.34 +
    1.35 +float get_frequency(int channel_index)
    1.36 +{
    1.37 +  return 220.0f * (channel_index+1);
    1.38 +}
    1.39 +
    1.40 +/* store the phase of the generated waveform */
    1.41 +typedef struct {
    1.42 +  int num_channels;
    1.43 +  float phase[MAX_NUM_CHANNELS];
    1.44 +  float sample_rate;
    1.45 +} synth_state;
    1.46 +
    1.47 +synth_state* synth_create(int num_channels, float sample_rate)
    1.48 +{
    1.49 +  synth_state* synth = (synth_state *) malloc(sizeof(synth_state));
    1.50 +  for(int i=0;i < MAX_NUM_CHANNELS;++i)
    1.51 +    synth->phase[i] = 0.0f;
    1.52 +  synth->num_channels = num_channels;
    1.53 +  synth->sample_rate = sample_rate;
    1.54 +  return synth;
    1.55 +}
    1.56 +
    1.57 +void synth_destroy(synth_state* synth)
    1.58 +{
    1.59 +  free(synth);
    1.60 +}
    1.61 +
    1.62 +void synth_run_float(synth_state* synth, float* audiobuffer, long nframes)
    1.63 +{
    1.64 +  for(int c=0;c < synth->num_channels;++c) {
    1.65 +    float freq = get_frequency(c);
    1.66 +    float phase_inc = 2.0 * M_PI * freq / synth->sample_rate;
    1.67 +    for(long n=0;n < nframes;++n) {
    1.68 +      audiobuffer[n*synth->num_channels+c] = sin(synth->phase[c]) * VOLUME;
    1.69 +      synth->phase[c] += phase_inc;
    1.70 +    }
    1.71 +  }
    1.72 +}
    1.73 +
    1.74 +long data_cb_float(cubeb_stream *stream, void *user, void *buffer, long nframes)
    1.75 +{
    1.76 +  synth_state *synth = (synth_state *)user;
    1.77 +  synth_run_float(synth, (float*)buffer, nframes);
    1.78 +  return nframes;
    1.79 +}
    1.80 +
    1.81 +void synth_run_16bit(synth_state* synth, short* audiobuffer, long nframes)
    1.82 +{
    1.83 +  for(int c=0;c < synth->num_channels;++c) {
    1.84 +    float freq = get_frequency(c);
    1.85 +    float phase_inc = 2.0 * M_PI * freq / synth->sample_rate;
    1.86 +    for(long n=0;n < nframes;++n) {
    1.87 +      audiobuffer[n*synth->num_channels+c] = sin(synth->phase[c]) * VOLUME * 32767.0f;
    1.88 +      synth->phase[c] += phase_inc;
    1.89 +    }
    1.90 +  }
    1.91 +}
    1.92 +
    1.93 +long data_cb_short(cubeb_stream *stream, void *user, void *buffer, long nframes)
    1.94 +{
    1.95 +  synth_state *synth = (synth_state *)user;
    1.96 +  synth_run_16bit(synth, (short*)buffer, nframes);
    1.97 +  return nframes;
    1.98 +}
    1.99 +
   1.100 +void state_cb(cubeb_stream *stream, void *user, cubeb_state state)
   1.101 +{
   1.102 +}
   1.103 +
   1.104 +/* Our android backends don't support float, only int16. */
   1.105 +int supports_float32(const char* backend_id)
   1.106 +{
   1.107 +  return (strcmp(backend_id, "opensl") != 0 &&
   1.108 +          strcmp(backend_id, "audiotrack") != 0);
   1.109 +}
   1.110 +
   1.111 +/* Some backends don't have code to deal with more than mono or stereo. */
   1.112 +int supports_channel_count(const char* backend_id, int nchannels)
   1.113 +{
   1.114 +  return nchannels <= 2 ||
   1.115 +    (strcmp(backend_id, "opensl") != 0 && strcmp(backend_id, "audiotrack") != 0);
   1.116 +}
   1.117 +
   1.118 +int run_test(int num_channels, int sampling_rate, int is_float)
   1.119 +{
   1.120 +  int ret = CUBEB_OK;
   1.121 +
   1.122 +  cubeb *ctx = NULL;
   1.123 +  synth_state* synth = NULL;
   1.124 +  cubeb_stream *stream = NULL;
   1.125 +  const char * backend_id = NULL;
   1.126 +
   1.127 +  ret = cubeb_init(&ctx, "Cubeb audio test");
   1.128 +  if (ret != CUBEB_OK) {
   1.129 +    fprintf(stderr, "Error initializing cubeb library\n");
   1.130 +    goto cleanup;
   1.131 +  }
   1.132 +
   1.133 +  backend_id = cubeb_get_backend_id(ctx);
   1.134 +
   1.135 +  if ((is_float && !supports_float32(backend_id)) ||
   1.136 +      !supports_channel_count(backend_id, num_channels)) {
   1.137 +    /* don't treat this as a test failure. */
   1.138 +    goto cleanup;
   1.139 +  }
   1.140 +
   1.141 +  fprintf(stderr, "Testing %d channel(s), %d Hz, %s (%s)\n", num_channels, sampling_rate, is_float ? "float" : "short", cubeb_get_backend_id(ctx));
   1.142 +
   1.143 +  cubeb_stream_params params;
   1.144 +  params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16NE;
   1.145 +  params.rate = sampling_rate;
   1.146 +  params.channels = num_channels;
   1.147 +
   1.148 +  synth = synth_create(params.channels, params.rate);
   1.149 +  if (synth == NULL) {
   1.150 +    fprintf(stderr, "Out of memory\n");
   1.151 +    goto cleanup;
   1.152 +  }
   1.153 +
   1.154 +  ret = cubeb_stream_init(ctx, &stream, "test tone", params,
   1.155 +                          100, is_float ? data_cb_float : data_cb_short, state_cb, synth);
   1.156 +  if (ret != CUBEB_OK) {
   1.157 +    fprintf(stderr, "Error initializing cubeb stream: %d\n", ret);
   1.158 +    goto cleanup;
   1.159 +  }
   1.160 +
   1.161 +  cubeb_stream_start(stream);
   1.162 +  delay(200);
   1.163 +  cubeb_stream_stop(stream);
   1.164 +
   1.165 +cleanup:
   1.166 +  cubeb_stream_destroy(stream);
   1.167 +  cubeb_destroy(ctx);
   1.168 +  synth_destroy(synth);
   1.169 +
   1.170 +  return ret;
   1.171 +}
   1.172 +
   1.173 +int main(int argc, char *argv[])
   1.174 +{
   1.175 +  int channel_values[] = {
   1.176 +    1,
   1.177 +    2,
   1.178 +    3,
   1.179 +    4,
   1.180 +    6,
   1.181 +  };
   1.182 +
   1.183 +  int freq_values[] = {
   1.184 +    16000,
   1.185 +    24000,
   1.186 +    44100,
   1.187 +    48000,
   1.188 +  };
   1.189 +
   1.190 +  for(int j = 0; j < NELEMS(channel_values); ++j) {
   1.191 +    for(int i = 0; i < NELEMS(freq_values); ++i) {
   1.192 +      assert(channel_values[j] < MAX_NUM_CHANNELS);
   1.193 +      fprintf(stderr, "--------------------------\n");
   1.194 +      assert(run_test(channel_values[j], freq_values[i], 0) == CUBEB_OK);
   1.195 +      assert(run_test(channel_values[j], freq_values[i], 1) == CUBEB_OK);
   1.196 +    }
   1.197 +  }
   1.198 +
   1.199 +  return CUBEB_OK;
   1.200 +}
   1.201 +
   1.202 +

mercurial