[Freeswitch-branches] [commit] r5596 - freeswitch/branches/greenlizard/src/mod/asr_tts/mod_openmrcp

Freeswitch SVN greenlizard at freeswitch.org
Mon Aug 13 19:48:43 EDT 2007


Author: greenlizard
Date: Mon Aug 13 19:48:43 2007
New Revision: 5596

Modified:
   freeswitch/branches/greenlizard/src/mod/asr_tts/mod_openmrcp/mod_openmrcp.c

Log:
speak-finished event now being handled, passed up to freeswitch to let it know speech has stopped.  audio sounds fine

Modified: freeswitch/branches/greenlizard/src/mod/asr_tts/mod_openmrcp/mod_openmrcp.c
==============================================================================
--- freeswitch/branches/greenlizard/src/mod/asr_tts/mod_openmrcp/mod_openmrcp.c	(original)
+++ freeswitch/branches/greenlizard/src/mod/asr_tts/mod_openmrcp/mod_openmrcp.c	Mon Aug 13 19:48:43 2007
@@ -176,7 +176,8 @@
 typedef enum {
 	FLAG_HAS_TEXT = (1 << 0),
 	FLAG_BARGE = (1 << 1),
-	FLAG_READY = (1 << 2)
+	FLAG_READY = (1 << 2),
+	FLAG_SPEAK_COMPLETE = (1 << 3)
 } mrcp_flag_t;
 
 typedef struct {
@@ -501,8 +502,21 @@
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "!tts_session\n");
 		return MRCP_STATUS_FAILURE;
 	}
+
+	if (mrcp_message->start_line.method_name) {
+		if (!strcmp(mrcp_message->start_line.method_name,"SPEAK-COMPLETE")) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "setting FLAG_SPEAK_COMPLETE\n");
+			switch_set_flag_locked(tts_session, FLAG_SPEAK_COMPLETE);
+		}
+		else {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "ignoring method: %s\n", mrcp_message->start_line.method_name);
+		}
+	}
+
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "mrcp msg body: %s\n", mrcp_message->body);
 		
+	
+
 	return tts_session_signal_event(tts_session,OPENMRCP_EVENT_CHANNEL_MODIFY);
 }
 
@@ -583,45 +597,6 @@
 }
 
 
-/**
- * Called back by openmrcp client thread every time it receives audio 
- * from the TTS server we are connected to.  Puts audio in a queueu
- * and it will be pulled out from read_tts
- */
-static apr_status_t openmrcp_tts_write_frame_NEW(audio_sink_t *sink, media_frame_t *frame)
-{
-	tts_session_t *tts_session = sink->object;	
-	media_frame_t *media_frame;
-	switch_byte_t *buffer;
-	size_t len;
-	apr_status_t status = MRCP_STATUS_SUCCESS;
-
-	len = frame->codec_frame.size;
-
-	// since *frame might get freed by caller (true or false?), allocate a
-	// new buffer and copy *data into it.
-	buffer = (switch_byte_t *) switch_core_alloc(tts_session->pool, sizeof(switch_byte_t)*len);
-	if (!buffer) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not allocate buffer\n");
-		return MRCP_STATUS_FAILURE;
-	}
-
-	// push audio to buffer 
-	switch_mutex_lock(tts_session->audio_lock);
-	if (switch_buffer_write(tts_session->audio_buffer, buffer, len) == 0) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Could not write to buffer\n");
-		status = MRCP_STATUS_FAILURE;
-	}
-	else {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "wrote %d bytes to buffer\n", len);
-	}
-	switch_mutex_unlock(tts_session->audio_lock);
-
-	return status;
-
-}
-
-
 /** 
  * Called back by openmcp client thread every time its ready for more audio to send
  * the recognition server we are connected to.  Reads data that was put into a 
@@ -1176,24 +1151,6 @@
  * TODO: check the blocking flag passed in flags and act accordingly  
  *       (see mod_cepstral.c)
  */
-static switch_status_t openmrcp_read_tts_OLD(switch_speech_handle_t *sh, void *data, size_t *datalen, uint32_t *rate, switch_speech_flag_t *flags)
-{
-	media_frame_t *queue_frame = NULL;
-	tts_session_t *tts_session = (tts_session_t *) sh->private_info;
-
-	if (switch_queue_trypop(tts_session->audio_queue, (void *) &queue_frame)) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "could not pop from queue\n");
-		return SWITCH_STATUS_SUCCESS;	
-	}
-	else {
-		memcpy(data, queue_frame->codec_frame.buffer, queue_frame->codec_frame.size);
-		*datalen = queue_frame->codec_frame.size;
-		*rate = 8000;
-		return SWITCH_STATUS_SUCCESS;	
-	}
-
-}
-
 static switch_status_t openmrcp_read_tts(switch_speech_handle_t *sh, void *data, size_t *datalen, uint32_t *rate, switch_speech_flag_t *flags)
 {
 	media_frame_t *queue_frame = NULL;
@@ -1201,27 +1158,44 @@
 	size_t return_len=0;
 	size_t amt2copy=0;
 	size_t desired = *datalen;
-
+	switch_byte_t *audiodata = (switch_byte_t *) data;
 
 	while(return_len < desired) {
 
+		if (switch_test_flag(tts_session, FLAG_SPEAK_COMPLETE)) {
+			// tell fs we are done
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FLAG_SPEAK_COMPLETE\n");
+			return SWITCH_STATUS_BREAK;
+		}
+
 		if (switch_queue_pop(tts_session->audio_queue, (void *) &queue_frame)) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "could not pop from queue\n");
+			if (switch_test_flag(tts_session, FLAG_SPEAK_COMPLETE)) {
+				// tell fs we are done
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FLAG_SPEAK_COMPLETE\n");
+				return SWITCH_STATUS_BREAK;
+			}
 			break;
 		}
 		else {
+			if (switch_test_flag(tts_session, FLAG_SPEAK_COMPLETE)) {
+				// tell fs we are done
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FLAG_SPEAK_COMPLETE\n");
+				return SWITCH_STATUS_BREAK;
+			}
+
 			if (queue_frame->codec_frame.size >= desired) {
 				amt2copy = desired;
 			}
 			else {
-				// limit the amt we copy to data to be LTE datalen
+				// limit the amt we copy to audiodata to be LTE datalen
 				// if the queue frame has _more_, just ignore it (TODO: fix this!)
 				amt2copy = queue_frame->codec_frame.size;
 			}
-			memcpy(data, queue_frame->codec_frame.buffer, amt2copy);
+			memcpy(audiodata, queue_frame->codec_frame.buffer, amt2copy);
 			return_len += amt2copy;
 			*datalen = return_len;
-			data += amt2copy;  // move pointer forward
+			audiodata += amt2copy;  // move pointer forward
 			*rate = 8000;
 		}
 
@@ -1232,49 +1206,13 @@
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "no data read from buffer\n");
 		return SWITCH_STATUS_FALSE;	
 	}
-	else if (datalen < desired) {
+	else if (*datalen < desired) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "return_len: (%d) < desired: (%d)\n", return_len, desired);
 	}
 
 	return SWITCH_STATUS_SUCCESS;	
 }
 
-static switch_status_t openmrcp_read_tts_NEW(switch_speech_handle_t *sh, void *data, size_t *datalen, uint32_t *rate, switch_speech_flag_t *flags)
-{
-	media_frame_t *queue_frame = NULL;
-	tts_session_t *tts_session = (tts_session_t *) sh->private_info;
-	switch_status_t status = SWITCH_STATUS_FALSE;
-	size_t used, padding = 0;
-	size_t desired = *datalen;
-
-	*rate = 8000;
-
-	switch_mutex_lock(tts_session->audio_lock);
-	used = switch_buffer_inuse(tts_session->audio_buffer);
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "used: %d\n", used);
-	switch_mutex_unlock(tts_session->audio_lock);
-
-	if (used == 0) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "used == 0\n");
-		//*datalen = 0;
-		return SWITCH_STATUS_SUCCESS;
-	}
-
-	if (used < desired) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "used < desired (%d)\n", desired);
-		//*datalen = 0;
-		return SWITCH_STATUS_SUCCESS;
-	}
-
-	switch_mutex_lock(tts_session->audio_lock);
-	*datalen = switch_buffer_read(tts_session->audio_buffer, data, desired);
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "successfully read %d bytes into data buffer\n", *datalen);
-	switch_mutex_unlock(tts_session->audio_lock);
-
-	return SWITCH_STATUS_SUCCESS;
-}
-
-
 
 static void openmrcp_flush_tts(switch_speech_handle_t *sh)
 {
@@ -1385,6 +1323,8 @@
 		}
 	}
 
+	switch_xml_free(xml);
+
 	return SWITCH_STATUS_SUCCESS;
 }
 



More information about the Freeswitch-branches mailing list