Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
pcm_format.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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/pcm_format.h
10 //! @brief PCM format.
11 
12 #ifndef ROC_AUDIO_PCM_FORMAT_H_
13 #define ROC_AUDIO_PCM_FORMAT_H_
14 
15 #include "roc_core/attributes.h"
16 #include "roc_core/stddefs.h"
17 
18 namespace roc {
19 namespace audio {
20 
21 //! PCM sample binary code.
22 enum PcmCode {
23  PcmCode_SInt8, //!< 8-bit signed integer.
24  PcmCode_UInt8, //!< 8-bit unsigned integer.
25  PcmCode_SInt16, //!< 16-bit signed integer.
26  PcmCode_UInt16, //!< 16-bit unsigned integer.
27  PcmCode_SInt18, //!< 18-bit signed integer (2.25 bytes).
28  PcmCode_UInt18, //!< 18-bit unsigned integer (2.25 bytes).
29  PcmCode_SInt18_3, //!< 18-bit signed integer, in low bits of 3-byte container.
30  PcmCode_UInt18_3, //!< 18-bit unsigned integer, in low bits of 3-byte container.
31  PcmCode_SInt18_4, //!< 18-bit signed integer, in low bits of 4-byte container.
32  PcmCode_UInt18_4, //!< 18-bit unsigned integer, in low bits of 4-byte container.
33  PcmCode_SInt20, //!< 20-bit signed integer (2.5 bytes).
34  PcmCode_UInt20, //!< 20-bit unsigned integer (2.5 bytes).
35  PcmCode_SInt20_3, //!< 20-bit signed integer, in low bits of 3-byte container.
36  PcmCode_UInt20_3, //!< 20-bit unsigned integer, in low bits of 3-byte container.
37  PcmCode_SInt20_4, //!< 20-bit signed integer, in low bits of 4-byte container.
38  PcmCode_UInt20_4, //!< 20-bit unsigned integer, in low bits of 4-byte container.
39  PcmCode_SInt24, //!< 24-bit signed integer (3 bytes).
40  PcmCode_UInt24, //!< 24-bit unsigned integer (3 bytes).
41  PcmCode_SInt24_4, //!< 24-bit signed integer, in low bits of 4-byte container.
42  PcmCode_UInt24_4, //!< 24-bit unsigned integer, in low bits of 4-byte container.
43  PcmCode_SInt32, //!< 32-bit signed integer.
44  PcmCode_UInt32, //!< 32-bit unsigned integer.
45  PcmCode_SInt64, //!< 64-bit signed integer.
46  PcmCode_UInt64, //!< 64-bit unsigned integer.
47  PcmCode_Float32, //!< 32-bit IEEE-754 float in range [-1.0; +1.0].
48  PcmCode_Float64, //!< 64-bit IEEE-754 float in range [-1.0; +1.0].
49 
50  PcmCode_Max //!< Maximum value.
51 };
52 
53 //! PCM sample endianess.
54 enum PcmEndian {
55  PcmEndian_Native, //!< Endian native to current CPU.
56  PcmEndian_Big, //!< Big endian.
57  PcmEndian_Little //!< Little endian.
58 };
59 
60 //! PCM format description.
61 struct PcmFormat {
62  //! Sample binary code.
64 
65  //! Sample endianess.
67 
68  //! Initialize.
70  : code()
71  , endian() {
72  }
73 
74  //! Initialize.
76  : code(enc)
77  , endian(end) {
78  }
79 
80  //! Check two formats for equality.
81  bool operator==(const PcmFormat& other) const {
82  return code == other.code && endian == other.endian;
83  }
84 
85  //! Check two formats for equality.
86  bool operator!=(const PcmFormat& other) const {
87  return !(*this == other);
88  }
89 };
90 
91 //! PCM format meta-information.
92 struct PcmTraits {
93  //! Number of significant bits per sample.
94  size_t bit_depth;
95 
96  //! Number of total bits per sample in packed form.
97  size_t bit_width;
98 
99  //! True for integers, false for floating point.
101 
102  //! True for signed integers and floating point.
103  bool is_signed;
104 
105  PcmTraits()
106  : bit_depth(0)
107  , bit_width(0)
108  , is_integer(false)
109  , is_signed(false) {
110  }
111 };
112 
113 //! Get string name of PCM format.
114 const char* pcm_format_to_str(const PcmFormat& fmt);
115 
116 //! Parse PCM format from string name.
117 ROC_ATTR_NODISCARD bool pcm_format_parse(const char* str, PcmFormat& fmt);
118 
119 //! Get traits for PCM format.
121 
122 } // namespace audio
123 } // namespace roc
124 
125 #endif // ROC_AUDIO_PCM_FORMAT_H_
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
const char * pcm_format_to_str(const PcmFormat &fmt)
Get string name of PCM format.
PcmTraits pcm_format_traits(const PcmFormat &fmt)
Get traits for PCM format.
PcmEndian
PCM sample endianess.
Definition: pcm_format.h:54
@ PcmEndian_Big
Big endian.
Definition: pcm_format.h:56
@ PcmEndian_Little
Little endian.
Definition: pcm_format.h:57
@ PcmEndian_Native
Endian native to current CPU.
Definition: pcm_format.h:55
ROC_ATTR_NODISCARD bool pcm_format_parse(const char *str, PcmFormat &fmt)
Parse PCM format from string name.
PcmCode
PCM sample binary code.
Definition: pcm_format.h:22
@ PcmCode_UInt64
64-bit unsigned integer.
Definition: pcm_format.h:46
@ PcmCode_UInt24
24-bit unsigned integer (3 bytes).
Definition: pcm_format.h:40
@ PcmCode_SInt20
20-bit signed integer (2.5 bytes).
Definition: pcm_format.h:33
@ PcmCode_SInt20_4
20-bit signed integer, in low bits of 4-byte container.
Definition: pcm_format.h:37
@ PcmCode_SInt18
18-bit signed integer (2.25 bytes).
Definition: pcm_format.h:27
@ PcmCode_SInt8
8-bit signed integer.
Definition: pcm_format.h:23
@ PcmCode_Float32
32-bit IEEE-754 float in range [-1.0; +1.0].
Definition: pcm_format.h:47
@ PcmCode_SInt16
16-bit signed integer.
Definition: pcm_format.h:25
@ PcmCode_SInt24
24-bit signed integer (3 bytes).
Definition: pcm_format.h:39
@ PcmCode_SInt20_3
20-bit signed integer, in low bits of 3-byte container.
Definition: pcm_format.h:35
@ PcmCode_SInt24_4
24-bit signed integer, in low bits of 4-byte container.
Definition: pcm_format.h:41
@ PcmCode_SInt64
64-bit signed integer.
Definition: pcm_format.h:45
@ PcmCode_UInt20_3
20-bit unsigned integer, in low bits of 3-byte container.
Definition: pcm_format.h:36
@ PcmCode_UInt32
32-bit unsigned integer.
Definition: pcm_format.h:44
@ PcmCode_UInt20
20-bit unsigned integer (2.5 bytes).
Definition: pcm_format.h:34
@ PcmCode_UInt8
8-bit unsigned integer.
Definition: pcm_format.h:24
@ PcmCode_UInt20_4
20-bit unsigned integer, in low bits of 4-byte container.
Definition: pcm_format.h:38
@ PcmCode_SInt32
32-bit signed integer.
Definition: pcm_format.h:43
@ PcmCode_SInt18_3
18-bit signed integer, in low bits of 3-byte container.
Definition: pcm_format.h:29
@ PcmCode_Max
Maximum value.
Definition: pcm_format.h:50
@ PcmCode_SInt18_4
18-bit signed integer, in low bits of 4-byte container.
Definition: pcm_format.h:31
@ PcmCode_UInt18
18-bit unsigned integer (2.25 bytes).
Definition: pcm_format.h:28
@ PcmCode_UInt18_4
18-bit unsigned integer, in low bits of 4-byte container.
Definition: pcm_format.h:32
@ PcmCode_Float64
64-bit IEEE-754 float in range [-1.0; +1.0].
Definition: pcm_format.h:48
@ PcmCode_UInt24_4
24-bit unsigned integer, in low bits of 4-byte container.
Definition: pcm_format.h:42
@ PcmCode_UInt16
16-bit unsigned integer.
Definition: pcm_format.h:26
@ PcmCode_UInt18_3
18-bit unsigned integer, in low bits of 3-byte container.
Definition: pcm_format.h:30
Root namespace.
Commonly used types and functions.
PCM format description.
Definition: pcm_format.h:61
PcmFormat(PcmCode enc, PcmEndian end)
Initialize.
Definition: pcm_format.h:75
PcmEndian endian
Sample endianess.
Definition: pcm_format.h:66
PcmFormat()
Initialize.
Definition: pcm_format.h:69
bool operator!=(const PcmFormat &other) const
Check two formats for equality.
Definition: pcm_format.h:86
bool operator==(const PcmFormat &other) const
Check two formats for equality.
Definition: pcm_format.h:81
PcmCode code
Sample binary code.
Definition: pcm_format.h:63
PCM format meta-information.
Definition: pcm_format.h:92
bool is_signed
True for signed integers and floating point.
Definition: pcm_format.h:103
size_t bit_width
Number of total bits per sample in packed form.
Definition: pcm_format.h:97
size_t bit_depth
Number of significant bits per sample.
Definition: pcm_format.h:94
bool is_integer
True for integers, false for floating point.
Definition: pcm_format.h:100