RavEngine
Loading...
Searching...
No Matches
resonance_audio_api_impl.h
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_GRAPH_RESONANCE_AUDIO_API_IMPL_H_
18#define RESONANCE_AUDIO_GRAPH_RESONANCE_AUDIO_API_IMPL_H_
19
20#include <atomic>
21#include <memory>
22#include <vector>
23
24#include "base/integral_types.h"
25#include "api/resonance_audio_api.h"
26#include "base/audio_buffer.h"
27#include "graph/graph_manager.h"
28#include "graph/system_settings.h"
29#include "utils/lockless_task_queue.h"
30
31namespace vraudio {
32
33// Implementation of ResonanceAudioApi interface.
35 public:
36 // Constructor that initializes |ResonanceAudioApi| with system configuration.
37 //
38 // @param num_channels Number of channels of audio output.
39 // @param frames_per_buffer Number of frames per buffer.
40 // @param sample_rate_hz System sample rate.
41 ResonanceAudioApiImpl(size_t num_channels, size_t frames_per_buffer,
42 int sample_rate_hz);
43
44 ~ResonanceAudioApiImpl() override;
45
47 // ResonanceAudioApi implementation.
49
50 // Obtain processed output buffers.
51 bool FillInterleavedOutputBuffer(size_t num_channels, size_t num_frames,
52 float* buffer_ptr) override;
53 bool FillInterleavedOutputBuffer(size_t num_channels, size_t num_frames,
54 int16* buffer_ptr) override;
55 bool FillPlanarOutputBuffer(size_t num_channels, size_t num_frames,
56 float* const* buffer_ptr) override;
57 bool FillPlanarOutputBuffer(size_t num_channels, size_t num_frames,
58 int16* const* buffer_ptr) override;
59
60 // Listener configuration.
61 void SetHeadPosition(float x, float y, float z) override;
62 void SetHeadRotation(float x, float y, float z, float w) override;
63 void SetMasterVolume(float volume) override;
64 void SetStereoSpeakerMode(bool enabled) override;
65
66 // Create and destroy sources.
67 SourceId CreateAmbisonicSource(size_t num_channels) override;
68 SourceId CreateStereoSource(size_t num_channels) override;
69 SourceId CreateSoundObjectSource(RenderingMode rendering_mode) override;
70 void DestroySource(SourceId source_id) override;
71
72 // Set source data.
73 void SetInterleavedBuffer(SourceId source_id, const float* audio_buffer_ptr,
74 size_t num_channels, size_t num_frames) override;
75 void SetInterleavedBuffer(SourceId source_id, const int16* audio_buffer_ptr,
76 size_t num_channels, size_t num_frames) override;
77 void SetPlanarBuffer(SourceId source_id, const float* const* audio_buffer_ptr,
78 size_t num_channels, size_t num_frames) override;
79 void SetPlanarBuffer(SourceId source_id, const int16* const* audio_buffer_ptr,
80 size_t num_channels, size_t num_frames) override;
81
82 // Source configuration.
83 void SetSourceDistanceAttenuation(SourceId source_id,
84 float distance_attenuation) override;
85 void SetSourceDistanceModel(SourceId source_id, DistanceRolloffModel rolloff,
86 float min_distance, float max_distance) override;
87 void SetSourcePosition(SourceId source_id, float x, float y,
88 float z) override;
89 void SetSourceRoomEffectsGain(SourceId source_id,
90 float room_effects_gain) override;
91 void SetSourceRotation(SourceId source_id, float x, float y, float z,
92 float w) override;
93 void SetSourceVolume(SourceId source_id, float volume) override;
94
95 // Sound object configuration.
96 void SetSoundObjectDirectivity(SourceId sound_object_source_id, float alpha,
97 float order) override;
98 void SetSoundObjectListenerDirectivity(SourceId sound_object_source_id,
99 float alpha, float order) override;
100 void SetSoundObjectNearFieldEffectGain(SourceId sound_object_source_id,
101 float gain) override;
102 void SetSoundObjectOcclusionIntensity(SourceId sound_object_source_id,
103 float intensity) override;
104 void SetSoundObjectSpread(SourceId sound_object_source_id,
105 float spread_deg) override;
106
107 // Room effects configuration.
108 void EnableRoomEffects(bool enable) override;
109 void SetReflectionProperties(
110 const ReflectionProperties& reflection_properties) override;
111 void SetReverbProperties(const ReverbProperties& reverb_properties) override;
112
114 // Internal API methods.
116
117 // Returns the last processed output buffer of the ambisonic mix.
118 //
119 // @return Pointer to ambisonic output buffer.
120 const AudioBuffer* GetAmbisonicOutputBuffer() const;
121
122 // Returns the last processed output buffer of the stereo (binaural) mix.
123 //
124 // @return Pointer to stereo output buffer.
125 const AudioBuffer* GetStereoOutputBuffer() const;
126
127 // Triggers processing of the audio graph with the updated system properties.
128 void ProcessNextBuffer();
129
130 private:
131 // This method triggers the processing of the audio graph and outputs a
132 // binaural stereo output buffer.
133 //
134 // @tparam OutputType Output sample format, only float and int16 are
135 // supported.
136 // @param num_channels Number of channels in output buffer.
137 // @param num_frames Size of buffer in frames.
138 // @param buffer_ptr Raw pointer to audio buffer.
139 // @return True if a valid output was successfully rendered, false otherwise.
140 template <typename OutputType>
141 bool FillOutputBuffer(size_t num_channels, size_t num_frames,
142 OutputType buffer_ptr);
143
144 // Sets the next audio buffer to a sound source.
145 //
146 // @param source_id Id of sound source.
147 // @param audio_buffer_ptr Pointer to planar or interleaved audio buffer.
148 // @param num_input_channels Number of input channels.
149 // @param num_frames Number of frames per channel / audio buffer.
150 template <typename SampleType>
151 void SetSourceBuffer(SourceId source_id, SampleType audio_buffer_ptr,
152 size_t num_input_channels, size_t num_frames);
153
154 // Graph manager used to create and destroy sound objects.
155 std::unique_ptr<GraphManager> graph_manager_;
156
157 // Manages system wide settings.
158 SystemSettings system_settings_;
159
160 // Task queue to cache manipulation of all the entities in the system. All
161 // tasks are executed from the audio thread.
162 LocklessTaskQueue task_queue_;
163
164 // Incremental source id counter.
165 std::atomic<int> source_id_counter_;
166};
167
168} // namespace vraudio
169
170#endif // RESONANCE_AUDIO_GRAPH_RESONANCE_AUDIO_API_IMPL_H_
Definition audio_buffer.h:78
Definition lockless_task_queue.h:29
Definition resonance_audio_api_impl.h:34
Definition resonance_audio_api.h:142
Definition system_settings.h:30
Definition resonance_audio_api.h:73
Definition resonance_audio_api.h:111