Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
mixer.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/mixer.h
10 //! @brief Mixer.
11 
12 #ifndef ROC_AUDIO_MIXER_H_
13 #define ROC_AUDIO_MIXER_H_
14 
16 #include "roc_audio/sample.h"
17 #include "roc_audio/sample_spec.h"
19 #include "roc_core/list.h"
20 #include "roc_core/noncopyable.h"
21 #include "roc_core/slice.h"
22 #include "roc_core/time.h"
23 #include "roc_packet/units.h"
24 
25 namespace roc {
26 namespace audio {
27 
28 //! Mixer.
29 //! Mixes multiple input streams into one output stream.
30 //!
31 //! For example, these two input streams:
32 //! @code
33 //! 1, 2, 3, ...
34 //! 4, 5, 6, ...
35 //! @endcode
36 //!
37 //! are transformed into this output stream:
38 //! @code
39 //! 5, 7, 9, ...
40 //! @endcode
41 //!
42 //! If timestamps are enabled, mixer computes capture timestamp of output
43 //! frame as the average capture timestamps of all mixed input frames.
44 //! This makes sense only when all inputs are synchronized and their
45 //! timestamps are close to each other.
46 class Mixer : public IFrameReader, public core::NonCopyable<> {
47 public:
48  //! Initialize.
49  //! @p buffer_factory is used to allocate a temporary buffer for mixing.
50  //! @p enable_timestamps defines whether to enable calculation of capture timestamps.
51  Mixer(core::BufferFactory<sample_t>& buffer_factory, bool enable_timestamps);
52 
53  //! Check if the mixer was succefully constructed.
54  bool is_valid() const;
55 
56  //! Add input reader.
58 
59  //! Remove input reader.
61 
62  //! Read audio frame.
63  //! @remarks
64  //! Reads samples from every input reader, mixes them, and fills @p frame
65  //! with the result.
66  virtual bool read(Frame& frame);
67 
68 private:
69  void read_(sample_t* out_data,
70  size_t out_size,
71  unsigned& out_flags,
72  core::nanoseconds_t& out_cts);
73 
75  core::Slice<sample_t> temp_buf_;
76 
77  const bool enable_timestamps_;
78 
79  bool valid_;
80 };
81 
82 } // namespace audio
83 } // namespace roc
84 
85 #endif // ROC_AUDIO_MIXER_H_
Buffer factory.
Audio frame.
Definition: frame.h:25
Frame reader interface.
Definition: iframe_reader.h:22
Mixer. Mixes multiple input streams into one output stream.
Definition: mixer.h:46
bool is_valid() const
Check if the mixer was succefully constructed.
void remove_input(IFrameReader &)
Remove input reader.
virtual bool read(Frame &frame)
Read audio frame.
Mixer(core::BufferFactory< sample_t > &buffer_factory, bool enable_timestamps)
Initialize. buffer_factory is used to allocate a temporary buffer for mixing. enable_timestamps defin...
void add_input(IFrameReader &)
Add input reader.
Buffer factory. Allows to instantiate fixed-size buffers.
Intrusive doubly-linked list.
Definition: list.h:35
Base class for non-copyable objects.
Definition: noncopyable.h:23
Frame reader interface.
Intrusive doubly-linked list.
float sample_t
Audio sample.
Definition: sample.h:22
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Audio sample.
Sample specifications.
Slice.
Time definitions.
Various units used in packets.