| 1 | /* | 
| 2 | Copyright 2018 Google Inc. All Rights Reserved. | 
| 3 |  | 
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); | 
| 5 | you may not use this file except in compliance with the License. | 
| 6 | You may obtain a copy of the License at | 
| 7 |  | 
| 8 |     http://www.apache.org/licenses/LICENSE-2.0 | 
| 9 |  | 
| 10 | Unless required by applicable law or agreed to in writing, software | 
| 11 | distributed under the License is distributed on an "AS-IS" BASIS, | 
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 13 | See the License for the specific language governing permissions and | 
| 14 | limitations 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 |  | 
| 31 | namespace vraudio { | 
| 32 |  | 
| 33 | // Implementation of ResonanceAudioApi interface. | 
| 34 | class ResonanceAudioApiImpl : public ResonanceAudioApi { | 
| 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 |  | 
| 46 |   ////////////////////////////////// | 
| 47 |   // ResonanceAudioApi implementation. | 
| 48 |   ////////////////////////////////// | 
| 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 |  | 
| 113 |   ////////////////////////////////// | 
| 114 |   // Internal API methods. | 
| 115 |   ////////////////////////////////// | 
| 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 |   // Returns the last processed buffer containing stereo data for the room reverb | 
| 128 |   // | 
| 129 |   // @return Pointer to room reverb stereo buffer. | 
| 130 |   const AudioBuffer* GetReverbBuffer() const; | 
| 131 |  | 
| 132 |   // Triggers processing of the audio graph with the updated system properties. | 
| 133 |   void ProcessNextBuffer(); | 
| 134 |  | 
| 135 |  private: | 
| 136 |   // This method triggers the processing of the audio graph and outputs a | 
| 137 |   // binaural stereo output buffer. | 
| 138 |   // | 
| 139 |   // @tparam OutputType Output sample format, only float and int16 are | 
| 140 |   //     supported. | 
| 141 |   // @param num_channels Number of channels in output buffer. | 
| 142 |   // @param num_frames Size of buffer in frames. | 
| 143 |   // @param buffer_ptr Raw pointer to audio buffer. | 
| 144 |   // @return True if a valid output was successfully rendered, false otherwise. | 
| 145 |   template <typename OutputType> | 
| 146 |   bool FillOutputBuffer(size_t num_channels, size_t num_frames, | 
| 147 |                         OutputType buffer_ptr); | 
| 148 |  | 
| 149 |   // Sets the next audio buffer to a sound source. | 
| 150 |   // | 
| 151 |   // @param source_id Id of sound source. | 
| 152 |   // @param audio_buffer_ptr Pointer to planar or interleaved audio buffer. | 
| 153 |   // @param num_input_channels Number of input channels. | 
| 154 |   // @param num_frames Number of frames per channel / audio buffer. | 
| 155 |   template <typename SampleType> | 
| 156 |   void SetSourceBuffer(SourceId source_id, SampleType audio_buffer_ptr, | 
| 157 |                        size_t num_input_channels, size_t num_frames); | 
| 158 |  | 
| 159 |   // Graph manager used to create and destroy sound objects. | 
| 160 |   std::unique_ptr<GraphManager> graph_manager_; | 
| 161 |  | 
| 162 |   // Manages system wide settings. | 
| 163 |   SystemSettings system_settings_; | 
| 164 |  | 
| 165 |   // Task queue to cache manipulation of all the entities in the system. All | 
| 166 |   // tasks are executed from the audio thread. | 
| 167 |   LocklessTaskQueue task_queue_; | 
| 168 |  | 
| 169 |   // Incremental source id counter. | 
| 170 |   std::atomic<int> source_id_counter_; | 
| 171 | }; | 
| 172 |  | 
| 173 | }  // namespace vraudio | 
| 174 |  | 
| 175 | #endif  // RESONANCE_AUDIO_GRAPH_RESONANCE_AUDIO_API_IMPL_H_ | 
| 176 |  |