Define 'valid mp3 chunk' for decodeAudioData (WebAudio API)

前端 未结 2 1708
刺人心
刺人心 2020-12-05 01:38

I\'m trying to use decodeAudioData to decode and play back an initial portion of a larger mp3 file, in javascript. My first, crude, approach was slicing a number of bytes of

相关标签:
2条回答
  • 2020-12-05 01:43

    After more experimentation with decodeAudioData (on Chrome) this is what I've found:

    • Any initial mp3 chunk will be successfully decoded as long as it is split on an mp3 frame boundary. Finding that boundary may not always be trivial (e.g. involve parsing mp3 headers) as even constant bitrate mp3s don't always contain constant-size frames. For example, 128kbps mp3 files contain 417-byte frames as well as 418-byte frames. (some frames contain an extra byte as padding).
    • An arbitrary mp3 chunk is not guaranteed to be decodable even if split on exact frame boundaries on 'both sides'. Some chunks of this sort can be decoded but others cause decodeAudioData to throw an error. I'm guessing this has to do with mp3 bit reservoir which creates a dependency between mp3 frames.
    0 讨论(0)
  • 2020-12-05 01:59

    If you split your file into pieces starting with valid MP3 headers (12 bits of '1' aligned on byte boundary : FF Fx), you most probably would be fine.

    0 讨论(0)
提交回复
热议问题