1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef RESONANCE_AUDIO_UTILS_BUFFER_UNPARTITIONER_H_
18#define RESONANCE_AUDIO_UTILS_BUFFER_UNPARTITIONER_H_
19
20#include <functional>
21#include <memory>
22
23#include "base/audio_buffer.h"
24#include "base/logging.h"
25
26namespace vraudio {
27
28// Unpackages |AudioBuffer| into output buffers of arbitrary sizes.
29class BufferUnpartitioner {
30 public:
31 // Callback to obtain input |AudioBuffer|s. Returns a nullptr if no buffers
32 // are available.
33 typedef std::function<const AudioBuffer*()> GetBufferCallback;
34
35 // Constructor.
36 //
37 // @param num_channels Number of audio channels in input and output buffers.
38 // @param frames_per_buffer Number of frames per input buffer.
39 // @param buffer_callback Callback to receive output |AudioBuffer|s.
40 BufferUnpartitioner(size_t num_channels, size_t frames_per_buffer,
41 GetBufferCallback buffer_callback);
42
43 // Returns the number of input buffers required for a given number of output
44 // frames and based on the current fill state of the internal |temp_buffer_|.
45 //
46 // @param num_output_frames Number of output frames.
47 // @return Number of required input |AudioBuffer|s.
48 size_t GetNumBuffersRequestedForNumInputFrames(
49 size_t num_output_frames) const;
50
51 // Returns the number of buffered frames in internal |temp_buffer_|.
52 size_t GetNumBufferedFrames() const;
53
54 // Requests an interleaved int16 output buffer. This method triggers
55 // |GetBufferCallback|.
56 //
57 // @param output_buffer Interleaved output buffer to write to.
58 // @param num_channels Number of channels in output buffer.
59 // @param num_frames Number of frames in output buffer.
60 // @return Number of frames actually written.
61 size_t GetBuffer(int16* output_buffer, size_t num_channels,
62 size_t num_frames);
63
64 // Requests an interleaved float output buffer. This method triggers
65 // |GetBufferCallback|.
66 //
67 // @param output_buffer Interleaved output buffer to write to.
68 // @param num_channels Number of channels in output buffer.
69 // @param num_frames Number of frames in output buffer.
70 // @return Number of frames actually written.
71 size_t GetBuffer(float* output_buffer, size_t num_channels,
72 size_t num_frames);
73
74 // Requests a planar int16 output buffer. This method triggers
75 // |GetBufferCallback|.
76 //
77 // @param output_buffer Planar output buffer to write to.
78 // @param num_channels Number of channels in output buffer.
79 // @param num_frames Number of frames in output buffer.
80 // @return Number of frames actually written.
81 size_t GetBuffer(int16** output_buffer, size_t num_channels,
82 size_t num_frames);
83
84 // Requests a planar float output buffer. This method triggers
85 // |GetBufferCallback|.
86 //
87 // @param output_buffer Planar output buffer to write to.
88 // @param num_channels Number of channels in output buffer.
89 // @param num_frames Number of frames in output buffer.
90 // @return Number of frames actually written.
91 size_t GetBuffer(float** output_buffer, size_t num_channels,
92 size_t num_frames);
93
94 // Clears internal temporary buffer that holds remaining audio frames.
95 void Clear();
96
97 private:
98 // Returns the number of frames that are buffered in |current_input_buffer_|.
99 //
100 // @return Number of frames in |current_input_buffer_|. If
101 // |current_input_buffer_| is undefined, it returns 0.
102 size_t GetNumFramesAvailableInBuffer() const;
103
104 // Writes output buffer into target buffer. Supported buffer types are planar
105 // and interleaved floating point abd interleaved int16 output. This method
106 // triggers |GetBufferCallback|.
107 //
108 // @tparam BufferType Output buffer type.
109 // @param buffer Output buffer to write to.
110 // @param num_channels Number of channels in output buffer.
111 // @param num_frames Number of frames in output buffer.
112 // @return Number of frames actually written.
113 template <typename BufferType>
114 size_t GetBufferTemplated(BufferType buffer, size_t num_channels,
115 size_t num_frames);
116
117 // Number of channels in output buffers.
118 const size_t num_channels_;
119
120 // Number of frames per buffer in output buffers.
121 const size_t frames_per_buffer_;
122
123 // Callback to request input |AudioBuffer|s.
124 const GetBufferCallback buffer_callback_;
125
126 // Temporary buffer containing remaining audio frames.
127 const AudioBuffer* current_input_buffer_; // Not owned.
128
129 // Current read position in |current_input_buffer_|.
130 size_t current_buffer_read_offset_frames_;
131};
132
133} // namespace vraudio
134
135#endif // RESONANCE_AUDIO_UTILS_BUFFER_UNPARTITIONER_H_
136

source code of qtmultimedia/src/3rdparty/resonance-audio/resonance_audio/utils/buffer_unpartitioner.h