-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (109 loc) · 3.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
124
125
126
const DeepSpeech = require('deepspeech');
const Sox = require('sox-stream');
const MemoryStream = require('memory-stream');
const Duplex = require('stream').Duplex;
const Wav = require('node-wav');
const fs = require('fs');
const fs = require('fs');
const SONGS_FILE = 'songs.json';
var SONGS = JSON.parse(fs.readFileSync(SONGS_FILE, 'utf8') );
function add_song(keySong,url){
validURL(url);
let id=validKEY(key);
if (id ==1) overwrite_key_request();
add_song_runtime(keySong,url);
}
function add_song_runtime(keySong,url){
SONGS[keySong] = url;
fs.writeFile("songs.json", JSON.stringify(SONGS), err => {
if (err) throw err;
console.log("Done writing"); // Success
});
}
function create_new_row_json(keySong,url){
SONGS[keySong]=url;
}
/**
*
* @param {*} key
* @returns 1 on key duplicated, 0 on unique key
*/
function validKEY(key){
let dupkey = 0;
for(songKey of SONGS){
if (dupkey = (key == songKey))break;
}
return dupkey;
}
function overwrite_key_request(){} //TODO
//add_song_runtime("pippo","https://www.youtube.com/watch?v=zAml4Y46ToQ");
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return !!pattern.test(str);
}
// Deepspeech
const LM_ALPHA = 0;
const LM_BETA = 0;
let modelPath = './model/output_graph.pbmm';
let model = new DeepSpeech.Model(modelPath);
let desiredSampleRate = model.sampleRate();
let scorerPath = './model/scorer';
model.enableExternalScorer(scorerPath);
/**
* Converts audio file to text using the deepspeech model
* @param {*} audioFile
* @returns transcribed text
*/
function transcribe(audioFile) {
const buffer = fs.readFileSync(audioFile);
const result = Wav.decode(buffer);
if (result.sampleRate < desiredSampleRate) {
console.error('Warning: original sample rate (' + result.sampleRate + ') is lower than ' + desiredSampleRate + 'Hz. Up-sampling might produce erratic speech recognition.');
}
let audioStream = new MemoryStream();
bufferToStream(buffer).
pipe(Sox({
input: {
volume: 0.8,
},
global: {
'no-dither': true,
},
output: {
bits: 16,
rate: desiredSampleRate,
channels: 1,
encoding: 'signed-integer',
endian: 'little',
compression: 0.0,
type: 'raw'
}
})
).
pipe(audioStream);
audioStream.on('finish', () => {
let audioBuffer = audioStream.toBuffer();
const audioLength = (audioBuffer.length / 2) * (1 / desiredSampleRate);
console.log('audio length', audioLength);
let result = model.stt(audioBuffer);
console.log(result);
return result;
});
}
/**
*
* @param {*} buffer
* @returns a stream
*/
function bufferToStream(buffer) {
let stream = new Duplex();
stream.push(buffer);
stream.push(null);
return stream;
}
let test_record = 'record.wav'