+42
![autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)








Saumya Talwani
GitHub
Poojan
sahil-infocusp
非法操作
Pandaaaa906
Asuka Minato
heyszt
autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Ijas
gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
木之本澪
KinomotoMio
不做了睡大觉
User
Copilot
edvatar
-LAN-
Leilei
HaKu
dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
wangxiaolei
Varun Chawla
Stephen Zhou
yyh
yyh
tda
root
Sisyphus
Niels Kaspers
hj24
Tyson Cung
Stephen Zhou
FFXN
slegarraga
99
Br1an
L1nSn0w
Yunlu Wen
akkoaya
盐粒 Yanli
lif
weiguang li
Copilot
crazywoola
HanWenbo
Coding On Star
CodingOnStar
Stable Genius
Stable Genius
ふるい
Xiyuan Chen
f50e44b24a
Signed-off-by: edvatar <[email protected]> Signed-off-by: -LAN- <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: majiayu000 <[email protected]> Co-authored-by: Poojan <[email protected]> Co-authored-by: sahil-infocusp <[email protected]> Co-authored-by: 非法操作 <[email protected]> Co-authored-by: Pandaaaa906 <[email protected]> Co-authored-by: Asuka Minato <[email protected]> Co-authored-by: heyszt <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Ijas <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: 木之本澪 <[email protected]> Co-authored-by: KinomotoMio <[email protected]> Co-authored-by: 不做了睡大觉 <[email protected]> Co-authored-by: User <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: edvatar <[email protected]> Co-authored-by: -LAN- <[email protected]> Co-authored-by: Leilei <[email protected]> Co-authored-by: HaKu <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: wangxiaolei <[email protected]> Co-authored-by: Varun Chawla <[email protected]> Co-authored-by: Stephen Zhou <[email protected]> Co-authored-by: yyh <[email protected]> Co-authored-by: yyh <[email protected]> Co-authored-by: tda <[email protected]> Co-authored-by: root <root@DESKTOP-KQLO90N> Co-authored-by: Sisyphus <[email protected]> Co-authored-by: Niels Kaspers <[email protected]> Co-authored-by: hj24 <[email protected]> Co-authored-by: Tyson Cung <[email protected]> Co-authored-by: Stephen Zhou <[email protected]> Co-authored-by: FFXN <[email protected]> Co-authored-by: slegarraga <[email protected]> Co-authored-by: 99 <[email protected]> Co-authored-by: Br1an <[email protected]> Co-authored-by: L1nSn0w <[email protected]> Co-authored-by: Yunlu Wen <[email protected]> Co-authored-by: akkoaya <[email protected]> Co-authored-by: 盐粒 Yanli <[email protected]> Co-authored-by: lif <[email protected]> Co-authored-by: weiguang li <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: HanWenbo <[email protected]> Co-authored-by: Coding On Star <[email protected]> Co-authored-by: CodingOnStar <[email protected]> Co-authored-by: Stable Genius <[email protected]> Co-authored-by: Stable Genius <[email protected]> Co-authored-by: ふるい <[email protected]> Co-authored-by: Xiyuan Chen <[email protected]>
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import lamejs from 'lamejs'
|
|
import BitStream from 'lamejs/src/js/BitStream'
|
|
import Lame from 'lamejs/src/js/Lame'
|
|
import MPEGMode from 'lamejs/src/js/MPEGMode'
|
|
|
|
/* v8 ignore next - @preserve */
|
|
if (globalThis) {
|
|
(globalThis as any).MPEGMode = MPEGMode
|
|
; (globalThis as any).Lame = Lame
|
|
; (globalThis as any).BitStream = BitStream
|
|
}
|
|
|
|
export const convertToMp3 = (recorder: any) => {
|
|
const wav = lamejs.WavHeader.readHeader(recorder.getWAV())
|
|
const { channels, sampleRate } = wav
|
|
const mp3enc = new lamejs.Mp3Encoder(channels, sampleRate, 128)
|
|
const result = recorder.getChannelData()
|
|
const buffer: BlobPart[] = []
|
|
|
|
const leftData = result.left && new Int16Array(result.left.buffer, 0, result.left.byteLength / 2)
|
|
const rightData = result.right && new Int16Array(result.right.buffer, 0, result.right.byteLength / 2)
|
|
const remaining = leftData.length + (rightData ? rightData.length : 0)
|
|
|
|
const maxSamples = 1152
|
|
const toArrayBuffer = (bytes: Int8Array) => {
|
|
const arrayBuffer = new ArrayBuffer(bytes.length)
|
|
new Uint8Array(arrayBuffer).set(bytes)
|
|
return arrayBuffer
|
|
}
|
|
|
|
for (let i = 0; i < remaining; i += maxSamples) {
|
|
const left = leftData.subarray(i, i + maxSamples)
|
|
let right = null
|
|
let mp3buf = null
|
|
|
|
if (channels === 2) {
|
|
right = rightData.subarray(i, i + maxSamples)
|
|
mp3buf = mp3enc.encodeBuffer(left, right)
|
|
}
|
|
else {
|
|
mp3buf = mp3enc.encodeBuffer(left)
|
|
}
|
|
|
|
if (mp3buf.length > 0)
|
|
buffer.push(toArrayBuffer(mp3buf))
|
|
}
|
|
|
|
const enc = mp3enc.flush()
|
|
|
|
if (enc.length > 0)
|
|
buffer.push(toArrayBuffer(enc))
|
|
|
|
return new Blob(buffer, { type: 'audio/mp3' })
|
|
}
|