/** Open file to record */ static apt_bool_t recorder_file_open(recorder_channel_t *recorder_channel, mrcp_message_t *request) { mrcp_engine_channel_t *channel = recorder_channel->channel; const apt_dir_layout_t *dir_layout = channel->engine->dir_layout; const mpf_codec_descriptor_t *descriptor = mrcp_engine_sink_stream_codec_get(channel); char *file_name = apr_psprintf(channel->pool,"rec-%dkHz-%s-%"MRCP_REQUEST_ID_FMT".pcm", descriptor ? descriptor->sampling_rate/1000 : 8, request->channel_id.session_id.buf, request->start_line.request_id); char *file_path = apt_datadir_filepath_get(dir_layout,file_name,channel->pool); if(!file_path) { return FALSE; } if(recorder_channel->audio_out) { fclose(recorder_channel->audio_out); recorder_channel->audio_out = NULL; } recorder_channel->audio_out = fopen(file_path,"wb"); if(!recorder_channel->audio_out) { return FALSE; } recorder_channel->file_name = file_name; 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; }
/** Process SPEAK request */ static apt_bool_t demo_synth_channel_speak(mrcp_engine_channel_t *channel, mrcp_message_t *request, mrcp_message_t *response) { char *file_path; demo_synth_channel_t *synth_channel = channel->method_obj; synth_channel->time_to_complete = 0; file_path = apt_datadir_filepath_get(channel->engine->dir_layout,DEMO_SPEECH_SOURCE_FILE,channel->pool); if(file_path) { synth_channel->audio_file = fopen(file_path,"rb"); if(synth_channel->audio_file) { apt_log(APT_PRIO_INFO,"Set [%s] as Speech Source",file_path); } else { apt_log(APT_PRIO_INFO,"No Speech Source [%s] Found",file_path); /* calculate estimated time to complete */ if(mrcp_generic_header_property_check(request,GENERIC_HEADER_CONTENT_LENGTH) == TRUE) { mrcp_generic_header_t *generic_header = mrcp_generic_header_get(request); if(generic_header) { synth_channel->time_to_complete = generic_header->content_length * 10; /* 10 msec per character */ } } } } response->start_line.request_state = MRCP_REQUEST_STATE_INPROGRESS; /* send asynchronous response */ mrcp_engine_channel_message_send(channel,response); synth_channel->speak_request = request; 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 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; }
FILE* VerifierSession::GetAudioIn(const mpf_codec_descriptor_t* pDescriptor, apr_pool_t* pool) const { const VerifierScenario* pScenario = GetScenario(); const char* pVoiceprintIdentifier = pScenario->GetVoiceprintIdentifier(); if(!pVoiceprintIdentifier) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"No Voiceprint Specified"); return NULL; } const char* pFileName = apr_psprintf(pool,"%s-%dkHz.pcm", pVoiceprintIdentifier, pDescriptor->sampling_rate/1000); apt_dir_layout_t* pDirLayout = pScenario->GetDirLayout(); const char* pFilePath = apt_datadir_filepath_get(pDirLayout,pFileName,pool); if(!pFilePath) return NULL; FILE* pFile = fopen(pFilePath,"rb"); if(!pFile) { apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Cannot Find [%s]",pFilePath); return NULL; } apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Set [%s] as Speech Source",pFilePath); return pFile; }
/* Load demo verification result */ static apt_bool_t demo_verifier_result_load(demo_verifier_channel_t *verifier_channel, mrcp_message_t *message) { FILE *file; mrcp_engine_channel_t *channel = verifier_channel->channel; const apt_dir_layout_t *dir_layout = channel->engine->dir_layout; char *file_path = apt_datadir_filepath_get(dir_layout,"result-verification.xml",message->pool); if(!file_path) { return FALSE; } /* read the demo result from file */ file = fopen(file_path,"r"); if(file) { mrcp_generic_header_t *generic_header; char text[1024]; apr_size_t size; size = fread(text,1,sizeof(text),file); apt_string_assign_n(&message->body,text,size,message->pool); fclose(file); /* get/allocate generic header */ generic_header = mrcp_generic_header_prepare(message); if(generic_header) { /* set content types */ apt_string_assign(&generic_header->content_type,"application/nlsml+xml",message->pool); mrcp_generic_header_property_add(message,GENERIC_HEADER_CONTENT_TYPE); } } return TRUE; }
FILE* SynthSession::GetAudioOut(const mpf_codec_descriptor_t* pDescriptor, apr_pool_t* pool) const { char* pFileName = apr_psprintf(pool,"synth-%dkHz-%s.pcm",pDescriptor->sampling_rate/1000, GetMrcpSessionId()); apt_dir_layout_t* pDirLayout = GetScenario()->GetDirLayout(); char* pFilePath = apt_datadir_filepath_get(pDirLayout,pFileName,pool); if(!pFilePath) return NULL; return fopen(pFilePath,"wb"); }
/** Create file reader descriptor */ static mpf_audio_file_descriptor_t* mpf_file_reader_descriptor_create(const mpf_suite_agent_t *agent, const mpf_suite_session_t *session) { const char *file_path = apt_datadir_filepath_get(agent->dir_layout,"demo-8kHz.pcm",session->pool); mpf_audio_file_descriptor_t *descriptor = apr_palloc(session->pool,sizeof(mpf_audio_file_descriptor_t)); descriptor->mask = FILE_READER; descriptor->read_handle = fopen(file_path,"rb"); if(!descriptor->read_handle) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Open File [%s]",file_path); } descriptor->write_handle = NULL; descriptor->codec_descriptor = mpf_codec_lpcm_descriptor_create(8000,1,session->pool); return descriptor; }
static void demo_message_body_set(mrcp_message_t *mrcp_message, const apt_dir_layout_t *dir_layout, const char *file_name) { char *file_path = apt_datadir_filepath_get(dir_layout,file_name,mrcp_message->pool); if(file_path) { FILE *file = fopen(file_path,"r"); if(file) { char text[1024]; apr_size_t size; size = fread(text,1,sizeof(text),file); apt_string_assign_n(&mrcp_message->body,text,size,mrcp_message->pool); fclose(file); } } }
/** Process RECOGNIZE request */ static apt_bool_t demo_recog_channel_recognize(mrcp_engine_channel_t *channel, mrcp_message_t *request, mrcp_message_t *response) { /* process RECOGNIZE request */ mrcp_recog_header_t *recog_header; demo_recog_channel_t *recog_channel = channel->method_obj; const mpf_codec_descriptor_t *descriptor = mrcp_engine_sink_stream_codec_get(channel); if(!descriptor) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Get Codec Descriptor "APT_SIDRES_FMT, MRCP_MESSAGE_SIDRES(request)); response->start_line.status_code = MRCP_STATUS_CODE_METHOD_FAILED; return FALSE; } recog_channel->timers_started = TRUE; /* get recognizer header */ recog_header = mrcp_resource_header_get(request); if(recog_header) { if(mrcp_resource_header_property_check(request,RECOGNIZER_HEADER_START_INPUT_TIMERS) == TRUE) { recog_channel->timers_started = recog_header->start_input_timers; } if(mrcp_resource_header_property_check(request,RECOGNIZER_HEADER_NO_INPUT_TIMEOUT) == TRUE) { mpf_activity_detector_noinput_timeout_set(recog_channel->detector,recog_header->no_input_timeout); } if(mrcp_resource_header_property_check(request,RECOGNIZER_HEADER_SPEECH_COMPLETE_TIMEOUT) == TRUE) { mpf_activity_detector_silence_timeout_set(recog_channel->detector,recog_header->speech_complete_timeout); } } if(!recog_channel->audio_out) { const apt_dir_layout_t *dir_layout = channel->engine->dir_layout; char *file_name = apr_psprintf(channel->pool,"utter-%dkHz-%s.pcm", descriptor->sampling_rate/1000, request->channel_id.session_id.buf); char *file_path = apt_datadir_filepath_get(dir_layout,file_name,channel->pool); if(file_path) { recog_channel->audio_out = fopen(file_path,"wb"); } } response->start_line.request_state = MRCP_REQUEST_STATE_INPROGRESS; /* send asynchronous response */ mrcp_engine_channel_message_send(channel,response); recog_channel->recog_request = request; 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; }
/** Process SPEAK request */ static apt_bool_t demo_synth_channel_speak(mrcp_engine_channel_t *channel, mrcp_message_t *request, mrcp_message_t *response) { char *file_path = NULL; demo_synth_channel_t *synth_channel = channel->method_obj; const mpf_codec_descriptor_t *descriptor = mrcp_engine_source_stream_codec_get(channel); if(!descriptor) { apt_log(SYNTH_LOG_MARK,APT_PRIO_WARNING,"Failed to Get Codec Descriptor "APT_SIDRES_FMT, MRCP_MESSAGE_SIDRES(request)); response->start_line.status_code = MRCP_STATUS_CODE_METHOD_FAILED; return FALSE; } synth_channel->time_to_complete = 0; if(channel->engine) { char *file_name = apr_psprintf(channel->pool,"demo-%dkHz.pcm",descriptor->sampling_rate/1000); file_path = apt_datadir_filepath_get(channel->engine->dir_layout,file_name,channel->pool); } if(file_path) { synth_channel->audio_file = fopen(file_path,"rb"); if(synth_channel->audio_file) { apt_log(SYNTH_LOG_MARK,APT_PRIO_INFO,"Set [%s] as Speech Source "APT_SIDRES_FMT, file_path, MRCP_MESSAGE_SIDRES(request)); } else { apt_log(SYNTH_LOG_MARK,APT_PRIO_INFO,"No Speech Source [%s] Found "APT_SIDRES_FMT, file_path, MRCP_MESSAGE_SIDRES(request)); /* calculate estimated time to complete */ if(mrcp_generic_header_property_check(request,GENERIC_HEADER_CONTENT_LENGTH) == TRUE) { mrcp_generic_header_t *generic_header = mrcp_generic_header_get(request); if(generic_header) { synth_channel->time_to_complete = generic_header->content_length * 10; /* 10 msec per character */ } } } } response->start_line.request_state = MRCP_REQUEST_STATE_INPROGRESS; /* send asynchronous response */ mrcp_engine_channel_message_send(channel,response); synth_channel->speak_request = request; return TRUE; }
const char* UmcScenario::LoadFileContent(const char* pFileName, apr_pool_t* pool) const { if(!m_pDirLayout || !pFileName) return NULL; char* pFilePath = apt_datadir_filepath_get(m_pDirLayout,pFileName,pool); if(!pFilePath) return NULL; FILE* pFile = fopen(pFilePath,"r"); if(!pFile) return NULL; char text[1024]; apr_size_t size; size = fread(text,1,sizeof(text)-1,pFile); text[size] = '\0'; fclose(pFile); return apr_pstrdup(pool,text); }
FILE* RecorderSession::GetAudioIn(const mpf_codec_descriptor_t* pDescriptor, apr_pool_t* pool) const { const char* pFileName = GetScenario()->GetAudioSource(); if(!pFileName) { pFileName = apr_psprintf(pool,"demo-%dkHz.pcm",pDescriptor->sampling_rate/1000); } apt_dir_layout_t* pDirLayout = GetScenario()->GetDirLayout(); const char* pFilePath = apt_datadir_filepath_get(pDirLayout,pFileName,pool); if(!pFilePath) return NULL; FILE* pFile = fopen(pFilePath,"rb"); if(!pFile) { apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Cannot Find [%s]",pFilePath); return NULL; } apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Set [%s] as Speech Source",pFilePath); return pFile; }
/** 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; }
const char* UmcScenario::LoadFileContent(const char* pFileName, apr_size_t& size, apr_pool_t* pool) const { if(!m_pDirLayout || !pFileName) return NULL; char* pFilePath = apt_datadir_filepath_get(m_pDirLayout,pFileName,pool); if(!pFilePath) return NULL; apr_file_t *pFile; if(apr_file_open(&pFile,pFilePath,APR_FOPEN_READ|APR_FOPEN_BINARY,0,pool) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Open File %s",pFilePath); return NULL; } apr_finfo_t finfo; if(apr_file_info_get(&finfo,APR_FINFO_SIZE,pFile) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Get File Info %s",pFilePath); apr_file_close(pFile); return NULL; } size = (apr_size_t)finfo.size; char* pContent = (char*) apr_palloc(pool,size+1); apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Load File Content size [%"APR_SIZE_T_FMT" bytes] %s",size,pFilePath); if(apr_file_read(pFile,pContent,&size) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Read Content %s",pFilePath); apr_file_close(pFile); return NULL; } pContent[size] = '\0'; apr_file_close(pFile); return pContent; }