@@ -608,6 +608,7 @@ gst_pocketsphinx_init(GstPocketSphinx * ps)
608608 gst_pad_new_from_static_template (& sink_factory , "sink" );
609609 ps -> srcpad =
610610 gst_pad_new_from_static_template (& src_factory , "src" );
611+ ps -> adapter = gst_adapter_new ();
611612
612613 /* Parse default command-line options. */
613614 ps -> config = ps_config_init (NULL );
@@ -641,6 +642,15 @@ gst_pocketsphinx_change_state(GstElement *element, GstStateChange transition)
641642 ("Failed to initialize PocketSphinx" ));
642643 return GST_STATE_CHANGE_FAILURE ;
643644 }
645+ ps -> ep = ps_endpointer_init (0 , 0.0 , 0 ,
646+ ps_config_int (ps -> config , "samprate" ), 0 );
647+ if (ps -> ep == NULL ) {
648+ GST_ELEMENT_ERROR (GST_ELEMENT (ps ), LIBRARY , INIT ,
649+ ("Failed to initialize PocketSphinx endpointer" ),
650+ ("Failed to initialize PocketSphinx endpointer" ));
651+ return GST_STATE_CHANGE_FAILURE ;
652+ }
653+ ps -> frame_size = ps_endpointer_frame_size (ps -> ep ) * 2 ;
644654 break ;
645655 case GST_STATE_CHANGE_READY_TO_NULL :
646656 ps_free (ps -> ps );
@@ -669,52 +679,45 @@ static GstFlowReturn
669679gst_pocketsphinx_chain (GstPad * pad , GstObject * parent , GstBuffer * buffer )
670680{
671681 GstPocketSphinx * ps ;
672- GstMapInfo info ;
673- gboolean in_speech ;
674682
675683 (void )pad ;
676684 ps = GST_POCKETSPHINX (parent );
677685
678- /* Start an utterance for the first buffer we get */
679- if (!ps -> listening_started ) {
680- ps -> listening_started = TRUE;
681- ps -> speech_started = FALSE;
682- ps_start_utt (ps -> ps );
683- }
684-
685- gst_buffer_map (buffer , & info , GST_MAP_READ );
686- ps_process_raw (ps -> ps ,
687- (short * ) info .data ,
688- info .size / sizeof (short ),
689- FALSE, FALSE);
690- gst_buffer_unmap (buffer , & info );
691-
692- in_speech = ps_get_in_speech (ps -> ps );
693- if (in_speech && !ps -> speech_started ) {
694- ps -> speech_started = TRUE;
695- }
696- if (!in_speech && ps -> speech_started ) {
697- gst_pocketsphinx_finalize_utt (ps );
698- } else if (ps -> last_result_time == 0
699- /* Get a partial result every now and then, see if it is different. */
700- /* Check every 100 milliseconds. */
701- || (GST_BUFFER_TIMESTAMP (buffer ) - ps -> last_result_time ) > 100 * 10 * 1000 ) {
702- int32 score ;
703- char const * hyp ;
704-
705- hyp = ps_get_hyp (ps -> ps , & score );
706- ps -> last_result_time = GST_BUFFER_TIMESTAMP (buffer );
707- if (hyp && strlen (hyp ) > 0 ) {
708- if (ps -> last_result == NULL || 0 != strcmp (ps -> last_result , hyp )) {
709- g_free (ps -> last_result );
710- ps -> last_result = g_strdup (hyp );
711- gst_pocketsphinx_post_message (ps , FALSE, ps -> last_result_time ,
712- ps_get_prob (ps -> ps ), hyp );
686+ gst_adapter_push (ps -> adapter , buffer );
687+ while (gst_adapter_available (ps -> adapter ) >= ps -> frame_size ) {
688+ const guint * data = gst_adapter_map (ps -> adapter , ps -> frame_size );
689+ int prev_in_speech = ps_endpointer_in_speech (ps -> ep );
690+ const int16 * speech = ps_endpointer_process (ps -> ep , (int16 * )data );
691+ if (speech != NULL ) {
692+ if (!prev_in_speech )
693+ ps_start_utt (ps -> ps );
694+ ps_process_raw (ps -> ps ,
695+ speech , ps -> frame_size / 2 ,
696+ FALSE, FALSE);
697+ if (!ps_endpointer_in_speech (ps -> ep )) {
698+ gst_pocketsphinx_finalize_utt (ps );
699+ } else if (ps -> last_result_time == 0
700+ /* Get a partial result every now and then, see if it is different. */
701+ /* Check every 100 milliseconds. */
702+ || (GST_BUFFER_TIMESTAMP (buffer ) - ps -> last_result_time ) > 100 * 10 * 1000 ) {
703+ int32 score ;
704+ char const * hyp ;
705+
706+ hyp = ps_get_hyp (ps -> ps , & score );
707+ ps -> last_result_time = GST_BUFFER_TIMESTAMP (buffer );
708+ if (hyp && strlen (hyp ) > 0 ) {
709+ if (ps -> last_result == NULL || 0 != strcmp (ps -> last_result , hyp )) {
710+ g_free (ps -> last_result );
711+ ps -> last_result = g_strdup (hyp );
712+ gst_pocketsphinx_post_message (ps , FALSE, ps -> last_result_time ,
713+ ps_get_prob (ps -> ps ), hyp );
714+ }
715+ }
713716 }
714717 }
715- }
716-
717- gst_buffer_unref ( buffer );
718+ gst_adapter_unmap ( ps -> adapter );
719+ gst_adapter_flush ( ps -> adapter , ps -> frame_size );
720+ }
718721 return GST_FLOW_OK ;
719722}
720723
@@ -727,11 +730,8 @@ gst_pocketsphinx_finalize_utt(GstPocketSphinx *ps)
727730 int32 score ;
728731
729732 hyp = NULL ;
730- if (!ps -> listening_started )
731- return ;
732733
733734 ps_end_utt (ps -> ps );
734- ps -> listening_started = FALSE;
735735 hyp = ps_get_hyp (ps -> ps , & score );
736736
737737 if (hyp ) {
0 commit comments