Node.js 将Google语音文本结果发送到服务器
我正试图将这个示例代码生成的文本从google for speech发送到nodejs服务器,并显示给用户Node.js 将Google语音文本结果发送到服务器,node.js,http,google-cloud-platform,google-speech-to-text-api,Node.js,Http,Google Cloud Platform,Google Speech To Text Api,我正试图将这个示例代码生成的文本从google for speech发送到nodejs服务器,并显示给用户 var http = require('http'); const recorder = require('node-record-lpcm16'); const speech = require('@google-cloud/speech'); const client = new speech.SpeechClient(); const encoding = 'LINEAR16';
var http = require('http');
const recorder = require('node-record-lpcm16');
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';
const request = {
config: {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
},
interimResults: false, // if you want interim results, set this to true
};
// create a recognize stream
const recognizeStream = client
.streamingRecognize(request)
.on('error', console.error)
.on('data', data =>
//console.log('test');
process.stdout.write(
data.results[0] && data.results[0].alternatives[0]
? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
: '\n\nReached transcription time limit, press Ctrl+C\n'
)
writeToServer(data.results[0].alternatives[0].transcript);
);
function writeToServer(data){
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(data);
res.end();
}).listen(8080);
}
// start recording and send the microphone input to the speech api.
// ensure SoX is installed, see https://www.npmjs.com/package/node-record-lpcm16#dependencies
recorder
.record({
sampleRateHertz: sampleRateHertz,
threshold: 0,
// other options, see https://www.npmjs.com/package/node-record-lpcm16#options
verbose: false,
recordProgram: 'rec', // try also "arecord" or "sox"
silence: '1.0',
})
.stream()
.on('error', console.error)
.pipe(recognizeStream);
console.log('listening, press Ctrl+C to stop.');
我很难从谷歌语音客户端访问成绩单。此外,对于如何从webapp而不是本地麦克风传递麦克风输入的参考,我们也很感激,因为我们的目标是通过浏览器从用户获取麦克风输入,并将其传递到google speech to text api。我向您推荐以下工作流程: 录制用户音频->将其发送到您的web应用->用谷歌语音转换成文本->将响应发送到客户端 对于我推荐的页面呈现,它是一个最小且灵活的Node.js web应用程序框架,为web和移动应用程序提供了一组强大的功能 设置web应用后,您可以尝试录制用户麦克风,然后将其发送到web应用 客户:
var filename = new Date().toISOString();
//filename to send to server without extension
//upload link
var upload = document.createElement('a');
upload.href = “/transcriptaudio”;
upload.innerHTML = "Upload";
upload.addEventListener("click", function(event) {
var xhr = new XMLHttpRequest();
xhr.onload = function(e) {
if (this.readyState === 4) {
console.log("Server returned: ", e.target.responseText);
}
};
var fd = new FormData();
fd.append("audio_data", blob, filename);
xhr.open("POST", "upload.php", true);
xhr.send(fd);
})
li.appendChild(document.createTextNode(" ")) //add a space in between
li.appendChild(upload) //add the upload link to li
服务器:
app.post(‘/transcriptaudio’, function (req, res) {
audio = req.body.audio_data;
// create a recognize stream
const recognizeStream = client
.streamingRecognize(audio)
.on('error', console.error)
.on('data', data =>
//console.log('test');
process.stdout.write(
data.results[0] && data.results[0].alternatives[0]
? Transcription: ${data.results[0].alternatives[0].transcript}\n
: '\n\nReached transcription time limit, press Ctrl+C\n'
)
res.send(data.results[0].alternatives[0].transcript);
);
});