Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
speex_resampler.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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/target_speexdsp/roc_audio/speex_resampler.h
10 //! @brief Speex resampler.
11 
12 #ifndef ROC_AUDIO_SPEEX_RESAMPLER_H_
13 #define ROC_AUDIO_SPEEX_RESAMPLER_H_
14 
15 #include "roc_audio/frame.h"
18 #include "roc_audio/iresampler.h"
20 #include "roc_audio/sample.h"
21 #include "roc_audio/sample_spec.h"
22 #include "roc_core/array.h"
23 #include "roc_core/noncopyable.h"
24 #include "roc_core/rate_limiter.h"
25 #include "roc_core/slice.h"
26 #include "roc_core/stddefs.h"
27 #include "roc_packet/units.h"
28 
29 #include <speex/speex_resampler.h>
30 
31 namespace roc {
32 namespace audio {
33 
34 //! Speex resampler.
35 //!
36 //! Resamples audio stream using SpeexDSP library.
37 //!
38 //! This backend is very fast even on weak CPUs, and provides good quality,
39 //! but it can't apply requested scaling very precisely.
40 class SpeexResampler : public IResampler, public core::NonCopyable<> {
41 public:
42  //! Initialize.
44  FrameFactory& frame_factory,
45  ResamplerProfile profile,
46  const SampleSpec& in_spec,
47  const SampleSpec& out_spec);
48 
49  ~SpeexResampler();
50 
51  //! Check if object is successfully constructed.
52  virtual bool is_valid() const;
53 
54  //! Set new resample factor.
55  virtual bool set_scaling(size_t input_rate, size_t output_rate, float multiplier);
56 
57  //! Get buffer to be filled with input data.
59 
60  //! Commit buffer with input data.
61  virtual void end_push_input();
62 
63  //! Read samples from input frame and fill output frame.
64  virtual size_t pop_output(sample_t* out_data, size_t out_size);
65 
66  //! How many samples were pushed but not processed yet.
67  virtual float n_left_to_process() const;
68 
69 private:
70  void report_stats_();
71 
72  SpeexResamplerState* speex_state_;
73 
74  // Channel count.
75  const spx_uint32_t num_ch_;
76 
77  // Frame with input samples.
78  core::Slice<sample_t> in_frame_;
79  spx_uint32_t in_frame_size_;
80  spx_uint32_t in_frame_pos_;
81 
82  // Counts how many output samples to throw away in order to
83  // compensate resampler's inner latency.
84  size_t initial_out_countdown_;
85 
86  // Stores initial latency in order to track its further changes.
87  size_t initial_in_latency_;
88 
89  // Stores how much speex resampler latency changed from the start, in order to
90  // reflect it in n_left_to_process() for better precision in capture timestamp
91  // calculations.
92  ssize_t in_latency_diff_;
93 
94  core::RateLimiter rate_limiter_;
95 
96  bool valid_;
97 };
98 
99 } // namespace audio
100 } // namespace roc
101 
102 #endif // ROC_AUDIO_SPEEX_RESAMPLER_H_
Dynamic array.
Audio writer interface.
Definition: iresampler.h:24
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:30
virtual void end_push_input()
Commit buffer with input data.
virtual bool is_valid() const
Check if object is successfully constructed.
virtual bool set_scaling(size_t input_rate, size_t output_rate, float multiplier)
Set new resample factor.
virtual size_t pop_output(sample_t *out_data, size_t out_size)
Read samples from input frame and fill output frame.
virtual const core::Slice< sample_t > & begin_push_input()
Get buffer to be filled with input data.
SpeexResampler(core::IArena &arena, FrameFactory &frame_factory, ResamplerProfile profile, const SampleSpec &in_spec, const SampleSpec &out_spec)
Initialize.
virtual float n_left_to_process() const
How many samples were pushed but not processed yet.
IArena & arena() const
Get arena.
Memory arena interface.
Definition: iarena.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Audio frame.
Frame factory.
Frame reader interface.
Audio resampler interface.
float sample_t
Raw audio sample.
Definition: sample.h:22
ResamplerProfile
Resampler parameters presets.
Root namespace.
Non-copyable object.
Rate limiter.
Resampler config.
Audio sample.
Sample specifications.
Slice.
Commonly used types and functions.
Various units used in packets.