Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

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

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 off the beginning of the mp3 and feeding them to decodeAudioData. Not surprisingly this fails.

After some digging it seems that decodeAudioData is only able to work with 'valid mp3 chunks' as documented by Fair Dinkum Thinkum, here.

However there is no clarification about the structure of a valid mp3 chunk (the author of the aforementioned doesn't go into this). I am aware of the various mp3 splitters that exist out there but i'd like to approach this programmatically. (I'm trying to implement a kind of 'poor man's streaming' using nodejs on the server side).

So, would splitting on mp3 frame headers be enough or do I need to do more? (perhaps 'closing' every chunk by appending some data at the end?) How about the 'byte reservoir'? Would this cause problems? For the record, I'm currently working with 128kbps cbr mp3s. Would this simplify the process in any way?

Any info on what decodeAudioData expects as vaild data would be appreciated.

Thank you.

PS: I realise that this is perhaps a request for clarification on Fair Dinkum Thinkum's post but my low reputation is keeping me from posting a comment. So I can't see how else to do it but with a new question. Thanks again.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...