forked from dazjo/android_device_huawei_u8815
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AudioHardware.cpp
2357 lines (2063 loc) · 76.4 KB
/
AudioHardware.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright 2008, The Android Open-Source Project
** Copyright (c) 2011, Code Aurora Forum. All rights reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <math.h>
//#define LOG_NDEBUG 0
#define LOG_TAG "AudioHardwareMSM76XXA"
#include <utils/Log.h>
#include <utils/String8.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <cutils/properties.h> // for property_get
// hardware specific functions
#include "AudioHardware.h"
extern "C" {
#include "HardwarePinSwitching.h"
}
//#include <media/AudioRecord.h>
#define COMBO_DEVICE_SUPPORTED // Headset speaker combo device not supported on this target
#define DUALMIC_KEY "dualmic_enabled"
#define TTY_MODE_KEY "tty_mode"
namespace android_audio_legacy {
static int audpre_index, tx_iir_index;
static void * acoustic;
const uint32_t AudioHardware::inputSamplingRates[] = {
8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
};
static int get_audpp_filter(void);
static int msm72xx_enable_postproc(bool state);
static int msm72xx_enable_preproc(bool state);
// Post processing paramters
static struct rx_iir_filter iir_cfg[3];
static struct adrc_filter adrc_cfg[3];
static struct mbadrc_filter mbadrc_cfg[3];
eqalizer eqalizer[3];
static uint16_t adrc_flag[3];
static uint16_t mbadrc_flag[3];
static uint16_t eq_flag[3];
static uint16_t rx_iir_flag[3];
static uint16_t agc_flag[3];
static uint16_t ns_flag[3];
static uint16_t txiir_flag[3];
static bool audpp_filter_inited = false;
static bool adrc_filter_exists[3];
static bool mbadrc_filter_exists[3];
static int post_proc_feature_mask = 0;
static bool playback_in_progress = false;
//Pre processing parameters
static struct tx_iir tx_iir_cfg[9];
static struct ns ns_cfg[9];
static struct tx_agc tx_agc_cfg[9];
static int enable_preproc_mask[9];
static int snd_device = -1;
#define PCM_OUT_DEVICE "/dev/msm_pcm_out"
#define PCM_IN_DEVICE "/dev/msm_pcm_in"
#define PCM_CTL_DEVICE "/dev/msm_pcm_ctl"
#define PREPROC_CTL_DEVICE "/dev/msm_preproc_ctl"
#define VOICE_MEMO_DEVICE "/dev/msm_voicememo"
#ifdef FM_RADIO
#define FM_DEVICE "/dev/msm_fm"
#endif
#define BTHEADSET_VGS "bt_headset_vgs"
static uint32_t SND_DEVICE_CURRENT=-1;
static uint32_t SND_DEVICE_HANDSET=-1;
static uint32_t SND_DEVICE_SPEAKER=-1;
static uint32_t SND_DEVICE_BT=-1;
static uint32_t SND_DEVICE_BT_EC_OFF=-1;
static uint32_t SND_DEVICE_HEADSET=-1;
static uint32_t SND_DEVICE_STEREO_HEADSET_AND_SPEAKER=-1;
static uint32_t SND_DEVICE_IN_S_SADC_OUT_HANDSET=-1;
static uint32_t SND_DEVICE_IN_S_SADC_OUT_SPEAKER_PHONE=-1;
static uint32_t SND_DEVICE_TTY_HEADSET=-1;
static uint32_t SND_DEVICE_TTY_HCO=-1;
static uint32_t SND_DEVICE_TTY_VCO=-1;
static uint32_t SND_DEVICE_CARKIT=-1;
static uint32_t SND_DEVICE_FM_SPEAKER=-1;
static uint32_t SND_DEVICE_FM_HEADSET=-1;
static uint32_t SND_DEVICE_NO_MIC_HEADSET=-1;
static uint32_t SND_DEVICE_FM_DIGITAL_STEREO_HEADSET=-1;
static uint32_t SND_DEVICE_FM_DIGITAL_SPEAKER_PHONE=-1;
static uint32_t SND_DEVICE_FM_DIGITAL_BT_A2DP_HEADSET=-1;
static uint32_t SND_DEVICE_FM_ANALOG_STEREO_HEADSET=-1;
static uint32_t SND_DEVICE_FM_ANALOG_STEREO_HEADSET_CODEC=-1;
// ----------------------------------------------------------------------------
AudioHardware::AudioHardware() :
mInit(false), mMicMute(true), mBluetoothNrec(true), mBluetoothId(0),
mOutput(0), mBluetoothVGS(false), mSndEndpoints(NULL), mCurSndDevice(-1),
mDualMicEnabled(false), mFmFd(-1), FmA2dpStatus(-1)
{
if (get_audpp_filter() == 0) {
audpp_filter_inited = true;
}
m7xsnddriverfd = open("/dev/msm_snd", O_RDWR);
if (m7xsnddriverfd >= 0) {
int rc = ioctl(m7xsnddriverfd, SND_GET_NUM_ENDPOINTS, &mNumSndEndpoints);
if (rc >= 0) {
mSndEndpoints = new msm_snd_endpoint[mNumSndEndpoints];
mInit = true;
LOGV("constructed (%d SND endpoints)", rc);
struct msm_snd_endpoint *ept = mSndEndpoints;
for (int cnt = 0; cnt < mNumSndEndpoints; cnt++, ept++) {
ept->id = cnt;
ioctl(m7xsnddriverfd, SND_GET_ENDPOINT, ept);
LOGV("cnt = %d ept->name = %s ept->id = %d\n", cnt, ept->name, ept->id);
#define CHECK_FOR(desc) if (!strcmp(ept->name, #desc)) SND_DEVICE_##desc = ept->id;
CHECK_FOR(CURRENT);
CHECK_FOR(HANDSET);
CHECK_FOR(SPEAKER);
CHECK_FOR(BT);
CHECK_FOR(BT_EC_OFF);
CHECK_FOR(HEADSET);
CHECK_FOR(STEREO_HEADSET_AND_SPEAKER);
CHECK_FOR(IN_S_SADC_OUT_HANDSET);
CHECK_FOR(IN_S_SADC_OUT_SPEAKER_PHONE);
CHECK_FOR(TTY_HEADSET);
CHECK_FOR(TTY_HCO);
CHECK_FOR(TTY_VCO);
CHECK_FOR(FM_DIGITAL_STEREO_HEADSET);
CHECK_FOR(FM_DIGITAL_SPEAKER_PHONE);
CHECK_FOR(FM_DIGITAL_BT_A2DP_HEADSET);
CHECK_FOR(FM_ANALOG_STEREO_HEADSET);
CHECK_FOR(FM_ANALOG_STEREO_HEADSET_CODEC);
#undef CHECK_FOR
}
}
else LOGE("Could not retrieve number of MSM SND endpoints.");
int AUTO_VOLUME_ENABLED = 1; // setting enabled as default
static const char *const path = "/system/etc/AutoVolumeControl.txt";
int txtfd;
struct stat st;
char *read_buf;
txtfd = open(path, O_RDONLY);
if (txtfd < 0) {
LOGE("failed to open AUTO_VOLUME_CONTROL %s: %s (%d)",
path, strerror(errno), errno);
}
else {
if (fstat(txtfd, &st) < 0) {
LOGE("failed to stat %s: %s (%d)",
path, strerror(errno), errno);
close(txtfd);
}
read_buf = (char *) mmap(0, st.st_size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE,
txtfd, 0);
if (read_buf == MAP_FAILED) {
LOGE("failed to mmap parameters file: %s (%d)",
strerror(errno), errno);
close(txtfd);
}
if(read_buf[0] =='0')
AUTO_VOLUME_ENABLED = 0;
munmap(read_buf, st.st_size);
close(txtfd);
}
ioctl(m7xsnddriverfd, SND_AVC_CTL, &AUTO_VOLUME_ENABLED);
ioctl(m7xsnddriverfd, SND_AGC_CTL, &AUTO_VOLUME_ENABLED);
} else
LOGE("Could not open MSM SND driver.");
}
AudioHardware::~AudioHardware()
{
for (size_t index = 0; index < mInputs.size(); index++) {
closeInputStream((AudioStreamIn*)mInputs[index]);
}
mInputs.clear();
closeOutputStream((AudioStreamOut*)mOutput);
delete [] mSndEndpoints;
if (acoustic) {
::dlclose(acoustic);
acoustic = 0;
}
if (m7xsnddriverfd > 0)
{
close(m7xsnddriverfd);
m7xsnddriverfd = -1;
}
for (int index = 0; index < 9; index++) {
enable_preproc_mask[index] = 0;
}
mInit = false;
}
status_t AudioHardware::initCheck()
{
return mInit ? NO_ERROR : NO_INIT;
}
AudioStreamOut* AudioHardware::openOutputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
{
{ // scope for the lock
Mutex::Autolock lock(mLock);
// only one output stream allowed
if (mOutput) {
if (status) {
*status = INVALID_OPERATION;
}
return 0;
}
// create new output stream
AudioStreamOutMSM72xx* out = new AudioStreamOutMSM72xx();
status_t lStatus = out->set(this, devices, format, channels, sampleRate);
if (status) {
*status = lStatus;
}
if (lStatus == NO_ERROR) {
mOutput = out;
} else {
delete out;
}
}
return mOutput;
}
void AudioHardware::closeOutputStream(AudioStreamOut* out) {
Mutex::Autolock lock(mLock);
if (mOutput == 0 || mOutput != out) {
LOGW("Attempt to close invalid output stream");
}
else {
delete mOutput;
mOutput = 0;
}
}
AudioStreamIn* AudioHardware::openInputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status,
AudioSystem::audio_in_acoustics acoustic_flags)
{
// check for valid input source
if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices)) {
return 0;
}
if ( (mMode == AudioSystem::MODE_IN_CALL) &&
(getInputSampleRate(*sampleRate) > AUDIO_HW_IN_SAMPLERATE) &&
(*format == AUDIO_HW_IN_FORMAT) )
{
LOGE("PCM recording, in a voice call, with sample rate more than 8K not supported \
re-configure with 8K and try software re-sampler ");
*status = BAD_VALUE;
*sampleRate = AUDIO_HW_IN_SAMPLERATE;
return 0;
}
mLock.lock();
AudioStreamInMSM72xx* in = new AudioStreamInMSM72xx();
status_t lStatus = in->set(this, devices, format, channels, sampleRate, acoustic_flags);
if (status) {
*status = lStatus;
}
if (lStatus != NO_ERROR) {
mLock.unlock();
delete in;
return 0;
}
mInputs.add(in);
mLock.unlock();
return in;
}
void AudioHardware::closeInputStream(AudioStreamIn* in) {
Mutex::Autolock lock(mLock);
ssize_t index = mInputs.indexOf((AudioStreamInMSM72xx *)in);
if (index < 0) {
LOGW("Attempt to close invalid input stream");
} else {
mLock.unlock();
delete mInputs[index];
mLock.lock();
mInputs.removeAt(index);
}
}
status_t AudioHardware::setMode(int mode)
{
status_t status = AudioHardwareBase::setMode(mode);
if (status == NO_ERROR) {
// make sure that doAudioRouteOrMute() is called by doRouting()
// even if the new device selected is the same as current one.
clearCurDevice();
}
return status;
}
bool AudioHardware::checkOutputStandby()
{
if (mOutput)
if (!mOutput->checkStandby())
return false;
return true;
}
status_t AudioHardware::setMicMute(bool state)
{
Mutex::Autolock lock(mLock);
return setMicMute_nosync(state);
}
// always call with mutex held
status_t AudioHardware::setMicMute_nosync(bool state)
{
if (mMicMute != state) {
mMicMute = state;
return doAudioRouteOrMute(SND_DEVICE_CURRENT);
}
return NO_ERROR;
}
status_t AudioHardware::getMicMute(bool* state)
{
*state = mMicMute;
return NO_ERROR;
}
status_t AudioHardware::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 value;
String8 key;
const char BT_NREC_KEY[] = "bt_headset_nrec";
const char BT_NAME_KEY[] = "bt_headset_name";
const char BT_NREC_VALUE_ON[] = "on";
LOGV("setParameters() %s", keyValuePairs.string());
if (keyValuePairs.length() == 0) return BAD_VALUE;
key = String8(BT_NREC_KEY);
if (param.get(key, value) == NO_ERROR) {
if (value == BT_NREC_VALUE_ON) {
mBluetoothNrec = true;
} else {
mBluetoothNrec = false;
LOGI("Turning noise reduction and echo cancellation off for BT "
"headset");
}
}
key = String8(BTHEADSET_VGS);
if (param.get(key, value) == NO_ERROR) {
if (value == BT_NREC_VALUE_ON) {
mBluetoothVGS = true;
} else {
mBluetoothVGS = false;
}
}
key = String8(BT_NAME_KEY);
if (param.get(key, value) == NO_ERROR) {
mBluetoothId = 0;
for (int i = 0; i < mNumSndEndpoints; i++) {
if (!strcasecmp(value.string(), mSndEndpoints[i].name)) {
mBluetoothId = mSndEndpoints[i].id;
LOGI("Using custom acoustic parameters for %s", value.string());
break;
}
}
if (mBluetoothId == 0) {
LOGI("Using default acoustic parameters "
"(%s not in acoustic database)", value.string());
doRouting(NULL);
}
}
key = String8(DUALMIC_KEY);
if (param.get(key, value) == NO_ERROR) {
if (value == "true") {
mDualMicEnabled = true;
LOGI("DualMike feature Enabled");
} else {
mDualMicEnabled = false;
LOGI("DualMike feature Disabled");
}
doRouting(NULL);
}
key = String8(TTY_MODE_KEY);
if (param.get(key, value) == NO_ERROR) {
if (value == "full") {
mTtyMode = TTY_FULL;
} else if (value == "hco") {
mTtyMode = TTY_HCO;
} else if (value == "vco") {
mTtyMode = TTY_VCO;
} else {
mTtyMode = TTY_OFF;
}
if(mMode != AudioSystem::MODE_IN_CALL){
return NO_ERROR;
}
doRouting(NULL);
}
return NO_ERROR;
}
String8 AudioHardware::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
String8 value;
String8 key = String8(DUALMIC_KEY);
if (param.get(key, value) == NO_ERROR) {
value = String8(mDualMicEnabled ? "true" : "false");
param.add(key, value);
}
key = String8(BTHEADSET_VGS);
if (param.get(key, value) == NO_ERROR) {
if(mBluetoothVGS)
param.addInt(String8("isVGS"), true);
}
#ifdef FM_RADIO
key = String8("Fm-radio");
if ( param.get(key,value) == NO_ERROR ) {
if (IsFmon()||(mCurSndDevice == SND_DEVICE_FM_ANALOG_STEREO_HEADSET)){
param.addInt(String8("isFMON"), true );
}
}
#endif
LOGV("AudioHardware::getParameters() %s", param.toString().string());
return param.toString();
}
int check_and_set_audpp_parameters(char *buf, int size)
{
char *p, *ps;
static const char *const seps = ",";
int table_num;
int i, j;
int device_id = 0;
int samp_index = 0;
eq_filter_type eq[12];
int fd;
void *audioeq;
void *(*eq_cal)(int32_t, int32_t, int32_t, uint16_t, int32_t, int32_t *, int32_t *, uint16_t *);
uint16_t numerator[6];
uint16_t denominator[4];
uint16_t shift[2];
if ((buf[0] == 'A') && ((buf[1] == '1') || (buf[1] == '2') || (buf[1] == '3'))) {
/* IIR filter */
if(buf[1] == '1') device_id=0;
if(buf[1] == '2') device_id=1;
if(buf[1] == '3') device_id=2;
if (!(p = strtok(buf, ",")))
goto token_err;
/* Table header */
table_num = strtol(p + 1, &ps, 10);
if (!(p = strtok(NULL, seps)))
goto token_err;
/* Table description */
if (!(p = strtok(NULL, seps)))
goto token_err;
for (i = 0; i < 48; i++) {
iir_cfg[device_id].iir_params[i] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
}
rx_iir_flag[device_id] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
iir_cfg[device_id].num_bands = (uint16_t)strtol(p, &ps, 16);
} else if ((buf[0] == 'B') && ((buf[1] == '1') || (buf[1] == '2') || (buf[1] == '3'))) {
/* This is the ADRC record we are looking for. Tokenize it */
if(buf[1] == '1') device_id=0;
if(buf[1] == '2') device_id=1;
if(buf[1] == '3') device_id=2;
adrc_filter_exists[device_id] = true;
if (!(p = strtok(buf, ",")))
goto token_err;
/* Table header */
table_num = strtol(p + 1, &ps, 10);
if (!(p = strtok(NULL, seps)))
goto token_err;
/* Table description */
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_flag[device_id] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_cfg[device_id].adrc_params[0] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_cfg[device_id].adrc_params[1] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_cfg[device_id].adrc_params[2] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_cfg[device_id].adrc_params[3] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_cfg[device_id].adrc_params[4] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_cfg[device_id].adrc_params[5] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_cfg[device_id].adrc_params[6] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
adrc_cfg[device_id].adrc_params[7] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
} else if (buf[0] == 'C' && ((buf[1] == '1') || (buf[1] == '2') || (buf[1] == '3'))) {
/* This is the EQ record we are looking for. Tokenize it */
if(buf[1] == '1') device_id=0;
if(buf[1] == '2') device_id=1;
if(buf[1] == '3') device_id=2;
if (!(p = strtok(buf, ",")))
goto token_err;
/* Table header */
table_num = strtol(p + 1, &ps, 10);
if (!(p = strtok(NULL, seps)))
goto token_err;
/* Table description */
if (!(p = strtok(NULL, seps)))
goto token_err;
eq_flag[device_id] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
LOGI("EQ flag = %02x.", eq_flag[device_id]);
audioeq = ::dlopen("/system/lib/libaudioeq.so", RTLD_NOW);
if (audioeq == NULL) {
LOGE("audioeq library open failure");
return -1;
}
eq_cal = (void *(*) (int32_t, int32_t, int32_t, uint16_t, int32_t, int32_t *, int32_t *, uint16_t *))::dlsym(audioeq, "audioeq_calccoefs");
memset(&eqalizer[device_id], 0, sizeof(eqalizer));
/* Temp add the bands here */
eqalizer[device_id].bands = 8;
for (i = 0; i < eqalizer[device_id].bands; i++) {
eq[i].gain = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
eq[i].freq = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
eq[i].type = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
eq[i].qf = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
eq_cal(eq[i].gain, eq[i].freq, 48000, eq[i].type, eq[i].qf, (int32_t*)numerator, (int32_t *)denominator, shift);
for (j = 0; j < 6; j++) {
eqalizer[device_id].params[ ( i * 6) + j] = numerator[j];
}
for (j = 0; j < 4; j++) {
eqalizer[device_id].params[(eqalizer[device_id].bands * 6) + (i * 4) + j] = denominator[j];
}
eqalizer[device_id].params[(eqalizer[device_id].bands * 10) + i] = shift[0];
}
::dlclose(audioeq);
} else if ((buf[0] == 'D') && ((buf[1] == '1') || (buf[1] == '2') || (buf[1] == '3'))) {
/* This is the MB_ADRC record we are looking for. Tokenize it */
if(buf[1] == '1') device_id=0;
if(buf[1] == '2') device_id=1;
if(buf[1] == '3') device_id=2;
mbadrc_filter_exists[device_id] = true;
if (!(p = strtok(buf, ",")))
goto token_err;
/* Table header */
table_num = strtol(p + 1, &ps, 10);
if (!(p = strtok(NULL, seps)))
goto token_err;
/* Table description */
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].num_bands = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].down_samp_level = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].adrc_delay = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].ext_buf_size = (uint16_t)strtol(p, &ps, 16);
int ext_buf_count = mbadrc_cfg[device_id].ext_buf_size / 2;
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].ext_partition = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].ext_buf_msw = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].ext_buf_lsw = (uint16_t)strtol(p, &ps, 16);
for(i = 0;i < mbadrc_cfg[device_id].num_bands; i++) {
for(j = 0; j < 10; j++) {
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].adrc_band[i].adrc_band_params[j] = (uint16_t)strtol(p, &ps, 16);
}
}
for(i = 0;i < mbadrc_cfg[device_id].ext_buf_size/2; i++) {
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_cfg[device_id].ext_buf.buff[i] = (uint16_t)strtol(p, &ps, 16);
}
if (!(p = strtok(NULL, seps)))
goto token_err;
mbadrc_flag[device_id] = (uint16_t)strtol(p, &ps, 16);
LOGV("MBADRC flag = %02x.", mbadrc_flag[device_id]);
}else if ((buf[0] == 'E') || (buf[0] == 'F') || (buf[0] == 'G')){
//Pre-Processing Features TX_IIR,NS,AGC
switch (buf[1]) {
case '1':
samp_index = 0;
break;
case '2':
samp_index = 1;
break;
case '3':
samp_index = 2;
break;
case '4':
samp_index = 3;
break;
case '5':
samp_index = 4;
break;
case '6':
samp_index = 5;
break;
case '7':
samp_index = 6;
break;
case '8':
samp_index = 7;
break;
case '9':
samp_index = 8;
break;
default:
return -EINVAL;
break;
}
if (buf[0] == 'E') {
/* TX_IIR filter */
if (!(p = strtok(buf, ","))){
goto token_err;}
/* Table header */
table_num = strtol(p + 1, &ps, 10);
if (!(p = strtok(NULL, seps))){
goto token_err;}
/* Table description */
if (!(p = strtok(NULL, seps))){
goto token_err;}
for (i = 0; i < 48; i++) {
j = (i >= 40)? i : ((i % 2)? (i - 1) : (i + 1));
tx_iir_cfg[samp_index].iir_params[j] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps))){
goto token_err;}
}
tx_iir_cfg[samp_index].active_flag = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps))){
goto token_err;}
txiir_flag[device_id] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
tx_iir_cfg[samp_index].num_bands = (uint16_t)strtol(p, &ps, 16);
tx_iir_cfg[samp_index].cmd_id = 0;
LOGV("TX IIR flag = %02x.", txiir_flag[device_id]);
if (txiir_flag[device_id] != 0)
enable_preproc_mask[samp_index] |= TX_IIR_ENABLE;
} else if(buf[0] == 'F') {
/* AGC filter */
if (!(p = strtok(buf, ",")))
goto token_err;
/* Table header */
table_num = strtol(p + 1, &ps, 10);
if (!(p = strtok(NULL, seps)))
goto token_err;
/* Table description */
if (!(p = strtok(NULL, seps)))
goto token_err;
tx_agc_cfg[samp_index].cmd_id = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
tx_agc_cfg[samp_index].tx_agc_param_mask = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
tx_agc_cfg[samp_index].tx_agc_enable_flag = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
tx_agc_cfg[samp_index].static_gain = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
tx_agc_cfg[samp_index].adaptive_gain_flag = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
for (i = 0; i < 19; i++) {
tx_agc_cfg[samp_index].agc_params[i] = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
}
agc_flag[device_id] = (uint16_t)strtol(p, &ps, 16);
LOGV("AGC flag = %02x.", agc_flag[device_id]);
if (agc_flag[device_id] != 0)
enable_preproc_mask[samp_index] |= AGC_ENABLE;
} else if ((buf[0] == 'G')) {
/* This is the NS record we are looking for. Tokenize it */
if (!(p = strtok(buf, ",")))
goto token_err;
/* Table header */
table_num = strtol(p + 1, &ps, 10);
if (!(p = strtok(NULL, seps)))
goto token_err;
/* Table description */
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_cfg[samp_index].cmd_id = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_cfg[samp_index].ec_mode_new = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_cfg[samp_index].dens_gamma_n = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_cfg[samp_index].dens_nfe_block_size = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_cfg[samp_index].dens_limit_ns = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_cfg[samp_index].dens_limit_ns_d = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_cfg[samp_index].wb_gamma_e = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_cfg[samp_index].wb_gamma_n = (uint16_t)strtol(p, &ps, 16);
if (!(p = strtok(NULL, seps)))
goto token_err;
ns_flag[device_id] = (uint16_t)strtol(p, &ps, 16);
LOGV("NS flag = %02x.", ns_flag[device_id]);
if (ns_flag[device_id] != 0)
enable_preproc_mask[samp_index] |= NS_ENABLE;
}
}
return 0;
token_err:
LOGE("malformatted pcm control buffer");
return -EINVAL;
}
static int get_audpp_filter(void)
{
struct stat st;
char *read_buf;
char *next_str, *current_str;
int csvfd;
LOGI("get_audpp_filter");
static const char *const path =
"/system/etc/AudioFilter.csv";
csvfd = open(path, O_RDONLY);
if (csvfd < 0) {
/* failed to open normal acoustic file ... */
LOGE("failed to open AUDIO_NORMAL_FILTER %s: %s (%d).",
path, strerror(errno), errno);
return -1;
} else LOGI("open %s success.", path);
if (fstat(csvfd, &st) < 0) {
LOGE("failed to stat %s: %s (%d).",
path, strerror(errno), errno);
close(csvfd);
return -1;
}
read_buf = (char *) mmap(0, st.st_size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE,
csvfd, 0);
if (read_buf == MAP_FAILED) {
LOGE("failed to mmap parameters file: %s (%d)",
strerror(errno), errno);
close(csvfd);
return -1;
}
current_str = read_buf;
while (*current_str != (char)EOF) {
int len;
next_str = strchr(current_str, '\n');
if (!next_str)
break;
len = next_str - current_str;
*next_str++ = '\0';
if (check_and_set_audpp_parameters(current_str, len)) {
LOGI("failed to set audpp parameters, exiting.");
munmap(read_buf, st.st_size);
close(csvfd);
return -1;
}
current_str = next_str;
}
munmap(read_buf, st.st_size);
close(csvfd);
return 0;
}
static int msm72xx_enable_postproc(bool state)
{
int fd;
int device_id=0;
if (!audpp_filter_inited)
{
LOGE("Parsing error in AudioFilter.csv.");
return -EINVAL;
}
if(snd_device < 0) {
LOGE("Enabling/Disabling post proc features for device: %d", snd_device);
return -EINVAL;
}
if(snd_device == SND_DEVICE_SPEAKER)
{
device_id = 0;
LOGI("set device to SND_DEVICE_SPEAKER device_id=0");
}
if(snd_device == SND_DEVICE_HANDSET)
{
device_id = 1;
LOGI("set device to SND_DEVICE_HANDSET device_id=1");
}
if(snd_device == SND_DEVICE_HEADSET)
{
device_id = 2;
LOGI("set device to SND_DEVICE_HEADSET device_id=2");
}
fd = open(PCM_CTL_DEVICE, O_RDWR);
if (fd < 0) {
LOGE("Cannot open PCM Ctl device");
return -EPERM;
}
if(mbadrc_filter_exists[device_id] && state)
{
LOGV("MBADRC Enabled");
post_proc_feature_mask &= ADRC_DISABLE;
if ((mbadrc_flag[device_id] == 0) && (post_proc_feature_mask & MBADRC_ENABLE))
{
LOGV("MBADRC Disable");
post_proc_feature_mask &= MBADRC_DISABLE;
}
else if(post_proc_feature_mask & MBADRC_ENABLE)
{
LOGV("MBADRC Enabled %d", post_proc_feature_mask);
if (ioctl(fd, AUDIO_SET_MBADRC, &mbadrc_cfg[device_id]) < 0)
{
LOGE("set mbadrc filter error");
}
}
}
else if (adrc_filter_exists[device_id] && state)
{
post_proc_feature_mask &= MBADRC_DISABLE;
LOGV("ADRC Enabled %d", post_proc_feature_mask);
if (adrc_flag[device_id] == 0 && (post_proc_feature_mask & ADRC_ENABLE))
post_proc_feature_mask &= ADRC_DISABLE;
else if(post_proc_feature_mask & ADRC_ENABLE)
{
LOGI("ADRC Filter ADRC FLAG = %02x.", adrc_flag[device_id]);
LOGI("ADRC Filter COMP THRESHOLD = %02x.", adrc_cfg[device_id].adrc_params[0]);
LOGI("ADRC Filter COMP SLOPE = %02x.", adrc_cfg[device_id].adrc_params[1]);
LOGI("ADRC Filter COMP RMS TIME = %02x.", adrc_cfg[device_id].adrc_params[2]);
LOGI("ADRC Filter COMP ATTACK[0] = %02x.", adrc_cfg[device_id].adrc_params[3]);
LOGI("ADRC Filter COMP ATTACK[1] = %02x.", adrc_cfg[device_id].adrc_params[4]);
LOGI("ADRC Filter COMP RELEASE[0] = %02x.", adrc_cfg[device_id].adrc_params[5]);