ea4b127767ad3b9ca1511e834d74e93ea9af9a09
[platform/core/uifw/inputdelegator.git] / src / WInputSttMicEffect.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <cmath>
18 #include <algorithm>
19 #include <numeric>
20
21 #include <efl_extension.h>
22 #include "WInputSttMicEffect.h"
23
24 namespace {
25 template<class T>
26 static unsigned long long SumSquare(unsigned long long const& a, T const& b) {
27         return a + b*b;
28 }
29
30 const double MAX_AMPLITUDE_MEAN_16 = 23170.115738161934;
31 const double MAX_AMPLITUDE_MEAN_08 = 89.803909382810;
32
33 unsigned int sample_count = SAMPLE_COUNT;
34
35 }
36
37 using namespace is::ui;
38
39
40
41 WInputSttMicEffect::WInputSttMicEffect()
42         : processing_effect_timer(NULL)
43         , progressbar(NULL)
44         , count (5)
45         , square_sum (0)
46         , handle (NULL) {
47
48 }
49
50
51
52 WInputSttMicEffect::~WInputSttMicEffect() {
53
54         ProcessingAnimationStop();
55 }
56
57
58
59 std::vector<int> WInputSttMicEffect::GetVolume() {
60
61         std::vector<int> result;
62
63         short pcm[512] = {0};
64         int size = 0;
65         int ret = 0;
66
67 //      ret = stt_get_spectrum(handle, (void *) pcm, &size);
68
69
70         count = 5;
71
72         if(STT_ERROR_NONE != ret) {
73                 PRINTFUNC(DLOG_ERROR, "stt_audio_snapshot invalid (%d)", ret);
74         }
75         else {
76                 unsigned int level = 0;
77                 unsigned int step = (unsigned int) (size/2/sample_count);
78
79                 for (unsigned int k = 0; k < sample_count; k++ ){
80                         square_sum = std::accumulate(pcm + k*step, pcm + k*step + 5, 0ull, SumSquare<short>);
81                         level = ConvertLevel();
82                         result.push_back(level);
83                 }
84         }
85
86         return result;
87 }
88
89
90
91 float WInputSttMicEffect::GetDecibel() const
92 {
93         float rms = std::sqrt( square_sum/count );
94         return 20.0*log10(rms);
95 }
96
97
98
99 int WInputSttMicEffect::ConvertLevel()
100 {
101         float db = GetDecibel();
102
103         if ( db <= 30.0 ){
104                 return 0;
105         }
106         else if ( db <= 33.3 ){
107                 return 1;
108         }
109         else if ( db <= 36.6 ){
110                 return 2;
111         }
112         else if ( db <= 40 ){
113                 return 3;
114         }
115         else if ( db <= 43.3 ){
116                 return 4;
117         }
118         else if ( db <= 46.6 ){
119                 return 5;
120         }
121         else if ( db <= 50 ){
122                 return 6;
123         }
124         else if ( db <= 53.3 ){
125                 return 7;
126         }
127         else if ( db <= 56.6 ){
128                 return 8;
129         }
130         else if ( db <= 60 ){
131                 return 9;
132         }
133         else{
134                 return 10;
135         }
136 }
137
138
139
140 void WInputSttMicEffect::ProcessingAnimationStart() {
141
142         elm_progressbar_pulse(progressbar, EINA_TRUE);
143
144         processing_effect_timer = ecore_timer_add ( 0.1,
145                 [](void *data)->Eina_Bool
146                 {
147                         if(!data) return ECORE_CALLBACK_CANCEL;
148 /*
149                         WInputSttMicEffect *effect = (WInputSttMicEffect *) data;
150                         Evas_Object *progressbar = effect->progressbar;
151
152                         double progress = eext_circle_value_get(progressbar);
153
154                         if (progress < 100)
155                                 progress += 5.0;
156                         else
157                                 progress = 0.0;
158
159                         eext_circle_value_set(progressbar, progress);
160 */
161                         return ECORE_CALLBACK_RENEW;
162                 }, this);
163 }
164
165
166
167 void WInputSttMicEffect::ProcessingAnimationStop() {
168
169         if(processing_effect_timer)
170         {
171                 ecore_timer_del(processing_effect_timer);
172                 processing_effect_timer = NULL;
173         }
174         elm_progressbar_pulse(progressbar, EINA_FALSE);
175 }
176
177
178
179 void WInputSttMicEffect::SetSttHandle(stt_h handle) {
180
181         this->handle = handle;
182 }
183
184
185
186 void WInputSttMicEffect::SetProgressBar(Evas_Object *progress) {
187
188         this->progressbar = progress;
189
190 }