2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
22 #include <sound_manager.h>
23 #include <sound_manager_internal.h>
25 #include <bluetooth_product.h>
29 #include "sttd_dbus.h"
30 #include "sttd_recorder.h"
31 #include "sttd_main.h"
34 #define FRAME_LENGTH 160
35 #define BUFFER_LENGTH FRAME_LENGTH * 2
37 static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
40 STTD_RECORDER_STATE_NONE = -1,
41 STTD_RECORDER_STATE_READY = 0, /**< Recorder is ready to start */
42 STTD_RECORDER_STATE_RECORDING /**< In the middle of recording */
43 } sttd_recorder_state;
48 stte_audio_type_e audio_type;
51 static stt_recorder_s* g_recorder = NULL;
53 static stt_recorder_audio_cb g_audio_cb;
55 static sound_stream_info_h g_stream_info_h;
57 static stt_recorder_interrupt_cb g_interrupt_cb;
59 static sttd_recorder_state g_recorder_state = STTD_RECORDER_STATE_NONE;
61 static int g_buffer_count;
63 static int g_stream_focus_id;
65 /* Sound buf save for test */
70 static char g_temp_file_name[128] = {'\0',};
74 static int g_count = 1;
78 static float get_volume_decibel(char* data, int size, stte_audio_type_e type);
80 //static stt_recorder_s* __get_recorder(int engine_id);
82 #define SMART_CONTROL_EXTEND_CMD 0x03
83 #define SMART_CONTROL_START_CMD 0x04
85 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
87 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
90 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
92 if (STTD_RECORDER_STATE_RECORDING != g_recorder_state) {
93 /* SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading normal func"); */
97 if (NULL != g_audio_cb) {
98 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
99 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to read audio");
100 sttd_recorder_stop();
103 if (NULL == g_recorder) {
107 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (int)voice_data->length, g_recorder->audio_type);
108 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
109 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
113 if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
114 SLOG(LOG_WARN, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
116 if (100000 == g_buffer_count) {
124 /* write pcm buffer */
125 fwrite(data, 1, len, g_pFile);
132 const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason_e reason)
135 case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA";
136 case SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM: return "SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM";
137 case SOUND_STREAM_FOCUS_CHANGED_BY_ALARM: return "SOUND_STREAM_FOCUS_CHANGED_BY_ALARM";
138 case SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION: return "SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION";
139 case SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY: return "SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY";
140 case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION";
141 case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION";
142 case SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE: return "SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE";
143 case SOUND_STREAM_FOCUS_CHANGED_BY_VOIP: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOIP";
144 case SOUND_STREAM_FOCUS_CHANGED_BY_CALL: return "SOUND_STREAM_FOCUS_CHANGED_BY_CALL";
145 case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY";
146 default: return "Undefined reason code";
150 void __recorder_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
151 sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data)
153 SLOG(LOG_WARN, TAG_STTD, "[Recorder] focus_state_watch_cb is called, focus_state(%d), reason(%s)", (int)focus_state, __stt_get_focus_changed_reason_code(reason));
155 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
156 SLOG(LOG_WARN, TAG_STTD, "[Recorder] Focus released as interrupt");
157 if (NULL != g_interrupt_cb) {
163 int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb)
165 if (NULL == audio_cb || NULL == interrupt_cb) {
166 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input param is NOT valid");
167 return STTD_ERROR_INVALID_PARAMETER;
170 if (STTD_RECORDER_STATE_NONE != g_recorder_state) {
171 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Current state of recorder is recording");
172 return STTD_ERROR_INVALID_STATE;
175 if (0 != pthread_mutex_init(&sttd_audio_in_handle_mutex, NULL)) {
176 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to initialize audio in handle mutex.");
179 g_audio_cb = audio_cb;
180 g_interrupt_cb = interrupt_cb;
181 g_recorder_state = STTD_RECORDER_STATE_NONE;
184 if (0 != sound_manager_create_stream_information_internal(SOUND_STREAM_TYPE_VOICE_RECOGNITION_SERVICE, NULL, NULL, &g_stream_info_h)) {
185 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create stream info");
188 if (0 != sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_BOTH, __recorder_focus_state_watch_cb, NULL, &g_stream_focus_id)) {
189 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to add focus state watch callback");
193 if (BT_ERROR_NONE != bt_product_init()) {
194 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to init bt");
195 return STTD_ERROR_OPERATION_FAILED;
198 if (BT_ERROR_NONE != bt_hid_host_initialize(_bt_cb_hid_state_changed, NULL)) {
199 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_host_initialize()");
200 return STTD_ERROR_OPERATION_FAILED;
207 int sttd_recorder_deinitialize()
209 if (0 != pthread_mutex_destroy(&sttd_audio_in_handle_mutex)) {
210 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
213 if (0 != sound_manager_remove_focus_state_watch_cb(g_stream_focus_id)) {
214 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to remove focus state watch callback");
217 if (0 != sound_manager_destroy_stream_information(g_stream_info_h)) {
218 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy stream info");
221 if (NULL != g_recorder) {
222 audio_in_destroy(g_recorder->audio_h);
223 g_recorder->audio_h = NULL;
228 #ifdef __UNUSED_CODES__
229 /* Remove all recorder */
231 stt_recorder_s *recorder = NULL;
233 iter = g_slist_nth(g_recorder_list, 0);
235 while (NULL != iter) {
236 recorder = iter->data;
238 if (NULL != recorder) {
239 g_recorder_list = g_slist_remove(g_recorder_list, recorder);
240 if (recorder->audio_h) {
241 audio_in_destroy(recorder->audio_h);
242 recorder->audio_h = NULL;
247 iter = g_slist_nth(g_recorder_list, 0);
252 bt_hid_host_deinitialize();
257 g_recorder_state = STTD_RECORDER_STATE_NONE;
262 int sttd_recorder_set_audio_session()
267 int sttd_recorder_unset_audio_session()
272 int sttd_recorder_create(stte_audio_type_e type, int channel, unsigned int sample_rate)
274 /* Check engine id is valid */
275 if (NULL != g_recorder) {
276 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is already registered");
277 return STTD_ERROR_INVALID_PARAMETER;
280 audio_in_h temp_in_h = NULL;
283 audio_channel_e audio_ch;
284 audio_sample_type_e audio_type;
287 case 1: audio_ch = AUDIO_CHANNEL_MONO; break;
288 case 2: audio_ch = AUDIO_CHANNEL_STEREO; break;
290 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input channel is not supported");
291 return STTD_ERROR_OPERATION_FAILED;
296 case STTE_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE; break;
297 case STTE_AUDIO_TYPE_PCM_U8: audio_type = AUDIO_SAMPLE_TYPE_U8; break;
299 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
300 return STTD_ERROR_OPERATION_FAILED;
305 ret = audio_in_create(sample_rate, audio_ch, audio_type, &temp_in_h);
306 if (AUDIO_IO_ERROR_NONE != ret) {
307 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
308 return STTD_ERROR_OPERATION_FAILED;
311 if (BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
312 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
313 return STTD_ERROR_OPERATION_FAILED;
317 stt_recorder_s* recorder;
318 recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
319 if (NULL == recorder) {
322 audio_in_destroy(temp_in_h);
326 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
327 return STTD_ERROR_OUT_OF_MEMORY;
331 recorder->audio_h = temp_in_h;
332 recorder->audio_type = type;
334 g_recorder = recorder;
336 g_recorder_state = STTD_RECORDER_STATE_READY;
341 int sttd_recorder_destroy()
343 // critical section required because this function can be called from stt engine thread context
344 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
345 pthread_mutex_lock(&sttd_audio_in_handle_mutex);
347 /* Check engine id is valid */
348 if (NULL == g_recorder) {
349 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
350 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
351 return STTD_ERROR_INVALID_PARAMETER;
356 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
357 if (g_recorder->audio_h) {
358 ret = audio_in_unprepare(g_recorder->audio_h);
359 if (AUDIO_IO_ERROR_NONE != ret) {
360 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
364 g_recorder_state = STTD_RECORDER_STATE_READY;
367 if (g_recorder->audio_h) {
368 ret = audio_in_destroy(g_recorder->audio_h);
369 if (AUDIO_IO_ERROR_NONE != ret) {
370 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
372 g_recorder->audio_h = NULL;
376 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
377 g_recorder_state = STTD_RECORDER_STATE_READY;
380 bt_hid_unset_audio_data_receive_cb();
386 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
387 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
392 static float get_volume_decibel(char* data, int size, stte_audio_type_e type)
394 #define MAX_AMPLITUDE_MEAN_16 32768
395 #define MAX_AMPLITUDE_MEAN_08 128
402 unsigned long long square_sum = 0;
404 if (type == STTE_AUDIO_TYPE_PCM_S16_LE)
409 for (i = 0; i < size; i += (depthByte<<1)) {
410 if (depthByte == 2) {
412 memcpy(&pcm16, data + i, sizeof(short));
413 square_sum += pcm16 * pcm16;
416 memcpy(&pcm8, data + i, sizeof(char));
417 square_sum += pcm8 * pcm8;
425 rms = sqrt((double)square_sum/count);
428 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
430 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_08);
436 Eina_Bool __read_audio_func(void *data)
439 static char g_buffer[BUFFER_LENGTH];
441 /* Check engine id is valid */
442 if (NULL == g_recorder) {
446 if (STTD_RECORDER_STATE_READY == g_recorder_state) {
447 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading func");
451 read_byte = audio_in_read(g_recorder->audio_h, g_buffer, BUFFER_LENGTH);
453 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
454 g_recorder_state = STTD_RECORDER_STATE_READY;
458 if (0 != g_audio_cb(g_buffer, read_byte)) {
459 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
460 sttd_recorder_stop();
464 if (0 == g_buffer_count % 30) {
465 float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, g_recorder->audio_type);
466 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
467 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
472 if (0 == g_buffer_count % 50) {
473 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
475 if (100000 == g_buffer_count) {
483 /* write pcm buffer */
484 fwrite(g_buffer, 1, BUFFER_LENGTH, g_pFile);
491 int sttd_recorder_start(int uid)
493 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
498 /* Check engine id is valid */
499 if (NULL == g_recorder) {
500 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
501 return STTD_ERROR_INVALID_PARAMETER;
504 ret = audio_in_set_sound_stream_info(g_recorder->audio_h, g_stream_info_h);
505 if (AUDIO_IO_ERROR_NONE != ret) {
506 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
509 ret = audio_in_prepare(g_recorder->audio_h);
510 if (AUDIO_IO_ERROR_NONE != ret) {
511 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
512 return STTD_ERROR_RECORDER_BUSY;
515 /* Add ecore timer to read audio data */
516 ecore_timer_add(0, __read_audio_func, NULL);
519 g_recorder_state = STTD_RECORDER_STATE_RECORDING;
520 g_recorder->uid = uid;
528 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
529 ret = access(g_temp_file_name, 0);
532 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File is already exist");
533 if (0 == remove(g_temp_file_name)) {
534 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Remove file");
544 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
547 g_pFile = fopen(g_temp_file_name, "wb+x");
549 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
557 int sttd_recorder_stop()
559 if (STTD_RECORDER_STATE_READY == g_recorder_state)
562 // critical section required because this function can be called from stt engine thread context
563 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
564 pthread_mutex_lock(&sttd_audio_in_handle_mutex);
566 /* Check engine id is valid */
567 if (NULL == g_recorder) {
568 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
569 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
570 return STTD_ERROR_INVALID_PARAMETER;
573 g_recorder->uid = -1;
577 ret = audio_in_unprepare(g_recorder->audio_h);
578 if (AUDIO_IO_ERROR_NONE != ret) {
579 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
583 bool stopped = false;
584 while (5 > bt_retry) {
585 ret = bt_hid_rc_stop_sending_voice(NULL);
586 if (BT_ERROR_NONE == ret) {
587 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Stop bt audio");
590 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
591 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
595 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] bt_hid_rc_stop_sending_voice(): ret(%d)", ret);
599 if (false == stopped) {
600 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio(%d)", ret);
601 // return STTD_ERROR_OPERATION_FAILED;
605 g_recorder_state = STTD_RECORDER_STATE_READY;
610 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
611 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
616 int sttd_recorder_start_file(int uid, const char *filepath)
618 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
621 /* Check engine id is valid */
622 if (NULL == g_recorder) {
623 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
624 return STTD_ERROR_INVALID_PARAMETER;
626 g_recorder_state = STTD_RECORDER_STATE_RECORDING;
627 g_recorder->uid = uid;
630 int totalReadBytes = 0;
632 FILE *infile = fopen(filepath, "rb");
635 if (infile != NULL) {
636 while (!feof(infile)) {
637 static char pcm_buff[BUFFER_LENGTH];
638 int read_byte = fread(pcm_buff, 1, BUFFER_LENGTH, infile);
639 totalReadBytes += read_byte;
640 if (0 != read_byte) {
641 if (0 != g_audio_cb(pcm_buff, read_byte)) {
642 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to call audio callback");
647 float vol_db = get_volume_decibel(pcm_buff, BUFFER_LENGTH, g_recorder->audio_type);
648 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
649 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
655 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", cnt, read_byte);
662 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] total bytes(%d)", cnt, totalReadBytes);
666 int sttd_recorder_stop_file()
668 if (STTD_RECORDER_STATE_READY == g_recorder_state)
671 /* Check engine id is valid */
672 if (NULL == g_recorder) {
673 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
674 return STTD_ERROR_INVALID_PARAMETER;
677 g_recorder->uid = -1;
678 g_recorder_state = STTD_RECORDER_STATE_READY;
683 int sttd_recorder_clear()
686 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder DEBUG] Clear recorder on TV");
688 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
689 sttd_recorder_stop();
691 if (STTD_RECORDER_STATE_READY == g_recorder_state) {
692 sttd_recorder_destroy();
696 return STTD_ERROR_NONE;