Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

报错:Lame is not defined,MPEGMode is not defined #101

Open
liluyao1998 opened this issue Dec 11, 2024 · 0 comments
Open

报错:Lame is not defined,MPEGMode is not defined #101

liluyao1998 opened this issue Dec 11, 2024 · 0 comments

Comments

@liluyao1998
Copy link

// utils/audioConverter.js
import lamejs from "lamejs";
// var lamejs = require("lamejs");

// 确保全局变量已定义
window.MPEGMode = {
STEREO: 0,
JOINT_STEREO: 1,
DUAL_CHANNEL: 2,
SINGLE_CHANNEL: 3,
};

export function wavToMp3(recorderData) {
console.log("lamejs", lamejs);
const channels = 1; // 单声道
const sampleRate = 16000; // 采样率
const kbps = 128; // 比特率

const mp3encoder = new lamejs.Mp3Encoder(channels, sampleRate, kbps);
const mp3Data = [];
const samples = new Int16Array(recorderData.buffer); // 从源中获取数据
const sampleBlockSize = 1152; // 576的倍数

for (let i = 0; i < samples.length; i += sampleBlockSize) {
const sampleChunk = samples.subarray(i, i + sampleBlockSize);
const mp3buf = mp3encoder.encodeBuffer(sampleChunk);
if (mp3buf.length > 0) {
mp3Data.push(mp3buf);
}
}

const mp3buf = mp3encoder.flush(); // 完成写入mp3
if (mp3buf.length > 0) {
mp3Data.push(new Int8Array(mp3buf));
}

const blob = new Blob(mp3Data, { type: "audio/mp3" });
return blob;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant