Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Toggle main menu visibility
Loading...
Searching...
No Matches
decimation_resampler.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2023 Roc Streaming authors
3
*
4
* This Source Code Form is subject to the terms of the Mozilla Public
5
* License, v. 2.0. If a copy of the MPL was not distributed with this
6
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
*/
8
9
//! @file roc_audio/decimation_resampler.h
10
//! @brief Decimating resampler.
11
12
#ifndef ROC_AUDIO_DECIMATION_RESAMPLER_H_
13
#define ROC_AUDIO_DECIMATION_RESAMPLER_H_
14
15
#include "
roc_audio/frame.h
"
16
#include "
roc_audio/frame_factory.h
"
17
#include "
roc_audio/iframe_reader.h
"
18
#include "
roc_audio/iresampler.h
"
19
#include "
roc_audio/sample.h
"
20
#include "
roc_audio/sample_spec.h
"
21
#include "
roc_core/noncopyable.h
"
22
#include "
roc_core/rate_limiter.h
"
23
#include "
roc_core/slice.h
"
24
#include "
roc_core/stddefs.h
"
25
26
namespace
roc
{
27
namespace
audio
{
28
29
//! Decimating resampler.
30
//!
31
//! Acts as decorator for another resampler instance.
32
//!
33
//! Performs resampling in two stages:
34
//! - first, uses underlying resampler to apply constant part of scaling factor based
35
//! on input and output rates; if these rates are equal, first stage is skipped
36
//! - then, uses decimation or duplication to apply dynamic part of scaling
37
//! factor, a.k.a. multiplier, by dropping or duplicating samples
38
//!
39
//! When input and output rates are the same, this backend implements fastest possible
40
//! resampling algorithm working almost at the speed of memcpy().
41
//!
42
//! Although decimation usually degrades quality a lot, it's not so dramatic in this
43
//! specific case because we use it only for dynamic part of scaling factor, which in
44
//! practice is very close to 1.0, and typically we remove or insert up to 20 samples
45
//! per second or so on 48kHz, which corresponds to ~ 0.4ms/second.
46
//!
47
//! When input and output rates are different, this backend uses another, underlying
48
//! resampler, but only for converting between input and output rates. It still uses
49
//! decimation or duplication for applying dynamic part of scaling factor.
50
class
DecimationResampler
:
public
IResampler
,
public
core::NonCopyable
<> {
51
public
:
52
//! Initialize.
53
DecimationResampler
(
const
core::SharedPtr<IResampler>
& inner_resampler,
54
core::IArena
&
arena
,
55
FrameFactory
& frame_factory,
56
const
SampleSpec
& in_spec,
57
const
SampleSpec
& out_spec);
58
59
~DecimationResampler
();
60
61
//! Check if object is successfully constructed.
62
virtual
bool
is_valid
()
const
;
63
64
//! Set new resample factor.
65
virtual
bool
set_scaling
(
size_t
input_rate,
size_t
output_rate,
float
multiplier);
66
67
//! Get buffer to be filled with input data.
68
virtual
const
core::Slice<sample_t>
&
begin_push_input
();
69
70
//! Commit buffer with input data.
71
virtual
void
end_push_input
();
72
73
//! Read samples from input frame and fill output frame.
74
virtual
size_t
pop_output
(
sample_t
* out_data,
size_t
out_size);
75
76
//! How many samples were pushed but not processed yet.
77
virtual
float
n_left_to_process
()
const
;
78
79
private
:
80
void
report_stats_();
81
82
const
core::SharedPtr<IResampler>
inner_resampler_;
83
bool
use_inner_resampler_;
84
85
SampleSpec
input_spec_;
86
SampleSpec
output_spec_;
87
float
multiplier_;
88
89
const
size_t
num_ch_;
90
91
core::Slice<sample_t>
in_buf_;
92
size_t
in_size_;
93
size_t
in_pos_;
94
95
float
out_acc_;
96
97
core::Slice<sample_t>
last_buf_;
98
99
size_t
total_count_;
100
size_t
decim_count_;
101
core::RateLimiter
report_limiter_;
102
103
bool
valid_;
104
};
105
106
}
// namespace audio
107
}
// namespace roc
108
109
#endif
// ROC_AUDIO_DECIMATION_RESAMPLER_H_
roc::audio::DecimationResampler::set_scaling
virtual bool set_scaling(size_t input_rate, size_t output_rate, float multiplier)
Set new resample factor.
roc::audio::DecimationResampler::end_push_input
virtual void end_push_input()
Commit buffer with input data.
roc::audio::DecimationResampler::is_valid
virtual bool is_valid() const
Check if object is successfully constructed.
roc::audio::DecimationResampler::pop_output
virtual size_t pop_output(sample_t *out_data, size_t out_size)
Read samples from input frame and fill output frame.
roc::audio::DecimationResampler::DecimationResampler
DecimationResampler(const core::SharedPtr< IResampler > &inner_resampler, core::IArena &arena, FrameFactory &frame_factory, const SampleSpec &in_spec, const SampleSpec &out_spec)
Initialize.
roc::audio::DecimationResampler::begin_push_input
virtual const core::Slice< sample_t > & begin_push_input()
Get buffer to be filled with input data.
roc::audio::DecimationResampler::n_left_to_process
virtual float n_left_to_process() const
How many samples were pushed but not processed yet.
roc::audio::FrameFactory
Frame factory.
Definition
frame_factory.h:38
roc::audio::IResampler::IResampler
IResampler(core::IArena &arena)
Initialization.
roc::audio::SampleSpec
Sample specification. Describes sample rate and channels.
Definition
sample_spec.h:30
roc::core::ArenaAllocation::arena
IArena & arena() const
Get arena.
Definition
allocation_policy.h:37
roc::core::IArena
Memory arena interface.
Definition
iarena.h:23
roc::core::NonCopyable
Base class for non-copyable objects.
Definition
noncopyable.h:23
roc::core::RateLimiter
Rate limiter.
Definition
rate_limiter.h:22
roc::core::SharedPtr
Shared ownership intrusive pointer.
Definition
shared_ptr.h:32
roc::core::Slice
Slice.
Definition
slice.h:55
frame.h
Audio frame.
frame_factory.h
Frame factory.
iframe_reader.h
Frame reader interface.
iresampler.h
Audio resampler interface.
roc::audio
Audio frames and audio processing.
roc::audio::sample_t
float sample_t
Raw audio sample.
Definition
sample.h:22
roc
Root namespace.
noncopyable.h
Non-copyable object.
rate_limiter.h
Rate limiter.
sample.h
Audio sample.
sample_spec.h
Sample specifications.
slice.h
Slice.
stddefs.h
Commonly used types and functions.
roc_audio
decimation_resampler.h
Generated by
1.17.0