/** 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; }
/** Create DEFINE-GRAMMAR request */ static mrcp_message_t* define_grammar_message_create(asr_session_t *asr_session, const char *grammar_file) { /* create MRCP message */ mrcp_message_t *mrcp_message = mrcp_application_message_create( asr_session->mrcp_session, asr_session->mrcp_channel, RECOGNIZER_DEFINE_GRAMMAR); if(mrcp_message) { mrcp_generic_header_t *generic_header; /* set message body */ const apt_dir_layout_t *dir_layout = mrcp_application_dir_layout_get(asr_session->engine->mrcp_app); apr_pool_t *pool = mrcp_application_session_pool_get(asr_session->mrcp_session); char *grammar_file_path = apt_datadir_filepath_get(dir_layout,grammar_file,pool); if(grammar_file_path) { apr_finfo_t finfo; apr_file_t *grammar_file; apt_str_t *content = &mrcp_message->body; if(apr_file_open(&grammar_file,grammar_file_path,APR_FOPEN_READ|APR_FOPEN_BINARY,0,pool) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Open Grammar File %s",grammar_file_path); return NULL; } if(apr_file_info_get(&finfo,APR_FINFO_SIZE,grammar_file) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Get Grammar File Info %s",grammar_file_path); apr_file_close(grammar_file); return NULL; } content->length = (apr_size_t)finfo.size; content->buf = (char*) apr_palloc(pool,content->length+1); apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Load Grammar File Content size [%"APR_SIZE_T_FMT" bytes] %s", content->length,grammar_file_path); if(apr_file_read(grammar_file,content->buf,&content->length) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Read Grammar File Content %s",grammar_file_path); apr_file_close(grammar_file); return NULL; } content->buf[content->length] = '\0'; apr_file_close(grammar_file); } /* get/allocate generic header */ generic_header = mrcp_generic_header_prepare(mrcp_message); if(generic_header) { /* set generic header fields */ if(mrcp_message->start_line.version == MRCP_VERSION_2) { apt_string_assign(&generic_header->content_type,"application/srgs+xml",mrcp_message->pool); } else { apt_string_assign(&generic_header->content_type,"application/grammar+xml",mrcp_message->pool); } mrcp_generic_header_property_add(mrcp_message,GENERIC_HEADER_CONTENT_TYPE); apt_string_assign(&generic_header->content_id,"demo-grammar",mrcp_message->pool); mrcp_generic_header_property_add(mrcp_message,GENERIC_HEADER_CONTENT_ID); } } return mrcp_message; }
/** 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; }
/** Create DEFINE-GRAMMAR request */ static mrcp_message_t* define_grammar_message_create(asr_session_t *asr_session, const char *grammar_file) { /* create MRCP message */ mrcp_message_t *mrcp_message = mrcp_application_message_create( asr_session->mrcp_session, asr_session->mrcp_channel, RECOGNIZER_DEFINE_GRAMMAR); if(mrcp_message) { mrcp_generic_header_t *generic_header; /* set message body */ const apt_dir_layout_t *dir_layout = mrcp_application_dir_layout_get(asr_session->engine->mrcp_app); apr_pool_t *pool = mrcp_application_session_pool_get(asr_session->mrcp_session); char *grammar_file_path = apt_datadir_filepath_get(dir_layout,grammar_file,pool); if(grammar_file_path) { char text[1024]; apr_size_t size; FILE *grammar = fopen(grammar_file_path,"r"); if(!grammar) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Cannot Open [%s]",grammar_file_path); return NULL; } size = fread(text,1,sizeof(text),grammar); apt_string_assign_n(&mrcp_message->body,text,size,mrcp_message->pool); fclose(grammar); } /* get/allocate generic header */ generic_header = mrcp_generic_header_prepare(mrcp_message); if(generic_header) { /* set generic header fields */ if(mrcp_message->start_line.version == MRCP_VERSION_2) { apt_string_assign(&generic_header->content_type,"application/srgs+xml",mrcp_message->pool); } else { apt_string_assign(&generic_header->content_type,"application/grammar+xml",mrcp_message->pool); } mrcp_generic_header_property_add(mrcp_message,GENERIC_HEADER_CONTENT_TYPE); apt_string_assign(&generic_header->content_id,"demo-grammar",mrcp_message->pool); mrcp_generic_header_property_add(mrcp_message,GENERIC_HEADER_CONTENT_ID); } } return mrcp_message; }
/** Open audio input file */ static apt_bool_t asr_input_file_open(asr_session_t *asr_session, const char *input_file) { const apt_dir_layout_t *dir_layout = mrcp_application_dir_layout_get(asr_session->engine->mrcp_app); apr_pool_t *pool = mrcp_application_session_pool_get(asr_session->mrcp_session); char *input_file_path = apt_datadir_filepath_get(dir_layout,input_file,pool); if(!input_file_path) { return FALSE; } if(asr_session->audio_in) { fclose(asr_session->audio_in); asr_session->audio_in = NULL; } asr_session->audio_in = fopen(input_file_path,"rb"); if(!asr_session->audio_in) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Cannot Open [%s]",input_file_path); return FALSE; } 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) { 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; }