Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2011 Mozilla Foundation |
michael@0 | 3 | * |
michael@0 | 4 | * This program is made available under an ISC-style license. See the |
michael@0 | 5 | * accompanying file LICENSE for details. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | /* libcubeb api/function test. Plays a simple tone. */ |
michael@0 | 9 | #ifdef NDEBUG |
michael@0 | 10 | #undef NDEBUG |
michael@0 | 11 | #endif |
michael@0 | 12 | #define _XOPEN_SOURCE 500 |
michael@0 | 13 | #include <stdio.h> |
michael@0 | 14 | #include <stdlib.h> |
michael@0 | 15 | #include <math.h> |
michael@0 | 16 | #include <assert.h> |
michael@0 | 17 | |
michael@0 | 18 | #include "cubeb/cubeb.h" |
michael@0 | 19 | #include "common.h" |
michael@0 | 20 | |
michael@0 | 21 | #define SAMPLE_FREQUENCY 48000 |
michael@0 | 22 | |
michael@0 | 23 | /* store the phase of the generated waveform */ |
michael@0 | 24 | struct cb_user_data { |
michael@0 | 25 | long position; |
michael@0 | 26 | }; |
michael@0 | 27 | |
michael@0 | 28 | long data_cb(cubeb_stream *stream, void *user, void *buffer, long nframes) |
michael@0 | 29 | { |
michael@0 | 30 | struct cb_user_data *u = (struct cb_user_data *)user; |
michael@0 | 31 | short *b = (short *)buffer; |
michael@0 | 32 | int i; |
michael@0 | 33 | |
michael@0 | 34 | if (stream == NULL || u == NULL) |
michael@0 | 35 | return CUBEB_ERROR; |
michael@0 | 36 | |
michael@0 | 37 | /* generate our test tone on the fly */ |
michael@0 | 38 | for (i = 0; i < nframes; i++) { |
michael@0 | 39 | /* North American dial tone */ |
michael@0 | 40 | b[i] = 16000*sin(2*M_PI*(i + u->position)*350/SAMPLE_FREQUENCY); |
michael@0 | 41 | b[i] += 16000*sin(2*M_PI*(i + u->position)*440/SAMPLE_FREQUENCY); |
michael@0 | 42 | /* European dial tone */ |
michael@0 | 43 | /*b[i] = 30000*sin(2*M_PI*(i + u->position)*425/SAMPLE_FREQUENCY);*/ |
michael@0 | 44 | } |
michael@0 | 45 | /* remember our phase to avoid clicking on buffer transitions */ |
michael@0 | 46 | /* we'll still click if position overflows */ |
michael@0 | 47 | u->position += nframes; |
michael@0 | 48 | |
michael@0 | 49 | return nframes; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | void state_cb(cubeb_stream *stream, void *user, cubeb_state state) |
michael@0 | 53 | { |
michael@0 | 54 | struct cb_user_data *u = (struct cb_user_data *)user; |
michael@0 | 55 | |
michael@0 | 56 | if (stream == NULL || u == NULL) |
michael@0 | 57 | return; |
michael@0 | 58 | |
michael@0 | 59 | switch (state) { |
michael@0 | 60 | case CUBEB_STATE_STARTED: |
michael@0 | 61 | printf("stream started\n"); break; |
michael@0 | 62 | case CUBEB_STATE_STOPPED: |
michael@0 | 63 | printf("stream stopped\n"); break; |
michael@0 | 64 | case CUBEB_STATE_DRAINED: |
michael@0 | 65 | printf("stream drained\n"); break; |
michael@0 | 66 | default: |
michael@0 | 67 | printf("unknown stream state %d\n", state); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | return; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | int main(int argc, char *argv[]) |
michael@0 | 74 | { |
michael@0 | 75 | cubeb *ctx; |
michael@0 | 76 | cubeb_stream *stream; |
michael@0 | 77 | cubeb_stream_params params; |
michael@0 | 78 | struct cb_user_data *user_data; |
michael@0 | 79 | int ret; |
michael@0 | 80 | |
michael@0 | 81 | ret = cubeb_init(&ctx, "Cubeb tone example"); |
michael@0 | 82 | if (ret != CUBEB_OK) { |
michael@0 | 83 | fprintf(stderr, "Error initializing cubeb library\n"); |
michael@0 | 84 | return ret; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | params.format = CUBEB_SAMPLE_S16NE; |
michael@0 | 88 | params.rate = SAMPLE_FREQUENCY; |
michael@0 | 89 | params.channels = 1; |
michael@0 | 90 | |
michael@0 | 91 | user_data = (struct cb_user_data *) malloc(sizeof(*user_data)); |
michael@0 | 92 | if (user_data == NULL) { |
michael@0 | 93 | fprintf(stderr, "Error allocating user data\n"); |
michael@0 | 94 | return CUBEB_ERROR; |
michael@0 | 95 | } |
michael@0 | 96 | user_data->position = 0; |
michael@0 | 97 | |
michael@0 | 98 | ret = cubeb_stream_init(ctx, &stream, "Cubeb tone (mono)", params, |
michael@0 | 99 | 250, data_cb, state_cb, user_data); |
michael@0 | 100 | if (ret != CUBEB_OK) { |
michael@0 | 101 | fprintf(stderr, "Error initializing cubeb stream\n"); |
michael@0 | 102 | return ret; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | cubeb_stream_start(stream); |
michael@0 | 106 | delay(500); |
michael@0 | 107 | cubeb_stream_stop(stream); |
michael@0 | 108 | |
michael@0 | 109 | cubeb_stream_destroy(stream); |
michael@0 | 110 | cubeb_destroy(ctx); |
michael@0 | 111 | |
michael@0 | 112 | assert(user_data->position); |
michael@0 | 113 | |
michael@0 | 114 | free(user_data); |
michael@0 | 115 | |
michael@0 | 116 | return CUBEB_OK; |
michael@0 | 117 | } |