Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
sample_spec.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 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/sample_spec.h
10 //! @brief Sample specifications.
11 
12 #ifndef ROC_AUDIO_SAMPLE_SPEC_H_
13 #define ROC_AUDIO_SAMPLE_SPEC_H_
14 
15 #include "roc_audio/channel_set.h"
16 #include "roc_audio/sample.h"
17 #include "roc_core/stddefs.h"
18 #include "roc_core/time.h"
19 #include "roc_packet/units.h"
20 
21 namespace roc {
22 namespace audio {
23 
24 //! Sample specification.
25 //! Describes sample rate and channels.
26 class SampleSpec {
27 public:
28  //! Construct empty specification.
30 
31  //! Construct specification with parameters.
33 
34  //! Construct specification with parameters.
35  //! @remarks
36  //! This is a convenient overload for the case when 32-bit mask is enough to
37  //! describe channels. Otherwise, use overload that accepts ChannelSet.
39  ChannelLayout channel_layout,
40  ChannelOrder channel_order,
41  ChannelMask channel_mask);
42 
43  //! @name Equality
44  //! @{
45 
46  //! Check two specifications for equality.
47  bool operator==(const SampleSpec& other) const;
48 
49  //! Check two specifications for equality.
50  bool operator!=(const SampleSpec& other) const;
51 
52  // @}
53 
54  //! @name Getters and setters
55  //! @{
56 
57  //! Check if sample spec has non-zero rate and valid channel set.
58  bool is_valid() const;
59 
60  //! Get sample rate.
61  //! @remarks
62  //! Defines sample frequency (number of samples per second).
63  size_t sample_rate() const;
64 
65  //! Set sample rate.
67 
68  //! Get channel set.
69  //! @remarks
70  //! Defines sample channels (layout and numbers).
71  const ChannelSet& channel_set() const;
72 
73  //! Get mutable channel set.
75 
76  //! Set channel set.
78 
79  //! Get number enabled channels in channel set.
80  //! @remarks
81  //! Shorthand for channel_set().num_channels().
82  size_t num_channels() const;
83 
84  // @}
85 
86  //! @name Nanosecond duration converters
87  //! @{
88 
89  //! Convert nanoseconds duration to number of samples per channel.
90  //! @pre
91  //! @p ns_duration should not be negative.
92  //! @note
93  //! In case of overflow, result is saturated.
94  size_t ns_2_samples_per_chan(core::nanoseconds_t ns_duration) const;
95 
96  //! Convert number of samples per channel to nanoseconds duration.
97  //! @note
98  //! In case of overflow, result is saturated.
99  core::nanoseconds_t samples_per_chan_2_ns(size_t n_samples) const;
100 
101  //! Convert (possibly fractional) number samples per channel to nanoseconds duration.
102  //! @note
103  //! In case of overflow, result is saturated.
105 
106  //! Convert nanoseconds duration to number of samples for all channels.
107  //! @pre
108  //! @p ns_duration should not be negative.
109  //! @post
110  //! result is always multiple of number of channels.
111  //! @note
112  //! In case of overflow, result is saturated.
113  size_t ns_2_samples_overall(core::nanoseconds_t ns_duration) const;
114 
115  //! Convert number of samples for all channels to nanoseconds duration.
116  //! @pre
117  //! @p n_samples should be multiple of number of channels.
118  //! @note
119  //! In case of overflow, result is saturated.
120  core::nanoseconds_t samples_overall_2_ns(size_t n_samples) const;
121 
122  //! Convert number of samples (possibly non-integer) to nanoseconds.
123  //! @note
124  //! In case of overflow, result is saturated.
126 
127  // @}
128 
129  //! @name RTP timestamp converters
130  //! @{
131 
132  //! Convert nanoseconds delta to stream timestamp delta.
133  //! @remarks
134  //! Same as ns_2_samples_per_chan(), but supports negative deltas.
137 
138  //! Convert stream timestamp delta to nanoseconds delta.
139  //! @remarks
140  //! Same as samples_per_chan_2_ns(), but supports negative deltas.
143 
144  // @}
145 
146 private:
147  size_t sample_rate_;
148  ChannelSet channel_set_;
149 };
150 
151 } // namespace audio
152 } // namespace roc
153 
154 #endif // ROC_AUDIO_SAMPLE_SPEC_H_
Channel set.
Channel set. Multi-word bitmask with bits corresponding to enabled channels. Meaning of each channel ...
Definition: channel_set.h:26
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:26
void set_channel_set(const ChannelSet &channel_set)
Set channel set.
size_t ns_2_samples_overall(core::nanoseconds_t ns_duration) const
Convert nanoseconds duration to number of samples for all channels.
core::nanoseconds_t fract_samples_overall_2_ns(float n_samples) const
Convert number of samples (possibly non-integer) to nanoseconds.
core::nanoseconds_t stream_timestamp_delta_2_ns(packet::stream_timestamp_diff_t sts_delta) const
Convert stream timestamp delta to nanoseconds delta.
bool operator==(const SampleSpec &other) const
Check two specifications for equality.
bool is_valid() const
Check if sample spec has non-zero rate and valid channel set.
SampleSpec(size_t sample_rate, ChannelLayout channel_layout, ChannelOrder channel_order, ChannelMask channel_mask)
Construct specification with parameters.
size_t num_channels() const
Get number enabled channels in channel set.
SampleSpec()
Construct empty specification.
size_t sample_rate() const
Get sample rate.
bool operator!=(const SampleSpec &other) const
Check two specifications for equality.
const ChannelSet & channel_set() const
Get channel set.
void set_sample_rate(size_t sample_rate)
Set sample rate.
core::nanoseconds_t samples_overall_2_ns(size_t n_samples) const
Convert number of samples for all channels to nanoseconds duration.
core::nanoseconds_t samples_per_chan_2_ns(size_t n_samples) const
Convert number of samples per channel to nanoseconds duration.
core::nanoseconds_t fract_samples_per_chan_2_ns(float n_samples) const
Convert (possibly fractional) number samples per channel to nanoseconds duration.
packet::stream_timestamp_diff_t ns_2_stream_timestamp_delta(core::nanoseconds_t ns_delta) const
Convert nanoseconds delta to stream timestamp delta.
size_t ns_2_samples_per_chan(core::nanoseconds_t ns_duration) const
Convert nanoseconds duration to number of samples per channel.
ChannelSet & channel_set()
Get mutable channel set.
SampleSpec(size_t sample_rate, const ChannelSet &channel_set)
Construct specification with parameters.
ChannelLayout
Channel layout. Defines meaning of channels in ChannelSet. ChannelMapper uses channel layout to decid...
Definition: channel_defs.h:23
uint32_t ChannelMask
Channel mask.
Definition: channel_defs.h:144
ChannelOrder
Surround channel order.
Definition: channel_defs.h:49
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
int32_t stream_timestamp_diff_t
Packet stream timestamp delta.
Definition: units.h:41
Root namespace.
Audio sample.
Commonly used types and functions.
Time definitions.
Various units used in packets.