Coding rule check
[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 WInputSttMicEffect::WInputSttMicEffect()
41         : processing_effect_timer(NULL)
42         , progressbar(NULL)
43         , count(5)
44         , square_sum(0)
45         , handle(NULL) {
46 }
47
48 WInputSttMicEffect::~WInputSttMicEffect() {
49         ProcessingAnimationStop();
50 }
51
52 std::vector<int> WInputSttMicEffect::GetVolume() {
53         std::vector<int> result;
54
55         short pcm[512] = {0};
56         int size = 0;
57         int ret = 0;
58
59 //      ret = stt_get_spectrum(handle, (void *) pcm, &size);
60         count = 5;
61
62         if (STT_ERROR_NONE != ret) {
63                 PRINTFUNC(DLOG_ERROR, "stt_audio_snapshot invalid (%d)", ret);
64         } else {
65                 unsigned int level = 0;
66                 unsigned int step = (unsigned int) (size/2/sample_count);
67
68                 for (unsigned int k = 0; k < sample_count; k++ ){
69                         square_sum = std::accumulate(pcm + k*step, pcm + k*step + 5, 0ull, SumSquare<short>);
70                         level = ConvertLevel();
71                         result.push_back(level);
72                 }
73         }
74         return result;
75 }
76
77 float WInputSttMicEffect::GetDecibel() const
78 {
79         float rms = std::sqrt(square_sum/count);
80         return 20.0*log10(rms);
81 }
82
83 int WInputSttMicEffect::ConvertLevel()
84 {
85         float db = GetDecibel();
86
87         if ( db <= 30.0 ){
88                 return 0;
89         } else if ( db <= 33.3 ){
90                 return 1;
91         } else if ( db <= 36.6 ){
92                 return 2;
93         } else if ( db <= 40 ){
94                 return 3;
95         } else if ( db <= 43.3 ){
96                 return 4;
97         } else if ( db <= 46.6 ){
98                 return 5;
99         } else if ( db <= 50 ){
100                 return 6;
101         } else if ( db <= 53.3 ){
102                 return 7;
103         } else if ( db <= 56.6 ){
104                 return 8;
105         } else if ( db <= 60 ){
106                 return 9;
107         } else {
108                 return 10;
109         }
110 }
111
112 void WInputSttMicEffect::ProcessingAnimationStart() {
113         elm_progressbar_pulse(progressbar, EINA_TRUE);
114
115         processing_effect_timer = ecore_timer_add(0.1,
116                 [](void *data)->Eina_Bool
117                 {
118                         if(!data) return ECORE_CALLBACK_CANCEL;
119 /*
120                         WInputSttMicEffect *effect = (WInputSttMicEffect *) data;
121                         Evas_Object *progressbar = effect->progressbar;
122
123                         double progress = eext_circle_value_get(progressbar);
124
125                         if (progress < 100)
126                                 progress += 5.0;
127                         else
128                                 progress = 0.0;
129
130                         eext_circle_value_set(progressbar, progress);
131 */
132                         return ECORE_CALLBACK_RENEW;
133                 }, this);
134 }
135
136 void WInputSttMicEffect::ProcessingAnimationStop() {
137         if(processing_effect_timer)
138         {
139                 ecore_timer_del(processing_effect_timer);
140                 processing_effect_timer = NULL;
141         }
142         elm_progressbar_pulse(progressbar, EINA_FALSE);
143 }
144
145
146
147 void WInputSttMicEffect::SetSttHandle(stt_h handle) {
148         this->handle = handle;
149 }
150
151
152
153 void WInputSttMicEffect::SetProgressBar(Evas_Object *progress) {
154         this->progressbar = progress;
155 }