MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / resample_interleaved_to_rate

Function resample_interleaved_to_rate

src/framework/audio/mixing.cpp:26–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24}
25
26std::vector<float> resample_interleaved_to_rate(
27 const std::vector<float> & samples,
28 int source_rate,
29 int channels,
30 int target_rate) {
31 if (source_rate == target_rate) {
32 return samples;
33 }
34 const int64_t frames = static_cast<int64_t>(samples.size() / static_cast<size_t>(channels));
35 std::vector<std::vector<float>> channel_outputs;
36 channel_outputs.reserve(static_cast<size_t>(channels));
37 int64_t output_frames = -1;
38 for (int ch = 0; ch < channels; ++ch) {
39 std::vector<float> mono(static_cast<size_t>(frames), 0.0F);
40 for (int64_t frame = 0; frame < frames; ++frame) {
41 mono[static_cast<size_t>(frame)] = samples[static_cast<size_t>(frame * channels + ch)];
42 }
43 SoxrResampleOptions options;
44 options.profile = SoxrResampleProfile::QualityOnly;
45 options.output_length_policy = SoxrOutputLengthPolicy::ExactExpected;
46 options.warning_context = "audio mix";
47 options.fallback_description = "linear resampling";
48 auto resampled = resample_mono_soxr_or_linear(mono, source_rate, target_rate, options);
49 if (output_frames < 0) {
50 output_frames = static_cast<int64_t>(resampled.size());
51 } else if (output_frames != static_cast<int64_t>(resampled.size())) {
52 throw std::runtime_error("audio mix resampled channels produced inconsistent lengths");
53 }
54 channel_outputs.push_back(std::move(resampled));
55 }
56 std::vector<float> out(static_cast<size_t>(output_frames * channels), 0.0F);
57 for (int64_t frame = 0; frame < output_frames; ++frame) {
58 for (int ch = 0; ch < channels; ++ch) {
59 out[static_cast<size_t>(frame * channels + ch)] = channel_outputs[static_cast<size_t>(ch)][static_cast<size_t>(frame)];
60 }
61 }
62 return out;
63}
64
65std::vector<float> adapt_audio(
66 const WavData & input,

Callers 1

adapt_audioFunction · 0.85

Calls 2

sizeMethod · 0.45

Tested by

no test coverage detected