Commit b0d4ba4e authored by Yoann HOUPERT's avatar Yoann HOUPERT

add transcription offline

parent 00848f7a
Pipeline #14046 failed with stage
MODELS_PATH=./models/current_model/model/
MODELS_PATH=./models/current_model/hublot_model/
MODEL_PATH_OFFLINE=./models/current_model/
OFFLINE_PORT=9050
OFFLINE_PORT=8888
YAML=/opt/models/gmm_hmm3.yaml
NB_WORKERS=2
ASD_SERVER_IP="localhost"
......
......@@ -64,8 +64,11 @@
"channels": 1,
"srate": 16000
},
"host": "hublot_offline_1",
"port": 9999,
"model": "current_model"
"host": "hublot_offline-server_1",
"port": 8888,
"model": "hublot_model",
"api":{
"upload": "upload"
}
}
}
......@@ -97,6 +97,8 @@ module.exports = config => {
connectionToClient.send(xmlHttp.responseText);
} catch (err) {
console.error('proxy: recoRecoWS error', err);
console.error('It is probably already close',);
}
} else {
console.error('Online reco: error trying to reach http://%s:%s/resources',
......@@ -131,9 +133,6 @@ module.exports = config => {
// (e.g. when stopping). This is expected
}
});
connectionToKaldi.on('close', () => {
connectionToClient.close()
});
});
return connectionToKaldi;
}
......@@ -179,7 +178,6 @@ module.exports = config => {
} else {
transcriptor.streamPush(thisUserStream, data)
}
console.log('stream data save')
streamData.wstream.write(data);
})
......
......@@ -22,13 +22,16 @@ const uuidv1 = require('uuid/v1');
const request = require('request');
const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg');
const ffprobeInstaller = require('@ffprobe-installer/ffprobe');
const ffprobeInstaller = require('@ffprobe-installer/ffprobe');
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
ffmpeg.setFfprobePath(ffprobeInstaller.path);
const successCode = 200;
const interalServerCode = 500;
module.exports = ffmpeg;
module.exports = config => {
......@@ -36,27 +39,67 @@ module.exports = config => {
let timestamp = undefined;
let userConf = 0;
function offlineTranscription(){
//TODO
function offlineTranscription(enhancementlCallback) {
let port = config.port;
if (process.env.OFFLINE_PORT !== undefined) {
port = process.env.OFFLINE_PORT;
}
const url = 'http://' + config.host + ':' + port + '/' + config.api.upload;
const uiid = uuidv1();
let myUuidString = uiid.toString();
const re = /-/g;
myUuidString = re[Symbol.replace](myUuidString, '');
const options = {
url,
formData: {
wavFile: {
value: fs.ReadStream(config.folder.audioMerged+'/merged.wav'),
options: {
filename: myUuidString,
contentType: 'audio/x-wav'
}
},
model: config.model
},
encoding: null
};
request.post(options, enhancementlCallback);
}
function resumeGenerator(){
function resumeGenerator() {
let nbFile = 0;
let cmd = ffmpeg()
fs.readdirSync(config.folder.audioMerged).forEach(file => {
cmd.input(config.folder.audioMerged+"/"+file)
cmd.input(config.folder.audioMerged + "/" + file)
nbFile++;
})
cmd.complexFilter('amix=inputs='+nbFile+':duration=first:dropout_transition=3')
.save(config.folder.audioMerged+"/merged.wav")
.on('end', function() {
})
cmd.complexFilter('amix=inputs=' + nbFile + ':duration=first:dropout_transition=3')
.save(config.folder.audioMerged + "/merged.wav")
.on('end', function () {
offlineTranscription((err, response, body) => {
let result;
if (err) {
result = {module: 'Offline transcription', error: 'Error during audio transcription', info: err.toString()}
} else if (response === undefined || body === undefined || response.statusCode !== successCode) {
result = {module: 'Offline transcription', error: 'Error during audio transcription'}
}
const json = JSON.parse(body);
if (json.status === 5) {
result = {module: 'Offline transcription', error: 'Error during audio transcription'}
}
const transcription = json.transcript.transcription;
console.log(transcription)
})
})
}
return {
createFolder : () => {
createFolder: () => {
if (!fs.existsSync(config.folder.audioRaw)) {
fs.mkdirSync(config.folder.audioRaw);
}
......@@ -67,67 +110,67 @@ module.exports = config => {
fs.mkdirSync(config.folder.audioMerged);
}
},
generateStream : () => {
if(timestamp === undefined){
generateStream: () => {
if (timestamp === undefined) {
timestamp = new Date().getTime()
}
const name = uuidv1()
const path = config.folder.audioRaw+"/"+name+".wav"
const path = config.folder.audioRaw + "/" + name + ".wav"
const streamData = {
"wstream" : fs.createWriteStream(path),
"pathFile" : path,
"wstream": fs.createWriteStream(path),
"pathFile": path,
"fileName": name,
"timestamp" : new Date().getTime()
"timestamp": new Date().getTime()
}
userConf++
return streamData
},
endStream : (streamData) => {
endStream: (streamData) => {
const toSecond = 1000
streamData.wstream.close();
let delay = ((timestamp - streamData.timestamp) / toSecond) * -1;
try{
try {
console.log('generate silence')
ffmpeg()
.input('anullsrc=sample_rate='+config.audio.srate)
.inputFormat('lavfi')
.duration(delay)
.output(config.folder.audioWav+"/silence_"+streamData.fileName+".wav")
.on('end', function() {
console.log('generate audio file')
ffmpeg(streamData.pathFile)
.toFormat(config.audio.format)
.noVideo()
.audioBitrate(config.audio.bitrate)
.audioChannels(config.audio.channels)
.audioFrequency(config.audio.srate)
.save(config.folder.audioWav+"/"+streamData.fileName+".wav")
.on('error', function(err) {
--userConf
console.log('An error occurred: ' + err.message);
})
.on('end', function() {
console.log('merge audio and silence')
ffmpeg()
.input(config.folder.audioWav+"/silence_"+streamData.fileName+".wav")
.input(config.folder.audioWav+"/"+streamData.fileName+".wav")
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
})
.on('end', function() {
console.log('Merging finished !'+streamData.fileName);
if(--userConf === 0){
console.log('generate room audio')
resumeGenerator(config)
}
})
.mergeToFile(config.folder.audioMerged+"/"+streamData.fileName+".wav", config.folder.audio)
.input('anullsrc=sample_rate=' + config.audio.srate)
.inputFormat('lavfi')
.duration(delay)
.output(config.folder.audioWav + "/silence_" + streamData.fileName + ".wav")
.on('end', function () {
console.log('generate audio file')
ffmpeg(streamData.pathFile)
.toFormat(config.audio.format)
.noVideo()
.audioBitrate(config.audio.bitrate)
.audioChannels(config.audio.channels)
.audioFrequency(config.audio.srate)
.save(config.folder.audioWav + "/" + streamData.fileName + ".wav")
.on('error', function (err) {
--userConf
console.log('An error occurred: ' + err.message);
})
.on('end', function () {
console.log('merge audio and silence')
ffmpeg()
.input(config.folder.audioWav + "/silence_" + streamData.fileName + ".wav")
.input(config.folder.audioWav + "/" + streamData.fileName + ".wav")
.on('error', function (err) {
console.log('An error occurred: ' + err.message);
})
.on('end', function () {
console.log('Merging finished !' + streamData.fileName);
if (--userConf === 0) {
console.log('generate room audio')
resumeGenerator(config)
}
})
.mergeToFile(config.folder.audioMerged + "/" + streamData.fileName + ".wav", config.folder.audio)
})
})
})
.run()
} catch(err){
.run()
} catch (err) {
// Sometimes, the client will close the connection unexpectedly
// (e.g. when stopping). This is expected
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment