blob: 22780f75089c100e20f62fee28dceb383d6b5d0b [file] [log] [blame]
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08001/*
2 * (C) 2014 DTS, Inc.
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 <utils/Log.h>
18#include <stdlib.h>
Mingming Yin497419f2015-07-01 16:57:32 -070019#include <string.h>
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080020#include "effect_util.h"
Sharad Sangleb27354b2015-06-18 15:58:55 +053021#include <string.h>
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080022
23#ifdef LOG_TAG
24#undef LOG_TAG
25#endif
26#define LOG_TAG "effect_util"
27
28/*#define LOG_NDEBUG 0*/
29
30enum {
31 EQUALIZER,
32 VIRTUALIZER,
33 BASSBOOST,
34};
35
36static const char *paramList[10] = {
37 "eq_enable",
38 "virt_enable",
39 "bb_enable",
40 "eq_param_level0",
41 "eq_param_level1",
42 "eq_param_level2",
43 "eq_param_level3",
44 "eq_param_level4",
45 "virt_param_strength",
46 "bassboost_param_strength"
47};
48
Dhanalakshmi Siddani79b98592015-03-23 11:59:02 +053049#define EFFECT_FILE "/data/misc/dts/effect"
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080050#define MAX_LENGTH_OF_INTEGER_IN_STRING 13
51
52#ifdef DTS_EAGLE
53void create_effect_state_node(int device_id)
54{
55 char prop[PROPERTY_VALUE_MAX];
56 int fd;
57 char buf[1024];
58 char path[PATH_MAX];
59 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
60
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070061 property_get("vendor.audio.use.dts_eagle", prop, "0");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080062 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
63 ALOGV("create_effect_node for - device_id: %d", device_id);
64 strlcpy(path, EFFECT_FILE, sizeof(path));
65 snprintf(value, sizeof(value), "%d", device_id);
66 strlcat(path, value, sizeof(path));
67 if ((fd=open(path, O_RDONLY)) < 0) {
68 ALOGV("No File exist");
69 } else {
70 ALOGV("A file with the same name exist. So, not creating again");
71 return;
72 }
73 if ((fd=creat(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
74 ALOGE("opening effect state node failed returned");
75 return;
76 }
77 chmod(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
78 snprintf(buf, sizeof(buf), "eq_enable=%d;virt_enable=%d;bb_enable=%d;eq_param_level0=%d;eq_param_level1=%d;eq_param_level2=%d;eq_param_level3=%d;eq_param_level4=%d;virt_param_strength=%d;bassboost_param_strength=%d", 0,0,0,0,0,0,0,0,0,0);
79 int n = write(fd, buf, strlen(buf));
80 ALOGV("number of bytes written: %d", n);
81 close(fd);
82 }
83}
84
85void update_effects_node(int device_id, int effect_type, int enable_or_set, int enable_disable, int strength, int eq_band, int eq_level)
86{
87 char prop[PROPERTY_VALUE_MAX];
88 char buf[1024];
89 int fd = 0;
90 int paramValue = 0;
91 char path[PATH_MAX];
92 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
93 char parameterValue[MAX_LENGTH_OF_INTEGER_IN_STRING];
94 int keyParamIndex = -1; //index in the paramlist array which has to be updated
95 char *s1, *s2;
96 char resultBuf[1024];
97 int index1 = -1;
98 //ALOGV("value of device_id and effect_type is %d and %d", device_id, effect_type);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070099 property_get("vendor.audio.use.dts_eagle", prop, "0");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -0800100 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
101 strlcpy(path, EFFECT_FILE, sizeof(path));
102 snprintf(value, sizeof(value), "%d", device_id);
103 strlcat(path, value, sizeof(path));
104 switch (effect_type)
105 {
106 case EQUALIZER:
107 if (enable_or_set) {
108 keyParamIndex = 0;
109 paramValue = enable_disable;
110 } else {
111 switch (eq_band) {
112 case 0:
113 keyParamIndex = 3;
114 break;
115 case 1:
116 keyParamIndex = 4;
117 break;
118 case 2:
119 keyParamIndex = 5;
120 break;
121 case 3:
122 keyParamIndex = 6;
123 break;
124 case 4:
125 keyParamIndex = 7;
126 break;
127 default:
128 break;
129 }
130 paramValue = eq_level;
131 }
132 break;
133 case VIRTUALIZER:
134 if(enable_or_set) {
135 keyParamIndex = 1;
136 paramValue = enable_disable;
137 } else {
138 keyParamIndex = 8;
139 paramValue = strength;
140 }
141 break;
142 case BASSBOOST:
143 if (enable_or_set) {
144 keyParamIndex = 2;
145 paramValue = enable_disable;
146 } else {
147 keyParamIndex = 9;
148 paramValue = strength;
149 }
150 break;
151 default:
152 break;
153 }
154 if(keyParamIndex !=-1) {
155 FILE *fp;
156 fp = fopen(path,"r");
157 if (fp != NULL) {
158 memset(buf, 0, 1024);
159 memset(resultBuf, 0, 1024);
160 if (fgets(buf, 1024, fp) != NULL) {
161 s1 = strstr(buf, paramList[keyParamIndex]);
162 s2 = strstr(s1,";");
163 index1 = s1 - buf;
164 strncpy(resultBuf, buf, index1);
165 strncat(resultBuf, paramList[keyParamIndex], sizeof(resultBuf)-strlen(resultBuf)-1);
166 strncat(resultBuf, "=", sizeof(resultBuf)-strlen(resultBuf)-1);
167 snprintf(parameterValue, sizeof(parameterValue), "%d", paramValue);
168 strncat(resultBuf, parameterValue, sizeof(resultBuf)-strlen(resultBuf)-1);
169 if (s2)
170 strncat(resultBuf, s2, sizeof(resultBuf)-strlen(resultBuf)-1);
171 fclose(fp);
172 if ((fd=open(path, O_TRUNC|O_WRONLY)) < 0) {
173 ALOGV("opening file for writing failed");
174 return;
175 }
176 int n = write(fd, resultBuf, strlen(resultBuf));
177 close(fd);
178 ALOGV("number of bytes written: %d", n);
179 } else {
180 ALOGV("file could not be read");
181 fclose(fp);
182 }
183 } else
184 ALOGV("file could not be opened");
185 }
186 }
187}
188
189void remove_effect_state_node(int device_id)
190{
191 char prop[PROPERTY_VALUE_MAX];
192 int fd;
193 char path[PATH_MAX];
194 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
195
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700196 property_get("vendor.audio.use.dts_eagle", prop, "0");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -0800197 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
198 ALOGV("remove_state_notifier_node: device_id - %d", device_id);
199 strlcpy(path, EFFECT_FILE, sizeof(path));
200 snprintf(value, sizeof(value), "%d", device_id);
201 strlcat(path, value, sizeof(path));
202 if ((fd=open(path, O_RDONLY)) < 0) {
203 ALOGV("open effect state node failed");
204 } else {
205 ALOGV("open effect state node successful");
206 ALOGV("Remove the file");
207 close(fd);
208 remove(path);
209 }
210 }
211}
212#endif