diff options
author | Paul Adenot <[email protected]> | 2023-09-22 17:48:20 +0200 |
---|---|---|
committer | Paul Adenot <[email protected]> | 2024-04-16 16:31:18 +0200 |
commit | a6752d6ffe33994c757a6134524cf201c5c63cba (patch) | |
tree | 7432c020a7567492dcbcc1414ca4349bb54fe66d /test | |
parent | 529c3d26443003e474535766f7bb67ca4bb0edee (diff) | |
download | cubeb-a6752d6ffe33994c757a6134524cf201c5c63cba.tar.gz cubeb-a6752d6ffe33994c757a6134524cf201c5c63cba.zip |
Add internal utility to log audio streams to disk from real-time audio callbacks
Diffstat (limited to 'test')
-rw-r--r-- | test/test_audio_dump.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/test/test_audio_dump.cpp b/test/test_audio_dump.cpp new file mode 100644 index 0000000..f01c609 --- /dev/null +++ b/test/test_audio_dump.cpp @@ -0,0 +1,74 @@ +/* + * Copyright © 2023 Mozilla Foundation + * + * This program is made available under an ISC-style license. See the + * accompanying file LICENSE for details. + */ + +#define NOMINMAX +#define _USE_MATH_DEFINES + +#include "cubeb/cubeb.h" +#include <ratio> + +#include "cubeb_audio_dump.h" +#include "gtest/gtest.h" +#include <chrono> +#include <cmath> +#include <fstream> +#include <iostream> +#include <thread> + +TEST(cubeb, audio_dump) +{ + cubeb_audio_dump_session_t session; + int rv = cubeb_audio_dump_init(&session); + ASSERT_EQ(rv, 0); + + cubeb_stream_params params; + params.rate = 44100; + params.channels = 2; + params.format = CUBEB_SAMPLE_FLOAT32NE; + + cubeb_audio_dump_stream_t dump_stream; + rv = cubeb_audio_dump_stream_init(session, &dump_stream, params, "test.wav"); + ASSERT_EQ(rv, 0); + + rv = cubeb_audio_dump_start(session); + ASSERT_EQ(rv, 0); + + float phase = 0; + const size_t buf_sz = 2 * 44100 / 50; + float buf[buf_sz]; + for (uint32_t iteration = 0; iteration < 50; iteration++) { + uint32_t write_idx = 0; + for (uint32_t i = 0; i < buf_sz / params.channels; i++) { + for (uint32_t j = 0; j < params.channels; j++) { + buf[write_idx++] = sin(phase); + } + phase += 440 * M_PI * 2 / 44100; + if (phase > 2 * M_PI) { + phase -= 2 * M_PI; + } + } + rv = cubeb_audio_dump_write(dump_stream, buf, 2 * 44100 / 50); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + ASSERT_EQ(rv, 0); + } + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + rv = cubeb_audio_dump_stop(session); + ASSERT_EQ(rv, 0); + + rv = cubeb_audio_dump_stream_shutdown(session, dump_stream); + ASSERT_EQ(rv, 0); + + rv = cubeb_audio_dump_shutdown(session); + ASSERT_EQ(rv, 0); + + std::ifstream file("test.wav"); + ASSERT_TRUE(file.good()); +} + +#undef NOMINMAX |