blob: 59b27ad09550689e9f50b90124f0bf13d315342e [file] [log] [blame]
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +05301/*
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#include <assert.h>
17#include <inttypes.h>
Saketh Sathuvallicb398ab2019-02-16 13:12:59 +053018#include <iterator>
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +053019#include <math.h>
20#include <stdlib.h>
21#include <string.h>
22#include <vector>
23
24#include <audio_utils/channels.h>
25#include <audio_utils/primitives.h>
26#include <log/log.h>
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +053027#include <system/audio.h>
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +053028
29#include "EffectBundle.h"
30#include "LVM_Private.h"
31
32#ifdef VERY_VERY_VERBOSE_LOGGING
33#define ALOGVV ALOGV
34#else
35#define ALOGVV(a...) \
36 do { \
37 } while (false)
38#endif
39
40#define CHECK_ARG(cond) \
41 { \
42 if (!(cond)) { \
43 ALOGE("\tLVM_ERROR : Invalid argument: " #cond); \
44 return -EINVAL; \
45 } \
46 \
47}
48
49#define LVM_ERROR_CHECK(LvmStatus, callingFunc, calledFunc) \
50 { \
51 if ((LvmStatus) == LVM_NULLADDRESS) { \
52 ALOGE( \
53 "\tLVM_ERROR : Parameter error - " \
54 "null pointer returned by %s in %s\n\n\n\n", \
55 callingFunc, calledFunc); \
56 } \
57 if ((LvmStatus) == LVM_ALIGNMENTERROR) { \
58 ALOGE( \
59 "\tLVM_ERROR : Parameter error - " \
60 "bad alignment returned by %s in %s\n\n\n\n", \
61 callingFunc, calledFunc); \
62 } \
63 if ((LvmStatus) == LVM_INVALIDNUMSAMPLES) { \
64 ALOGE( \
65 "\tLVM_ERROR : Parameter error - " \
66 "bad number of samples returned by %s in %s\n\n\n\n", \
67 callingFunc, calledFunc); \
68 } \
69 if ((LvmStatus) == LVM_OUTOFRANGE) { \
70 ALOGE( \
71 "\tLVM_ERROR : Parameter error - " \
72 "out of range returned by %s in %s\n", \
73 callingFunc, calledFunc); \
74 } \
75 }
76
77struct lvmConfigParams_t {
78 int samplingFreq = 44100;
79 int nrChannels = 2;
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +053080 int chMask = AUDIO_CHANNEL_OUT_STEREO;
81 int vcBal = 0;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +053082 int fChannels = 2;
Andy Hungb144b4b2018-12-20 14:15:49 -080083 bool monoMode = false;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +053084 int bassEffectLevel = 0;
85 int eqPresetLevel = 0;
86 int frameLength = 256;
87 LVM_BE_Mode_en bassEnable = LVM_BE_OFF;
88 LVM_TE_Mode_en trebleEnable = LVM_TE_OFF;
89 LVM_EQNB_Mode_en eqEnable = LVM_EQNB_OFF;
90 LVM_Mode_en csEnable = LVM_MODE_OFF;
91};
92
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +053093constexpr audio_channel_mask_t lvmConfigChMask[] = {
94 AUDIO_CHANNEL_OUT_MONO,
95 AUDIO_CHANNEL_OUT_STEREO,
96 AUDIO_CHANNEL_OUT_2POINT1,
97 AUDIO_CHANNEL_OUT_2POINT0POINT2,
98 AUDIO_CHANNEL_OUT_QUAD,
99 AUDIO_CHANNEL_OUT_QUAD_BACK,
100 AUDIO_CHANNEL_OUT_QUAD_SIDE,
101 AUDIO_CHANNEL_OUT_SURROUND,
102 (1 << 4) - 1,
103 AUDIO_CHANNEL_OUT_2POINT1POINT2,
104 AUDIO_CHANNEL_OUT_3POINT0POINT2,
105 AUDIO_CHANNEL_OUT_PENTA,
106 (1 << 5) - 1,
107 AUDIO_CHANNEL_OUT_3POINT1POINT2,
108 AUDIO_CHANNEL_OUT_5POINT1,
109 AUDIO_CHANNEL_OUT_5POINT1_BACK,
110 AUDIO_CHANNEL_OUT_5POINT1_SIDE,
111 (1 << 6) - 1,
112 AUDIO_CHANNEL_OUT_6POINT1,
113 (1 << 7) - 1,
114 AUDIO_CHANNEL_OUT_5POINT1POINT2,
115 AUDIO_CHANNEL_OUT_7POINT1,
116 (1 << 8) - 1,
117};
118
119
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530120void printUsage() {
121 printf("\nUsage: ");
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530122 printf("\n <executable> -i:<input_file> -o:<out_file> [options]\n");
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530123 printf("\nwhere, \n <inputfile> is the input file name");
124 printf("\n on which LVM effects are applied");
125 printf("\n <outputfile> processed output file");
126 printf("\n and options are mentioned below");
127 printf("\n");
128 printf("\n -help (or) -h");
129 printf("\n Prints this usage information");
130 printf("\n");
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530131 printf("\n -chMask:<channel_mask>\n");
132 printf("\n 0 - AUDIO_CHANNEL_OUT_MONO");
133 printf("\n 1 - AUDIO_CHANNEL_OUT_STEREO");
134 printf("\n 2 - AUDIO_CHANNEL_OUT_2POINT1");
135 printf("\n 3 - AUDIO_CHANNEL_OUT_2POINT0POINT2");
136 printf("\n 4 - AUDIO_CHANNEL_OUT_QUAD");
137 printf("\n 5 - AUDIO_CHANNEL_OUT_QUAD_BACK");
138 printf("\n 6 - AUDIO_CHANNEL_OUT_QUAD_SIDE");
139 printf("\n 7 - AUDIO_CHANNEL_OUT_SURROUND");
140 printf("\n 8 - canonical channel index mask for 4 ch: (1 << 4) - 1");
141 printf("\n 9 - AUDIO_CHANNEL_OUT_2POINT1POINT2");
142 printf("\n 10 - AUDIO_CHANNEL_OUT_3POINT0POINT2");
143 printf("\n 11 - AUDIO_CHANNEL_OUT_PENTA");
144 printf("\n 12 - canonical channel index mask for 5 ch: (1 << 5) - 1");
145 printf("\n 13 - AUDIO_CHANNEL_OUT_3POINT1POINT2");
146 printf("\n 14 - AUDIO_CHANNEL_OUT_5POINT1");
147 printf("\n 15 - AUDIO_CHANNEL_OUT_5POINT1_BACK");
148 printf("\n 16 - AUDIO_CHANNEL_OUT_5POINT1_SIDE");
149 printf("\n 17 - canonical channel index mask for 6 ch: (1 << 6) - 1");
150 printf("\n 18 - AUDIO_CHANNEL_OUT_6POINT1");
151 printf("\n 19 - canonical channel index mask for 7 ch: (1 << 7) - 1");
152 printf("\n 20 - AUDIO_CHANNEL_OUT_5POINT1POINT2");
153 printf("\n 21 - AUDIO_CHANNEL_OUT_7POINT1");
154 printf("\n 22 - canonical channel index mask for 8 ch: (1 << 8) - 1");
155 printf("\n default 0");
156 printf("\n -vcBal:<Left Right Balance control in dB [-96 to 96 dB]>");
157 printf("\n -ve values reduce Right channel while +ve value reduces Left channel");
158 printf("\n default 0");
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530159 printf("\n -fch:<file_channels> (1 through 8)\n\n");
Andy Hungb144b4b2018-12-20 14:15:49 -0800160 printf("\n -M");
161 printf("\n Mono mode (force all input audio channels to be identical)");
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530162 printf("\n -basslvl:<effect_level>");
Saketh Sathuvallicb398ab2019-02-16 13:12:59 +0530163 printf("\n A value that ranges between %d - %d default 0", LVM_BE_MIN_EFFECTLEVEL,
164 LVM_BE_MAX_EFFECTLEVEL);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530165 printf("\n");
166 printf("\n -eqPreset:<preset Value>");
Saketh Sathuvallicb398ab2019-02-16 13:12:59 +0530167 const size_t numPresetLvls = std::size(gEqualizerPresets);
168 for (size_t i = 0; i < numPresetLvls; ++i) {
169 printf("\n %zu - %s", i, gEqualizerPresets[i].name);
170 }
171 printf("\n default - 0");
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530172 printf("\n -bE ");
173 printf("\n Enable Dynamic Bass Enhancement");
174 printf("\n");
175 printf("\n -tE ");
176 printf("\n Enable Treble Boost");
177 printf("\n");
178 printf("\n -csE ");
179 printf("\n Enable Concert Surround");
180 printf("\n");
181 printf("\n -eqE ");
182 printf("\n Enable Equalizer");
183}
184
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530185
186//----------------------------------------------------------------------------
187// LvmBundle_init()
188//----------------------------------------------------------------------------
189// Purpose: Initialize engine with default configuration, creates instance
190// with all effects disabled.
191//
192// Inputs:
193// pContext: effect engine context
194//
195// Outputs:
196//
197//----------------------------------------------------------------------------
198
199int LvmBundle_init(struct EffectContext *pContext, LVM_ControlParams_t *params) {
200 ALOGV("\tLvmBundle_init start");
201
202 pContext->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
203 pContext->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
204 pContext->config.inputCfg.format = EFFECT_BUFFER_FORMAT;
205 pContext->config.inputCfg.samplingRate = 44100;
206 pContext->config.inputCfg.bufferProvider.getBuffer = NULL;
207 pContext->config.inputCfg.bufferProvider.releaseBuffer = NULL;
208 pContext->config.inputCfg.bufferProvider.cookie = NULL;
209 pContext->config.inputCfg.mask = EFFECT_CONFIG_ALL;
210 pContext->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
211 pContext->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
212 pContext->config.outputCfg.format = EFFECT_BUFFER_FORMAT;
213 pContext->config.outputCfg.samplingRate = 44100;
214 pContext->config.outputCfg.bufferProvider.getBuffer = NULL;
215 pContext->config.outputCfg.bufferProvider.releaseBuffer = NULL;
216 pContext->config.outputCfg.bufferProvider.cookie = NULL;
217 pContext->config.outputCfg.mask = EFFECT_CONFIG_ALL;
218
219 if (pContext->pBundledContext->hInstance != NULL) {
220 ALOGV(
221 "\tLvmBundle_init pContext->pBassBoost != NULL "
222 "-> Calling pContext->pBassBoost->free()");
Saketh Sathuvalli6d7fe7f2019-03-26 18:48:54 +0530223 LVM_DelInstanceHandle(&pContext->pBundledContext->hInstance);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530224
225 ALOGV(
226 "\tLvmBundle_init pContext->pBassBoost != NULL "
227 "-> Called pContext->pBassBoost->free()");
228 }
229
230 LVM_ReturnStatus_en LvmStatus = LVM_SUCCESS; /* Function call status */
231 LVM_InstParams_t InstParams; /* Instance parameters */
232 LVM_EQNB_BandDef_t BandDefs[MAX_NUM_BANDS]; /* Equaliser band definitions */
233 LVM_HeadroomParams_t HeadroomParams; /* Headroom parameters */
234 LVM_HeadroomBandDef_t HeadroomBandDef[LVM_HEADROOM_MAX_NBANDS];
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530235
236 /* Set the capabilities */
237 InstParams.BufferMode = LVM_UNMANAGED_BUFFERS;
238 InstParams.MaxBlockSize = MAX_CALL_SIZE;
239 InstParams.EQNB_NumBands = MAX_NUM_BANDS;
240 InstParams.PSA_Included = LVM_PSA_ON;
241
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530242 LvmStatus = LVM_GetInstanceHandle(&pContext->pBundledContext->hInstance,
Saketh Sathuvalli6d7fe7f2019-03-26 18:48:54 +0530243 &InstParams);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530244
245 LVM_ERROR_CHECK(LvmStatus, "LVM_GetInstanceHandle", "LvmBundle_init");
246 if (LvmStatus != LVM_SUCCESS) return -EINVAL;
247
248 ALOGV(
249 "\tLvmBundle_init CreateInstance Succesfully called "
250 "LVM_GetInstanceHandle\n");
251
252 /* Set the initial process parameters */
253 /* General parameters */
254 params->OperatingMode = LVM_MODE_ON;
255 params->SampleRate = LVM_FS_44100;
256 params->SourceFormat = LVM_STEREO;
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530257 params->ChMask = AUDIO_CHANNEL_OUT_STEREO;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530258 params->SpeakerType = LVM_HEADPHONES;
259
260 pContext->pBundledContext->SampleRate = LVM_FS_44100;
261
262 /* Concert Sound parameters */
263 params->VirtualizerOperatingMode = LVM_MODE_OFF;
264 params->VirtualizerType = LVM_CONCERTSOUND;
265 params->VirtualizerReverbLevel = 100;
266 params->CS_EffectLevel = LVM_CS_EFFECT_NONE;
267
268 /* N-Band Equaliser parameters */
269 params->EQNB_OperatingMode = LVM_EQNB_ON;
270 params->EQNB_NBands = FIVEBAND_NUMBANDS;
271 params->pEQNB_BandDefinition = &BandDefs[0];
272
273 for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
274 BandDefs[i].Frequency = EQNB_5BandPresetsFrequencies[i];
275 BandDefs[i].QFactor = EQNB_5BandPresetsQFactors[i];
276 BandDefs[i].Gain = EQNB_5BandSoftPresets[i];
277 }
278
279 /* Volume Control parameters */
280 params->VC_EffectLevel = 0;
281 params->VC_Balance = 0;
282
283 /* Treble Enhancement parameters */
284 params->TE_OperatingMode = LVM_TE_OFF;
285 params->TE_EffectLevel = 0;
286
287 /* PSA Control parameters */
288 params->PSA_Enable = LVM_PSA_OFF;
289 params->PSA_PeakDecayRate = (LVM_PSA_DecaySpeed_en)0;
290
291 /* Bass Enhancement parameters */
292 params->BE_OperatingMode = LVM_BE_ON;
293 params->BE_EffectLevel = 0;
294 params->BE_CentreFreq = LVM_BE_CENTRE_90Hz;
295 params->BE_HPF = LVM_BE_HPF_ON;
296
297 /* PSA Control parameters */
298 params->PSA_Enable = LVM_PSA_OFF;
299 params->PSA_PeakDecayRate = LVM_PSA_SPEED_MEDIUM;
300
301 /* TE Control parameters */
302 params->TE_OperatingMode = LVM_TE_OFF;
303 params->TE_EffectLevel = 0;
304
305 /* Activate the initial settings */
306 LvmStatus =
307 LVM_SetControlParameters(pContext->pBundledContext->hInstance, params);
308
309 LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmBundle_init");
310 if (LvmStatus != LVM_SUCCESS) return -EINVAL;
311
312 ALOGV(
313 "\tLvmBundle_init CreateInstance Succesfully called "
314 "LVM_SetControlParameters\n");
315
316 /* Set the headroom parameters */
317 HeadroomBandDef[0].Limit_Low = 20;
318 HeadroomBandDef[0].Limit_High = 4999;
319 HeadroomBandDef[0].Headroom_Offset = 0;
320 HeadroomBandDef[1].Limit_Low = 5000;
321 HeadroomBandDef[1].Limit_High = 24000;
322 HeadroomBandDef[1].Headroom_Offset = 0;
323 HeadroomParams.pHeadroomDefinition = &HeadroomBandDef[0];
324 HeadroomParams.Headroom_OperatingMode = LVM_HEADROOM_ON;
325 HeadroomParams.NHeadroomBands = 2;
326
327 LvmStatus = LVM_SetHeadroomParams(pContext->pBundledContext->hInstance,
328 &HeadroomParams);
329
330 LVM_ERROR_CHECK(LvmStatus, "LVM_SetHeadroomParams", "LvmBundle_init");
331 if (LvmStatus != LVM_SUCCESS) return -EINVAL;
332
333 ALOGV(
334 "\tLvmBundle_init CreateInstance Succesfully called "
335 "LVM_SetHeadroomParams\n");
336 ALOGV("\tLvmBundle_init End");
337 return 0;
338} /* end LvmBundle_init */
339
340int lvmCreate(struct EffectContext *pContext,
341 lvmConfigParams_t *plvmConfigParams,
342 LVM_ControlParams_t *params) {
343 int ret = 0;
344 pContext->pBundledContext = NULL;
345 pContext->pBundledContext = (BundledEffectContext *)malloc(sizeof(struct BundledEffectContext));
346 if (NULL == pContext->pBundledContext) {
347 return -EINVAL;
348 }
349
350 pContext->pBundledContext->SessionNo = 0;
351 pContext->pBundledContext->SessionId = 0;
352 pContext->pBundledContext->hInstance = NULL;
353 pContext->pBundledContext->bVolumeEnabled = LVM_FALSE;
354 pContext->pBundledContext->bEqualizerEnabled = LVM_FALSE;
355 pContext->pBundledContext->bBassEnabled = LVM_FALSE;
356 pContext->pBundledContext->bBassTempDisabled = LVM_FALSE;
357 pContext->pBundledContext->bVirtualizerEnabled = LVM_FALSE;
358 pContext->pBundledContext->bVirtualizerTempDisabled = LVM_FALSE;
359 pContext->pBundledContext->nOutputDevice = AUDIO_DEVICE_NONE;
360 pContext->pBundledContext->nVirtualizerForcedDevice = AUDIO_DEVICE_NONE;
361 pContext->pBundledContext->NumberEffectsEnabled = 0;
362 pContext->pBundledContext->NumberEffectsCalled = 0;
363 pContext->pBundledContext->firstVolume = LVM_TRUE;
364 pContext->pBundledContext->volume = 0;
365
366 /* Saved strength is used to return the exact strength that was used in the
367 * set to the get
368 * because we map the original strength range of 0:1000 to 1:15, and this will
369 * avoid
370 * quantisation like effect when returning
371 */
372 pContext->pBundledContext->BassStrengthSaved = 0;
373 pContext->pBundledContext->VirtStrengthSaved = 0;
374 pContext->pBundledContext->CurPreset = PRESET_CUSTOM;
375 pContext->pBundledContext->levelSaved = 0;
376 pContext->pBundledContext->bMuteEnabled = LVM_FALSE;
377 pContext->pBundledContext->bStereoPositionEnabled = LVM_FALSE;
378 pContext->pBundledContext->positionSaved = 0;
379 pContext->pBundledContext->workBuffer = NULL;
380 pContext->pBundledContext->frameCount = -1;
381 pContext->pBundledContext->SamplesToExitCountVirt = 0;
382 pContext->pBundledContext->SamplesToExitCountBb = 0;
383 pContext->pBundledContext->SamplesToExitCountEq = 0;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530384 for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
385 pContext->pBundledContext->bandGaindB[i] = EQNB_5BandSoftPresets[i];
386 }
387 pContext->config.inputCfg.channels = plvmConfigParams->nrChannels;
388 ALOGV("\tEffectCreate - Calling LvmBundle_init");
389 ret = LvmBundle_init(pContext, params);
390
391 if (ret < 0) {
392 ALOGE("\tLVM_ERROR : lvmCreate() Bundle init failed");
393 return ret;
394 }
395 return 0;
396}
397
398int lvmControl(struct EffectContext *pContext,
399 lvmConfigParams_t *plvmConfigParams,
400 LVM_ControlParams_t *params) {
401 LVM_ReturnStatus_en LvmStatus = LVM_SUCCESS; /* Function call status */
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530402
403 /* Set the initial process parameters */
404 /* General parameters */
405 params->OperatingMode = LVM_MODE_ON;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530406 params->SpeakerType = LVM_HEADPHONES;
407
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530408 params->ChMask = plvmConfigParams->chMask;
409 params->NrChannels = plvmConfigParams->nrChannels;
410 if (params->NrChannels == 1) {
Saketh Sathuvalli0c5015f2018-11-28 18:37:06 +0530411 params->SourceFormat = LVM_MONO;
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530412 } else if (params->NrChannels == 2) {
Saketh Sathuvalli0c5015f2018-11-28 18:37:06 +0530413 params->SourceFormat = LVM_STEREO;
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530414 } else if (params->NrChannels > 2 && params->NrChannels <= 8) { // FCC_2 FCC_8
Saketh Sathuvalli0c5015f2018-11-28 18:37:06 +0530415 params->SourceFormat = LVM_MULTICHANNEL;
416 } else {
417 return -EINVAL;
418 }
419
420 LVM_Fs_en sampleRate;
421 switch (plvmConfigParams->samplingFreq) {
422 case 8000:
423 sampleRate = LVM_FS_8000;
424 break;
425 case 11025:
426 sampleRate = LVM_FS_11025;
427 break;
428 case 12000:
429 sampleRate = LVM_FS_12000;
430 break;
431 case 16000:
432 sampleRate = LVM_FS_16000;
433 break;
434 case 22050:
435 sampleRate = LVM_FS_22050;
436 break;
437 case 24000:
438 sampleRate = LVM_FS_24000;
439 break;
440 case 32000:
441 sampleRate = LVM_FS_32000;
442 break;
443 case 44100:
444 sampleRate = LVM_FS_44100;
445 break;
446 case 48000:
447 sampleRate = LVM_FS_48000;
448 break;
449 case 88200:
450 sampleRate = LVM_FS_88200;
451 break;
452 case 96000:
453 sampleRate = LVM_FS_96000;
454 break;
455 case 176400:
456 sampleRate = LVM_FS_176400;
457 break;
458 case 192000:
459 sampleRate = LVM_FS_192000;
460 break;
461 default:
462 return -EINVAL;
463 }
464 params->SampleRate = sampleRate;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530465
466 /* Concert Sound parameters */
467 params->VirtualizerOperatingMode = plvmConfigParams->csEnable;
468 params->VirtualizerType = LVM_CONCERTSOUND;
469 params->VirtualizerReverbLevel = 100;
470 params->CS_EffectLevel = LVM_CS_EFFECT_NONE;
471
472 /* N-Band Equaliser parameters */
Saketh Sathuvalli0c5015f2018-11-28 18:37:06 +0530473 const int eqPresetLevel = plvmConfigParams->eqPresetLevel;
474 LVM_EQNB_BandDef_t BandDefs[MAX_NUM_BANDS]; /* Equaliser band definitions */
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530475 for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
476 BandDefs[i].Frequency = EQNB_5BandPresetsFrequencies[i];
477 BandDefs[i].QFactor = EQNB_5BandPresetsQFactors[i];
478 BandDefs[i].Gain =
479 EQNB_5BandSoftPresets[(FIVEBAND_NUMBANDS * eqPresetLevel) + i];
480 }
Saketh Sathuvalli0c5015f2018-11-28 18:37:06 +0530481 params->EQNB_OperatingMode = plvmConfigParams->eqEnable;
482 // Caution: raw pointer to stack data, stored in instance by LVM_SetControlParameters.
483 params->pEQNB_BandDefinition = &BandDefs[0];
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530484
485 /* Volume Control parameters */
486 params->VC_EffectLevel = 0;
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530487 params->VC_Balance = plvmConfigParams->vcBal;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530488
489 /* Treble Enhancement parameters */
490 params->TE_OperatingMode = plvmConfigParams->trebleEnable;
491
492 /* PSA Control parameters */
493 params->PSA_Enable = LVM_PSA_ON;
494
495 /* Bass Enhancement parameters */
496 params->BE_OperatingMode = plvmConfigParams->bassEnable;
497
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530498 /* Activate the initial settings */
499 LvmStatus =
500 LVM_SetControlParameters(pContext->pBundledContext->hInstance, params);
501
502 LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmBundle_init");
503 if (LvmStatus != LVM_SUCCESS) return -EINVAL;
504
505 LvmStatus = LVM_ApplyNewSettings(pContext->pBundledContext->hInstance);
506
507 if (LvmStatus != LVM_SUCCESS) return -EINVAL;
508
509 return 0;
510}
511
512int lvmExecute(float *floatIn, float *floatOut, struct EffectContext *pContext,
513 lvmConfigParams_t *plvmConfigParams) {
514 const int frameLength = plvmConfigParams->frameLength;
515 return
516 LVM_Process(pContext->pBundledContext->hInstance, /* Instance handle */
517 floatIn, /* Input buffer */
518 floatOut, /* Output buffer */
519 (LVM_UINT16)frameLength, /* Number of samples to read */
520 0); /* Audio Time */
521}
522
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530523int lvmMainProcess(EffectContext *pContext,
524 LVM_ControlParams_t *pParams,
525 lvmConfigParams_t *plvmConfigParams,
526 FILE *finp,
527 FILE *fout) {
528 int errCode = lvmControl(pContext, plvmConfigParams, pParams);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530529 if (errCode) {
530 ALOGE("Error: lvmControl returned with %d\n", errCode);
531 return errCode;
532 }
533
534 const int channelCount = plvmConfigParams->nrChannels;
535 const int frameLength = plvmConfigParams->frameLength;
536 const int frameSize = channelCount * sizeof(float); // processing size
537 const int ioChannelCount = plvmConfigParams->fChannels;
538 const int ioFrameSize = ioChannelCount * sizeof(short); // file load size
539 const int maxChannelCount = std::max(channelCount, ioChannelCount);
540 /*
541 * Mono input will be converted to 2 channels internally in the process call
542 * by copying the same data into the second channel.
543 * Hence when channelCount is 1, output buffer should be allocated for
544 * 2 channels. The memAllocChCount takes care of allocation of sufficient
545 * memory for the output buffer.
546 */
547 const int memAllocChCount = (channelCount == 1 ? 2 : channelCount);
548
549 std::vector<short> in(frameLength * maxChannelCount);
550 std::vector<short> out(frameLength * maxChannelCount);
551 std::vector<float> floatIn(frameLength * channelCount);
552 std::vector<float> floatOut(frameLength * memAllocChCount);
553
554 int frameCounter = 0;
555 while (fread(in.data(), ioFrameSize, frameLength, finp) == (size_t)frameLength) {
556 if (ioChannelCount != channelCount) {
557 adjust_channels(in.data(), ioChannelCount, in.data(), channelCount,
558 sizeof(short), frameLength * ioFrameSize);
559 }
560 memcpy_to_float_from_i16(floatIn.data(), in.data(), frameLength * channelCount);
561
Andy Hungb144b4b2018-12-20 14:15:49 -0800562 // Mono mode will replicate the first channel to all other channels.
563 // This ensures all audio channels are identical. This is useful for testing
564 // Bass Boost, which extracts a mono signal for processing.
565 if (plvmConfigParams->monoMode && channelCount > 1) {
566 for (int i = 0; i < frameLength; ++i) {
567 auto *fp = &floatIn[i * channelCount];
568 std::fill(fp + 1, fp + channelCount, *fp); // replicate ch 0
569 }
570 }
Saketh Sathuvallicb398ab2019-02-16 13:12:59 +0530571#ifndef BYPASS_EXEC
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530572 errCode = lvmExecute(floatIn.data(), floatOut.data(), pContext, plvmConfigParams);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530573 if (errCode) {
574 printf("\nError: lvmExecute returned with %d\n", errCode);
575 return errCode;
576 }
577
578 (void)frameSize; // eliminate warning
579#else
580 memcpy(floatOut.data(), floatIn.data(), frameLength * frameSize);
581#endif
582 memcpy_to_i16_from_float(out.data(), floatOut.data(), frameLength * channelCount);
583 if (ioChannelCount != channelCount) {
584 adjust_channels(out.data(), channelCount, out.data(), ioChannelCount,
585 sizeof(short), frameLength * channelCount * sizeof(short));
586 }
587 (void) fwrite(out.data(), ioFrameSize, frameLength, fout);
588 frameCounter += frameLength;
589 }
590 printf("frameCounter: [%d]\n", frameCounter);
591 return 0;
592}
593
594int main(int argc, const char *argv[]) {
595 if (argc == 1) {
596 printUsage();
597 return -1;
598 }
599
600 lvmConfigParams_t lvmConfigParams{}; // default initialize
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530601 const char *infile = nullptr;
602 const char *outfile = nullptr;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530603
604 for (int i = 1; i < argc; i++) {
605 printf("%s ", argv[i]);
606 if (!strncmp(argv[i], "-i:", 3)) {
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530607 infile = argv[i] + 3;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530608 } else if (!strncmp(argv[i], "-o:", 3)) {
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530609 outfile = argv[i] + 3;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530610 } else if (!strncmp(argv[i], "-fs:", 4)) {
611 const int samplingFreq = atoi(argv[i] + 4);
612 if (samplingFreq != 8000 && samplingFreq != 11025 &&
613 samplingFreq != 12000 && samplingFreq != 16000 &&
614 samplingFreq != 22050 && samplingFreq != 24000 &&
615 samplingFreq != 32000 && samplingFreq != 44100 &&
Saketh Sathuvalli0c5015f2018-11-28 18:37:06 +0530616 samplingFreq != 48000 && samplingFreq != 88200 &&
617 samplingFreq != 96000 && samplingFreq != 176400 &&
618 samplingFreq != 192000) {
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530619 printf("Error: Unsupported Sampling Frequency : %d\n", samplingFreq);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530620 return -1;
621 }
622 lvmConfigParams.samplingFreq = samplingFreq;
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530623 } else if (!strncmp(argv[i], "-chMask:", 8)) {
624 const int chMaskConfigIdx = atoi(argv[i] + 8);
625 if (chMaskConfigIdx < 0 || (size_t)chMaskConfigIdx >= std::size(lvmConfigChMask)) {
626 ALOGE("\nError: Unsupported Channel Mask : %d\n", chMaskConfigIdx);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530627 return -1;
628 }
Saketh Sathuvalli7b66e3b2019-02-08 15:18:54 +0530629 const audio_channel_mask_t chMask = lvmConfigChMask[chMaskConfigIdx];
630 lvmConfigParams.chMask = chMask;
631 lvmConfigParams.nrChannels = audio_channel_count_from_out_mask(chMask);
632 } else if (!strncmp(argv[i], "-vcBal:", 7)) {
633 const int vcBalance = atoi(argv[i] + 7);
634 if (vcBalance > 96 || vcBalance < -96) {
635 ALOGE("\nError: Unsupported volume balance value: %d\n", vcBalance);
636 }
637 lvmConfigParams.vcBal = vcBalance;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530638 } else if (!strncmp(argv[i], "-fch:", 5)) {
639 const int fChannels = atoi(argv[i] + 5);
640 if (fChannels > 8 || fChannels < 1) {
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530641 printf("Error: Unsupported number of file channels : %d\n", fChannels);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530642 return -1;
643 }
644 lvmConfigParams.fChannels = fChannels;
Andy Hungb144b4b2018-12-20 14:15:49 -0800645 } else if (!strcmp(argv[i],"-M")) {
646 lvmConfigParams.monoMode = true;
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530647 } else if (!strncmp(argv[i], "-basslvl:", 9)) {
648 const int bassEffectLevel = atoi(argv[i] + 9);
Saketh Sathuvallicb398ab2019-02-16 13:12:59 +0530649 if (bassEffectLevel > LVM_BE_MAX_EFFECTLEVEL || bassEffectLevel < LVM_BE_MIN_EFFECTLEVEL) {
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530650 printf("Error: Unsupported Bass Effect Level : %d\n",
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530651 bassEffectLevel);
652 printUsage();
653 return -1;
654 }
655 lvmConfigParams.bassEffectLevel = bassEffectLevel;
656 } else if (!strncmp(argv[i], "-eqPreset:", 10)) {
657 const int eqPresetLevel = atoi(argv[i] + 10);
Saketh Sathuvallicb398ab2019-02-16 13:12:59 +0530658 const int numPresetLvls = std::size(gEqualizerPresets);
659 if (eqPresetLevel >= numPresetLvls || eqPresetLevel < 0) {
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530660 printf("Error: Unsupported Equalizer Preset : %d\n", eqPresetLevel);
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530661 printUsage();
662 return -1;
663 }
664 lvmConfigParams.eqPresetLevel = eqPresetLevel;
665 } else if (!strcmp(argv[i], "-bE")) {
666 lvmConfigParams.bassEnable = LVM_BE_ON;
667 } else if (!strcmp(argv[i], "-eqE")) {
668 lvmConfigParams.eqEnable = LVM_EQNB_ON;
669 } else if (!strcmp(argv[i], "-tE")) {
670 lvmConfigParams.trebleEnable = LVM_TE_ON;
671 } else if (!strcmp(argv[i], "-csE")) {
672 lvmConfigParams.csEnable = LVM_MODE_ON;
673 } else if (!strcmp(argv[i], "-h")) {
674 printUsage();
675 return 0;
676 }
677 }
678
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530679 if (infile == nullptr || outfile == nullptr) {
680 printf("Error: missing input/output files\n");
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530681 printUsage();
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530682 return -1;
683 }
684
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530685 FILE *finp = fopen(infile, "rb");
686 if (finp == nullptr) {
687 printf("Cannot open input file %s", infile);
688 return -1;
689 }
690
691 FILE *fout = fopen(outfile, "wb");
692 if (fout == nullptr) {
693 printf("Cannot open output file %s", outfile);
694 fclose(finp);
695 return -1;
696 }
697
698 EffectContext context;
699 LVM_ControlParams_t params;
700 int errCode = lvmCreate(&context, &lvmConfigParams, &params);
701 if (errCode == 0) {
702 errCode = lvmMainProcess(&context, &params, &lvmConfigParams, finp, fout);
703 if (errCode != 0) {
704 printf("Error: lvmMainProcess returned with the error: %d",errCode);
705 }
706 } else {
707 printf("Error: lvmCreate returned with the error: %d", errCode);
708 }
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530709 fclose(finp);
710 fclose(fout);
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530711 /* Free the allocated buffers */
712 if (context.pBundledContext != nullptr) {
713 if (context.pBundledContext->hInstance != nullptr) {
Saketh Sathuvalli6d7fe7f2019-03-26 18:48:54 +0530714 LVM_DelInstanceHandle(&context.pBundledContext->hInstance);
Saketh Sathuvalliab18bac2019-01-24 17:15:10 +0530715 }
716 free(context.pBundledContext);
717 }
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530718
719 if (errCode) {
Saketh Sathuvallib99e1bc2018-02-21 17:10:34 +0530720 return -1;
721 }
722 return 0;
723}