Skip to content

Commit adf20a3

Browse files
committed
feat: update gst plugin for new endpointer
1 parent 1729e58 commit adf20a3

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

gst/gstpocketsphinx.c

+43-43
Original file line numberDiff line numberDiff line change
@@ -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
669679
gst_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) {

gst/gstpocketsphinx.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#define __GST_POCKETSPHINX_H__
4141

4242
#include <gst/gst.h>
43+
#include <gst/base/gstadapter.h>
4344
#include <pocketsphinx.h>
4445

4546
G_BEGIN_DECLS
@@ -61,16 +62,16 @@ typedef struct _GstPocketSphinxClass GstPocketSphinxClass;
6162
struct _GstPocketSphinx
6263
{
6364
GstElement element;
64-
65+
GstAdapter *adapter;
6566
GstPad *sinkpad, *srcpad;
6667

6768
ps_decoder_t *ps;
69+
ps_endpointer_t *ep;
6870
ps_config_t *config;
6971

70-
gchar *latdir; /**< Output directory for word lattices. */
72+
size_t frame_size;
7173

72-
gboolean speech_started;
73-
gboolean listening_started;
74+
gchar *latdir; /**< Output directory for word lattices. */
7475
gint uttno;
7576

7677
GstClockTime last_result_time; /**< Timestamp of last partial result. */

0 commit comments

Comments
 (0)