Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 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 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #define LOG_TAG "PreProcessing" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/Timers.h> |
| 23 | #include <hardware/audio_effect.h> |
| 24 | #include <audio_effects/effect_aec.h> |
| 25 | #include <audio_effects/effect_agc.h> |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 26 | #ifndef WEBRTC_LEGACY |
| 27 | #include <audio_effects/effect_agc2.h> |
| 28 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 29 | #include <audio_effects/effect_ns.h> |
Eric Laurent | 5387696 | 2012-01-31 12:35:20 -0800 | [diff] [blame] | 30 | #include <module_common_types.h> |
| 31 | #include <audio_processing.h> |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 32 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 33 | #include "speex/speex_resampler.h" |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 34 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 35 | |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 36 | // undefine to perform multi channels API functional tests |
| 37 | //#define DUAL_MIC_TEST |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 38 | |
| 39 | //------------------------------------------------------------------------------ |
| 40 | // local definitions |
| 41 | //------------------------------------------------------------------------------ |
| 42 | |
| 43 | // maximum number of sessions |
| 44 | #define PREPROC_NUM_SESSIONS 8 |
| 45 | |
| 46 | // types of pre processing modules |
| 47 | enum preproc_id |
| 48 | { |
| 49 | PREPROC_AGC, // Automatic Gain Control |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 50 | #ifndef WEBRTC_LEGACY |
| 51 | PREPROC_AGC2, // Automatic Gain Control 2 |
| 52 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 53 | PREPROC_AEC, // Acoustic Echo Canceler |
| 54 | PREPROC_NS, // Noise Suppressor |
| 55 | PREPROC_NUM_EFFECTS |
| 56 | }; |
| 57 | |
| 58 | // Session state |
| 59 | enum preproc_session_state { |
| 60 | PREPROC_SESSION_STATE_INIT, // initialized |
| 61 | PREPROC_SESSION_STATE_CONFIG // configuration received |
| 62 | }; |
| 63 | |
| 64 | // Effect/Preprocessor state |
| 65 | enum preproc_effect_state { |
| 66 | PREPROC_EFFECT_STATE_INIT, // initialized |
| 67 | PREPROC_EFFECT_STATE_CREATED, // webRTC engine created |
| 68 | PREPROC_EFFECT_STATE_CONFIG, // configuration received/disabled |
| 69 | PREPROC_EFFECT_STATE_ACTIVE // active/enabled |
| 70 | }; |
| 71 | |
| 72 | // handle on webRTC engine |
| 73 | typedef void* preproc_fx_handle_t; |
| 74 | |
| 75 | typedef struct preproc_session_s preproc_session_t; |
| 76 | typedef struct preproc_effect_s preproc_effect_t; |
| 77 | typedef struct preproc_ops_s preproc_ops_t; |
| 78 | |
| 79 | // Effect operation table. Functions for all pre processors are declared in sPreProcOps[] table. |
| 80 | // Function pointer can be null if no action required. |
| 81 | struct preproc_ops_s { |
| 82 | int (* create)(preproc_effect_t *fx); |
| 83 | int (* init)(preproc_effect_t *fx); |
| 84 | int (* reset)(preproc_effect_t *fx); |
| 85 | void (* enable)(preproc_effect_t *fx); |
| 86 | void (* disable)(preproc_effect_t *fx); |
| 87 | int (* set_parameter)(preproc_effect_t *fx, void *param, void *value); |
Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 88 | int (* get_parameter)(preproc_effect_t *fx, void *param, uint32_t *size, void *value); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 89 | int (* set_device)(preproc_effect_t *fx, uint32_t device); |
| 90 | }; |
| 91 | |
| 92 | // Effect context |
| 93 | struct preproc_effect_s { |
| 94 | const struct effect_interface_s *itfe; |
| 95 | uint32_t procId; // type of pre processor (enum preproc_id) |
| 96 | uint32_t state; // current state (enum preproc_effect_state) |
| 97 | preproc_session_t *session; // session the effect is on |
| 98 | const preproc_ops_t *ops; // effect ops table |
| 99 | preproc_fx_handle_t engine; // handle on webRTC engine |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 100 | uint32_t type; // subtype of effect |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 101 | #ifdef DUAL_MIC_TEST |
| 102 | bool aux_channels_on; // support auxiliary channels |
| 103 | size_t cur_channel_config; // current auciliary channel configuration |
| 104 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // Session context |
| 108 | struct preproc_session_s { |
| 109 | struct preproc_effect_s effects[PREPROC_NUM_EFFECTS]; // effects in this session |
| 110 | uint32_t state; // current state (enum preproc_session_state) |
| 111 | int id; // audio session ID |
| 112 | int io; // handle of input stream this session is on |
| 113 | webrtc::AudioProcessing* apm; // handle on webRTC audio processing module (APM) |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 114 | #ifndef WEBRTC_LEGACY |
| 115 | // Audio Processing module builder |
| 116 | webrtc::AudioProcessingBuilder ap_builder; |
| 117 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 118 | size_t apmFrameCount; // buffer size for webRTC process (10 ms) |
| 119 | uint32_t apmSamplingRate; // webRTC APM sampling rate (8/16 or 32 kHz) |
| 120 | size_t frameCount; // buffer size before input resampler ( <=> apmFrameCount) |
| 121 | uint32_t samplingRate; // sampling rate at effect process interface |
| 122 | uint32_t inChannelCount; // input channel count |
| 123 | uint32_t outChannelCount; // output channel count |
| 124 | uint32_t createdMsk; // bit field containing IDs of crested pre processors |
| 125 | uint32_t enabledMsk; // bit field containing IDs of enabled pre processors |
| 126 | uint32_t processedMsk; // bit field containing IDs of pre processors already |
| 127 | // processed in current round |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 128 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 129 | webrtc::AudioFrame *procFrame; // audio frame passed to webRTC AMP ProcessStream() |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 130 | #else |
| 131 | // audio config strucutre |
| 132 | webrtc::AudioProcessing::Config config; |
| 133 | webrtc::StreamConfig inputConfig; // input stream configuration |
| 134 | webrtc::StreamConfig outputConfig; // output stream configuration |
| 135 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 136 | int16_t *inBuf; // input buffer used when resampling |
| 137 | size_t inBufSize; // input buffer size in frames |
| 138 | size_t framesIn; // number of frames in input buffer |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 139 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 140 | SpeexResamplerState *inResampler; // handle on input speex resampler |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 141 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 142 | int16_t *outBuf; // output buffer used when resampling |
| 143 | size_t outBufSize; // output buffer size in frames |
| 144 | size_t framesOut; // number of frames in output buffer |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 145 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 146 | SpeexResamplerState *outResampler; // handle on output speex resampler |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 147 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 148 | uint32_t revChannelCount; // number of channels on reverse stream |
| 149 | uint32_t revEnabledMsk; // bit field containing IDs of enabled pre processors |
| 150 | // with reverse channel |
| 151 | uint32_t revProcessedMsk; // bit field containing IDs of pre processors with reverse |
| 152 | // channel already processed in current round |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 153 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 154 | webrtc::AudioFrame *revFrame; // audio frame passed to webRTC AMP AnalyzeReverseStream() |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 155 | #else |
| 156 | webrtc::StreamConfig revConfig; // reverse stream configuration. |
| 157 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 158 | int16_t *revBuf; // reverse channel input buffer |
| 159 | size_t revBufSize; // reverse channel input buffer size |
| 160 | size_t framesRev; // number of frames in reverse channel input buffer |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 161 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 162 | SpeexResamplerState *revResampler; // handle on reverse channel input speex resampler |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 163 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 166 | #ifdef DUAL_MIC_TEST |
| 167 | enum { |
| 168 | PREPROC_CMD_DUAL_MIC_ENABLE = EFFECT_CMD_FIRST_PROPRIETARY, // enable dual mic mode |
| 169 | PREPROC_CMD_DUAL_MIC_PCM_DUMP_START, // start pcm capture |
| 170 | PREPROC_CMD_DUAL_MIC_PCM_DUMP_STOP // stop pcm capture |
| 171 | }; |
| 172 | |
| 173 | enum { |
| 174 | CHANNEL_CFG_MONO, |
| 175 | CHANNEL_CFG_STEREO, |
| 176 | CHANNEL_CFG_MONO_AUX, |
| 177 | CHANNEL_CFG_STEREO_AUX, |
| 178 | CHANNEL_CFG_CNT, |
| 179 | CHANNEL_CFG_FIRST_AUX = CHANNEL_CFG_MONO_AUX, |
| 180 | }; |
| 181 | |
| 182 | const channel_config_t sDualMicConfigs[CHANNEL_CFG_CNT] = { |
| 183 | {AUDIO_CHANNEL_IN_MONO , 0}, |
| 184 | {AUDIO_CHANNEL_IN_STEREO , 0}, |
| 185 | {AUDIO_CHANNEL_IN_FRONT , AUDIO_CHANNEL_IN_BACK}, |
| 186 | {AUDIO_CHANNEL_IN_STEREO , AUDIO_CHANNEL_IN_RIGHT} |
| 187 | }; |
| 188 | |
| 189 | bool sHasAuxChannels[PREPROC_NUM_EFFECTS] = { |
| 190 | false, // PREPROC_AGC |
| 191 | true, // PREPROC_AEC |
| 192 | true, // PREPROC_NS |
| 193 | }; |
| 194 | |
| 195 | bool gDualMicEnabled; |
| 196 | FILE *gPcmDumpFh; |
| 197 | static pthread_mutex_t gPcmDumpLock = PTHREAD_MUTEX_INITIALIZER; |
| 198 | #endif |
| 199 | |
| 200 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 201 | //------------------------------------------------------------------------------ |
| 202 | // Effect descriptors |
| 203 | //------------------------------------------------------------------------------ |
| 204 | |
| 205 | // UUIDs for effect types have been generated from http://www.itu.int/ITU-T/asn1/uuid.html |
| 206 | // as the pre processing effects are not defined by OpenSL ES |
| 207 | |
| 208 | // Automatic Gain Control |
| 209 | static const effect_descriptor_t sAgcDescriptor = { |
| 210 | { 0x0a8abfe0, 0x654c, 0x11e0, 0xba26, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type |
| 211 | { 0xaa8130e0, 0x66fc, 0x11e0, 0xbad0, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid |
| 212 | EFFECT_CONTROL_API_VERSION, |
| 213 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), |
| 214 | 0, //FIXME indicate CPU load |
| 215 | 0, //FIXME indicate memory usage |
| 216 | "Automatic Gain Control", |
| 217 | "The Android Open Source Project" |
| 218 | }; |
| 219 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 220 | #ifndef WEBRTC_LEGACY |
| 221 | // Automatic Gain Control 2 |
| 222 | static const effect_descriptor_t sAgc2Descriptor = { |
| 223 | { 0xae3c653b, 0xbe18, 0x4ab8, 0x8938, { 0x41, 0x8f, 0x0a, 0x7f, 0x06, 0xac } }, // type |
| 224 | { 0x89f38e65, 0xd4d2, 0x4d64, 0xad0e, { 0x2b, 0x3e, 0x79, 0x9e, 0xa8, 0x86 } }, // uuid |
| 225 | EFFECT_CONTROL_API_VERSION, |
| 226 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), |
| 227 | 0, //FIXME indicate CPU load |
| 228 | 0, //FIXME indicate memory usage |
| 229 | "Automatic Gain Control 2", |
| 230 | "The Android Open Source Project" |
| 231 | }; |
| 232 | #endif |
| 233 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 234 | // Acoustic Echo Cancellation |
| 235 | static const effect_descriptor_t sAecDescriptor = { |
| 236 | { 0x7b491460, 0x8d4d, 0x11e0, 0xbd61, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type |
| 237 | { 0xbb392ec0, 0x8d4d, 0x11e0, 0xa896, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid |
| 238 | EFFECT_CONTROL_API_VERSION, |
| 239 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), |
| 240 | 0, //FIXME indicate CPU load |
| 241 | 0, //FIXME indicate memory usage |
| 242 | "Acoustic Echo Canceler", |
| 243 | "The Android Open Source Project" |
| 244 | }; |
| 245 | |
| 246 | // Noise suppression |
| 247 | static const effect_descriptor_t sNsDescriptor = { |
| 248 | { 0x58b4b260, 0x8e06, 0x11e0, 0xaa8e, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type |
| 249 | { 0xc06c8400, 0x8e06, 0x11e0, 0x9cb6, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid |
| 250 | EFFECT_CONTROL_API_VERSION, |
| 251 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), |
| 252 | 0, //FIXME indicate CPU load |
| 253 | 0, //FIXME indicate memory usage |
| 254 | "Noise Suppression", |
| 255 | "The Android Open Source Project" |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | static const effect_descriptor_t *sDescriptors[PREPROC_NUM_EFFECTS] = { |
| 260 | &sAgcDescriptor, |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 261 | #ifndef WEBRTC_LEGACY |
| 262 | &sAgc2Descriptor, |
| 263 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 264 | &sAecDescriptor, |
| 265 | &sNsDescriptor |
| 266 | }; |
| 267 | |
| 268 | //------------------------------------------------------------------------------ |
| 269 | // Helper functions |
| 270 | //------------------------------------------------------------------------------ |
| 271 | |
| 272 | const effect_uuid_t * const sUuidToPreProcTable[PREPROC_NUM_EFFECTS] = { |
| 273 | FX_IID_AGC, |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 274 | #ifndef WEBRTC_LEGACY |
| 275 | FX_IID_AGC2, |
| 276 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 277 | FX_IID_AEC, |
| 278 | FX_IID_NS |
| 279 | }; |
| 280 | |
| 281 | |
| 282 | const effect_uuid_t * ProcIdToUuid(int procId) |
| 283 | { |
| 284 | if (procId >= PREPROC_NUM_EFFECTS) { |
| 285 | return EFFECT_UUID_NULL; |
| 286 | } |
| 287 | return sUuidToPreProcTable[procId]; |
| 288 | } |
| 289 | |
| 290 | uint32_t UuidToProcId(const effect_uuid_t * uuid) |
| 291 | { |
| 292 | size_t i; |
| 293 | for (i = 0; i < PREPROC_NUM_EFFECTS; i++) { |
| 294 | if (memcmp(uuid, sUuidToPreProcTable[i], sizeof(*uuid)) == 0) { |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | return i; |
| 299 | } |
| 300 | |
| 301 | bool HasReverseStream(uint32_t procId) |
| 302 | { |
| 303 | if (procId == PREPROC_AEC) { |
| 304 | return true; |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | //------------------------------------------------------------------------------ |
| 311 | // Automatic Gain Control (AGC) |
| 312 | //------------------------------------------------------------------------------ |
| 313 | |
Eric Laurent | 5387696 | 2012-01-31 12:35:20 -0800 | [diff] [blame] | 314 | static const int kAgcDefaultTargetLevel = 3; |
| 315 | static const int kAgcDefaultCompGain = 9; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 316 | static const bool kAgcDefaultLimiter = true; |
| 317 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 318 | #ifndef WEBRTC_LEGACY |
| 319 | int Agc2Init (preproc_effect_t *effect) |
| 320 | { |
| 321 | ALOGV("Agc2Init"); |
| 322 | effect->session->config = effect->session->apm->GetConfig(); |
| 323 | effect->session->config.gain_controller2.fixed_digital.gain_db = 0.f; |
| 324 | effect->session->config.gain_controller2.adaptive_digital.level_estimator = |
| 325 | effect->session->config.gain_controller2.kRms; |
| 326 | effect->session->config.gain_controller2.adaptive_digital.extra_saturation_margin_db = 2.f; |
| 327 | effect->session->apm->ApplyConfig(effect->session->config); |
| 328 | return 0; |
| 329 | } |
| 330 | #endif |
| 331 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 332 | int AgcInit (preproc_effect_t *effect) |
| 333 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 334 | ALOGV("AgcInit"); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 335 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 336 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); |
| 337 | agc->set_mode(webrtc::GainControl::kFixedDigital); |
| 338 | agc->set_target_level_dbfs(kAgcDefaultTargetLevel); |
| 339 | agc->set_compression_gain_db(kAgcDefaultCompGain); |
| 340 | agc->enable_limiter(kAgcDefaultLimiter); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 341 | #else |
| 342 | effect->session->config = effect->session->apm->GetConfig(); |
| 343 | effect->session->config.gain_controller1.target_level_dbfs = kAgcDefaultTargetLevel; |
| 344 | effect->session->config.gain_controller1.compression_gain_db = kAgcDefaultCompGain; |
| 345 | effect->session->config.gain_controller1.enable_limiter = kAgcDefaultLimiter; |
| 346 | effect->session->apm->ApplyConfig(effect->session->config); |
| 347 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 351 | #ifndef WEBRTC_LEGACY |
| 352 | int Agc2Create(preproc_effect_t *effect) |
| 353 | { |
| 354 | Agc2Init(effect); |
| 355 | return 0; |
| 356 | } |
| 357 | #endif |
| 358 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 359 | int AgcCreate(preproc_effect_t *effect) |
| 360 | { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 361 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 362 | webrtc::GainControl *agc = effect->session->apm->gain_control(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 363 | ALOGV("AgcCreate got agc %p", agc); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 364 | if (agc == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 365 | ALOGW("AgcCreate Error"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 366 | return -ENOMEM; |
| 367 | } |
| 368 | effect->engine = static_cast<preproc_fx_handle_t>(agc); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 369 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 370 | AgcInit(effect); |
| 371 | return 0; |
| 372 | } |
| 373 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 374 | #ifndef WEBRTC_LEGACY |
| 375 | int Agc2GetParameter(preproc_effect_t *effect, |
| 376 | void *pParam, |
| 377 | uint32_t *pValueSize, |
| 378 | void *pValue) |
| 379 | { |
| 380 | int status = 0; |
| 381 | uint32_t param = *(uint32_t *)pParam; |
| 382 | agc2_settings_t *pProperties = (agc2_settings_t *)pValue; |
| 383 | |
| 384 | switch (param) { |
| 385 | case AGC2_PARAM_FIXED_DIGITAL_GAIN: |
| 386 | if (*pValueSize < sizeof(float)) { |
| 387 | *pValueSize = 0.f; |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | break; |
| 391 | case AGC2_PARAM_ADAPT_DIGI_LEVEL_ESTIMATOR: |
| 392 | if (*pValueSize < sizeof(int32_t)) { |
| 393 | *pValueSize = 0; |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | break; |
| 397 | case AGC2_PARAM_ADAPT_DIGI_EXTRA_SATURATION_MARGIN: |
| 398 | if (*pValueSize < sizeof(float)) { |
| 399 | *pValueSize = 0.f; |
| 400 | return -EINVAL; |
| 401 | } |
| 402 | break; |
| 403 | case AGC2_PARAM_PROPERTIES: |
| 404 | if (*pValueSize < sizeof(agc2_settings_t)) { |
| 405 | *pValueSize = 0; |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | break; |
| 409 | |
| 410 | default: |
| 411 | ALOGW("Agc2GetParameter() unknown param %08x", param); |
| 412 | status = -EINVAL; |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | effect->session->config = effect->session->apm->GetConfig(); |
| 417 | switch (param) { |
| 418 | case AGC2_PARAM_FIXED_DIGITAL_GAIN: |
| 419 | *(float *) pValue = |
| 420 | (float)(effect->session->config.gain_controller2.fixed_digital.gain_db); |
| 421 | ALOGV("Agc2GetParameter() target level %f dB", *(float *) pValue); |
| 422 | break; |
| 423 | case AGC2_PARAM_ADAPT_DIGI_LEVEL_ESTIMATOR: |
| 424 | *(uint32_t *) pValue = |
| 425 | (uint32_t)(effect->session->config.gain_controller2.adaptive_digital. |
| 426 | level_estimator); |
| 427 | ALOGV("Agc2GetParameter() level estimator %d", |
| 428 | *(webrtc::AudioProcessing::Config::GainController2::LevelEstimator *) pValue); |
| 429 | break; |
| 430 | case AGC2_PARAM_ADAPT_DIGI_EXTRA_SATURATION_MARGIN: |
| 431 | *(float *) pValue = |
| 432 | (float)(effect->session->config.gain_controller2.adaptive_digital. |
| 433 | extra_saturation_margin_db); |
| 434 | ALOGV("Agc2GetParameter() extra saturation margin %f dB", *(float *) pValue); |
| 435 | break; |
| 436 | case AGC2_PARAM_PROPERTIES: |
| 437 | pProperties->fixedDigitalGain = |
| 438 | (float)(effect->session->config.gain_controller2.fixed_digital.gain_db); |
| 439 | pProperties->level_estimator = |
| 440 | (uint32_t)(effect->session->config.gain_controller2.adaptive_digital. |
| 441 | level_estimator); |
| 442 | pProperties->extraSaturationMargin = |
| 443 | (float)(effect->session->config.gain_controller2.adaptive_digital. |
| 444 | extra_saturation_margin_db); |
| 445 | break; |
| 446 | default: |
| 447 | ALOGW("Agc2GetParameter() unknown param %d", param); |
| 448 | status = -EINVAL; |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | return status; |
| 453 | } |
| 454 | #endif |
| 455 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 456 | int AgcGetParameter(preproc_effect_t *effect, |
| 457 | void *pParam, |
Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 458 | uint32_t *pValueSize, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 459 | void *pValue) |
| 460 | { |
| 461 | int status = 0; |
| 462 | uint32_t param = *(uint32_t *)pParam; |
| 463 | t_agc_settings *pProperties = (t_agc_settings *)pValue; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 464 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 465 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 466 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 467 | |
| 468 | switch (param) { |
| 469 | case AGC_PARAM_TARGET_LEVEL: |
| 470 | case AGC_PARAM_COMP_GAIN: |
| 471 | if (*pValueSize < sizeof(int16_t)) { |
| 472 | *pValueSize = 0; |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | break; |
| 476 | case AGC_PARAM_LIMITER_ENA: |
| 477 | if (*pValueSize < sizeof(bool)) { |
| 478 | *pValueSize = 0; |
| 479 | return -EINVAL; |
| 480 | } |
| 481 | break; |
| 482 | case AGC_PARAM_PROPERTIES: |
| 483 | if (*pValueSize < sizeof(t_agc_settings)) { |
| 484 | *pValueSize = 0; |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | break; |
| 488 | |
| 489 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 490 | ALOGW("AgcGetParameter() unknown param %08x", param); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 491 | status = -EINVAL; |
| 492 | break; |
| 493 | } |
| 494 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 495 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 496 | switch (param) { |
| 497 | case AGC_PARAM_TARGET_LEVEL: |
| 498 | *(int16_t *) pValue = (int16_t)(agc->target_level_dbfs() * -100); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 499 | ALOGV("AgcGetParameter() target level %d milliBels", *(int16_t *) pValue); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 500 | break; |
| 501 | case AGC_PARAM_COMP_GAIN: |
| 502 | *(int16_t *) pValue = (int16_t)(agc->compression_gain_db() * 100); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 503 | ALOGV("AgcGetParameter() comp gain %d milliBels", *(int16_t *) pValue); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 504 | break; |
| 505 | case AGC_PARAM_LIMITER_ENA: |
| 506 | *(bool *) pValue = (bool)agc->is_limiter_enabled(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 507 | ALOGV("AgcGetParameter() limiter enabled %s", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 508 | (*(int16_t *) pValue != 0) ? "true" : "false"); |
| 509 | break; |
| 510 | case AGC_PARAM_PROPERTIES: |
| 511 | pProperties->targetLevel = (int16_t)(agc->target_level_dbfs() * -100); |
| 512 | pProperties->compGain = (int16_t)(agc->compression_gain_db() * 100); |
| 513 | pProperties->limiterEnabled = (bool)agc->is_limiter_enabled(); |
| 514 | break; |
| 515 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 516 | ALOGW("AgcGetParameter() unknown param %d", param); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 517 | status = -EINVAL; |
| 518 | break; |
| 519 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 520 | #else |
| 521 | effect->session->config = effect->session->apm->GetConfig(); |
| 522 | switch (param) { |
| 523 | case AGC_PARAM_TARGET_LEVEL: |
| 524 | *(int16_t *) pValue = |
| 525 | (int16_t)(effect->session->config.gain_controller1.target_level_dbfs * -100); |
| 526 | ALOGV("AgcGetParameter() target level %d milliBels", *(int16_t *) pValue); |
| 527 | break; |
| 528 | case AGC_PARAM_COMP_GAIN: |
| 529 | *(int16_t *) pValue = |
| 530 | (int16_t)(effect->session->config.gain_controller1.compression_gain_db * -100); |
| 531 | ALOGV("AgcGetParameter() comp gain %d milliBels", *(int16_t *) pValue); |
| 532 | break; |
| 533 | case AGC_PARAM_LIMITER_ENA: |
| 534 | *(bool *) pValue = |
| 535 | (bool)(effect->session->config.gain_controller1.enable_limiter); |
| 536 | ALOGV("AgcGetParameter() limiter enabled %s", |
| 537 | (*(int16_t *) pValue != 0) ? "true" : "false"); |
| 538 | break; |
| 539 | case AGC_PARAM_PROPERTIES: |
| 540 | pProperties->targetLevel = |
| 541 | (int16_t)(effect->session->config.gain_controller1.target_level_dbfs * -100); |
| 542 | pProperties->compGain = |
| 543 | (int16_t)(effect->session->config.gain_controller1.compression_gain_db * -100); |
| 544 | pProperties->limiterEnabled = |
| 545 | (bool)(effect->session->config.gain_controller1.enable_limiter); |
| 546 | break; |
| 547 | default: |
| 548 | ALOGW("AgcGetParameter() unknown param %d", param); |
| 549 | status = -EINVAL; |
| 550 | break; |
| 551 | } |
| 552 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 553 | return status; |
| 554 | } |
| 555 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 556 | #ifndef WEBRTC_LEGACY |
| 557 | int Agc2SetParameter (preproc_effect_t *effect, void *pParam, void *pValue) |
| 558 | { |
| 559 | int status = 0; |
| 560 | uint32_t param = *(uint32_t *)pParam; |
| 561 | float valueFloat = 0.f; |
| 562 | agc2_settings_t *pProperties = (agc2_settings_t *)pValue; |
| 563 | effect->session->config = effect->session->apm->GetConfig(); |
| 564 | switch (param) { |
| 565 | case AGC2_PARAM_FIXED_DIGITAL_GAIN: |
| 566 | valueFloat = (float)(*(int32_t *) pValue); |
| 567 | ALOGV("Agc2SetParameter() fixed digital gain %f dB", valueFloat); |
| 568 | effect->session->config.gain_controller2.fixed_digital.gain_db = valueFloat; |
| 569 | break; |
| 570 | case AGC2_PARAM_ADAPT_DIGI_LEVEL_ESTIMATOR: |
| 571 | ALOGV("Agc2SetParameter() level estimator %d", *(webrtc::AudioProcessing::Config:: |
| 572 | GainController2::LevelEstimator *) pValue); |
| 573 | effect->session->config.gain_controller2.adaptive_digital.level_estimator = |
| 574 | (*(webrtc::AudioProcessing::Config::GainController2::LevelEstimator *) pValue); |
| 575 | break; |
| 576 | case AGC2_PARAM_ADAPT_DIGI_EXTRA_SATURATION_MARGIN: |
| 577 | valueFloat = (float)(*(int32_t *) pValue); |
| 578 | ALOGV("Agc2SetParameter() extra saturation margin %f dB", valueFloat); |
| 579 | effect->session->config.gain_controller2.adaptive_digital.extra_saturation_margin_db = |
| 580 | valueFloat; |
| 581 | break; |
| 582 | case AGC2_PARAM_PROPERTIES: |
| 583 | ALOGV("Agc2SetParameter() properties gain %f, level %d margin %f", |
| 584 | pProperties->fixedDigitalGain, |
| 585 | pProperties->level_estimator, |
| 586 | pProperties->extraSaturationMargin); |
| 587 | effect->session->config.gain_controller2.fixed_digital.gain_db = |
| 588 | pProperties->fixedDigitalGain; |
| 589 | effect->session->config.gain_controller2.adaptive_digital.level_estimator = |
| 590 | (webrtc::AudioProcessing::Config::GainController2::LevelEstimator)pProperties-> |
| 591 | level_estimator; |
| 592 | effect->session->config.gain_controller2.adaptive_digital.extra_saturation_margin_db = |
| 593 | pProperties->extraSaturationMargin; |
| 594 | break; |
| 595 | default: |
| 596 | ALOGW("Agc2SetParameter() unknown param %08x value %08x", param, *(uint32_t *)pValue); |
| 597 | status = -EINVAL; |
| 598 | break; |
| 599 | } |
| 600 | effect->session->apm->ApplyConfig(effect->session->config); |
| 601 | |
| 602 | ALOGV("Agc2SetParameter() done status %d", status); |
| 603 | |
| 604 | return status; |
| 605 | } |
| 606 | #endif |
| 607 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 608 | int AgcSetParameter (preproc_effect_t *effect, void *pParam, void *pValue) |
| 609 | { |
| 610 | int status = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 611 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 612 | uint32_t param = *(uint32_t *)pParam; |
| 613 | t_agc_settings *pProperties = (t_agc_settings *)pValue; |
| 614 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); |
| 615 | |
| 616 | switch (param) { |
| 617 | case AGC_PARAM_TARGET_LEVEL: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 618 | ALOGV("AgcSetParameter() target level %d milliBels", *(int16_t *)pValue); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 619 | status = agc->set_target_level_dbfs(-(*(int16_t *)pValue / 100)); |
| 620 | break; |
| 621 | case AGC_PARAM_COMP_GAIN: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 622 | ALOGV("AgcSetParameter() comp gain %d milliBels", *(int16_t *)pValue); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 623 | status = agc->set_compression_gain_db(*(int16_t *)pValue / 100); |
| 624 | break; |
| 625 | case AGC_PARAM_LIMITER_ENA: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 626 | ALOGV("AgcSetParameter() limiter enabled %s", *(bool *)pValue ? "true" : "false"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 627 | status = agc->enable_limiter(*(bool *)pValue); |
| 628 | break; |
| 629 | case AGC_PARAM_PROPERTIES: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 630 | ALOGV("AgcSetParameter() properties level %d, gain %d limiter %d", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 631 | pProperties->targetLevel, |
| 632 | pProperties->compGain, |
| 633 | pProperties->limiterEnabled); |
| 634 | status = agc->set_target_level_dbfs(-(pProperties->targetLevel / 100)); |
| 635 | if (status != 0) break; |
| 636 | status = agc->set_compression_gain_db(pProperties->compGain / 100); |
| 637 | if (status != 0) break; |
| 638 | status = agc->enable_limiter(pProperties->limiterEnabled); |
| 639 | break; |
| 640 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 641 | ALOGW("AgcSetParameter() unknown param %08x value %08x", param, *(uint32_t *)pValue); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 642 | status = -EINVAL; |
| 643 | break; |
| 644 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 645 | #else |
| 646 | uint32_t param = *(uint32_t *)pParam; |
| 647 | t_agc_settings *pProperties = (t_agc_settings *)pValue; |
| 648 | effect->session->config = effect->session->apm->GetConfig(); |
| 649 | switch (param) { |
| 650 | case AGC_PARAM_TARGET_LEVEL: |
| 651 | ALOGV("AgcSetParameter() target level %d milliBels", *(int16_t *)pValue); |
| 652 | effect->session->config.gain_controller1.target_level_dbfs = |
| 653 | (-(*(int16_t *)pValue / 100)); |
| 654 | break; |
| 655 | case AGC_PARAM_COMP_GAIN: |
| 656 | ALOGV("AgcSetParameter() comp gain %d milliBels", *(int16_t *)pValue); |
| 657 | effect->session->config.gain_controller1.compression_gain_db = |
| 658 | (*(int16_t *)pValue / 100); |
| 659 | break; |
| 660 | case AGC_PARAM_LIMITER_ENA: |
| 661 | ALOGV("AgcSetParameter() limiter enabled %s", *(bool *)pValue ? "true" : "false"); |
| 662 | effect->session->config.gain_controller1.enable_limiter = |
| 663 | (*(bool *)pValue); |
| 664 | break; |
| 665 | case AGC_PARAM_PROPERTIES: |
| 666 | ALOGV("AgcSetParameter() properties level %d, gain %d limiter %d", |
| 667 | pProperties->targetLevel, |
| 668 | pProperties->compGain, |
| 669 | pProperties->limiterEnabled); |
| 670 | effect->session->config.gain_controller1.target_level_dbfs = |
| 671 | -(pProperties->targetLevel / 100); |
| 672 | effect->session->config.gain_controller1.compression_gain_db = |
| 673 | pProperties->compGain / 100; |
| 674 | effect->session->config.gain_controller1.enable_limiter = |
| 675 | pProperties->limiterEnabled; |
| 676 | break; |
| 677 | default: |
| 678 | ALOGW("AgcSetParameter() unknown param %08x value %08x", param, *(uint32_t *)pValue); |
| 679 | status = -EINVAL; |
| 680 | break; |
| 681 | } |
| 682 | effect->session->apm->ApplyConfig(effect->session->config); |
| 683 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 684 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 685 | ALOGV("AgcSetParameter() done status %d", status); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 686 | |
| 687 | return status; |
| 688 | } |
| 689 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 690 | #ifndef WEBRTC_LEGACY |
| 691 | void Agc2Enable(preproc_effect_t *effect) |
| 692 | { |
| 693 | effect->session->config = effect->session->apm->GetConfig(); |
| 694 | effect->session->config.gain_controller2.enabled = true; |
| 695 | effect->session->apm->ApplyConfig(effect->session->config); |
| 696 | } |
| 697 | #endif |
| 698 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 699 | void AgcEnable(preproc_effect_t *effect) |
| 700 | { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 701 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 702 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 703 | ALOGV("AgcEnable agc %p", agc); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 704 | agc->Enable(true); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 705 | #else |
| 706 | effect->session->config = effect->session->apm->GetConfig(); |
| 707 | effect->session->config.gain_controller1.enabled = true; |
| 708 | effect->session->apm->ApplyConfig(effect->session->config); |
| 709 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 712 | #ifndef WEBRTC_LEGACY |
| 713 | void Agc2Disable(preproc_effect_t *effect) |
| 714 | { |
| 715 | effect->session->config = effect->session->apm->GetConfig(); |
| 716 | effect->session->config.gain_controller2.enabled = false; |
| 717 | effect->session->apm->ApplyConfig(effect->session->config); |
| 718 | } |
| 719 | #endif |
| 720 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 721 | void AgcDisable(preproc_effect_t *effect) |
| 722 | { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 723 | #ifdef WEBRTC_LEGACY |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 724 | ALOGV("AgcDisable"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 725 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); |
| 726 | agc->Enable(false); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 727 | #else |
| 728 | effect->session->config = effect->session->apm->GetConfig(); |
| 729 | effect->session->config.gain_controller1.enabled = false; |
| 730 | effect->session->apm->ApplyConfig(effect->session->config); |
| 731 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 734 | static const preproc_ops_t sAgcOps = { |
| 735 | AgcCreate, |
| 736 | AgcInit, |
| 737 | NULL, |
| 738 | AgcEnable, |
| 739 | AgcDisable, |
| 740 | AgcSetParameter, |
| 741 | AgcGetParameter, |
| 742 | NULL |
| 743 | }; |
| 744 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 745 | #ifndef WEBRTC_LEGACY |
| 746 | static const preproc_ops_t sAgc2Ops = { |
| 747 | Agc2Create, |
| 748 | Agc2Init, |
| 749 | NULL, |
| 750 | Agc2Enable, |
| 751 | Agc2Disable, |
| 752 | Agc2SetParameter, |
| 753 | Agc2GetParameter, |
| 754 | NULL |
| 755 | }; |
| 756 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 757 | |
| 758 | //------------------------------------------------------------------------------ |
| 759 | // Acoustic Echo Canceler (AEC) |
| 760 | //------------------------------------------------------------------------------ |
| 761 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 762 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 763 | static const webrtc::EchoControlMobile::RoutingMode kAecDefaultMode = |
| 764 | webrtc::EchoControlMobile::kEarpiece; |
| 765 | static const bool kAecDefaultComfortNoise = true; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 766 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 767 | |
| 768 | int AecInit (preproc_effect_t *effect) |
| 769 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 770 | ALOGV("AecInit"); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 771 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 772 | webrtc::EchoControlMobile *aec = static_cast<webrtc::EchoControlMobile *>(effect->engine); |
| 773 | aec->set_routing_mode(kAecDefaultMode); |
| 774 | aec->enable_comfort_noise(kAecDefaultComfortNoise); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 775 | #else |
| 776 | effect->session->config = |
| 777 | effect->session->apm->GetConfig() ; |
| 778 | effect->session->config.echo_canceller.mobile_mode = false; |
| 779 | effect->session->apm->ApplyConfig(effect->session->config); |
| 780 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | int AecCreate(preproc_effect_t *effect) |
| 785 | { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 786 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 787 | webrtc::EchoControlMobile *aec = effect->session->apm->echo_control_mobile(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 788 | ALOGV("AecCreate got aec %p", aec); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 789 | if (aec == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 790 | ALOGW("AgcCreate Error"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 791 | return -ENOMEM; |
| 792 | } |
| 793 | effect->engine = static_cast<preproc_fx_handle_t>(aec); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 794 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 795 | AecInit (effect); |
| 796 | return 0; |
| 797 | } |
| 798 | |
Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 799 | int AecGetParameter(preproc_effect_t *effect, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 800 | void *pParam, |
Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 801 | uint32_t *pValueSize, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 802 | void *pValue) |
| 803 | { |
| 804 | int status = 0; |
| 805 | uint32_t param = *(uint32_t *)pParam; |
| 806 | |
| 807 | if (*pValueSize < sizeof(uint32_t)) { |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | switch (param) { |
| 811 | case AEC_PARAM_ECHO_DELAY: |
| 812 | case AEC_PARAM_PROPERTIES: |
| 813 | *(uint32_t *)pValue = 1000 * effect->session->apm->stream_delay_ms(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 814 | ALOGV("AecGetParameter() echo delay %d us", *(uint32_t *)pValue); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 815 | break; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 816 | #ifndef WEBRTC_LEGACY |
| 817 | case AEC_PARAM_MOBILE_MODE: |
| 818 | effect->session->config = |
| 819 | effect->session->apm->GetConfig() ; |
| 820 | *(uint32_t *)pValue = effect->session->config.echo_canceller.mobile_mode; |
| 821 | ALOGV("AecGetParameter() mobile mode %d us", *(uint32_t *)pValue); |
| 822 | break; |
| 823 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 824 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 825 | ALOGW("AecGetParameter() unknown param %08x value %08x", param, *(uint32_t *)pValue); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 826 | status = -EINVAL; |
| 827 | break; |
| 828 | } |
| 829 | return status; |
| 830 | } |
| 831 | |
| 832 | int AecSetParameter (preproc_effect_t *effect, void *pParam, void *pValue) |
| 833 | { |
| 834 | int status = 0; |
| 835 | uint32_t param = *(uint32_t *)pParam; |
| 836 | uint32_t value = *(uint32_t *)pValue; |
| 837 | |
| 838 | switch (param) { |
| 839 | case AEC_PARAM_ECHO_DELAY: |
| 840 | case AEC_PARAM_PROPERTIES: |
| 841 | status = effect->session->apm->set_stream_delay_ms(value/1000); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 842 | ALOGV("AecSetParameter() echo delay %d us, status %d", value, status); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 843 | break; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 844 | #ifndef WEBRTC_LEGACY |
| 845 | case AEC_PARAM_MOBILE_MODE: |
| 846 | effect->session->config = |
| 847 | effect->session->apm->GetConfig() ; |
| 848 | effect->session->config.echo_canceller.mobile_mode = value; |
| 849 | ALOGV("AecSetParameter() mobile mode %d us", value); |
| 850 | effect->session->apm->ApplyConfig(effect->session->config); |
| 851 | break; |
| 852 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 853 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 854 | ALOGW("AecSetParameter() unknown param %08x value %08x", param, *(uint32_t *)pValue); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 855 | status = -EINVAL; |
| 856 | break; |
| 857 | } |
| 858 | return status; |
| 859 | } |
| 860 | |
| 861 | void AecEnable(preproc_effect_t *effect) |
| 862 | { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 863 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 864 | webrtc::EchoControlMobile *aec = static_cast<webrtc::EchoControlMobile *>(effect->engine); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 865 | ALOGV("AecEnable aec %p", aec); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 866 | aec->Enable(true); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 867 | #else |
| 868 | effect->session->config = effect->session->apm->GetConfig(); |
| 869 | effect->session->config.echo_canceller.enabled = true; |
| 870 | effect->session->apm->ApplyConfig(effect->session->config); |
| 871 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | void AecDisable(preproc_effect_t *effect) |
| 875 | { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 876 | #ifdef WEBRTC_LEGACY |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 877 | ALOGV("AecDisable"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 878 | webrtc::EchoControlMobile *aec = static_cast<webrtc::EchoControlMobile *>(effect->engine); |
| 879 | aec->Enable(false); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 880 | #else |
| 881 | effect->session->config = effect->session->apm->GetConfig(); |
| 882 | effect->session->config.echo_canceller.enabled = false; |
| 883 | effect->session->apm->ApplyConfig(effect->session->config); |
| 884 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | int AecSetDevice(preproc_effect_t *effect, uint32_t device) |
| 888 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 889 | ALOGV("AecSetDevice %08x", device); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 890 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 891 | webrtc::EchoControlMobile *aec = static_cast<webrtc::EchoControlMobile *>(effect->engine); |
| 892 | webrtc::EchoControlMobile::RoutingMode mode = webrtc::EchoControlMobile::kQuietEarpieceOrHeadset; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 893 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 894 | |
Eric Laurent | 8895925 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 895 | if (audio_is_input_device(device)) { |
| 896 | return 0; |
| 897 | } |
| 898 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 899 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 900 | switch(device) { |
| 901 | case AUDIO_DEVICE_OUT_EARPIECE: |
| 902 | mode = webrtc::EchoControlMobile::kEarpiece; |
| 903 | break; |
| 904 | case AUDIO_DEVICE_OUT_SPEAKER: |
| 905 | mode = webrtc::EchoControlMobile::kSpeakerphone; |
| 906 | break; |
| 907 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 908 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 909 | case AUDIO_DEVICE_OUT_USB_HEADSET: |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 910 | default: |
| 911 | break; |
| 912 | } |
| 913 | aec->set_routing_mode(mode); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 914 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static const preproc_ops_t sAecOps = { |
| 919 | AecCreate, |
| 920 | AecInit, |
| 921 | NULL, |
| 922 | AecEnable, |
| 923 | AecDisable, |
| 924 | AecSetParameter, |
| 925 | AecGetParameter, |
| 926 | AecSetDevice |
| 927 | }; |
| 928 | |
| 929 | //------------------------------------------------------------------------------ |
| 930 | // Noise Suppression (NS) |
| 931 | //------------------------------------------------------------------------------ |
| 932 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 933 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 934 | static const webrtc::NoiseSuppression::Level kNsDefaultLevel = webrtc::NoiseSuppression::kModerate; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 935 | #else |
| 936 | static const webrtc::AudioProcessing::Config::NoiseSuppression::Level kNsDefaultLevel = |
| 937 | webrtc::AudioProcessing::Config::NoiseSuppression::kModerate; |
| 938 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 939 | |
| 940 | int NsInit (preproc_effect_t *effect) |
| 941 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 942 | ALOGV("NsInit"); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 943 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 944 | webrtc::NoiseSuppression *ns = static_cast<webrtc::NoiseSuppression *>(effect->engine); |
| 945 | ns->set_level(kNsDefaultLevel); |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 946 | webrtc::Config config; |
| 947 | std::vector<webrtc::Point> geometry; |
| 948 | // TODO(aluebs): Make the geometry settable. |
| 949 | geometry.push_back(webrtc::Point(-0.03f, 0.f, 0.f)); |
| 950 | geometry.push_back(webrtc::Point(-0.01f, 0.f, 0.f)); |
| 951 | geometry.push_back(webrtc::Point(0.01f, 0.f, 0.f)); |
| 952 | geometry.push_back(webrtc::Point(0.03f, 0.f, 0.f)); |
| 953 | // The geometry needs to be set with Beamforming enabled. |
| 954 | config.Set<webrtc::Beamforming>( |
| 955 | new webrtc::Beamforming(true, geometry)); |
| 956 | effect->session->apm->SetExtraOptions(config); |
| 957 | config.Set<webrtc::Beamforming>( |
| 958 | new webrtc::Beamforming(false, geometry)); |
| 959 | effect->session->apm->SetExtraOptions(config); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 960 | #else |
| 961 | effect->session->config = |
| 962 | effect->session->apm->GetConfig() ; |
| 963 | effect->session->config.noise_suppression.level = |
| 964 | kNsDefaultLevel; |
| 965 | effect->session->apm->ApplyConfig(effect->session->config); |
| 966 | #endif |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 967 | effect->type = NS_TYPE_SINGLE_CHANNEL; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | int NsCreate(preproc_effect_t *effect) |
| 972 | { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 973 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 974 | webrtc::NoiseSuppression *ns = effect->session->apm->noise_suppression(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 975 | ALOGV("NsCreate got ns %p", ns); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 976 | if (ns == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 977 | ALOGW("AgcCreate Error"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 978 | return -ENOMEM; |
| 979 | } |
| 980 | effect->engine = static_cast<preproc_fx_handle_t>(ns); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 981 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 982 | NsInit (effect); |
| 983 | return 0; |
| 984 | } |
| 985 | |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 986 | int NsGetParameter(preproc_effect_t *effect __unused, |
| 987 | void *pParam __unused, |
| 988 | uint32_t *pValueSize __unused, |
| 989 | void *pValue __unused) |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 990 | { |
| 991 | int status = 0; |
| 992 | return status; |
| 993 | } |
| 994 | |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 995 | int NsSetParameter (preproc_effect_t *effect, void *pParam, void *pValue) |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 996 | { |
| 997 | int status = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 998 | #ifdef WEBRTC_LEGACY |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 999 | webrtc::NoiseSuppression *ns = static_cast<webrtc::NoiseSuppression *>(effect->engine); |
| 1000 | uint32_t param = *(uint32_t *)pParam; |
| 1001 | uint32_t value = *(uint32_t *)pValue; |
| 1002 | switch(param) { |
| 1003 | case NS_PARAM_LEVEL: |
| 1004 | ns->set_level((webrtc::NoiseSuppression::Level)value); |
| 1005 | ALOGV("NsSetParameter() level %d", value); |
| 1006 | break; |
| 1007 | case NS_PARAM_TYPE: |
| 1008 | { |
| 1009 | webrtc::Config config; |
| 1010 | std::vector<webrtc::Point> geometry; |
| 1011 | bool is_beamforming_enabled = |
| 1012 | value == NS_TYPE_MULTI_CHANNEL && ns->is_enabled(); |
| 1013 | config.Set<webrtc::Beamforming>( |
| 1014 | new webrtc::Beamforming(is_beamforming_enabled, geometry)); |
| 1015 | effect->session->apm->SetExtraOptions(config); |
| 1016 | effect->type = value; |
| 1017 | ALOGV("NsSetParameter() type %d", value); |
| 1018 | break; |
| 1019 | } |
| 1020 | default: |
| 1021 | ALOGW("NsSetParameter() unknown param %08x value %08x", param, value); |
| 1022 | status = -EINVAL; |
| 1023 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1024 | #else |
| 1025 | uint32_t param = *(uint32_t *)pParam; |
| 1026 | uint32_t value = *(uint32_t *)pValue; |
| 1027 | effect->session->config = |
| 1028 | effect->session->apm->GetConfig(); |
| 1029 | switch (param) { |
| 1030 | case NS_PARAM_LEVEL: |
| 1031 | effect->session->config.noise_suppression.level = |
| 1032 | (webrtc::AudioProcessing::Config::NoiseSuppression::Level)value; |
| 1033 | ALOGV("NsSetParameter() level %d", value); |
| 1034 | break; |
| 1035 | default: |
| 1036 | ALOGW("NsSetParameter() unknown param %08x value %08x", param, value); |
| 1037 | status = -EINVAL; |
| 1038 | } |
| 1039 | effect->session->apm->ApplyConfig(effect->session->config); |
| 1040 | #endif |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 1041 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1042 | return status; |
| 1043 | } |
| 1044 | |
| 1045 | void NsEnable(preproc_effect_t *effect) |
| 1046 | { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1047 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1048 | webrtc::NoiseSuppression *ns = static_cast<webrtc::NoiseSuppression *>(effect->engine); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1049 | ALOGV("NsEnable ns %p", ns); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1050 | ns->Enable(true); |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 1051 | if (effect->type == NS_TYPE_MULTI_CHANNEL) { |
| 1052 | webrtc::Config config; |
| 1053 | std::vector<webrtc::Point> geometry; |
| 1054 | config.Set<webrtc::Beamforming>(new webrtc::Beamforming(true, geometry)); |
| 1055 | effect->session->apm->SetExtraOptions(config); |
| 1056 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1057 | #else |
| 1058 | effect->session->config = |
| 1059 | effect->session->apm->GetConfig(); |
| 1060 | effect->session->config.noise_suppression.enabled = true; |
| 1061 | effect->session->apm->ApplyConfig(effect->session->config); |
| 1062 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | void NsDisable(preproc_effect_t *effect) |
| 1066 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1067 | ALOGV("NsDisable"); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1068 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1069 | webrtc::NoiseSuppression *ns = static_cast<webrtc::NoiseSuppression *>(effect->engine); |
| 1070 | ns->Enable(false); |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 1071 | webrtc::Config config; |
| 1072 | std::vector<webrtc::Point> geometry; |
| 1073 | config.Set<webrtc::Beamforming>(new webrtc::Beamforming(false, geometry)); |
| 1074 | effect->session->apm->SetExtraOptions(config); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1075 | #else |
| 1076 | effect->session->config = |
| 1077 | effect->session->apm->GetConfig(); |
| 1078 | effect->session->config.noise_suppression.enabled = false; |
| 1079 | effect->session->apm->ApplyConfig(effect->session->config); |
| 1080 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static const preproc_ops_t sNsOps = { |
| 1084 | NsCreate, |
| 1085 | NsInit, |
| 1086 | NULL, |
| 1087 | NsEnable, |
| 1088 | NsDisable, |
| 1089 | NsSetParameter, |
| 1090 | NsGetParameter, |
| 1091 | NULL |
| 1092 | }; |
| 1093 | |
| 1094 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1095 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1096 | static const preproc_ops_t *sPreProcOps[PREPROC_NUM_EFFECTS] = { |
| 1097 | &sAgcOps, |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1098 | #ifndef WEBRTC_LEGACY |
| 1099 | &sAgc2Ops, |
| 1100 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1101 | &sAecOps, |
| 1102 | &sNsOps |
| 1103 | }; |
| 1104 | |
| 1105 | |
| 1106 | //------------------------------------------------------------------------------ |
| 1107 | // Effect functions |
| 1108 | //------------------------------------------------------------------------------ |
| 1109 | |
| 1110 | void Session_SetProcEnabled(preproc_session_t *session, uint32_t procId, bool enabled); |
| 1111 | |
| 1112 | extern "C" const struct effect_interface_s sEffectInterface; |
| 1113 | extern "C" const struct effect_interface_s sEffectInterfaceReverse; |
| 1114 | |
| 1115 | #define BAD_STATE_ABORT(from, to) \ |
| 1116 | LOG_ALWAYS_FATAL("Bad state transition from %d to %d", from, to); |
| 1117 | |
| 1118 | int Effect_SetState(preproc_effect_t *effect, uint32_t state) |
| 1119 | { |
| 1120 | int status = 0; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1121 | ALOGV("Effect_SetState proc %d, new %d old %d", effect->procId, state, effect->state); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1122 | switch(state) { |
| 1123 | case PREPROC_EFFECT_STATE_INIT: |
| 1124 | switch(effect->state) { |
| 1125 | case PREPROC_EFFECT_STATE_ACTIVE: |
| 1126 | effect->ops->disable(effect); |
| 1127 | Session_SetProcEnabled(effect->session, effect->procId, false); |
Andy Hung | 320fd85 | 2018-10-09 14:06:37 -0700 | [diff] [blame] | 1128 | break; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1129 | case PREPROC_EFFECT_STATE_CONFIG: |
| 1130 | case PREPROC_EFFECT_STATE_CREATED: |
| 1131 | case PREPROC_EFFECT_STATE_INIT: |
| 1132 | break; |
| 1133 | default: |
| 1134 | BAD_STATE_ABORT(effect->state, state); |
| 1135 | } |
| 1136 | break; |
| 1137 | case PREPROC_EFFECT_STATE_CREATED: |
| 1138 | switch(effect->state) { |
| 1139 | case PREPROC_EFFECT_STATE_INIT: |
| 1140 | status = effect->ops->create(effect); |
| 1141 | break; |
| 1142 | case PREPROC_EFFECT_STATE_CREATED: |
| 1143 | case PREPROC_EFFECT_STATE_ACTIVE: |
| 1144 | case PREPROC_EFFECT_STATE_CONFIG: |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1145 | ALOGE("Effect_SetState invalid transition"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1146 | status = -ENOSYS; |
| 1147 | break; |
| 1148 | default: |
| 1149 | BAD_STATE_ABORT(effect->state, state); |
| 1150 | } |
| 1151 | break; |
| 1152 | case PREPROC_EFFECT_STATE_CONFIG: |
| 1153 | switch(effect->state) { |
| 1154 | case PREPROC_EFFECT_STATE_INIT: |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1155 | ALOGE("Effect_SetState invalid transition"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1156 | status = -ENOSYS; |
| 1157 | break; |
| 1158 | case PREPROC_EFFECT_STATE_ACTIVE: |
| 1159 | effect->ops->disable(effect); |
| 1160 | Session_SetProcEnabled(effect->session, effect->procId, false); |
| 1161 | break; |
| 1162 | case PREPROC_EFFECT_STATE_CREATED: |
| 1163 | case PREPROC_EFFECT_STATE_CONFIG: |
| 1164 | break; |
| 1165 | default: |
| 1166 | BAD_STATE_ABORT(effect->state, state); |
| 1167 | } |
| 1168 | break; |
| 1169 | case PREPROC_EFFECT_STATE_ACTIVE: |
| 1170 | switch(effect->state) { |
| 1171 | case PREPROC_EFFECT_STATE_INIT: |
| 1172 | case PREPROC_EFFECT_STATE_CREATED: |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1173 | ALOGE("Effect_SetState invalid transition"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1174 | status = -ENOSYS; |
| 1175 | break; |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1176 | case PREPROC_EFFECT_STATE_ACTIVE: |
| 1177 | // enabling an already enabled effect is just ignored |
| 1178 | break; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1179 | case PREPROC_EFFECT_STATE_CONFIG: |
| 1180 | effect->ops->enable(effect); |
| 1181 | Session_SetProcEnabled(effect->session, effect->procId, true); |
| 1182 | break; |
| 1183 | default: |
| 1184 | BAD_STATE_ABORT(effect->state, state); |
| 1185 | } |
| 1186 | break; |
| 1187 | default: |
| 1188 | BAD_STATE_ABORT(effect->state, state); |
| 1189 | } |
| 1190 | if (status == 0) { |
| 1191 | effect->state = state; |
| 1192 | } |
| 1193 | return status; |
| 1194 | } |
| 1195 | |
| 1196 | int Effect_Init(preproc_effect_t *effect, uint32_t procId) |
| 1197 | { |
| 1198 | if (HasReverseStream(procId)) { |
| 1199 | effect->itfe = &sEffectInterfaceReverse; |
| 1200 | } else { |
| 1201 | effect->itfe = &sEffectInterface; |
| 1202 | } |
| 1203 | effect->ops = sPreProcOps[procId]; |
| 1204 | effect->procId = procId; |
| 1205 | effect->state = PREPROC_EFFECT_STATE_INIT; |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | int Effect_Create(preproc_effect_t *effect, |
| 1210 | preproc_session_t *session, |
| 1211 | effect_handle_t *interface) |
| 1212 | { |
| 1213 | effect->session = session; |
| 1214 | *interface = (effect_handle_t)&effect->itfe; |
| 1215 | return Effect_SetState(effect, PREPROC_EFFECT_STATE_CREATED); |
| 1216 | } |
| 1217 | |
| 1218 | int Effect_Release(preproc_effect_t *effect) |
| 1219 | { |
| 1220 | return Effect_SetState(effect, PREPROC_EFFECT_STATE_INIT); |
| 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | //------------------------------------------------------------------------------ |
| 1225 | // Session functions |
| 1226 | //------------------------------------------------------------------------------ |
| 1227 | |
| 1228 | #define RESAMPLER_QUALITY SPEEX_RESAMPLER_QUALITY_VOIP |
| 1229 | |
| 1230 | static const int kPreprocDefaultSr = 16000; |
| 1231 | static const int kPreProcDefaultCnl = 1; |
| 1232 | |
| 1233 | int Session_Init(preproc_session_t *session) |
| 1234 | { |
| 1235 | size_t i; |
| 1236 | int status = 0; |
| 1237 | |
| 1238 | session->state = PREPROC_SESSION_STATE_INIT; |
| 1239 | session->id = 0; |
| 1240 | session->io = 0; |
| 1241 | session->createdMsk = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1242 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1243 | session->apm = NULL; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1244 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1245 | for (i = 0; i < PREPROC_NUM_EFFECTS && status == 0; i++) { |
| 1246 | status = Effect_Init(&session->effects[i], i); |
| 1247 | } |
| 1248 | return status; |
| 1249 | } |
| 1250 | |
| 1251 | |
| 1252 | extern "C" int Session_CreateEffect(preproc_session_t *session, |
| 1253 | int32_t procId, |
| 1254 | effect_handle_t *interface) |
| 1255 | { |
| 1256 | int status = -ENOMEM; |
| 1257 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1258 | ALOGV("Session_CreateEffect procId %d, createdMsk %08x", procId, session->createdMsk); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1259 | |
| 1260 | if (session->createdMsk == 0) { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1261 | #ifdef WEBRTC_LEGACY |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1262 | session->apm = webrtc::AudioProcessing::Create(); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1263 | if (session->apm == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1264 | ALOGW("Session_CreateEffect could not get apm engine"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1265 | goto error; |
| 1266 | } |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1267 | const webrtc::ProcessingConfig processing_config = { |
| 1268 | {{kPreprocDefaultSr, kPreProcDefaultCnl}, |
| 1269 | {kPreprocDefaultSr, kPreProcDefaultCnl}, |
| 1270 | {kPreprocDefaultSr, kPreProcDefaultCnl}, |
| 1271 | {kPreprocDefaultSr, kPreProcDefaultCnl}}}; |
| 1272 | session->apm->Initialize(processing_config); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1273 | session->procFrame = new webrtc::AudioFrame(); |
| 1274 | if (session->procFrame == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1275 | ALOGW("Session_CreateEffect could not allocate audio frame"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1276 | goto error; |
| 1277 | } |
| 1278 | session->revFrame = new webrtc::AudioFrame(); |
| 1279 | if (session->revFrame == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1280 | ALOGW("Session_CreateEffect could not allocate reverse audio frame"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1281 | goto error; |
| 1282 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1283 | #else |
| 1284 | session->apm = session->ap_builder.Create(); |
| 1285 | if (session->apm == NULL) { |
| 1286 | ALOGW("Session_CreateEffect could not get apm engine"); |
| 1287 | goto error; |
| 1288 | } |
| 1289 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1290 | session->apmSamplingRate = kPreprocDefaultSr; |
| 1291 | session->apmFrameCount = (kPreprocDefaultSr) / 100; |
| 1292 | session->frameCount = session->apmFrameCount; |
| 1293 | session->samplingRate = kPreprocDefaultSr; |
| 1294 | session->inChannelCount = kPreProcDefaultCnl; |
| 1295 | session->outChannelCount = kPreProcDefaultCnl; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1296 | #ifdef WEBRTC_LEGACY |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1297 | session->procFrame->sample_rate_hz_ = kPreprocDefaultSr; |
| 1298 | session->procFrame->num_channels_ = kPreProcDefaultCnl; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1299 | #else |
| 1300 | session->inputConfig.set_sample_rate_hz(kPreprocDefaultSr); |
| 1301 | session->inputConfig.set_num_channels(kPreProcDefaultCnl); |
| 1302 | session->outputConfig.set_sample_rate_hz(kPreprocDefaultSr); |
| 1303 | session->outputConfig.set_num_channels(kPreProcDefaultCnl); |
| 1304 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1305 | session->revChannelCount = kPreProcDefaultCnl; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1306 | #ifdef WEBRTC_LEGACY |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1307 | session->revFrame->sample_rate_hz_ = kPreprocDefaultSr; |
| 1308 | session->revFrame->num_channels_ = kPreProcDefaultCnl; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1309 | #else |
| 1310 | session->revConfig.set_sample_rate_hz(kPreprocDefaultSr); |
| 1311 | session->revConfig.set_num_channels(kPreProcDefaultCnl); |
| 1312 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1313 | session->enabledMsk = 0; |
| 1314 | session->processedMsk = 0; |
| 1315 | session->revEnabledMsk = 0; |
| 1316 | session->revProcessedMsk = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1317 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1318 | session->inResampler = NULL; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1319 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1320 | session->inBuf = NULL; |
| 1321 | session->inBufSize = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1322 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1323 | session->outResampler = NULL; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1324 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1325 | session->outBuf = NULL; |
| 1326 | session->outBufSize = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1327 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1328 | session->revResampler = NULL; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1329 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1330 | session->revBuf = NULL; |
| 1331 | session->revBufSize = 0; |
| 1332 | } |
| 1333 | status = Effect_Create(&session->effects[procId], session, interface); |
| 1334 | if (status < 0) { |
| 1335 | goto error; |
| 1336 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1337 | ALOGV("Session_CreateEffect OK"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1338 | session->createdMsk |= (1<<procId); |
| 1339 | return status; |
| 1340 | |
| 1341 | error: |
| 1342 | if (session->createdMsk == 0) { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1343 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1344 | delete session->revFrame; |
| 1345 | session->revFrame = NULL; |
| 1346 | delete session->procFrame; |
| 1347 | session->procFrame = NULL; |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1348 | delete session->apm; |
Chih-Hung Hsieh | f29dc04 | 2018-07-31 15:22:49 -0700 | [diff] [blame] | 1349 | session->apm = NULL; // NOLINT(clang-analyzer-cplusplus.NewDelete) |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1350 | #else |
| 1351 | delete session->apm; |
| 1352 | session->apm = NULL; |
| 1353 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1354 | } |
| 1355 | return status; |
| 1356 | } |
| 1357 | |
| 1358 | int Session_ReleaseEffect(preproc_session_t *session, |
| 1359 | preproc_effect_t *fx) |
| 1360 | { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1361 | ALOGW_IF(Effect_Release(fx) != 0, " Effect_Release() failed for proc ID %d", fx->procId); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1362 | session->createdMsk &= ~(1<<fx->procId); |
| 1363 | if (session->createdMsk == 0) { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1364 | #ifdef WEBRTC_LEGACY |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1365 | delete session->apm; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1366 | session->apm = NULL; |
| 1367 | delete session->procFrame; |
| 1368 | session->procFrame = NULL; |
| 1369 | delete session->revFrame; |
| 1370 | session->revFrame = NULL; |
| 1371 | if (session->inResampler != NULL) { |
| 1372 | speex_resampler_destroy(session->inResampler); |
| 1373 | session->inResampler = NULL; |
| 1374 | } |
| 1375 | if (session->outResampler != NULL) { |
| 1376 | speex_resampler_destroy(session->outResampler); |
| 1377 | session->outResampler = NULL; |
| 1378 | } |
| 1379 | if (session->revResampler != NULL) { |
| 1380 | speex_resampler_destroy(session->revResampler); |
| 1381 | session->revResampler = NULL; |
| 1382 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1383 | #else |
| 1384 | delete session->apm; |
| 1385 | session->apm = NULL; |
| 1386 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1387 | delete session->inBuf; |
| 1388 | session->inBuf = NULL; |
| 1389 | delete session->outBuf; |
| 1390 | session->outBuf = NULL; |
| 1391 | delete session->revBuf; |
| 1392 | session->revBuf = NULL; |
| 1393 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1394 | session->id = 0; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | int Session_SetConfig(preproc_session_t *session, effect_config_t *config) |
| 1402 | { |
Alex Luebs | 3f11ef0 | 2016-01-15 15:51:58 -0800 | [diff] [blame] | 1403 | uint32_t inCnl = audio_channel_count_from_in_mask(config->inputCfg.channels); |
| 1404 | uint32_t outCnl = audio_channel_count_from_in_mask(config->outputCfg.channels); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1405 | |
| 1406 | if (config->inputCfg.samplingRate != config->outputCfg.samplingRate || |
| 1407 | config->inputCfg.format != config->outputCfg.format || |
| 1408 | config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1409 | return -EINVAL; |
| 1410 | } |
| 1411 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1412 | ALOGV("Session_SetConfig sr %d cnl %08x", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1413 | config->inputCfg.samplingRate, config->inputCfg.channels); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1414 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1415 | int status; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1416 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1417 | |
| 1418 | // AEC implementation is limited to 16kHz |
| 1419 | if (config->inputCfg.samplingRate >= 32000 && !(session->createdMsk & (1 << PREPROC_AEC))) { |
| 1420 | session->apmSamplingRate = 32000; |
| 1421 | } else |
| 1422 | if (config->inputCfg.samplingRate >= 16000) { |
| 1423 | session->apmSamplingRate = 16000; |
| 1424 | } else if (config->inputCfg.samplingRate >= 8000) { |
| 1425 | session->apmSamplingRate = 8000; |
| 1426 | } |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1427 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1428 | #ifdef WEBRTC_LEGACY |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1429 | const webrtc::ProcessingConfig processing_config = { |
Alex Luebs | 3f11ef0 | 2016-01-15 15:51:58 -0800 | [diff] [blame] | 1430 | {{static_cast<int>(session->apmSamplingRate), inCnl}, |
| 1431 | {static_cast<int>(session->apmSamplingRate), outCnl}, |
| 1432 | {static_cast<int>(session->apmSamplingRate), inCnl}, |
| 1433 | {static_cast<int>(session->apmSamplingRate), inCnl}}}; |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1434 | status = session->apm->Initialize(processing_config); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1435 | if (status < 0) { |
| 1436 | return -EINVAL; |
| 1437 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1438 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1439 | |
| 1440 | session->samplingRate = config->inputCfg.samplingRate; |
| 1441 | session->apmFrameCount = session->apmSamplingRate / 100; |
| 1442 | if (session->samplingRate == session->apmSamplingRate) { |
| 1443 | session->frameCount = session->apmFrameCount; |
| 1444 | } else { |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1445 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1446 | session->frameCount = (session->apmFrameCount * session->samplingRate) / |
| 1447 | session->apmSamplingRate + 1; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1448 | #else |
| 1449 | session->frameCount = (session->apmFrameCount * session->samplingRate) / |
| 1450 | session->apmSamplingRate; |
| 1451 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1452 | } |
| 1453 | session->inChannelCount = inCnl; |
| 1454 | session->outChannelCount = outCnl; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1455 | #ifdef WEBRTC_LEGACY |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1456 | session->procFrame->num_channels_ = inCnl; |
| 1457 | session->procFrame->sample_rate_hz_ = session->apmSamplingRate; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1458 | #else |
| 1459 | session->inputConfig.set_sample_rate_hz(session->samplingRate); |
| 1460 | session->inputConfig.set_num_channels(inCnl); |
| 1461 | session->outputConfig.set_sample_rate_hz(session->samplingRate); |
| 1462 | session->outputConfig.set_num_channels(inCnl); |
| 1463 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1464 | |
| 1465 | session->revChannelCount = inCnl; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1466 | #ifdef WEBRTC_LEGACY |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1467 | session->revFrame->num_channels_ = inCnl; |
| 1468 | session->revFrame->sample_rate_hz_ = session->apmSamplingRate; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1469 | #else |
| 1470 | session->revConfig.set_sample_rate_hz(session->samplingRate); |
| 1471 | session->revConfig.set_num_channels(inCnl); |
| 1472 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1473 | |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1474 | // force process buffer reallocation |
| 1475 | session->inBufSize = 0; |
| 1476 | session->outBufSize = 0; |
| 1477 | session->framesIn = 0; |
| 1478 | session->framesOut = 0; |
| 1479 | |
| 1480 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1481 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1482 | if (session->inResampler != NULL) { |
| 1483 | speex_resampler_destroy(session->inResampler); |
| 1484 | session->inResampler = NULL; |
| 1485 | } |
| 1486 | if (session->outResampler != NULL) { |
| 1487 | speex_resampler_destroy(session->outResampler); |
| 1488 | session->outResampler = NULL; |
| 1489 | } |
| 1490 | if (session->revResampler != NULL) { |
| 1491 | speex_resampler_destroy(session->revResampler); |
| 1492 | session->revResampler = NULL; |
| 1493 | } |
| 1494 | if (session->samplingRate != session->apmSamplingRate) { |
| 1495 | int error; |
| 1496 | session->inResampler = speex_resampler_init(session->inChannelCount, |
| 1497 | session->samplingRate, |
| 1498 | session->apmSamplingRate, |
| 1499 | RESAMPLER_QUALITY, |
| 1500 | &error); |
| 1501 | if (session->inResampler == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1502 | ALOGW("Session_SetConfig Cannot create speex resampler: %s", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1503 | speex_resampler_strerror(error)); |
| 1504 | return -EINVAL; |
| 1505 | } |
| 1506 | session->outResampler = speex_resampler_init(session->outChannelCount, |
| 1507 | session->apmSamplingRate, |
| 1508 | session->samplingRate, |
| 1509 | RESAMPLER_QUALITY, |
| 1510 | &error); |
| 1511 | if (session->outResampler == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1512 | ALOGW("Session_SetConfig Cannot create speex resampler: %s", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1513 | speex_resampler_strerror(error)); |
| 1514 | speex_resampler_destroy(session->inResampler); |
| 1515 | session->inResampler = NULL; |
| 1516 | return -EINVAL; |
| 1517 | } |
| 1518 | session->revResampler = speex_resampler_init(session->inChannelCount, |
| 1519 | session->samplingRate, |
| 1520 | session->apmSamplingRate, |
| 1521 | RESAMPLER_QUALITY, |
| 1522 | &error); |
| 1523 | if (session->revResampler == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1524 | ALOGW("Session_SetConfig Cannot create speex resampler: %s", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1525 | speex_resampler_strerror(error)); |
| 1526 | speex_resampler_destroy(session->inResampler); |
| 1527 | session->inResampler = NULL; |
| 1528 | speex_resampler_destroy(session->outResampler); |
| 1529 | session->outResampler = NULL; |
| 1530 | return -EINVAL; |
| 1531 | } |
| 1532 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1533 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1534 | |
| 1535 | session->state = PREPROC_SESSION_STATE_CONFIG; |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1539 | void Session_GetConfig(preproc_session_t *session, effect_config_t *config) |
| 1540 | { |
| 1541 | memset(config, 0, sizeof(effect_config_t)); |
| 1542 | config->inputCfg.samplingRate = config->outputCfg.samplingRate = session->samplingRate; |
| 1543 | config->inputCfg.format = config->outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1544 | config->inputCfg.channels = audio_channel_in_mask_from_count(session->inChannelCount); |
| 1545 | // "out" doesn't mean output device, so this is the correct API to convert channel count to mask |
| 1546 | config->outputCfg.channels = audio_channel_in_mask_from_count(session->outChannelCount); |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1547 | config->inputCfg.mask = config->outputCfg.mask = |
| 1548 | (EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT); |
| 1549 | } |
| 1550 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1551 | int Session_SetReverseConfig(preproc_session_t *session, effect_config_t *config) |
| 1552 | { |
| 1553 | if (config->inputCfg.samplingRate != config->outputCfg.samplingRate || |
| 1554 | config->inputCfg.format != config->outputCfg.format || |
| 1555 | config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1556 | return -EINVAL; |
| 1557 | } |
| 1558 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1559 | ALOGV("Session_SetReverseConfig sr %d cnl %08x", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1560 | config->inputCfg.samplingRate, config->inputCfg.channels); |
| 1561 | |
| 1562 | if (session->state < PREPROC_SESSION_STATE_CONFIG) { |
| 1563 | return -ENOSYS; |
| 1564 | } |
| 1565 | if (config->inputCfg.samplingRate != session->samplingRate || |
| 1566 | config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1567 | return -EINVAL; |
| 1568 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 1569 | uint32_t inCnl = audio_channel_count_from_out_mask(config->inputCfg.channels); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1570 | #ifdef WEBRTC_LEGACY |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1571 | const webrtc::ProcessingConfig processing_config = { |
Alex Luebs | 3f11ef0 | 2016-01-15 15:51:58 -0800 | [diff] [blame] | 1572 | {{static_cast<int>(session->apmSamplingRate), session->inChannelCount}, |
| 1573 | {static_cast<int>(session->apmSamplingRate), session->outChannelCount}, |
| 1574 | {static_cast<int>(session->apmSamplingRate), inCnl}, |
| 1575 | {static_cast<int>(session->apmSamplingRate), inCnl}}}; |
Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1576 | int status = session->apm->Initialize(processing_config); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1577 | if (status < 0) { |
| 1578 | return -EINVAL; |
| 1579 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1580 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1581 | session->revChannelCount = inCnl; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1582 | #ifdef WEBRTC_LEGACY |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1583 | session->revFrame->num_channels_ = inCnl; |
| 1584 | session->revFrame->sample_rate_hz_ = session->apmSamplingRate; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1585 | #endif |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1586 | // force process buffer reallocation |
| 1587 | session->revBufSize = 0; |
| 1588 | session->framesRev = 0; |
| 1589 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1590 | return 0; |
| 1591 | } |
| 1592 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1593 | void Session_GetReverseConfig(preproc_session_t *session, effect_config_t *config) |
| 1594 | { |
| 1595 | memset(config, 0, sizeof(effect_config_t)); |
| 1596 | config->inputCfg.samplingRate = config->outputCfg.samplingRate = session->samplingRate; |
| 1597 | config->inputCfg.format = config->outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1598 | config->inputCfg.channels = config->outputCfg.channels = |
Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1599 | audio_channel_in_mask_from_count(session->revChannelCount); |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1600 | config->inputCfg.mask = config->outputCfg.mask = |
| 1601 | (EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT); |
| 1602 | } |
| 1603 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1604 | void Session_SetProcEnabled(preproc_session_t *session, uint32_t procId, bool enabled) |
| 1605 | { |
| 1606 | if (enabled) { |
| 1607 | if(session->enabledMsk == 0) { |
| 1608 | session->framesIn = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1609 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1610 | if (session->inResampler != NULL) { |
| 1611 | speex_resampler_reset_mem(session->inResampler); |
| 1612 | } |
| 1613 | session->framesOut = 0; |
| 1614 | if (session->outResampler != NULL) { |
| 1615 | speex_resampler_reset_mem(session->outResampler); |
| 1616 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1617 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1618 | } |
| 1619 | session->enabledMsk |= (1 << procId); |
| 1620 | if (HasReverseStream(procId)) { |
| 1621 | session->framesRev = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1622 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1623 | if (session->revResampler != NULL) { |
| 1624 | speex_resampler_reset_mem(session->revResampler); |
| 1625 | } |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1626 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1627 | session->revEnabledMsk |= (1 << procId); |
| 1628 | } |
| 1629 | } else { |
| 1630 | session->enabledMsk &= ~(1 << procId); |
| 1631 | if (HasReverseStream(procId)) { |
| 1632 | session->revEnabledMsk &= ~(1 << procId); |
| 1633 | } |
| 1634 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1635 | ALOGV("Session_SetProcEnabled proc %d, enabled %d enabledMsk %08x revEnabledMsk %08x", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1636 | procId, enabled, session->enabledMsk, session->revEnabledMsk); |
| 1637 | session->processedMsk = 0; |
| 1638 | if (HasReverseStream(procId)) { |
| 1639 | session->revProcessedMsk = 0; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | //------------------------------------------------------------------------------ |
| 1644 | // Bundle functions |
| 1645 | //------------------------------------------------------------------------------ |
| 1646 | |
| 1647 | static int sInitStatus = 1; |
| 1648 | static preproc_session_t sSessions[PREPROC_NUM_SESSIONS]; |
| 1649 | |
| 1650 | preproc_session_t *PreProc_GetSession(int32_t procId, int32_t sessionId, int32_t ioId) |
| 1651 | { |
| 1652 | size_t i; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1653 | for (i = 0; i < PREPROC_NUM_SESSIONS; i++) { |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1654 | if (sSessions[i].id == sessionId) { |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1655 | if (sSessions[i].createdMsk & (1 << procId)) { |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | return &sSessions[i]; |
| 1659 | } |
| 1660 | } |
| 1661 | for (i = 0; i < PREPROC_NUM_SESSIONS; i++) { |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1662 | if (sSessions[i].id == 0) { |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1663 | sSessions[i].id = sessionId; |
| 1664 | sSessions[i].io = ioId; |
| 1665 | return &sSessions[i]; |
| 1666 | } |
| 1667 | } |
| 1668 | return NULL; |
| 1669 | } |
| 1670 | |
| 1671 | |
| 1672 | int PreProc_Init() { |
| 1673 | size_t i; |
| 1674 | int status = 0; |
| 1675 | |
| 1676 | if (sInitStatus <= 0) { |
| 1677 | return sInitStatus; |
| 1678 | } |
| 1679 | for (i = 0; i < PREPROC_NUM_SESSIONS && status == 0; i++) { |
| 1680 | status = Session_Init(&sSessions[i]); |
| 1681 | } |
| 1682 | sInitStatus = status; |
| 1683 | return sInitStatus; |
| 1684 | } |
| 1685 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 1686 | const effect_descriptor_t *PreProc_GetDescriptor(const effect_uuid_t *uuid) |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1687 | { |
| 1688 | size_t i; |
| 1689 | for (i = 0; i < PREPROC_NUM_EFFECTS; i++) { |
| 1690 | if (memcmp(&sDescriptors[i]->uuid, uuid, sizeof(effect_uuid_t)) == 0) { |
| 1691 | return sDescriptors[i]; |
| 1692 | } |
| 1693 | } |
| 1694 | return NULL; |
| 1695 | } |
| 1696 | |
| 1697 | |
| 1698 | extern "C" { |
| 1699 | |
| 1700 | //------------------------------------------------------------------------------ |
| 1701 | // Effect Control Interface Implementation |
| 1702 | //------------------------------------------------------------------------------ |
| 1703 | |
| 1704 | int PreProcessingFx_Process(effect_handle_t self, |
| 1705 | audio_buffer_t *inBuffer, |
| 1706 | audio_buffer_t *outBuffer) |
| 1707 | { |
| 1708 | preproc_effect_t * effect = (preproc_effect_t *)self; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1709 | |
| 1710 | if (effect == NULL){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1711 | ALOGV("PreProcessingFx_Process() ERROR effect == NULL"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1712 | return -EINVAL; |
| 1713 | } |
| 1714 | preproc_session_t * session = (preproc_session_t *)effect->session; |
| 1715 | |
| 1716 | if (inBuffer == NULL || inBuffer->raw == NULL || |
| 1717 | outBuffer == NULL || outBuffer->raw == NULL){ |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1718 | ALOGW("PreProcessingFx_Process() ERROR bad pointer"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1719 | return -EINVAL; |
| 1720 | } |
| 1721 | |
| 1722 | session->processedMsk |= (1<<effect->procId); |
| 1723 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1724 | // ALOGV("PreProcessingFx_Process In %d frames enabledMsk %08x processedMsk %08x", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1725 | // inBuffer->frameCount, session->enabledMsk, session->processedMsk); |
| 1726 | |
| 1727 | if ((session->processedMsk & session->enabledMsk) == session->enabledMsk) { |
| 1728 | effect->session->processedMsk = 0; |
| 1729 | size_t framesRq = outBuffer->frameCount; |
| 1730 | size_t framesWr = 0; |
| 1731 | if (session->framesOut) { |
| 1732 | size_t fr = session->framesOut; |
| 1733 | if (outBuffer->frameCount < fr) { |
| 1734 | fr = outBuffer->frameCount; |
| 1735 | } |
| 1736 | memcpy(outBuffer->s16, |
| 1737 | session->outBuf, |
| 1738 | fr * session->outChannelCount * sizeof(int16_t)); |
Jiabin Huang | 0c9544a | 2020-06-04 00:46:19 +0000 | [diff] [blame] | 1739 | memmove(session->outBuf, |
| 1740 | session->outBuf + fr * session->outChannelCount, |
| 1741 | (session->framesOut - fr) * session->outChannelCount * sizeof(int16_t)); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1742 | session->framesOut -= fr; |
| 1743 | framesWr += fr; |
| 1744 | } |
| 1745 | outBuffer->frameCount = framesWr; |
| 1746 | if (framesWr == framesRq) { |
| 1747 | inBuffer->frameCount = 0; |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1751 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1752 | if (session->inResampler != NULL) { |
| 1753 | size_t fr = session->frameCount - session->framesIn; |
| 1754 | if (inBuffer->frameCount < fr) { |
| 1755 | fr = inBuffer->frameCount; |
| 1756 | } |
| 1757 | if (session->inBufSize < session->framesIn + fr) { |
Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1758 | int16_t *buf; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1759 | session->inBufSize = session->framesIn + fr; |
Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1760 | buf = (int16_t *)realloc(session->inBuf, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1761 | session->inBufSize * session->inChannelCount * sizeof(int16_t)); |
Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1762 | if (buf == NULL) { |
| 1763 | session->framesIn = 0; |
| 1764 | free(session->inBuf); |
| 1765 | session->inBuf = NULL; |
| 1766 | return -ENOMEM; |
| 1767 | } |
| 1768 | session->inBuf = buf; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1769 | } |
| 1770 | memcpy(session->inBuf + session->framesIn * session->inChannelCount, |
| 1771 | inBuffer->s16, |
| 1772 | fr * session->inChannelCount * sizeof(int16_t)); |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1773 | #ifdef DUAL_MIC_TEST |
| 1774 | pthread_mutex_lock(&gPcmDumpLock); |
| 1775 | if (gPcmDumpFh != NULL) { |
| 1776 | fwrite(inBuffer->raw, |
| 1777 | fr * session->inChannelCount * sizeof(int16_t), 1, gPcmDumpFh); |
| 1778 | } |
| 1779 | pthread_mutex_unlock(&gPcmDumpLock); |
| 1780 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1781 | |
| 1782 | session->framesIn += fr; |
| 1783 | inBuffer->frameCount = fr; |
| 1784 | if (session->framesIn < session->frameCount) { |
| 1785 | return 0; |
| 1786 | } |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1787 | spx_uint32_t frIn = session->framesIn; |
| 1788 | spx_uint32_t frOut = session->apmFrameCount; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1789 | if (session->inChannelCount == 1) { |
| 1790 | speex_resampler_process_int(session->inResampler, |
| 1791 | 0, |
| 1792 | session->inBuf, |
| 1793 | &frIn, |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1794 | session->procFrame->data_, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1795 | &frOut); |
| 1796 | } else { |
| 1797 | speex_resampler_process_interleaved_int(session->inResampler, |
| 1798 | session->inBuf, |
| 1799 | &frIn, |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1800 | session->procFrame->data_, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1801 | &frOut); |
| 1802 | } |
Jiabin Huang | 0c9544a | 2020-06-04 00:46:19 +0000 | [diff] [blame] | 1803 | memmove(session->inBuf, |
| 1804 | session->inBuf + frIn * session->inChannelCount, |
| 1805 | (session->framesIn - frIn) * session->inChannelCount * sizeof(int16_t)); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1806 | session->framesIn -= frIn; |
| 1807 | } else { |
| 1808 | size_t fr = session->frameCount - session->framesIn; |
| 1809 | if (inBuffer->frameCount < fr) { |
| 1810 | fr = inBuffer->frameCount; |
| 1811 | } |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1812 | memcpy(session->procFrame->data_ + session->framesIn * session->inChannelCount, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1813 | inBuffer->s16, |
| 1814 | fr * session->inChannelCount * sizeof(int16_t)); |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1815 | |
| 1816 | #ifdef DUAL_MIC_TEST |
| 1817 | pthread_mutex_lock(&gPcmDumpLock); |
| 1818 | if (gPcmDumpFh != NULL) { |
| 1819 | fwrite(inBuffer->raw, |
| 1820 | fr * session->inChannelCount * sizeof(int16_t), 1, gPcmDumpFh); |
| 1821 | } |
| 1822 | pthread_mutex_unlock(&gPcmDumpLock); |
| 1823 | #endif |
| 1824 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1825 | session->framesIn += fr; |
| 1826 | inBuffer->frameCount = fr; |
| 1827 | if (session->framesIn < session->frameCount) { |
| 1828 | return 0; |
| 1829 | } |
| 1830 | session->framesIn = 0; |
| 1831 | } |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 1832 | session->procFrame->samples_per_channel_ = session->apmFrameCount; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1833 | |
| 1834 | effect->session->apm->ProcessStream(session->procFrame); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1835 | #else |
| 1836 | size_t fr = session->frameCount - session->framesIn; |
| 1837 | if (inBuffer->frameCount < fr) { |
| 1838 | fr = inBuffer->frameCount; |
| 1839 | } |
| 1840 | session->framesIn += fr; |
| 1841 | inBuffer->frameCount = fr; |
| 1842 | if (session->framesIn < session->frameCount) { |
| 1843 | return 0; |
| 1844 | } |
| 1845 | session->framesIn = 0; |
| 1846 | if (int status = effect->session->apm->ProcessStream( |
| 1847 | (const int16_t* const)inBuffer->s16, |
| 1848 | (const webrtc::StreamConfig)effect->session->inputConfig, |
| 1849 | (const webrtc::StreamConfig)effect->session->outputConfig, |
| 1850 | (int16_t* const)outBuffer->s16); |
| 1851 | status != 0) { |
| 1852 | ALOGE("Process Stream failed with error %d\n", status); |
| 1853 | return status; |
| 1854 | } |
| 1855 | outBuffer->frameCount = inBuffer->frameCount; |
| 1856 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1857 | |
| 1858 | if (session->outBufSize < session->framesOut + session->frameCount) { |
Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1859 | int16_t *buf; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1860 | session->outBufSize = session->framesOut + session->frameCount; |
Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1861 | buf = (int16_t *)realloc(session->outBuf, |
| 1862 | session->outBufSize * session->outChannelCount * sizeof(int16_t)); |
| 1863 | if (buf == NULL) { |
| 1864 | session->framesOut = 0; |
| 1865 | free(session->outBuf); |
| 1866 | session->outBuf = NULL; |
| 1867 | return -ENOMEM; |
| 1868 | } |
| 1869 | session->outBuf = buf; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1872 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1873 | if (session->outResampler != NULL) { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1874 | spx_uint32_t frIn = session->apmFrameCount; |
| 1875 | spx_uint32_t frOut = session->frameCount; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1876 | if (session->inChannelCount == 1) { |
| 1877 | speex_resampler_process_int(session->outResampler, |
| 1878 | 0, |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1879 | session->procFrame->data_, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1880 | &frIn, |
| 1881 | session->outBuf + session->framesOut * session->outChannelCount, |
| 1882 | &frOut); |
| 1883 | } else { |
| 1884 | speex_resampler_process_interleaved_int(session->outResampler, |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1885 | session->procFrame->data_, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1886 | &frIn, |
| 1887 | session->outBuf + session->framesOut * session->outChannelCount, |
| 1888 | &frOut); |
| 1889 | } |
| 1890 | session->framesOut += frOut; |
| 1891 | } else { |
| 1892 | memcpy(session->outBuf + session->framesOut * session->outChannelCount, |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1893 | session->procFrame->data_, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1894 | session->frameCount * session->outChannelCount * sizeof(int16_t)); |
| 1895 | session->framesOut += session->frameCount; |
| 1896 | } |
| 1897 | size_t fr = session->framesOut; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 1898 | #else |
| 1899 | fr = session->framesOut; |
| 1900 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1901 | if (framesRq - framesWr < fr) { |
| 1902 | fr = framesRq - framesWr; |
| 1903 | } |
| 1904 | memcpy(outBuffer->s16 + framesWr * session->outChannelCount, |
| 1905 | session->outBuf, |
| 1906 | fr * session->outChannelCount * sizeof(int16_t)); |
Jiabin Huang | 0c9544a | 2020-06-04 00:46:19 +0000 | [diff] [blame] | 1907 | memmove(session->outBuf, |
| 1908 | session->outBuf + fr * session->outChannelCount, |
| 1909 | (session->framesOut - fr) * session->outChannelCount * sizeof(int16_t)); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1910 | session->framesOut -= fr; |
| 1911 | outBuffer->frameCount += fr; |
| 1912 | |
| 1913 | return 0; |
| 1914 | } else { |
| 1915 | return -ENODATA; |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | int PreProcessingFx_Command(effect_handle_t self, |
| 1920 | uint32_t cmdCode, |
| 1921 | uint32_t cmdSize, |
| 1922 | void *pCmdData, |
| 1923 | uint32_t *replySize, |
| 1924 | void *pReplyData) |
| 1925 | { |
| 1926 | preproc_effect_t * effect = (preproc_effect_t *) self; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1927 | |
| 1928 | if (effect == NULL){ |
| 1929 | return -EINVAL; |
| 1930 | } |
| 1931 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1932 | //ALOGV("PreProcessingFx_Command: command %d cmdSize %d",cmdCode, cmdSize); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1933 | |
| 1934 | switch (cmdCode){ |
| 1935 | case EFFECT_CMD_INIT: |
| 1936 | if (pReplyData == NULL || *replySize != sizeof(int)){ |
| 1937 | return -EINVAL; |
| 1938 | } |
| 1939 | if (effect->ops->init) { |
| 1940 | effect->ops->init(effect); |
| 1941 | } |
| 1942 | *(int *)pReplyData = 0; |
| 1943 | break; |
| 1944 | |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1945 | case EFFECT_CMD_SET_CONFIG: { |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1946 | if (pCmdData == NULL|| |
| 1947 | cmdSize != sizeof(effect_config_t)|| |
| 1948 | pReplyData == NULL|| |
| 1949 | *replySize != sizeof(int)){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1950 | ALOGV("PreProcessingFx_Command cmdCode Case: " |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1951 | "EFFECT_CMD_SET_CONFIG: ERROR"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1952 | return -EINVAL; |
| 1953 | } |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1954 | #ifdef DUAL_MIC_TEST |
| 1955 | // make sure that the config command is accepted by making as if all effects were |
| 1956 | // disabled: this is OK for functional tests |
| 1957 | uint32_t enabledMsk = effect->session->enabledMsk; |
| 1958 | if (gDualMicEnabled) { |
| 1959 | effect->session->enabledMsk = 0; |
| 1960 | } |
| 1961 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1962 | *(int *)pReplyData = Session_SetConfig(effect->session, (effect_config_t *)pCmdData); |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1963 | #ifdef DUAL_MIC_TEST |
| 1964 | if (gDualMicEnabled) { |
| 1965 | effect->session->enabledMsk = enabledMsk; |
| 1966 | } |
| 1967 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1968 | if (*(int *)pReplyData != 0) { |
| 1969 | break; |
| 1970 | } |
Eric Laurent | 76533e9 | 2012-02-17 17:52:04 -0800 | [diff] [blame] | 1971 | if (effect->state != PREPROC_EFFECT_STATE_ACTIVE) { |
| 1972 | *(int *)pReplyData = Effect_SetState(effect, PREPROC_EFFECT_STATE_CONFIG); |
| 1973 | } |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1974 | } break; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1975 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1976 | case EFFECT_CMD_GET_CONFIG: |
| 1977 | if (pReplyData == NULL || |
| 1978 | *replySize != sizeof(effect_config_t)) { |
| 1979 | ALOGV("\tLVM_ERROR : PreProcessingFx_Command cmdCode Case: " |
| 1980 | "EFFECT_CMD_GET_CONFIG: ERROR"); |
| 1981 | return -EINVAL; |
| 1982 | } |
| 1983 | |
Eric Laurent | 94fef38 | 2012-02-06 14:28:54 -0800 | [diff] [blame] | 1984 | Session_GetConfig(effect->session, (effect_config_t *)pReplyData); |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1985 | break; |
| 1986 | |
| 1987 | case EFFECT_CMD_SET_CONFIG_REVERSE: |
| 1988 | if (pCmdData == NULL || |
| 1989 | cmdSize != sizeof(effect_config_t) || |
| 1990 | pReplyData == NULL || |
| 1991 | *replySize != sizeof(int)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1992 | ALOGV("PreProcessingFx_Command cmdCode Case: " |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1993 | "EFFECT_CMD_SET_CONFIG_REVERSE: ERROR"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1994 | return -EINVAL; |
| 1995 | } |
| 1996 | *(int *)pReplyData = Session_SetReverseConfig(effect->session, |
| 1997 | (effect_config_t *)pCmdData); |
| 1998 | if (*(int *)pReplyData != 0) { |
| 1999 | break; |
| 2000 | } |
| 2001 | break; |
| 2002 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 2003 | case EFFECT_CMD_GET_CONFIG_REVERSE: |
| 2004 | if (pReplyData == NULL || |
| 2005 | *replySize != sizeof(effect_config_t)){ |
| 2006 | ALOGV("PreProcessingFx_Command cmdCode Case: " |
| 2007 | "EFFECT_CMD_GET_CONFIG_REVERSE: ERROR"); |
| 2008 | return -EINVAL; |
| 2009 | } |
| 2010 | Session_GetReverseConfig(effect->session, (effect_config_t *)pCmdData); |
| 2011 | break; |
| 2012 | |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2013 | case EFFECT_CMD_RESET: |
| 2014 | if (effect->ops->reset) { |
| 2015 | effect->ops->reset(effect); |
| 2016 | } |
| 2017 | break; |
| 2018 | |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 2019 | case EFFECT_CMD_GET_PARAM: { |
| 2020 | effect_param_t *p = (effect_param_t *)pCmdData; |
| 2021 | |
| 2022 | if (pCmdData == NULL || cmdSize < sizeof(effect_param_t) || |
| 2023 | cmdSize < (sizeof(effect_param_t) + p->psize) || |
| 2024 | pReplyData == NULL || replySize == NULL || |
| 2025 | *replySize < (sizeof(effect_param_t) + p->psize)){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2026 | ALOGV("PreProcessingFx_Command cmdCode Case: " |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2027 | "EFFECT_CMD_GET_PARAM: ERROR"); |
| 2028 | return -EINVAL; |
| 2029 | } |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2030 | |
| 2031 | memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize); |
| 2032 | |
| 2033 | p = (effect_param_t *)pReplyData; |
| 2034 | |
| 2035 | int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t); |
| 2036 | |
| 2037 | if (effect->ops->get_parameter) { |
| 2038 | p->status = effect->ops->get_parameter(effect, p->data, |
Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 2039 | &p->vsize, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2040 | p->data + voffset); |
| 2041 | *replySize = sizeof(effect_param_t) + voffset + p->vsize; |
| 2042 | } |
| 2043 | } break; |
| 2044 | |
| 2045 | case EFFECT_CMD_SET_PARAM:{ |
| 2046 | if (pCmdData == NULL|| |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 2047 | cmdSize < sizeof(effect_param_t) || |
| 2048 | pReplyData == NULL || replySize == NULL || |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2049 | *replySize != sizeof(int32_t)){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2050 | ALOGV("PreProcessingFx_Command cmdCode Case: " |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2051 | "EFFECT_CMD_SET_PARAM: ERROR"); |
| 2052 | return -EINVAL; |
| 2053 | } |
| 2054 | effect_param_t *p = (effect_param_t *) pCmdData; |
| 2055 | |
| 2056 | if (p->psize != sizeof(int32_t)){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2057 | ALOGV("PreProcessingFx_Command cmdCode Case: " |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2058 | "EFFECT_CMD_SET_PARAM: ERROR, psize is not sizeof(int32_t)"); |
| 2059 | return -EINVAL; |
| 2060 | } |
| 2061 | if (effect->ops->set_parameter) { |
| 2062 | *(int *)pReplyData = effect->ops->set_parameter(effect, |
| 2063 | (void *)p->data, |
| 2064 | p->data + p->psize); |
| 2065 | } |
| 2066 | } break; |
| 2067 | |
| 2068 | case EFFECT_CMD_ENABLE: |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 2069 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2070 | ALOGV("PreProcessingFx_Command cmdCode Case: EFFECT_CMD_ENABLE: ERROR"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2071 | return -EINVAL; |
| 2072 | } |
| 2073 | *(int *)pReplyData = Effect_SetState(effect, PREPROC_EFFECT_STATE_ACTIVE); |
| 2074 | break; |
| 2075 | |
| 2076 | case EFFECT_CMD_DISABLE: |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 2077 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2078 | ALOGV("PreProcessingFx_Command cmdCode Case: EFFECT_CMD_DISABLE: ERROR"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2079 | return -EINVAL; |
| 2080 | } |
| 2081 | *(int *)pReplyData = Effect_SetState(effect, PREPROC_EFFECT_STATE_CONFIG); |
| 2082 | break; |
| 2083 | |
| 2084 | case EFFECT_CMD_SET_DEVICE: |
| 2085 | case EFFECT_CMD_SET_INPUT_DEVICE: |
| 2086 | if (pCmdData == NULL || |
| 2087 | cmdSize != sizeof(uint32_t)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2088 | ALOGV("PreProcessingFx_Command cmdCode Case: EFFECT_CMD_SET_DEVICE: ERROR"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2089 | return -EINVAL; |
| 2090 | } |
| 2091 | |
| 2092 | if (effect->ops->set_device) { |
| 2093 | effect->ops->set_device(effect, *(uint32_t *)pCmdData); |
| 2094 | } |
| 2095 | break; |
| 2096 | |
| 2097 | case EFFECT_CMD_SET_VOLUME: |
| 2098 | case EFFECT_CMD_SET_AUDIO_MODE: |
| 2099 | break; |
| 2100 | |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 2101 | #ifdef DUAL_MIC_TEST |
| 2102 | ///// test commands start |
| 2103 | case PREPROC_CMD_DUAL_MIC_ENABLE: { |
| 2104 | if (pCmdData == NULL|| cmdSize != sizeof(uint32_t) || |
| 2105 | pReplyData == NULL || replySize == NULL) { |
| 2106 | ALOGE("PreProcessingFx_Command cmdCode Case: " |
| 2107 | "PREPROC_CMD_DUAL_MIC_ENABLE: ERROR"); |
| 2108 | *replySize = 0; |
| 2109 | return -EINVAL; |
| 2110 | } |
| 2111 | gDualMicEnabled = *(bool *)pCmdData; |
| 2112 | if (gDualMicEnabled) { |
| 2113 | effect->aux_channels_on = sHasAuxChannels[effect->procId]; |
| 2114 | } else { |
| 2115 | effect->aux_channels_on = false; |
| 2116 | } |
| 2117 | effect->cur_channel_config = (effect->session->inChannelCount == 1) ? |
| 2118 | CHANNEL_CFG_MONO : CHANNEL_CFG_STEREO; |
| 2119 | |
| 2120 | ALOGV("PREPROC_CMD_DUAL_MIC_ENABLE: %s", gDualMicEnabled ? "enabled" : "disabled"); |
| 2121 | *replySize = sizeof(int); |
| 2122 | *(int *)pReplyData = 0; |
| 2123 | } break; |
| 2124 | case PREPROC_CMD_DUAL_MIC_PCM_DUMP_START: { |
| 2125 | if (pCmdData == NULL|| pReplyData == NULL || replySize == NULL) { |
| 2126 | ALOGE("PreProcessingFx_Command cmdCode Case: " |
| 2127 | "PREPROC_CMD_DUAL_MIC_PCM_DUMP_START: ERROR"); |
| 2128 | *replySize = 0; |
| 2129 | return -EINVAL; |
| 2130 | } |
| 2131 | pthread_mutex_lock(&gPcmDumpLock); |
| 2132 | if (gPcmDumpFh != NULL) { |
| 2133 | fclose(gPcmDumpFh); |
| 2134 | gPcmDumpFh = NULL; |
| 2135 | } |
| 2136 | char *path = strndup((char *)pCmdData, cmdSize); |
| 2137 | gPcmDumpFh = fopen((char *)path, "wb"); |
| 2138 | pthread_mutex_unlock(&gPcmDumpLock); |
| 2139 | ALOGV("PREPROC_CMD_DUAL_MIC_PCM_DUMP_START: path %s gPcmDumpFh %p", |
| 2140 | path, gPcmDumpFh); |
| 2141 | ALOGE_IF(gPcmDumpFh <= 0, "gPcmDumpFh open error %d %s", errno, strerror(errno)); |
| 2142 | free(path); |
| 2143 | *replySize = sizeof(int); |
| 2144 | *(int *)pReplyData = 0; |
| 2145 | } break; |
| 2146 | case PREPROC_CMD_DUAL_MIC_PCM_DUMP_STOP: { |
| 2147 | if (pReplyData == NULL || replySize == NULL) { |
| 2148 | ALOGE("PreProcessingFx_Command cmdCode Case: " |
| 2149 | "PREPROC_CMD_DUAL_MIC_PCM_DUMP_STOP: ERROR"); |
| 2150 | *replySize = 0; |
| 2151 | return -EINVAL; |
| 2152 | } |
| 2153 | pthread_mutex_lock(&gPcmDumpLock); |
| 2154 | if (gPcmDumpFh != NULL) { |
| 2155 | fclose(gPcmDumpFh); |
| 2156 | gPcmDumpFh = NULL; |
| 2157 | } |
| 2158 | pthread_mutex_unlock(&gPcmDumpLock); |
| 2159 | ALOGV("PREPROC_CMD_DUAL_MIC_PCM_DUMP_STOP"); |
| 2160 | *replySize = sizeof(int); |
| 2161 | *(int *)pReplyData = 0; |
| 2162 | } break; |
| 2163 | ///// test commands end |
| 2164 | |
| 2165 | case EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS: { |
| 2166 | if(!gDualMicEnabled) { |
| 2167 | return -EINVAL; |
| 2168 | } |
| 2169 | if (pCmdData == NULL|| cmdSize != 2 * sizeof(uint32_t) || |
| 2170 | pReplyData == NULL || replySize == NULL) { |
| 2171 | ALOGE("PreProcessingFx_Command cmdCode Case: " |
| 2172 | "EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS: ERROR"); |
| 2173 | *replySize = 0; |
| 2174 | return -EINVAL; |
| 2175 | } |
| 2176 | if (*(uint32_t *)pCmdData != EFFECT_FEATURE_AUX_CHANNELS || |
| 2177 | !effect->aux_channels_on) { |
| 2178 | ALOGV("PreProcessingFx_Command feature EFFECT_FEATURE_AUX_CHANNELS not supported by" |
| 2179 | " fx %d", effect->procId); |
| 2180 | *(uint32_t *)pReplyData = -ENOSYS; |
| 2181 | *replySize = sizeof(uint32_t); |
| 2182 | break; |
| 2183 | } |
| 2184 | size_t num_configs = *((uint32_t *)pCmdData + 1); |
| 2185 | if (*replySize < (2 * sizeof(uint32_t) + |
| 2186 | num_configs * sizeof(channel_config_t))) { |
| 2187 | *replySize = 0; |
| 2188 | return -EINVAL; |
| 2189 | } |
| 2190 | |
| 2191 | *((uint32_t *)pReplyData + 1) = CHANNEL_CFG_CNT; |
| 2192 | if (num_configs < CHANNEL_CFG_CNT || |
| 2193 | *replySize < (2 * sizeof(uint32_t) + |
| 2194 | CHANNEL_CFG_CNT * sizeof(channel_config_t))) { |
| 2195 | *(uint32_t *)pReplyData = -ENOMEM; |
| 2196 | } else { |
| 2197 | num_configs = CHANNEL_CFG_CNT; |
| 2198 | *(uint32_t *)pReplyData = 0; |
| 2199 | } |
| 2200 | ALOGV("PreProcessingFx_Command EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS num config %d", |
| 2201 | num_configs); |
| 2202 | |
| 2203 | *replySize = 2 * sizeof(uint32_t) + num_configs * sizeof(channel_config_t); |
| 2204 | *((uint32_t *)pReplyData + 1) = num_configs; |
| 2205 | memcpy((uint32_t *)pReplyData + 2, &sDualMicConfigs, num_configs * sizeof(channel_config_t)); |
| 2206 | } break; |
| 2207 | case EFFECT_CMD_GET_FEATURE_CONFIG: |
| 2208 | if(!gDualMicEnabled) { |
| 2209 | return -EINVAL; |
| 2210 | } |
| 2211 | if (pCmdData == NULL|| cmdSize != sizeof(uint32_t) || |
| 2212 | pReplyData == NULL || replySize == NULL || |
| 2213 | *replySize < sizeof(uint32_t) + sizeof(channel_config_t)) { |
| 2214 | ALOGE("PreProcessingFx_Command cmdCode Case: " |
| 2215 | "EFFECT_CMD_GET_FEATURE_CONFIG: ERROR"); |
| 2216 | return -EINVAL; |
| 2217 | } |
| 2218 | if (*(uint32_t *)pCmdData != EFFECT_FEATURE_AUX_CHANNELS || !effect->aux_channels_on) { |
| 2219 | *(uint32_t *)pReplyData = -ENOSYS; |
| 2220 | *replySize = sizeof(uint32_t); |
| 2221 | break; |
| 2222 | } |
| 2223 | ALOGV("PreProcessingFx_Command EFFECT_CMD_GET_FEATURE_CONFIG"); |
| 2224 | *(uint32_t *)pReplyData = 0; |
| 2225 | *replySize = sizeof(uint32_t) + sizeof(channel_config_t); |
| 2226 | memcpy((uint32_t *)pReplyData + 1, |
| 2227 | &sDualMicConfigs[effect->cur_channel_config], |
| 2228 | sizeof(channel_config_t)); |
| 2229 | break; |
| 2230 | case EFFECT_CMD_SET_FEATURE_CONFIG: { |
| 2231 | ALOGV("PreProcessingFx_Command EFFECT_CMD_SET_FEATURE_CONFIG: " |
| 2232 | "gDualMicEnabled %d effect->aux_channels_on %d", |
| 2233 | gDualMicEnabled, effect->aux_channels_on); |
| 2234 | if(!gDualMicEnabled) { |
| 2235 | return -EINVAL; |
| 2236 | } |
| 2237 | if (pCmdData == NULL|| cmdSize != (sizeof(uint32_t) + sizeof(channel_config_t)) || |
| 2238 | pReplyData == NULL || replySize == NULL || |
| 2239 | *replySize < sizeof(uint32_t)) { |
| 2240 | ALOGE("PreProcessingFx_Command cmdCode Case: " |
| 2241 | "EFFECT_CMD_SET_FEATURE_CONFIG: ERROR\n" |
| 2242 | "pCmdData %p cmdSize %d pReplyData %p replySize %p *replySize %d", |
| 2243 | pCmdData, cmdSize, pReplyData, replySize, replySize ? *replySize : -1); |
| 2244 | return -EINVAL; |
| 2245 | } |
| 2246 | *replySize = sizeof(uint32_t); |
| 2247 | if (*(uint32_t *)pCmdData != EFFECT_FEATURE_AUX_CHANNELS || !effect->aux_channels_on) { |
| 2248 | *(uint32_t *)pReplyData = -ENOSYS; |
| 2249 | ALOGV("PreProcessingFx_Command cmdCode Case: " |
| 2250 | "EFFECT_CMD_SET_FEATURE_CONFIG: ERROR\n" |
| 2251 | "CmdData %d effect->aux_channels_on %d", |
| 2252 | *(uint32_t *)pCmdData, effect->aux_channels_on); |
| 2253 | break; |
| 2254 | } |
| 2255 | size_t i; |
| 2256 | for (i = 0; i < CHANNEL_CFG_CNT;i++) { |
| 2257 | if (memcmp((uint32_t *)pCmdData + 1, |
| 2258 | &sDualMicConfigs[i], sizeof(channel_config_t)) == 0) { |
| 2259 | break; |
| 2260 | } |
| 2261 | } |
| 2262 | if (i == CHANNEL_CFG_CNT) { |
| 2263 | *(uint32_t *)pReplyData = -EINVAL; |
| 2264 | ALOGW("PreProcessingFx_Command EFFECT_CMD_SET_FEATURE_CONFIG invalid config" |
| 2265 | "[%08x].[%08x]", *((uint32_t *)pCmdData + 1), *((uint32_t *)pCmdData + 2)); |
| 2266 | } else { |
| 2267 | effect->cur_channel_config = i; |
| 2268 | *(uint32_t *)pReplyData = 0; |
| 2269 | ALOGV("PreProcessingFx_Command EFFECT_CMD_SET_FEATURE_CONFIG New config" |
| 2270 | "[%08x].[%08x]", sDualMicConfigs[i].main_channels, sDualMicConfigs[i].aux_channels); |
| 2271 | } |
| 2272 | } break; |
| 2273 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2274 | default: |
| 2275 | return -EINVAL; |
| 2276 | } |
| 2277 | return 0; |
| 2278 | } |
| 2279 | |
| 2280 | |
| 2281 | int PreProcessingFx_GetDescriptor(effect_handle_t self, |
| 2282 | effect_descriptor_t *pDescriptor) |
| 2283 | { |
| 2284 | preproc_effect_t * effect = (preproc_effect_t *) self; |
| 2285 | |
| 2286 | if (effect == NULL || pDescriptor == NULL) { |
| 2287 | return -EINVAL; |
| 2288 | } |
| 2289 | |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2290 | *pDescriptor = *sDescriptors[effect->procId]; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2291 | |
| 2292 | return 0; |
| 2293 | } |
| 2294 | |
| 2295 | int PreProcessingFx_ProcessReverse(effect_handle_t self, |
| 2296 | audio_buffer_t *inBuffer, |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 2297 | audio_buffer_t *outBuffer __unused) |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2298 | { |
| 2299 | preproc_effect_t * effect = (preproc_effect_t *)self; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2300 | |
| 2301 | if (effect == NULL){ |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2302 | ALOGW("PreProcessingFx_ProcessReverse() ERROR effect == NULL"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2303 | return -EINVAL; |
| 2304 | } |
| 2305 | preproc_session_t * session = (preproc_session_t *)effect->session; |
| 2306 | |
| 2307 | if (inBuffer == NULL || inBuffer->raw == NULL){ |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2308 | ALOGW("PreProcessingFx_ProcessReverse() ERROR bad pointer"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2309 | return -EINVAL; |
| 2310 | } |
| 2311 | |
| 2312 | session->revProcessedMsk |= (1<<effect->procId); |
| 2313 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2314 | // ALOGV("PreProcessingFx_ProcessReverse In %d frames revEnabledMsk %08x revProcessedMsk %08x", |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2315 | // inBuffer->frameCount, session->revEnabledMsk, session->revProcessedMsk); |
| 2316 | |
| 2317 | |
| 2318 | if ((session->revProcessedMsk & session->revEnabledMsk) == session->revEnabledMsk) { |
| 2319 | effect->session->revProcessedMsk = 0; |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 2320 | #ifdef WEBRTC_LEGACY |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2321 | if (session->revResampler != NULL) { |
| 2322 | size_t fr = session->frameCount - session->framesRev; |
| 2323 | if (inBuffer->frameCount < fr) { |
| 2324 | fr = inBuffer->frameCount; |
| 2325 | } |
| 2326 | if (session->revBufSize < session->framesRev + fr) { |
Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 2327 | int16_t *buf; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2328 | session->revBufSize = session->framesRev + fr; |
Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 2329 | buf = (int16_t *)realloc(session->revBuf, |
| 2330 | session->revBufSize * session->inChannelCount * sizeof(int16_t)); |
| 2331 | if (buf == NULL) { |
| 2332 | session->framesRev = 0; |
| 2333 | free(session->revBuf); |
| 2334 | session->revBuf = NULL; |
| 2335 | return -ENOMEM; |
| 2336 | } |
| 2337 | session->revBuf = buf; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2338 | } |
| 2339 | memcpy(session->revBuf + session->framesRev * session->inChannelCount, |
| 2340 | inBuffer->s16, |
| 2341 | fr * session->inChannelCount * sizeof(int16_t)); |
| 2342 | |
| 2343 | session->framesRev += fr; |
| 2344 | inBuffer->frameCount = fr; |
| 2345 | if (session->framesRev < session->frameCount) { |
| 2346 | return 0; |
| 2347 | } |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2348 | spx_uint32_t frIn = session->framesRev; |
| 2349 | spx_uint32_t frOut = session->apmFrameCount; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2350 | if (session->inChannelCount == 1) { |
| 2351 | speex_resampler_process_int(session->revResampler, |
| 2352 | 0, |
| 2353 | session->revBuf, |
| 2354 | &frIn, |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 2355 | session->revFrame->data_, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2356 | &frOut); |
| 2357 | } else { |
| 2358 | speex_resampler_process_interleaved_int(session->revResampler, |
| 2359 | session->revBuf, |
| 2360 | &frIn, |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 2361 | session->revFrame->data_, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2362 | &frOut); |
| 2363 | } |
Jiabin Huang | 0c9544a | 2020-06-04 00:46:19 +0000 | [diff] [blame] | 2364 | memmove(session->revBuf, |
| 2365 | session->revBuf + frIn * session->inChannelCount, |
| 2366 | (session->framesRev - frIn) * session->inChannelCount * sizeof(int16_t)); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2367 | session->framesRev -= frIn; |
| 2368 | } else { |
| 2369 | size_t fr = session->frameCount - session->framesRev; |
| 2370 | if (inBuffer->frameCount < fr) { |
| 2371 | fr = inBuffer->frameCount; |
| 2372 | } |
Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 2373 | memcpy(session->revFrame->data_ + session->framesRev * session->inChannelCount, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2374 | inBuffer->s16, |
| 2375 | fr * session->inChannelCount * sizeof(int16_t)); |
| 2376 | session->framesRev += fr; |
| 2377 | inBuffer->frameCount = fr; |
| 2378 | if (session->framesRev < session->frameCount) { |
| 2379 | return 0; |
| 2380 | } |
| 2381 | session->framesRev = 0; |
| 2382 | } |
Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 2383 | session->revFrame->samples_per_channel_ = session->apmFrameCount; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2384 | effect->session->apm->AnalyzeReverseStream(session->revFrame); |
Saketh Sathuvalli | 0833703 | 2020-09-22 21:13:45 +0530 | [diff] [blame] | 2385 | #else |
| 2386 | size_t fr = session->frameCount - session->framesRev; |
| 2387 | if (inBuffer->frameCount < fr) { |
| 2388 | fr = inBuffer->frameCount; |
| 2389 | } |
| 2390 | session->framesRev += fr; |
| 2391 | inBuffer->frameCount = fr; |
| 2392 | if (session->framesRev < session->frameCount) { |
| 2393 | return 0; |
| 2394 | } |
| 2395 | session->framesRev = 0; |
| 2396 | if (int status = effect->session->apm->ProcessReverseStream( |
| 2397 | (const int16_t* const)inBuffer->s16, |
| 2398 | (const webrtc::StreamConfig)effect->session->revConfig, |
| 2399 | (const webrtc::StreamConfig)effect->session->revConfig, |
| 2400 | (int16_t* const)outBuffer->s16); |
| 2401 | status != 0) { |
| 2402 | ALOGE("Process Reverse Stream failed with error %d\n", status); |
| 2403 | return status; |
| 2404 | } |
| 2405 | #endif |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2406 | return 0; |
| 2407 | } else { |
| 2408 | return -ENODATA; |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | // effect_handle_t interface implementation for effect |
| 2414 | const struct effect_interface_s sEffectInterface = { |
| 2415 | PreProcessingFx_Process, |
| 2416 | PreProcessingFx_Command, |
| 2417 | PreProcessingFx_GetDescriptor, |
| 2418 | NULL |
| 2419 | }; |
| 2420 | |
| 2421 | const struct effect_interface_s sEffectInterfaceReverse = { |
| 2422 | PreProcessingFx_Process, |
| 2423 | PreProcessingFx_Command, |
| 2424 | PreProcessingFx_GetDescriptor, |
| 2425 | PreProcessingFx_ProcessReverse |
| 2426 | }; |
| 2427 | |
| 2428 | //------------------------------------------------------------------------------ |
| 2429 | // Effect Library Interface Implementation |
| 2430 | //------------------------------------------------------------------------------ |
| 2431 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 2432 | int PreProcessingLib_Create(const effect_uuid_t *uuid, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2433 | int32_t sessionId, |
| 2434 | int32_t ioId, |
| 2435 | effect_handle_t *pInterface) |
| 2436 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2437 | ALOGV("EffectCreate: uuid: %08x session %d IO: %d", uuid->timeLow, sessionId, ioId); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2438 | |
| 2439 | int status; |
| 2440 | const effect_descriptor_t *desc; |
| 2441 | preproc_session_t *session; |
| 2442 | uint32_t procId; |
| 2443 | |
| 2444 | if (PreProc_Init() != 0) { |
| 2445 | return sInitStatus; |
| 2446 | } |
| 2447 | desc = PreProc_GetDescriptor(uuid); |
| 2448 | if (desc == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2449 | ALOGW("EffectCreate: fx not found uuid: %08x", uuid->timeLow); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2450 | return -EINVAL; |
| 2451 | } |
| 2452 | procId = UuidToProcId(&desc->type); |
| 2453 | |
| 2454 | session = PreProc_GetSession(procId, sessionId, ioId); |
| 2455 | if (session == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2456 | ALOGW("EffectCreate: no more session available"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2457 | return -EINVAL; |
| 2458 | } |
| 2459 | |
| 2460 | status = Session_CreateEffect(session, procId, pInterface); |
| 2461 | |
| 2462 | if (status < 0 && session->createdMsk == 0) { |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2463 | session->id = 0; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2464 | } |
| 2465 | return status; |
| 2466 | } |
| 2467 | |
| 2468 | int PreProcessingLib_Release(effect_handle_t interface) |
| 2469 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2470 | ALOGV("EffectRelease start %p", interface); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2471 | if (PreProc_Init() != 0) { |
| 2472 | return sInitStatus; |
| 2473 | } |
| 2474 | |
| 2475 | preproc_effect_t *fx = (preproc_effect_t *)interface; |
| 2476 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2477 | if (fx->session->id == 0) { |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2478 | return -EINVAL; |
| 2479 | } |
| 2480 | return Session_ReleaseEffect(fx->session, fx); |
| 2481 | } |
| 2482 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 2483 | int PreProcessingLib_GetDescriptor(const effect_uuid_t *uuid, |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2484 | effect_descriptor_t *pDescriptor) { |
| 2485 | |
| 2486 | if (pDescriptor == NULL || uuid == NULL){ |
| 2487 | return -EINVAL; |
| 2488 | } |
| 2489 | |
| 2490 | const effect_descriptor_t *desc = PreProc_GetDescriptor(uuid); |
| 2491 | if (desc == NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2492 | ALOGV("PreProcessingLib_GetDescriptor() not found"); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2493 | return -EINVAL; |
| 2494 | } |
| 2495 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2496 | ALOGV("PreProcessingLib_GetDescriptor() got fx %s", desc->name); |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2497 | |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2498 | *pDescriptor = *desc; |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2499 | return 0; |
| 2500 | } |
| 2501 | |
Marco Nelissen | 7f16b19 | 2012-10-25 16:05:57 -0700 | [diff] [blame] | 2502 | // This is the only symbol that needs to be exported |
| 2503 | __attribute__ ((visibility ("default"))) |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2504 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { |
synergydev | c9d8ea7 | 2013-10-19 22:51:33 -0700 | [diff] [blame] | 2505 | .tag = AUDIO_EFFECT_LIBRARY_TAG, |
| 2506 | .version = EFFECT_LIBRARY_API_VERSION, |
| 2507 | .name = "Audio Preprocessing Library", |
| 2508 | .implementor = "The Android Open Source Project", |
| 2509 | .create_effect = PreProcessingLib_Create, |
| 2510 | .release_effect = PreProcessingLib_Release, |
| 2511 | .get_descriptor = PreProcessingLib_GetDescriptor |
Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 2512 | }; |
| 2513 | |
| 2514 | }; // extern "C" |