Add get_volume_decibel in vc_mgr_send_audio_streaming 35/222435/1
authorsooyeon.kim <sooyeon.kim@samsung.com>
Thu, 26 Dec 2019 07:35:59 +0000 (16:35 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Wed, 15 Jan 2020 02:35:26 +0000 (02:35 +0000)
Change-Id: I733e21ae281f230ab790bfe227a29cb3c7f07906
Signed-off-by: sooyeon.kim <sooyeon.kim@samsung.com>
(cherry picked from commit c88dd1938805c7bedb3c97d56ad48fac628cac7b)

client/vc_mgr.c

index 9f397e7cfc498e7ad741cae49b85bd2dacb28432..511275016819ada058efa2fe50b050567d45bfe6 100644 (file)
@@ -22,6 +22,7 @@
 #include <pthread.h>
 #include <sound_manager.h>
 #include <sound_manager_internal.h>
+#include <math.h>
 
 #include "vc_cmd_db.h"
 #include "vc_config_mgr.h"
@@ -2326,7 +2327,7 @@ int vc_mgr_cancel(void)
 }
 
 static int g_cnt = 0;
-static Eina_Bool __vc_mgr_set_volume(void* data)
+static Eina_Bool __vc_mgr_set_volume_timer_cb(void* data)
 {
        g_cnt++;
        g_volume_db = g_prev_volume_db + (g_cur_volume_db - g_prev_volume_db) / 5 * g_cnt;
@@ -2341,6 +2342,7 @@ static Eina_Bool __vc_mgr_set_volume(void* data)
 
 int __vc_mgr_cb_set_volume(float volume)
 {
+       // called in vc_mgr_dbus
        g_prev_volume_db = g_volume_db;
        g_cur_volume_db = volume;
 
@@ -2351,7 +2353,15 @@ int __vc_mgr_cb_set_volume(float volume)
        }
 
        g_cnt = 1;
-       g_m_set_volume_timer = ecore_timer_add(0.05, __vc_mgr_set_volume, NULL);
+       g_m_set_volume_timer = ecore_timer_add(0.05, __vc_mgr_set_volume_timer_cb, NULL);
+
+       return 0;
+}
+
+int __vc_mgr_set_volume(float volume)
+{
+       // called by app directly
+       g_volume_db = volume;
 
        return 0;
 }
@@ -4367,6 +4377,38 @@ int vc_mgr_send_utterance_status(int pid, int utt_id, int utt_status)
        return 0;
 }
 
+static float __get_volume_decibel(char* data, int size)
+{
+#define MAX_AMPLITUDE_MEAN_16 32768
+
+       int i, depthByte;
+       int count = 0;
+
+       float db = 0.0;
+       float rms = 0.0;
+       unsigned long long square_sum = 0;
+       short pcm16 = 0;
+
+       depthByte = 2;
+
+       for (i = 0; i < size; i += (depthByte<<1)) {
+               pcm16 = 0;
+               memcpy(&pcm16, data + i, sizeof(short));
+               square_sum += pcm16 * pcm16;
+               count++;
+       }
+
+       if (0 == count || 0 == square_sum) {
+               SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No data");
+               rms = 1.0;
+       } else {
+               rms = sqrt((float)square_sum/count);
+       }
+
+       db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
+       return db;
+}
+
 int vc_mgr_send_audio_streaming(vc_audio_streaming_event_e event, unsigned char* buffer, unsigned int len)
 {
        SLOG(LOG_INFO, TAG_VCM, "[Manager] Send audio streaming to the engine service");
@@ -4410,6 +4452,10 @@ int vc_mgr_send_audio_streaming(vc_audio_streaming_event_e event, unsigned char*
                return VC_ERROR_INVALID_STATE;
        }
 
+       /* set volume */
+       float volume = __get_volume_decibel((char*)buffer, len);
+       __vc_mgr_set_volume(volume);
+
        /* send audio streaming */
        int ret = vc_mgr_dbus_send_audio_streaming(g_vc_m->handle, event, buffer, len);
        if (0 != ret) {