Is there a way get something like decibel levels from an audio file and transform that information into a json array?

前端 未结 2 1833
深忆病人
深忆病人 2020-12-09 00:31

So that I can use the information to coordinate a page animation like making elements brighter as the decibel levels get higher

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 00:49

    Yes, you need to grab the raw PCM samples (like Kennis mentions). However, to calculate the overall volume levels, you want to grab the RMS (Root Mean Square) of the values. Also, you will likely want to pay attention to all the channels in the stream, not just the first channel (so you can accurately reflect the volume level for a stereo stream for example).

    There are some tricks (make sure you use multiplication of the same samples across channels, not addition). Then you will be adding them all together (again like Kennis is doing). If you want it an actual decibels, there is a log step that is required as well.

    There is an example as an answer to this other question.

    Relevant code:

    var rms = Math.sqrt(sum / (_buffer.length / 2));
    var decibel = 20 * (Math.log(rms) / Math.log(10));
    

提交回复
热议问题