Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
resampler_builtin.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 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/resampler_builtin.h
10//! @brief Resampler.
11
12#ifndef ROC_AUDIO_RESAMPLER_BUILTIN_H_
13#define ROC_AUDIO_RESAMPLER_BUILTIN_H_
14
15#include "roc_audio/frame.h"
19#include "roc_audio/sample.h"
21#include "roc_core/array.h"
24#include "roc_core/slice.h"
25#include "roc_core/stddefs.h"
26#include "roc_packet/units.h"
27
28namespace roc {
29namespace audio {
30
31//! Resamples audio stream with non-integer dynamically changing factor.
33public:
34 //! Initialize.
36 core::BufferFactory<sample_t>& buffer_factory,
37 ResamplerProfile profile,
38 core::nanoseconds_t frame_length,
39 const audio::SampleSpec& sample_spec);
40
42
43 //! Check if object is successfully constructed.
44 virtual bool valid() const;
45
46 //! Set new resample factor.
47 //! @remarks
48 //! Resampling algorithm needs some window of input samples. The length of the window
49 //! (length of sinc impulse response) is a compromise between SNR and speed. It
50 //! depends on current resampling factor. So we choose length of input buffers to let
51 //! it handle maximum length of input. If new scaling factor breaks equation this
52 //! function returns false.
53 virtual bool set_scaling(size_t input_rate, size_t output_rate, float multiplier);
54
55 //! Get buffer to be filled with input data.
57
58 //! Commit buffer with input data.
59 virtual void end_push_input();
60
61 //! Read samples from input frame and fill output frame.
62 virtual size_t pop_output(Frame& out);
63
64private:
65 typedef uint32_t fixedpoint_t;
66 typedef uint64_t long_fixedpoint_t;
67 typedef int32_t signed_fixedpoint_t;
68 typedef int64_t signed_long_fixedpoint_t;
69
70 const audio::SampleSpec sample_spec_;
71
72 inline size_t channelize_index(const size_t i, const size_t ch_offset) const {
73 return i * sample_spec_.num_channels() + ch_offset;
74 }
75
76 bool alloc_frames_(core::BufferFactory<sample_t>&);
77
78 bool check_config_() const;
79
80 bool fill_sinc_();
81 sample_t sinc_(fixedpoint_t x, float fract_x);
82
83 // Computes single sample of the particular audio channel.
84 // channel_offset a serial number of the channel
85 // (e.g. left -- 0, right -- 1, etc.).
86 sample_t resample_(size_t channel_offset);
87
88 core::Slice<sample_t> frames_[3];
89 size_t n_ready_frames_;
90
91 const sample_t* prev_frame_;
92 const sample_t* curr_frame_;
93 const sample_t* next_frame_;
94
95 float scaling_;
96
97 const size_t frame_size_;
98 const size_t frame_size_ch_;
99
100 const size_t window_size_;
101 const fixedpoint_t qt_half_sinc_window_size_;
102
103 const size_t window_interp_;
104 const size_t window_interp_bits_;
105
106 core::Array<sample_t> sinc_table_;
107 const sample_t* sinc_table_ptr_;
108
109 // half window len in Q8.24 in terms of input signal
110 fixedpoint_t qt_half_window_size_;
111 const fixedpoint_t qt_epsilon_;
112
113 const fixedpoint_t qt_frame_size_;
114
115 // time position of output sample in terms of input samples indexes
116 // for example 0 -- time position of first sample in curr_frame_
117 fixedpoint_t qt_sample_;
118
119 // time distance between two output samples, equals to resampling factor
120 fixedpoint_t qt_dt_;
121
122 // the step with which we iterate over the sinc_table_
123 fixedpoint_t qt_sinc_step_;
124
125 const sample_t cutoff_freq_;
126
127 bool valid_;
128};
129
130} // namespace audio
131} // namespace roc
132
133#endif // ROC_AUDIO_RESAMPLER_BUILTIN_H_
Dynamic array.
Buffer factory.
Resamples audio stream with non-integer dynamically changing factor.
virtual void end_push_input()
Commit buffer with input data.
virtual bool set_scaling(size_t input_rate, size_t output_rate, float multiplier)
Set new resample factor.
virtual size_t pop_output(Frame &out)
Read samples from input frame and fill output frame.
BuiltinResampler(core::IAllocator &allocator, core::BufferFactory< sample_t > &buffer_factory, ResamplerProfile profile, core::nanoseconds_t frame_length, const audio::SampleSpec &sample_spec)
Initialize.
virtual bool valid() const
Check if object is successfully constructed.
virtual const core::Slice< sample_t > & begin_push_input()
Get buffer to be filled with input data.
Audio frame.
Definition frame.h:22
Audio writer interface.
Definition iresampler.h:23
Sample stream specification. Defines sample rate and channel layout.
Definition sample_spec.h:24
size_t num_channels() const
Get number of channels.
Dynamic array.
Definition array.h:38
Memory allocator interface.
Definition iallocator.h:23
Base class for non-copyable objects.
Definition noncopyable.h:23
Audio frame.
Frame reader interface.
Audio resampler interface.
float sample_t
Audio sample.
Definition sample.h:22
ResamplerProfile
Resampler parameters presets.
int64_t nanoseconds_t
Nanoseconds.
Definition time.h:58
Root namespace.
Non-copyable object.
Resampler profile.
Audio sample.
Sample specifications.
Slice.
Commonly used types and functions.
Various units used in packets.