/** Initiate recognition based on specified grammar and input file */ ASR_CLIENT_DECLARE(apt_bool_t) asr_session_file_recognize( asr_session_t *asr_session, const char *grammar_file, const char *input_file) { const mrcp_app_message_t *app_message; mrcp_message_t *mrcp_message; app_message = NULL; mrcp_message = define_grammar_message_create(asr_session,grammar_file); if(!mrcp_message) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create DEFINE-GRAMMAR Request"); return FALSE; } /* Send DEFINE-GRAMMAR request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_message_send(asr_session->mrcp_session,asr_session->mrcp_channel,mrcp_message) == TRUE) { apr_thread_cond_wait(asr_session->wait_object,asr_session->mutex); app_message = asr_session->app_message; asr_session->app_message = NULL; } apr_thread_mutex_unlock(asr_session->mutex); if(mrcp_response_check(app_message,MRCP_REQUEST_STATE_COMPLETE) == FALSE) { return FALSE; } /* Reset prev recog result (if any) */ asr_session->recog_complete = NULL; app_message = NULL; mrcp_message = recognize_message_create(asr_session); if(!mrcp_message) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create RECOGNIZE Request"); return FALSE; } /* Send RECOGNIZE request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_message_send(asr_session->mrcp_session,asr_session->mrcp_channel,mrcp_message) == TRUE) { apr_thread_cond_wait(asr_session->wait_object,asr_session->mutex); app_message = asr_session->app_message; asr_session->app_message = NULL; } apr_thread_mutex_unlock(asr_session->mutex); if(mrcp_response_check(app_message,MRCP_REQUEST_STATE_INPROGRESS) == FALSE) { return FALSE; } /* Open input file and start streaming */ asr_session->input_mode = INPUT_MODE_FILE; if(asr_input_file_open(asr_session,input_file) == FALSE) { return FALSE; } asr_session->streaming = TRUE; return TRUE; }
/** Handle the responses sent to channel add requests */ static apt_bool_t synth_application_on_channel_add(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel, mrcp_sig_status_code_e status) { synth_app_channel_t *synth_channel = mrcp_application_channel_object_get(channel); if(status == MRCP_SIG_STATUS_CODE_SUCCESS) { mrcp_message_t *mrcp_message; const apt_dir_layout_t *dir_layout = mrcp_application_dir_layout_get(application); /* create and send SPEAK request */ mrcp_message = demo_speak_message_create(session,channel,dir_layout); if(mrcp_message) { mrcp_application_message_send(session,channel,mrcp_message); } if(synth_channel && session) { char *file_name = apr_pstrcat(session->pool,"synth-",session->id.buf,".pcm",NULL); char *file_path = apt_datadir_filepath_get(dir_layout,file_name,session->pool); if(file_path) { synth_channel->audio_out = fopen(file_path,"wb"); } } } else { /* error case, just terminate the demo */ mrcp_application_session_terminate(session); } return TRUE; }
/** Handle the responses sent to channel add requests */ static apt_bool_t demo_application_on_channel_add(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel, mrcp_sig_status_code_e status) { if(status == MRCP_SIG_STATUS_CODE_SUCCESS) { mpf_rtp_termination_descriptor_t *rtp_descriptor; mrcp_message_t *mrcp_message; const apt_dir_layout_t *dir_layout = mrcp_application_dir_layout_get(application); /* create and send SPEAK request */ mrcp_message = demo_speak_message_create(session,channel,dir_layout); if(mrcp_message) { mrcp_application_message_send(session,channel,mrcp_message); } rtp_descriptor = mrcp_application_rtp_descriptor_get(channel); if(rtp_descriptor) { mpf_rtp_media_descriptor_t *local_media = rtp_descriptor->audio.local; mpf_rtp_media_descriptor_t *remote_media = rtp_descriptor->audio.remote; if(local_media && remote_media) { apt_log(APT_PRIO_INFO,"Media Attributes: L[%s/%d] R[%s/%d]", local_media->base.ip.buf, local_media->base.port, remote_media->base.ip.buf, remote_media->base.port); } } } else { /* error case, just terminate the demo */ mrcp_application_session_terminate(session); } return TRUE; }
/** Handle the DEFINE-GRAMMAR responses */ static apt_bool_t recog_application_on_define_grammar(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel) { recog_app_channel_t *recog_channel = mrcp_application_channel_object_get(channel); mrcp_message_t *mrcp_message; const apt_dir_layout_t *dir_layout = mrcp_application_dir_layout_get(application); /* create and send RECOGNIZE request */ mrcp_message = demo_recognize_message_create(session,channel,dir_layout); if(mrcp_message) { mrcp_application_message_send(session,channel,mrcp_message); } if(recog_channel) { const mpf_codec_descriptor_t *descriptor = mrcp_application_source_descriptor_get(channel); char *file_name = apr_psprintf(session->pool,"one-%dkHz.pcm", descriptor ? descriptor->sampling_rate/1000 : 8); char *file_path = apt_datadir_filepath_get(dir_layout,file_name,session->pool); if(file_path) { recog_channel->audio_in = fopen(file_path,"rb"); if(recog_channel->audio_in) { apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Set [%s] as Speech Source",file_path); } else { apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Cannot Find [%s]",file_path); /* set some estimated time to complete */ recog_channel->time_to_complete = 5000; // 5 sec } } } return TRUE; }
bool UmcSession::SendMrcpRequest(mrcp_channel_t* pMrcpChannel, mrcp_message_t* pMrcpMessage) { if(!m_Running) return false; return (mrcp_application_message_send(m_pMrcpSession,pMrcpChannel,pMrcpMessage) == TRUE); }
/* Stop SPEAK/RECOGNIZE request on speech channel. */ int speech_channel_stop(speech_channel_t *schannel) { int status = 0; if (schannel == NULL) return -1; if (schannel->mutex != NULL) apr_thread_mutex_lock(schannel->mutex); if (schannel->state == SPEECH_CHANNEL_PROCESSING) { mrcp_method_id method; mrcp_message_t *mrcp_message; if (schannel->type == SPEECH_CHANNEL_SYNTHESIZER) method = SYNTHESIZER_STOP; else method = RECOGNIZER_STOP; ast_log(LOG_DEBUG, "(%s) Stopping %s\n", schannel->name, speech_channel_type_to_string(schannel->type)); /* Send STOP to MRCP server. */ mrcp_message = mrcp_application_message_create(schannel->unimrcp_session, schannel->unimrcp_channel, method); if (mrcp_message == NULL) { ast_log(LOG_ERROR, "(%s) Failed to create STOP message\n", schannel->name); status = -1; } else { if (!mrcp_application_message_send(schannel->unimrcp_session, schannel->unimrcp_channel, mrcp_message)) ast_log(LOG_WARNING, "(%s) Failed to send STOP message\n", schannel->name); else if (schannel->cond != NULL) { while (schannel->state == SPEECH_CHANNEL_PROCESSING) { if (apr_thread_cond_timedwait(schannel->cond, schannel->mutex, SPEECH_CHANNEL_TIMEOUT_USEC) == APR_TIMEUP) { break; } } } if (schannel->state == SPEECH_CHANNEL_PROCESSING) { ast_log(LOG_ERROR, "(%s) Timed out waiting for session to close. Continuing\n", schannel->name); schannel->state = SPEECH_CHANNEL_ERROR; status = -1; } else if (schannel->state == SPEECH_CHANNEL_ERROR) { ast_log(LOG_ERROR, "(%s) Channel error\n", schannel->name); schannel->state = SPEECH_CHANNEL_ERROR; status = -1; } else { ast_log(LOG_DEBUG, "(%s) %s stopped\n", schannel->name, speech_channel_type_to_string(schannel->type)); } } } if (schannel->mutex != NULL) apr_thread_mutex_unlock(schannel->mutex); return status; }
/* MRCP connection established, start communication */ static apt_bool_t OnChannelAdd(mrcp_application_t* application, mrcp_session_t* session, mrcp_channel_t* channel, mrcp_sig_status_code_e status) { mrcp_message_t* msg; mrcp_generic_header_t* hdr; (void) application; if (status != MRCP_SIG_STATUS_CODE_SUCCESS) return sess_failed("Failed to add channel"); /* Start processing here */ msg = mrcp_application_message_create(session, channel, SYNTHESIZER_SPEAK); hdr = mrcp_generic_header_get(msg); apt_string_set(&hdr->content_type, "text/plain"); mrcp_generic_header_property_add(msg, GENERIC_HEADER_CONTENT_TYPE); apt_string_set(&msg->body, text); return mrcp_application_message_send(session, channel, msg); }
/** Handle the responses sent to channel add requests */ static apt_bool_t recog_application_on_channel_add(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel, mrcp_sig_status_code_e status) { if(status == MRCP_SIG_STATUS_CODE_SUCCESS) { mrcp_message_t *mrcp_message; const apt_dir_layout_t *dir_layout = mrcp_application_dir_layout_get(application); /* create and send DEFINE-GRAMMAR request */ mrcp_message = demo_define_grammar_message_create(session,channel,dir_layout); if(mrcp_message) { mrcp_application_message_send(session,channel,mrcp_message); } } else { /* error case, just terminate the demo */ mrcp_application_session_terminate(session); } return TRUE; }
/** \brief Send MRCP request to client stack and wait for async response */ static apt_bool_t uni_recog_mrcp_request_send(uni_speech_t *uni_speech, mrcp_message_t *message) { apt_bool_t res = FALSE; apr_thread_mutex_lock(uni_speech->mutex); uni_speech->mrcp_request = message; /* Send MRCP request */ ast_log(LOG_DEBUG, "(%s) Send MRCP request method-id: %d\n",uni_speech->name,(int)message->start_line.method_id); res = mrcp_application_message_send(uni_speech->session,uni_speech->channel,message); if(res == TRUE) { /* Wait for MRCP response */ ast_log(LOG_DEBUG, "(%s) Wait for MRCP response\n",uni_speech->name); if(apr_thread_cond_timedwait(uni_speech->wait_object,uni_speech->mutex,MRCP_APP_REQUEST_TIMEOUT) != APR_SUCCESS) { ast_log(LOG_ERROR, "(%s) Failed to get MRCP response: request timed out\n",uni_speech->name); uni_speech->mrcp_response = NULL; } /* Wake up and check received response */ if(uni_speech->mrcp_response) { mrcp_message_t *mrcp_response = uni_speech->mrcp_response; ast_log(LOG_DEBUG, "(%s) Process MRCP response method-id: %d status-code: %d\n", uni_speech->name, (int)mrcp_response->start_line.method_id, mrcp_response->start_line.status_code); if(mrcp_response->start_line.status_code != MRCP_STATUS_CODE_SUCCESS && mrcp_response->start_line.status_code != MRCP_STATUS_CODE_SUCCESS_WITH_IGNORE) { ast_log(LOG_WARNING, "(%s) MRCP request failed method-id: %d status-code: %d\n", uni_speech->name, (int)mrcp_response->start_line.method_id, mrcp_response->start_line.status_code); res = FALSE; } } else { ast_log(LOG_ERROR, "(%s) No MRCP response available\n",uni_speech->name); res = FALSE; } } else { ast_log(LOG_WARNING, "(%s) Failed to send MRCP request\n",uni_speech->name); } uni_speech->mrcp_request = NULL; apr_thread_mutex_unlock(uni_speech->mutex); return res; }
/** \brief Send MRCP request to client stack and wait for async response */ static apt_bool_t uni_recog_mrcp_request_send(uni_speech_t *uni_speech, mrcp_message_t *message) { apt_bool_t res = FALSE; apr_thread_mutex_lock(uni_speech->mutex); uni_speech->mrcp_request = message; /* Send MRCP request */ ast_log(LOG_DEBUG, "Send MRCP request\n"); res = mrcp_application_message_send(uni_speech->session,uni_speech->channel,message); if(res == TRUE) { /* Wait for MRCP response */ ast_log(LOG_DEBUG, "Wait for MRCP response\n"); if(apr_thread_cond_timedwait(uni_speech->wait_object,uni_speech->mutex,MRCP_APP_REQUEST_TIMEOUT) != APR_SUCCESS) { ast_log(LOG_ERROR, "Failed to get response, request timed out\n"); uni_speech->mrcp_response = NULL; } ast_log(LOG_DEBUG, "Waked up\n"); } uni_speech->mrcp_request = NULL; apr_thread_mutex_unlock(uni_speech->mutex); return res; }
/** Handle the responses sent to channel add requests */ static apt_bool_t synth_application_on_channel_add(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel, mrcp_sig_status_code_e status) { if(status == MRCP_SIG_STATUS_CODE_SUCCESS) { mrcp_message_t *mrcp_message; synth_app_channel_t *synth_channel = mrcp_application_channel_object_get(channel); apr_pool_t *pool = mrcp_application_session_pool_get(session); const apt_dir_layout_t *dir_layout = mrcp_application_dir_layout_get(application); const mpf_codec_descriptor_t *descriptor = mrcp_application_sink_descriptor_get(channel); if(!descriptor) { /* terminate the demo */ apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Get Media Sink Descriptor"); return mrcp_application_session_terminate(session); } /* create and send SPEAK request */ mrcp_message = demo_speak_message_create(session,channel,dir_layout); if(mrcp_message) { mrcp_application_message_send(session,channel,mrcp_message); } if(synth_channel) { const apt_str_t *id = mrcp_application_session_id_get(session); char *file_name = apr_psprintf(pool,"synth-%dkHz-%s.pcm", descriptor->sampling_rate/1000, id->buf); char *file_path = apt_datadir_filepath_get(dir_layout,file_name,pool); if(file_path) { synth_channel->audio_out = fopen(file_path,"wb"); } } } else { /* error case, just terminate the demo */ mrcp_application_session_terminate(session); } return TRUE; }
/** Initiate recognition based on specified grammar and input stream */ ASR_CLIENT_DECLARE(const char*) asr_session_stream_recognize( asr_session_t *asr_session, const char *grammar_file) { const mrcp_app_message_t *app_message; mrcp_message_t *mrcp_message; app_message = NULL; mrcp_message = define_grammar_message_create(asr_session,grammar_file); if(!mrcp_message) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create DEFINE-GRAMMAR Request"); return NULL; } /* Send DEFINE-GRAMMAR request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_message_send(asr_session->mrcp_session,asr_session->mrcp_channel,mrcp_message) == TRUE) { apr_thread_cond_wait(asr_session->wait_object,asr_session->mutex); app_message = asr_session->app_message; asr_session->app_message = NULL; } apr_thread_mutex_unlock(asr_session->mutex); if(mrcp_response_check(app_message,MRCP_REQUEST_STATE_COMPLETE) == FALSE) { return NULL; } /* Reset prev recog result (if any) */ asr_session->recog_complete = NULL; app_message = NULL; mrcp_message = recognize_message_create(asr_session); if(!mrcp_message) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create RECOGNIZE Request"); return NULL; } /* Send RECOGNIZE request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_message_send(asr_session->mrcp_session,asr_session->mrcp_channel,mrcp_message) == TRUE) { apr_thread_cond_wait(asr_session->wait_object,asr_session->mutex); app_message = asr_session->app_message; asr_session->app_message = NULL; } apr_thread_mutex_unlock(asr_session->mutex); if(mrcp_response_check(app_message,MRCP_REQUEST_STATE_INPROGRESS) == FALSE) { return NULL; } /* Reset media buffer */ mpf_frame_buffer_restart(asr_session->media_buffer); /* Set input mode and start streaming */ asr_session->input_mode = INPUT_MODE_STREAM; asr_session->streaming = TRUE; /* Wait for events either START-OF-INPUT or RECOGNITION-COMPLETE */ do { apr_thread_mutex_lock(asr_session->mutex); app_message = NULL; if(apr_thread_cond_timedwait(asr_session->wait_object,asr_session->mutex, 60 * 1000000) != APR_SUCCESS) { apr_thread_mutex_unlock(asr_session->mutex); return NULL; } app_message = asr_session->app_message; asr_session->app_message = NULL; apr_thread_mutex_unlock(asr_session->mutex); mrcp_message = mrcp_event_get(app_message); if(mrcp_message && mrcp_message->start_line.method_id == RECOGNIZER_RECOGNITION_COMPLETE) { asr_session->recog_complete = mrcp_message; } } while(!asr_session->recog_complete); /* Get results */ return nlsml_input_get(asr_session->recog_complete); }
/* Send SPEAK request to synthesizer. */ static int synth_channel_speak(speech_channel_t *schannel, const char *content, const char *content_type, apr_hash_t *header_fields) { int status = 0; mrcp_message_t *mrcp_message = NULL; mrcp_generic_header_t *generic_header = NULL; mrcp_synth_header_t *synth_header = NULL; if (!schannel || !content || !content_type) { ast_log(LOG_ERROR, "synth_channel_speak: unknown channel error!\n"); return -1; } apr_thread_mutex_lock(schannel->mutex); if (schannel->state != SPEECH_CHANNEL_READY) { apr_thread_mutex_unlock(schannel->mutex); return -1; } if ((mrcp_message = mrcp_application_message_create(schannel->unimrcp_session, schannel->unimrcp_channel, SYNTHESIZER_SPEAK)) == NULL) { ast_log(LOG_ERROR, "(%s) Failed to create SPEAK message\n", schannel->name); apr_thread_mutex_unlock(schannel->mutex); return -1; } /* Set generic header fields (content-type). */ if ((generic_header = (mrcp_generic_header_t *)mrcp_generic_header_prepare(mrcp_message)) == NULL) { apr_thread_mutex_unlock(schannel->mutex); return -1; } apt_string_assign(&generic_header->content_type, content_type, mrcp_message->pool); mrcp_generic_header_property_add(mrcp_message, GENERIC_HEADER_CONTENT_TYPE); /* Set synthesizer header fields (voice, rate, etc.). */ if ((synth_header = (mrcp_synth_header_t *)mrcp_resource_header_prepare(mrcp_message)) == NULL) { apr_thread_mutex_unlock(schannel->mutex); return -1; } /* Add params to MRCP message. */ speech_channel_set_params(schannel, mrcp_message, header_fields); /* Set body (plain text or SSML). */ apt_string_assign(&mrcp_message->body, content, schannel->pool); /* Empty audio queue and send SPEAK to MRCP server. */ audio_queue_clear(schannel->audio_queue); if (!mrcp_application_message_send(schannel->unimrcp_session, schannel->unimrcp_channel, mrcp_message)) { ast_log(LOG_ERROR,"(%s) Failed to send SPEAK message", schannel->name); apr_thread_mutex_unlock(schannel->mutex); return -1; } /* Wait for IN PROGRESS. */ apr_thread_cond_timedwait(schannel->cond, schannel->mutex, SPEECH_CHANNEL_TIMEOUT_USEC); if (schannel->state != SPEECH_CHANNEL_PROCESSING) { apr_thread_mutex_unlock(schannel->mutex); return -1; } apr_thread_mutex_unlock(schannel->mutex); return status; }