summaryrefslogtreecommitdiff
path: root/src/tangara/audio/resample.cpp
diff options
context:
space:
mode:
authorcooljqln <cooljqln@noreply.codeberg.org>2024-05-03 04:48:17 +0000
committercooljqln <cooljqln@noreply.codeberg.org>2024-05-03 04:48:17 +0000
commit3ceb8025ee4330c177101ed30ec17dfb0002f41e (patch)
tree58350210f15df7d00d967cac6f30eeceeb031a3c /src/tangara/audio/resample.cpp
parent964da15a0b84f8e5f00e8abac2f7dfda0bf60488 (diff)
parent9fafd797a5504f458b5fcae4a1d28a68da936315 (diff)
downloadtangara-fw-3ceb8025ee4330c177101ed30ec17dfb0002f41e.tar.gz
Merge pull request 'Break dependency cycles with our components by merging co-dependent components together' (#68) from jqln/component-merge into main
Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/68
Diffstat (limited to 'src/tangara/audio/resample.cpp')
-rw-r--r--src/tangara/audio/resample.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/tangara/audio/resample.cpp b/src/tangara/audio/resample.cpp
new file mode 100644
index 00000000..143ce230
--- /dev/null
+++ b/src/tangara/audio/resample.cpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+#include "audio/resample.hpp"
+
+#include <algorithm>
+#include <cmath>
+#include <cstdint>
+#include <cstdlib>
+#include <cstring>
+#include <numeric>
+
+#include "esp_log.h"
+#include "speex/speex_resampler.h"
+
+#include "sample.hpp"
+
+namespace audio {
+
+static constexpr int kQuality = SPEEX_RESAMPLER_QUALITY_MIN;
+
+Resampler::Resampler(uint32_t source_sample_rate,
+ uint32_t target_sample_rate,
+ uint8_t num_channels)
+ : err_(0),
+ resampler_(speex_resampler_init(num_channels,
+ source_sample_rate,
+ target_sample_rate,
+ kQuality,
+ &err_)),
+ num_channels_(num_channels) {
+ assert(err_ == 0);
+}
+
+Resampler::~Resampler() {
+ speex_resampler_destroy(resampler_);
+}
+
+auto Resampler::Process(std::span<sample::Sample> input,
+ std::span<sample::Sample> output,
+ bool end_of_data) -> std::pair<size_t, size_t> {
+ uint32_t samples_used = input.size() / num_channels_;
+ uint32_t samples_produced = output.size() / num_channels_;
+
+ int err = speex_resampler_process_interleaved_int(
+ resampler_, input.data(), &samples_used, output.data(),
+ &samples_produced);
+ assert(err == 0);
+
+ return {samples_used * num_channels_, samples_produced * num_channels_};
+}
+
+} // namespace audio