Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
pcm_mapper.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_mapper.h
10 //! @brief PCM format mapper.
11 
12 #ifndef ROC_AUDIO_PCM_MAPPER_H_
13 #define ROC_AUDIO_PCM_MAPPER_H_
14 
15 #include "roc_audio/pcm_format.h"
16 #include "roc_core/noncopyable.h"
17 #include "roc_core/stddefs.h"
18 
19 namespace roc {
20 namespace audio {
21 
22 //! PCM format mapper.
23 //! Convert between PCM formats.
24 class PcmMapper : public core::NonCopyable<> {
25 public:
26  //! Initialize.
27  //! @pre
28  //! @p input_fmt and @p output_fmt should be PCM formats.
29  PcmMapper(PcmFormat input_fmt, PcmFormat output_fmt);
30 
31  //! Get input format.
33 
34  //! Get output format.
36 
37  //! Get number of input samples per channel for given number of bytes.
38  size_t input_sample_count(size_t input_bytes) const;
39 
40  //! Get number of input samples per channel for given number of bytes.
41  size_t output_sample_count(size_t output_bytes) const;
42 
43  //! Get number of input bytes for given number of samples per channel.
44  size_t input_byte_count(size_t input_samples) const;
45 
46  //! Get number of output bytes for given number of samples per channel.
47  size_t output_byte_count(size_t output_samples) const;
48 
49  //! Get number of input bits for given number of samples per channel.
50  size_t input_bit_count(size_t input_samples) const;
51 
52  //! Get number of output bits for given number of samples per channel.
53  size_t output_bit_count(size_t output_samples) const;
54 
55  //! Map samples from input to output format.
56  //! @remarks
57  //! @p in_data is a pointer to input buffer
58  //! @p in_byte_size is size of input buffer in bytes
59  //! @p in_bit_off is an offset in input buffer in bits
60  //! @p out_data is a pointer to output buffer
61  //! @p out_byte_size is size of output buffer in bytes
62  //! @p out_bit_off is an offset in output buffer in bits
63  //! @p n_samples is number of input and output samples for all channels
64  //! @returns
65  //! number of samples actually mapped, which may be truncated if
66  //! input or output buffer is smaller than requested
67  //! @note
68  //! updates @p in_bit_off and @p out_bit_off
69  size_t map(const void* in_data,
70  size_t in_byte_size,
71  size_t& in_bit_off,
72  void* out_data,
73  size_t out_byte_size,
74  size_t& out_bit_off,
75  size_t n_samples);
76 
77 private:
78  const PcmFormat input_fmt_;
79  const PcmFormat output_fmt_;
80 
81  const PcmTraits input_traits_;
82  const PcmTraits output_traits_;
83 
84  PcmMapFn map_func_;
85 };
86 
87 } // namespace audio
88 } // namespace roc
89 
90 #endif // ROC_AUDIO_PCM_MAPPER_H_
PCM format mapper. Convert between PCM formats.
Definition: pcm_mapper.h:24
size_t output_sample_count(size_t output_bytes) const
Get number of input samples per channel for given number of bytes.
PcmMapper(PcmFormat input_fmt, PcmFormat output_fmt)
Initialize.
size_t map(const void *in_data, size_t in_byte_size, size_t &in_bit_off, void *out_data, size_t out_byte_size, size_t &out_bit_off, size_t n_samples)
Map samples from input to output format.
size_t output_bit_count(size_t output_samples) const
Get number of output bits for given number of samples per channel.
PcmFormat input_format() const
Get input format.
size_t input_sample_count(size_t input_bytes) const
Get number of input samples per channel for given number of bytes.
size_t input_bit_count(size_t input_samples) const
Get number of input bits for given number of samples per channel.
PcmFormat output_format() const
Get output format.
size_t input_byte_count(size_t input_samples) const
Get number of input bytes for given number of samples per channel.
size_t output_byte_count(size_t output_samples) const
Get number of output bytes for given number of samples per channel.
Base class for non-copyable objects.
Definition: noncopyable.h:23
void(* PcmMapFn)(const uint8_t *in_data, size_t &in_bit_off, uint8_t *out_data, size_t &out_bit_off, size_t n_samples)
PCM mapping function.
Definition: pcm_format.h:235
PcmFormat
PCM format. Defines PCM sample coding and endian.
Definition: pcm_format.h:22
Root namespace.
Non-copyable object.
PCM format.
Commonly used types and functions.
PCM format meta-information.
Definition: pcm_format.h:201