-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpcm2bv_stream.js
148 lines (134 loc) · 4.35 KB
/
pcm2bv_stream.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const m=require("./Pcm2BV.js");
const Readable = require('stream').Readable;
const path = require("path");
const ROOT_PATH = path.resolve(__dirname);
const BufferManager = require(ROOT_PATH+'/lib/buffermanager.js').BufferManager;
m.run();
let cInBuf;
let cOutBuf;
let loadPromise=new Promise((resolve,reject)=>{
m['postRun'].push(()=>{
m._init();
cInBuf=m._malloc(160);
cOutBuf=m._malloc(160);
resolve();
});
});
module.exports.ready=async function (buffer){
return loadPromise;
};
class BV32RecorderWrapper extends Readable {
constructor(options) {
super(options);
this.buffer_manager=new BufferManager();
this._source = options.recorder;
let onData = this.onData = function(chunk) {
// if push() returns false, then stop reading from source
//console.log("on record data:" + chunk.length);
this.buffer_manager.add(chunk);
while(this.buffer_manager.size()>160){
let buffer=this.buffer_manager.slice(0,160);
this.buffer_manager.delete(160);
let encodedBuffer=encode(buffer);
//console.log("encoded to bv format:" + encodedBuffer.length);
if (!this.onData || !this.push(encodedBuffer)) {
if (this._source) {
this._source.removeListener("data", onData);
}
}
}
}.bind(this);
process.nextTick(()=>{
this._source.on("data", onData);
});
// When the source ends, push the EOF-signaling `null` chunk
this._source.on("end", () => {
this.push(null);
//fs.writeFileSync("recorder.pcm",this.buffer_manager.slice(0));
});
this._source.on("error", () => {
this.stopRecording();
});
}
// _read will be called when the stream wants to pull more data in
// the advisory size argument is ignored in this case.
_read(size) {
this._source.read(size);
}
stopRecording() {
//fs.writeFileSync("recorder.pcm",this.buffer_manager.slice(0));
this.push(null);
if(this._source.stopRecording){
this._source.stopRecording();
}
this._source.removeListener("data", this.onData);
this.onData = null;
this._source = null;
}
}
function encode(buf){
let arr = new Uint8Array(buf);
m.writeArrayToMemory(arr,cInBuf);
let ret=m._encode(cInBuf,160,cOutBuf,21);
//console.log(ret);//should be 20
let encoded=m.HEAPU8.subarray(cOutBuf, cOutBuf+ret)
//console.log(Buffer.from(encoded));
return Buffer.from(encoded);
//return encoded;
//console.log("hello world");
}
function decode(buf){
let arr = new Uint8Array(buf);
//console.log(arr);
m.writeArrayToMemory(arr,cInBuf);
let ret=m._decode(cInBuf,20,cOutBuf,160);
console.log(ret);//should be 160
let decoded=m.HEAPU8.subarray(cOutBuf, cOutBuf+ret)
//console.log(Buffer.from(encoded));
return Buffer.from(decoded);
//console.log("hello world");
}
module.exports.BV32RecorderWrapper=BV32RecorderWrapper;
if(module === require.main) {
module.exports.ready().then(()=>{
//const fs=require("fs");
//let stream=fs.createReadStream("recorder1.pcm");
//let wrapper=new BV32RecorderWrapper({recorder:stream});
//wrapper.on("data",(data)=>{
// console.log(data);
//});
const fs=require("fs");
let buf=fs.readFileSync("recorder1.pcm");
let bm=new BufferManager();
for(let i=0;true;i++){
if(buf.length >(i+1)*160){
let tmp=buf.slice(i*160,(i+1)*160);
console.log(tmp,i);
let ret=encode(tmp);
console.log(ret);
bm.add(ret);
}else{
break;
}
}
//fs.writeFileSync("test.bv",ret);
m._init();
let bm1=new BufferManager();
for(let i=0;true;i++){
if(bm.size()>(i+1)*20){
let tmp=bm.slice(i*20,(i+1)*20);
console.log(tmp,i);
let ret=decode(tmp);
console.log(ret);
bm1.add(ret);
}else{
break;
}
}
fs.writeFileSync("recorder1_bv.pcm",bm1.toBuffer());
//let decodeBuf=decode(ret);
//console.log(decodeBuf);
//let ret=encode(buf.slice(0,160));
//console.log(ret);
});
}