-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
165 lines (141 loc) · 3.78 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const { Readable } = require('stream')
const binding = require('./js-binding')
class OpusEncoder {
constructor(sampleRate, channels) {
return binding.OpusEncoder.create(sampleRate, channels)
}
static create(sampleRate, channels) {
return binding.OpusEncoder.create(sampleRate, channels)
}
static [Symbol.hasInstance](target) {
return target instanceof binding.OpusEncoder
}
}
/**
* @typedef StreamProbeResult
* @property {Readable} stream The source stream
* @property {binding.ProbeResult} result The probe result
*/
/**
* Attempt to probe a Readable stream
* @param {Readable} stream The readable stream to probe
* @param {Object} options The options
* @param {number} [options.probeSize=2 * 1024 * 1024] The amount of bytes to read from the stream. Defaults to 2 MB.
* @param {boolean} [options.sync=false] Whether to probe synchronously. Defaults to false.
* @returns {Promise<StreamProbeResult>}
*/
async function probeStream(
stream,
options = {
probeSize: 2 * 1024 * 1024,
sync: false,
},
) {
const { probeSize = 2 * 1024 * 1024, sync = false } = options
return new Promise((resolve, reject) => {
if (stream.readableObjectMode) {
reject(new Error('Cannot probe a readable stream in object mode'))
return
}
if (stream.readableEnded) {
reject(new Error('Cannot probe a stream that has ended'))
return
}
let readBuffer = Buffer.alloc(0)
/**
* @type {binding.ProbeResult}
*/
let resolved = null
const finish = (data) => {
stream.off('data', onData)
stream.off('close', onClose)
stream.off('end', onClose)
stream.pause()
resolved = data
if (stream.readableEnded) {
resolve({
stream: Readable.from(readBuffer),
result: data,
})
} else {
if (readBuffer.length > 0) {
stream.push(readBuffer)
}
resolve({
stream,
result: data,
})
}
}
const onClose = async () => {
try {
const probeInner = sync ? probeSync : probe
const result = await probeInner(readBuffer)
if (result != null) resolved = result
} catch {}
finish(resolved || null)
}
const onData = (buffer) => {
readBuffer = Buffer.concat([readBuffer, buffer])
if (readBuffer.length >= probeSize) {
stream.off('data', onData)
stream.pause()
process.nextTick(onClose)
}
}
stream.once('error', reject)
stream.on('data', onData)
stream.once('close', onClose)
stream.once('end', onClose)
})
}
/**
* Reads metadata tags
* @param {import('./js-binding').ProbeResult} result The probe result to read metadata from
*/
function readMetadata(result) {
const res = {
title: null,
author: null,
album: null,
genre: null,
year: null,
duration: null,
composer: null,
bpm: null,
}
if (!result.metadata?.length) return res
result.metadata.forEach((m) => {
if (!m.value) return
if (m.name === 'TIT2') {
res.title = m.value
} else if (m.name === 'TPE1' || m.name === 'TPUB') {
res.author = m.value
} else if (m.name === 'TALB') {
res.album = m.value
} else if (m.name === 'TCON') {
res.genre = m.value
} else if (m.name === 'TYER') {
res.year = m.value
} else if (m.name === 'TLEN') {
res.duration = parseInt(m.value)
} else if (m.name === 'TCOM') {
res.composer = m.value
} else if (m.name === 'TBPM') {
res.bpm = parseInt(m.value)
}
})
return res
}
const { CodecType, probe, probeSync, getOpusVersion } = binding
const { version } = require('./package.json')
module.exports = {
CodecType,
probe,
probeSync,
probeStream,
readMetadata,
OpusEncoder,
getOpusVersion,
version,
}