-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (59 loc) · 2.03 KB
/
Copy pathindex.js
File metadata and controls
77 lines (59 loc) · 2.03 KB
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
'use strict';
const Format = require('bin-format');
const bitutils = require('./src/bitutils');
/* Partial FLAC header format.
* (we only change the number of samples in the data stream)
*/
var FLACStreamInfo = new Format()
.uint32BE('header')
.uint16BE('minblocksize')
.uint16BE('maxblocksize')
.buffer('minframesize', 3) // uint24
.buffer('maxframesize', 3) // uint24
.buffer('data', 8) // uint64
.buffer('md5', 16);
var FLACHeader = new Format()
.buffer('signature', 4)
.nest('streaminfo', FLACStreamInfo)
.buffer('unparsed', 'eof');
function update_num_samples(headerData, numSamples) {
var parsed = FLACHeader.parse(headerData);
var bits = bitutils.bufferToBits(parsed.streaminfo.data);
var numSamplesBits = bitutils.uintToBits(numSamples, 36);
for (var i=0; i<numSamplesBits.length; ++i) {
bits[bits.length - 36 + i] = numSamplesBits[i];
}
parsed.streaminfo.data = bitutils.bitsToBuffer(bits);
return FLACHeader.write(parsed);
}
function subsection(writable, seekdata, start, end, get_read_stream) {
if (start < 0 || start >= seekdata.seekpoints.length)
throw new Error('Stream start index out of bounds');
if (end < 0 || end >= seekdata.seekpoints.length)
throw new Error('Stream end index out of bounds');
if (end <= start)
throw new Error('Stream end index larger or equal to start index');
var chunkStart = seekdata.seekpoints[start];
var chunkEnd = seekdata.seekpoints[end];
var numSamples = chunkEnd.sample - chunkStart.sample;
var headerReader = get_read_stream(0, seekdata.audio_offset);
var buffers = [];
headerReader.on('data', (chunk) => {
buffers.push(chunk);
});
headerReader.on('end', () => {
var headerData = Buffer.concat(buffers);
buffers.length = 0;
headerData = update_num_samples(headerData, numSamples);
writable.write(headerData);
var sampleData = get_read_stream(
seekdata.audio_offset + chunkStart.offset,
seekdata.audio_offset + chunkEnd.offset
);
sampleData.on('end', () => {
writable.end();
});
sampleData.pipe(writable);
});
}
module.exports = subsection;