cubeb/test/test_record.cpp

126 строки
2.7 KiB
C++
Исходник Обычный вид История

/*
* Copyright © 2016 Mozilla Foundation
*
* This program is made available under an ISC-style license. See the
* accompanying file LICENSE for details.
*/
/* libcubeb api/function test. Record the mic and check there is sound. */
#ifdef NDEBUG
#undef NDEBUG
#endif
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
Address review comments. Build system. Review comments: cubeb_resampler_speex_one_way ctor should take uint32_t. Review comments: Use the default device for test_record.cpp Review comments: use the correct path to include cubeb.h. Review comments: use std::unique_ptr instead of auto_ptr, and remove auto_ptr implementation. Review comments: Add test_duplex{,.exe} to .gitignore. Review comments: Formatting in noop_resampler::fill. Review comments: rename auto_array::resize to auto_array::reserve. Review comments: Rename the method that push silence in an auto_array push_silence. Review comments: Make test_duplex work with backend that use integers. Review comments: indent in cubeb_resampler.cpp. Review comments: call the target rate in the public interface of the resampler `target_rate`. Review comments: Clarify the comment on cubeb_speex_resampler_one_way::drain. Review comments: trailing space in cubeb_resampler_one_way::output assert. Review comments: space between T and *. Review comments: s/outut/output/. Review comments: return before {. Review comments: Use unique_ptr to create resampler objects. We can't really use std::move here, as we appear to target platforms that don't have it (Gecko on Android uses stlport that does not have std::move). Review comments: check that the delay lign creation succeeded. Review comments: Remove frame_count_at_rate. Review comments: assert speex resampling suceeds. Review comments: s/l atency/latency/. Review comments: Remove comment. Review comments: skip the tests that require an available audio input if none is available.
2016-02-15 12:43:52 +03:00
#include "cubeb/cubeb.h"
#include "common.h"
#ifdef CUBEB_GECKO_BUILD
#include "TestHarness.h"
#endif
#define SAMPLE_FREQUENCY 48000
#if (defined(_WIN32) || defined(__WIN32__))
#define STREAM_FORMAT CUBEB_SAMPLE_FLOAT32LE
#else
#define STREAM_FORMAT CUBEB_SAMPLE_S16LE
#endif
struct user_state
{
bool seen_noise;
};
long data_cb(cubeb_stream *stream, void *user, const void * inputbuffer, void *outputbuffer, long nframes)
{
user_state * u = reinterpret_cast<user_state*>(user);
#if STREAM_FORMAT != CUBEB_SAMPLE_FLOAT32LE
short *b = (short *)inputbuffer;
#else
float *b = (float *)inputbuffer;
#endif
if (stream == NULL || inputbuffer == NULL || outputbuffer != NULL) {
return CUBEB_ERROR;
}
bool seen_noise = false;
for (long i = 0; i < nframes; i++) {
if (b[i] != 0.0) {
seen_noise = true;
}
}
u->seen_noise |= seen_noise;
return nframes;
}
void state_cb(cubeb_stream *stream, void *user, cubeb_state state)
{
if (stream == NULL)
return;
switch (state) {
case CUBEB_STATE_STARTED:
printf("stream started\n"); break;
case CUBEB_STATE_STOPPED:
printf("stream stopped\n"); break;
case CUBEB_STATE_DRAINED:
printf("stream drained\n"); break;
default:
printf("unknown stream state %d\n", state);
}
return;
}
int main(int argc, char *argv[])
{
#ifdef CUBEB_GECKO_BUILD
ScopedXPCOM xpcom("test_record");
#endif
cubeb *ctx;
cubeb_stream *stream;
cubeb_stream_params params;
int r;
user_state stream_state = { false };
r = cubeb_init(&ctx, "Cubeb record example");
if (r != CUBEB_OK) {
fprintf(stderr, "Error initializing cubeb library\n");
return r;
}
Address review comments. Build system. Review comments: cubeb_resampler_speex_one_way ctor should take uint32_t. Review comments: Use the default device for test_record.cpp Review comments: use the correct path to include cubeb.h. Review comments: use std::unique_ptr instead of auto_ptr, and remove auto_ptr implementation. Review comments: Add test_duplex{,.exe} to .gitignore. Review comments: Formatting in noop_resampler::fill. Review comments: rename auto_array::resize to auto_array::reserve. Review comments: Rename the method that push silence in an auto_array push_silence. Review comments: Make test_duplex work with backend that use integers. Review comments: indent in cubeb_resampler.cpp. Review comments: call the target rate in the public interface of the resampler `target_rate`. Review comments: Clarify the comment on cubeb_speex_resampler_one_way::drain. Review comments: trailing space in cubeb_resampler_one_way::output assert. Review comments: space between T and *. Review comments: s/outut/output/. Review comments: return before {. Review comments: Use unique_ptr to create resampler objects. We can't really use std::move here, as we appear to target platforms that don't have it (Gecko on Android uses stlport that does not have std::move). Review comments: check that the delay lign creation succeeded. Review comments: Remove frame_count_at_rate. Review comments: assert speex resampling suceeds. Review comments: s/l atency/latency/. Review comments: Remove comment. Review comments: skip the tests that require an available audio input if none is available.
2016-02-15 12:43:52 +03:00
/* This test needs an available input device, skip it if this host does not
* have one. */
if (!has_available_input_device(ctx)) {
return 0;
}
params.format = STREAM_FORMAT;
params.rate = SAMPLE_FREQUENCY;
params.channels = 1;
r = cubeb_stream_init(ctx, &stream, "Cubeb record (mono)", NULL, &params, NULL, nullptr,
250, data_cb, state_cb, &stream_state);
if (r != CUBEB_OK) {
fprintf(stderr, "Error initializing cubeb stream\n");
return r;
}
cubeb_stream_start(stream);
delay(500);
cubeb_stream_stop(stream);
cubeb_stream_destroy(stream);
cubeb_destroy(ctx);
assert(stream_state.seen_noise);
return CUBEB_OK;
}