media/libcubeb/tests/test_tone.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libcubeb/tests/test_tone.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +/*
     1.5 + * Copyright © 2011 Mozilla Foundation
     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 test. Plays a simple tone. */
    1.12 +#ifdef NDEBUG
    1.13 +#undef NDEBUG
    1.14 +#endif
    1.15 +#define _XOPEN_SOURCE 500
    1.16 +#include <stdio.h>
    1.17 +#include <stdlib.h>
    1.18 +#include <math.h>
    1.19 +#include <assert.h>
    1.20 +
    1.21 +#include "cubeb/cubeb.h"
    1.22 +#include "common.h"
    1.23 +
    1.24 +#define SAMPLE_FREQUENCY 48000
    1.25 +
    1.26 +/* store the phase of the generated waveform */
    1.27 +struct cb_user_data {
    1.28 +  long position;
    1.29 +};
    1.30 +
    1.31 +long data_cb(cubeb_stream *stream, void *user, void *buffer, long nframes)
    1.32 +{
    1.33 +  struct cb_user_data *u = (struct cb_user_data *)user;
    1.34 +  short *b = (short *)buffer;
    1.35 +  int i;
    1.36 +
    1.37 +  if (stream == NULL || u == NULL)
    1.38 +    return CUBEB_ERROR;
    1.39 +
    1.40 +  /* generate our test tone on the fly */
    1.41 +  for (i = 0; i < nframes; i++) {
    1.42 +    /* North American dial tone */
    1.43 +    b[i]  = 16000*sin(2*M_PI*(i + u->position)*350/SAMPLE_FREQUENCY);
    1.44 +    b[i] += 16000*sin(2*M_PI*(i + u->position)*440/SAMPLE_FREQUENCY);
    1.45 +    /* European dial tone */
    1.46 +    /*b[i]  = 30000*sin(2*M_PI*(i + u->position)*425/SAMPLE_FREQUENCY);*/
    1.47 +  }
    1.48 +  /* remember our phase to avoid clicking on buffer transitions */
    1.49 +  /* we'll still click if position overflows */
    1.50 +  u->position += nframes;
    1.51 +
    1.52 +  return nframes;
    1.53 +}
    1.54 +
    1.55 +void state_cb(cubeb_stream *stream, void *user, cubeb_state state)
    1.56 +{
    1.57 +  struct cb_user_data *u = (struct cb_user_data *)user;
    1.58 +
    1.59 +  if (stream == NULL || u == NULL)
    1.60 +    return;
    1.61 +
    1.62 +  switch (state) {
    1.63 +    case CUBEB_STATE_STARTED:
    1.64 +      printf("stream started\n"); break;
    1.65 +    case CUBEB_STATE_STOPPED:
    1.66 +      printf("stream stopped\n"); break;
    1.67 +    case CUBEB_STATE_DRAINED:
    1.68 +      printf("stream drained\n"); break;
    1.69 +    default:
    1.70 +      printf("unknown stream state %d\n", state);
    1.71 +  }
    1.72 +
    1.73 +  return;
    1.74 +}
    1.75 +
    1.76 +int main(int argc, char *argv[])
    1.77 +{
    1.78 +  cubeb *ctx;
    1.79 +  cubeb_stream *stream;
    1.80 +  cubeb_stream_params params;
    1.81 +  struct cb_user_data *user_data;
    1.82 +  int ret;
    1.83 +
    1.84 +  ret = cubeb_init(&ctx, "Cubeb tone example");
    1.85 +  if (ret != CUBEB_OK) {
    1.86 +    fprintf(stderr, "Error initializing cubeb library\n");
    1.87 +    return ret;
    1.88 +  }
    1.89 +
    1.90 +  params.format = CUBEB_SAMPLE_S16NE;
    1.91 +  params.rate = SAMPLE_FREQUENCY;
    1.92 +  params.channels = 1;
    1.93 +
    1.94 +  user_data = (struct cb_user_data *) malloc(sizeof(*user_data));
    1.95 +  if (user_data == NULL) {
    1.96 +    fprintf(stderr, "Error allocating user data\n");
    1.97 +    return CUBEB_ERROR;
    1.98 +  }
    1.99 +  user_data->position = 0;
   1.100 +
   1.101 +  ret = cubeb_stream_init(ctx, &stream, "Cubeb tone (mono)", params,
   1.102 +                          250, data_cb, state_cb, user_data);
   1.103 +  if (ret != CUBEB_OK) {
   1.104 +    fprintf(stderr, "Error initializing cubeb stream\n");
   1.105 +    return ret;
   1.106 +  }
   1.107 +
   1.108 +  cubeb_stream_start(stream);
   1.109 +  delay(500);
   1.110 +  cubeb_stream_stop(stream);
   1.111 +
   1.112 +  cubeb_stream_destroy(stream);
   1.113 +  cubeb_destroy(ctx);
   1.114 +
   1.115 +  assert(user_data->position);
   1.116 +
   1.117 +  free(user_data);
   1.118 +
   1.119 +  return CUBEB_OK;
   1.120 +}

mercurial