From 5e93208fcbb1be5b9ea2938d6e6de2238d3d5cc9 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Wed, 11 Jan 2023 15:23:22 +0300 Subject: [PATCH] Add audio::record helper function --- libs/audio/include/psemek/audio/recorder.hpp | 5 +++++ libs/audio/source/recorder.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/libs/audio/include/psemek/audio/recorder.hpp b/libs/audio/include/psemek/audio/recorder.hpp index 10cf9d80..dd9f9a43 100644 --- a/libs/audio/include/psemek/audio/recorder.hpp +++ b/libs/audio/include/psemek/audio/recorder.hpp @@ -1,7 +1,9 @@ #pragma once #include +#include #include +#include #include #include @@ -25,4 +27,7 @@ namespace psemek::audio std::shared_ptr make_recorder(); std::shared_ptr make_recorder(stream_ptr stream); + std::shared_ptr record(stream_ptr stream); + std::shared_ptr record(stream_ptr stream, duration duration); + } diff --git a/libs/audio/source/recorder.cpp b/libs/audio/source/recorder.cpp index 87a260a9..90fda08b 100644 --- a/libs/audio/source/recorder.cpp +++ b/libs/audio/source/recorder.cpp @@ -75,4 +75,18 @@ namespace psemek::audio return recorder; } + std::shared_ptr record(stream_ptr stream) + { + if (!stream->length()) + throw std::runtime_error("cannot record an infinite stream"); + return record(stream, *stream->length()); + } + + std::shared_ptr record(stream_ptr stream, duration duration) + { + auto recorder = make_recorder(std::move(stream)); + recorder->request(duration.samples()); + return load_raw(recorder->grab_buffer()); + } + }