【问题标题】:Nodejs - IBM Watson Speech To Text Websocket connection errorNodejs - IBM Watson Speech To Text Websocket 连接错误
【发布时间】:2020-02-11 01:38:29
【问题描述】:

我一直在尝试遵循 IBM (https://developer.ibm.com/tutorials/add-a-trigger-word-to-your-watson-assistant/) 的官方指南来构建一个 Nodejs 语音助手,它可以在识别“唤醒词”后回答。该指南似乎有点过时了,所以我决定将它与来自 npm 的 Assistant V2 和 ibm-watson 5.2.0 包一起使用。

我收到一个 WebSocket 连接错误,但没有太多信息,问题似乎在第 33 行,我发送到“recognizeUsingWebsocket”方法的参数。我错过了这个参数吗?

const AssistantV2 = require('ibm-watson/assistant/v2');
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const mic = require('mic');

const conversation = new AssistantV2({
    authenticator: new IamAuthenticator({ apikey: '<api_key>' }),
    url: 'https://gateway-lon.watsonplatform.net/assistant/api/',
    version: '2018-09-19'
});

const speechToText = new SpeechToTextV1({
    authenticator: new IamAuthenticator({ apikey: '<api_key>' }),
    serviceUrl: 'https://gateway-lon.watsonplatform.net/speech-to-text/api'
});

const textToSpeech = new TextToSpeechV1({
    authenticator: new IamAuthenticator({ apikey: '<api_key>' })
});

const micParams = {
    rate: 44100,
    channels: 2,
    debug: true,
    exitOnSilence: 6
};
const microphone = mic(micParams);
const micInputStream = microphone.getAudioStream();

const textStream = micInputStream
    .pipe(
        speechToText.recognizeUsingWebSocket({
            accessToken:'<access_token>',
            contentType: 'audio/l16; rate=44100; channels=2',
            interimResults: true,
            inactivityTimeout: -1
        })
    )
    .setEncoding('utf8');

const speakResponse = (text) => {
    var params = {
        text: text,
        accept: 'audio/wav',
        voice: 'en-US_AllisonVoice'
    };

    var writeStream = fs.createWriteStream('output.wav');
    textToSpeech
        .synthesize(params)
        .then((audio) => {
            audio.pipe(writeStream);
        })
        .catch((err) => {
            console.log('error:', err);
        });

    writeStream.on('finish', function() {
        ffprobe('output.wav', function(err, probeData) {
            if (probeData) {
                pauseDuration = probeData.format.duration;
                microphone.pause();
                speaker.play('output.wav');
                startTime = new Date();
            }
        });
    });
    writeStream.on('error', function(err) {
        console.log('Text-to-speech streaming error: ' + err);
    });
};

function printContext(header) {
    if (debug) {
        console.log(header);

        if (context.system) {
            if (context.system.dialog_stack) {
                const util = require('util');
                console.log("     dialog_stack: ['" + util.inspect(context.system.dialog_stack, false, null) + "']");
            }
        }
    }
}

function watsonSays(response) {
    if (typeof response !== 'undefined') {
        console.log('Watson says:', response);
    }
}

function isActive(text) {
    var elapsedTime = new Date() - startTime;

    if (elapsedTime > SLEEP_TIME) {
        // go to sleep
        startTime = new Date();
        botIsActive = false;
    }

    if (botIsActive) {
        // in active conversation, so stay awake
        startTime = new Date();
        return true;
    } else {
        // we are asleep - did we get a wake up call?
        if (text.toLowerCase().indexOf(wakeWord) > -1) {
            // time to wake up
            console.log('App just woke up');
            botIsActive = true;
        } else {
            // false alarm, go back to sleep
            console.log('App needs the wake up command');
        }
        return botIsActive;
    }
}

function performConversation() {
    console.log('App is listening, you may speak now.');
    textStream.on('data', (user_speech_text) => {
        userSpeechText = user_speech_text.toLowerCase();
        console.log('\n\nApp hears: ', user_speech_text);
        if (isActive(user_speech_text)) {
            conversation.message(
                {
                    assistantId: process.env.ASSISTANT_ID,
                    sessionId: process.env.SESSION_ID,
                    input: { text: user_speech_text }
                },
                (err, response) => {
                    console.log(err);
                    context = response.context;

                    watson_response = response.output.text[0];
                    if (watson_response) {
                        speakResponse(watson_response);
                    }
                    watsonSays(watson_response);
                }
            );
        }
    });
}

microphone.start();
performConversation();

【问题讨论】:

    标签: node.js ibm-watson voice-recognition


    【解决方案1】:

    您似乎没有为 websockets 使用正确的端点: 注意:api 端点在 2019 年 12 月有一个新版本

    你使用: https://gateway-lon.watsonplatform.net/speech-to-text/api

    它应该是这样的:(我认为前缀 wss 是关键) wss://api.{location}.speech-to-text.watson.cloud.ibm.com/instances/{instance_id}/v1/recognize

    cf API 参考:https://cloud.ibm.com/apidocs/speech-to-text/speech-to-text#websocket_methods

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      相关资源
      最近更新 更多