I'm using bundle-text to import and bundle an audio file (it's strange I know but I need the whole library in a single file). My method is converting the string into a Uint8Array and then use window.AudioContext to decodeAudioData. When the file is a wav it works (although quality sounds bad) and when I use mp3 I get the error Failed to execute 'decodeAudioData' on 'BaseAudioContext': Unable to decode audio data}. I tried converting the mp3 to base64 with an online service and saved the string that way. After converting from base64 with atob the decodeAudioData works. When I examine the difference between the bundle-text version of the string and the base64 version of the string I see small differences that makes me think bundle-text is corrupting the file or having trouble with a file of that size.
The start and end of the file after being converted from base64 looks like:
import { mp3example } from './files.js'
const fromBase64 = atob(mp3example)
console.log({ fromBase64 })
import whistle_8_T7 from 'bundle-text:/public/sound/whistle/whistle_8_T7.mp3'
console.log({ whistle_8_T7 })
let arrayBuffer = Uint8Array.from(whistle_8_T7, (e) => e.charCodeAt(0))
const audioContext = new window.AudioContext()
audioContext.decodeAudioData(
arrayBuffer.buffer,
(audioBuffer) => {
// Now you have the AudioBuffer
console.log('Decoded AudioBuffer:', audioBuffer)
// You can now use the AudioBuffer, for example, to play the sound
const source = audioContext.createBufferSource()
source.buffer = audioBuffer
source.connect(audioContext.destination)
source.start(0)
},
(error) => {
console.error('Error decoding audio data:', { error })
}
)
🐛 bug report
I'm using
bundle-text
to import and bundle an audio file (it's strange I know but I need the whole library in a single file). My method is converting the string into aUint8Array
and then usewindow.AudioContext
todecodeAudioData
. When the file is awav
it works (although quality sounds bad) and when I usemp3
I get the errorFailed to execute 'decodeAudioData' on 'BaseAudioContext': Unable to decode audio data}
. I tried converting the mp3 to base64 with an online service and saved the string that way. After converting from base64 withatob
thedecodeAudioData
works. When I examine the difference between thebundle-text
version of the string and the base64 version of the string I see small differences that makes me thinkbundle-text
is corrupting the file or having trouble with a file of that size.The start and end of the file after being converted from base64 looks like:
Whereas the start and end of the file after being imported from
bundle-text
looks like:🎛 Configuration (.babelrc, package.json, cli command)
🤔 Expected Behavior
I would expect the reading of the mp3 file as text to be convertible back into a playable file
😯 Current Behavior
Instead the file gets corrupted and is unable to be played as an audio file
💁 Possible Solution
Might be something to do with the size?
🔦 Context
I needed to inline mp3s so that I could ship a single file library despite being large.
💻 Code Sample
https://github.com/trifle-labs/anybody-problem
🌍 Your Environment