Class: Rist::Transcriber

Inherits:
Object
  • Object
show all
Defined in:
lib/rist/transcriber.rb,
ext/transcriber/transcriber.c

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Transcriber

Returns a new instance of Transcriber.

Parameters:

  • options (Hash) (defaults to: {})

    the options to initialize a transcriber identical to Pocketsphinx arguments



6
7
8
9
# File 'lib/rist/transcriber.rb', line 6

def initialize(options = {})
  arguments = options.inject([]) { |result, (key, value)| result + ["-#{key.to_s}", value.to_s] }
  initialize_pocketsphinx(arguments);
end

Instance Method Details

#transcribe {|utterance| ... } ⇒ Object

Transcribe speech continuously.

for block { |utterance| … }

Yields:

  • (utterance)

    do whatever you want to do with the utterance transcribed

Yield Parameters:

  • utterance (String)

    the utterance transcribed



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/transcriber/transcriber.c', line 72

static VALUE transcriber_transcribe(VALUE self) {
    Transcriber * transcriber;
    ad_rec_t * ad;
    int16 adbuf[4096];
    int32 k;
    uint8 utt_started, in_speech;
    char const * hyp;
    char const * uttid;
    cmd_ln_t * config;
    ps_decoder_t * ps;

    Data_Get_Struct(self, Transcriber, transcriber);
    ps = transcriber -> decoder;
    config = ps_get_config(ps);

    if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),
                          (int) cmd_ln_float32_r(config,
                                                 "-samprate"))) == NULL)
        close_and_raise(ad, rb_eRuntimeError, "failed to open audio device");
    if (ad_start_rec(ad) < 0)
        close_and_raise(ad, rb_eRuntimeError, "failed to start recording");

    if (ps_start_utt(ps, NULL) < 0)
        close_and_raise(ad, rb_eRuntimeError, "failed to start utterance");
    utt_started = FALSE;

    for (;;) {
        if ((k = ad_read(ad, adbuf, 4096)) < 0)
            close_and_raise(ad, rb_eRuntimeError, "failed to read audio");
        rb_funcall(self, rb_intern("sleep"), 1, rb_float_new(0.1));

        ps_process_raw(ps, adbuf, k, FALSE, FALSE);
        in_speech = ps_get_in_speech(ps);
        if (in_speech && !utt_started) {
            utt_started = TRUE;
        }
        if (!in_speech && utt_started) {
            // speech -> silence transition, 
            // time to start new utterance
            ps_end_utt(ps);
            hyp = ps_get_hyp(ps, NULL, &uttid);

            // yield result
            rb_yield(rb_str_new2(hyp));

            if (ps_start_utt(ps, NULL) < 0)
                close_and_raise(ad, rb_eRuntimeError, "failed to start utterance");
            utt_started = FALSE;
        }
    }
    ad_close(ad);
}