Adopt wave VI for voice mode
[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 #include <stt.h>
21 #include <efl_extension.h>
22 #include "WInputSttMicEffect.h"
23 #include <dlog.h>
24 #undef LOG_TAG
25 #define LOG_TAG "INPUT_DELEGATOR"
26
27 namespace {
28 template<class T>
29 static float SumSquare(float const& a, T const& b) {
30     return a + b*b;
31 }
32
33 const double MAX_AMPLITUDE_MEAN_16 = 23170.115738161934;
34 const double MAX_AMPLITUDE_MEAN_08 = 89.803909382810;
35
36 unsigned int sample_count = SAMPLE_COUNT;
37
38 }
39
40 using namespace is::ui;
41
42
43 WInputSttMicEffect::WInputSttMicEffect()
44     : square_sum(0)
45     , count(5)
46     , handle(NULL)
47     , processing_effect_timer(NULL)
48     , progressbar(NULL) {
49 }
50
51 WInputSttMicEffect::~WInputSttMicEffect() {
52     ProcessingAnimationStop();
53 }
54
55 std::vector<int> WInputSttMicEffect::GetVolume() {
56     std::vector<int> result;
57
58     float pcm[512] = {0};
59     int size = 50;
60     int ret = 0;
61
62     ret = stt_get_recording_volume(handle, pcm);
63     count = 5;
64
65     if (STT_ERROR_NONE != ret) {
66     } else {
67         unsigned int level = 0;
68         unsigned int step = (unsigned int) (size/2/sample_count);
69
70         for (unsigned int k = 0; k < sample_count; k++ ){
71             square_sum = (unsigned long long)std::accumulate(pcm + k*step, pcm + k*step + 5, 0ull, SumSquare<float>);
72             level = ConvertLevel();
73             result.push_back(level);
74         }
75     }
76     return result;
77 }
78
79 float WInputSttMicEffect::GetDecibel() const
80 {
81     float rms = std::sqrt(square_sum/count);
82     return 20.0*log10(rms);
83 }
84
85 int WInputSttMicEffect::ConvertLevel()
86 {
87     float db = GetDecibel();
88
89     if ( db <= 30.0 ){
90         return 0;
91     } else if ( db <= 33.3 ){
92         return 1;
93     } else if ( db <= 36.6 ){
94         return 2;
95     } else if ( db <= 40 ){
96         return 3;
97     } else if ( db <= 43.3 ){
98         return 4;
99     } else if ( db <= 46.6 ){
100         return 5;
101     } else if ( db <= 50 ){
102         return 6;
103     } else if ( db <= 53.3 ){
104         return 7;
105     } else if ( db <= 56.6 ){
106         return 8;
107     } else if ( db <= 60 ){
108         return 9;
109     } else {
110         return 10;
111     }
112 }
113
114 void WInputSttMicEffect::ProcessingAnimationStart() {
115     elm_progressbar_pulse(progressbar, EINA_TRUE);
116
117     processing_effect_timer = ecore_timer_add(0.1,
118         [](void *data)->Eina_Bool
119         {
120             if (!data) return ECORE_CALLBACK_CANCEL;
121 /*
122             WInputSttMicEffect *effect = (WInputSttMicEffect *) data;
123             Evas_Object *progressbar = effect->progressbar;
124
125                double progress = eext_circle_value_get(progressbar);
126
127             if (progress < 100)
128                 progress += 5.0;
129             else
130                 progress = 0.0;
131
132             eext_circle_value_set(progressbar, progress);
133 */
134             return ECORE_CALLBACK_RENEW;
135         }, this);
136 }
137
138 void WInputSttMicEffect::ProcessingAnimationStop() {
139     if (processing_effect_timer)
140     {
141         ecore_timer_del(processing_effect_timer);
142         processing_effect_timer = NULL;
143     }
144     elm_progressbar_pulse(progressbar, EINA_FALSE);
145 }
146
147 void WInputSttMicEffect::SetSttHandle(stt_h handle) {
148     this->handle = handle;
149 }
150
151 void WInputSttMicEffect::SetProgressBar(Evas_Object *progress) {
152     this->progressbar = progress;
153 }