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

开发环境可以正常运行,打包后执行到 const mp3enc = new lamejs.Mp3Encoder(channels, sampleRate, 128)的时候就报错 TypeError: Cannot read properties of undefined (reading 'SHORT_TYPE') #100

Open
hqiaozhang opened this issue Oct 29, 2024 · 0 comments

Comments

@hqiaozhang
Copy link

开发环境可以正常运行,打包后执行到 const mp3enc = new lamejs.Mp3Encoder(channels, sampleRate, 128)的时候就报错
TypeError: Cannot read properties of undefined (reading 'SHORT_TYPE')

=====

import lamejs from 'lamejs'
import MPEGMode from 'lamejs/src/js/MPEGMode'
import Lame from 'lamejs/src/js/Lame'
import BitStream from 'lamejs/src/js/BitStream'

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())
console.log('wav===', wav)
const { channels, sampleRate } = wav
console.log(channels, sampleRate)
console.log('lamejs==', lamejs)
const mp3enc = new lamejs.Mp3Encoder(channels, sampleRate, 128)
console.log('mp3enc===', mp3enc)
const result = recorder.getChannelData()
const buffer = []

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
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 && mp3buf.length > 0)
  buffer.push(mp3buf)

}

const enc = mp3enc.flush()

if (enc.length > 0)
buffer.push(enc)

return new Blob(buffer, { type: 'audio/mp3' })
}

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