javascript - AnalyserNode 如何(如果有的话)用于分析整个音频文件,并获取可视化所需的数据?
问题描述
特别是,我对这种AnalyserNode.getFloatTimeDomainData()
方法很感兴趣。
我可以使用这种方法来创建代表整个音频文件的波形吗?
还是 AnalyserNode 仅适用于音频流?
MDN 文档说:
AnalyserNode 接口表示能够提供实时频域和时域分析信息的节点。它是一个 AudioNode,将音频流从输入不变地传递到输出,但允许您获取生成的数据、处理它并创建音频可视化。
https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode
所有示例似乎都 AudioBuffer.getChannelData()
用于获取数据以可视化整个音频文件。如本文所述:
https://css-tricks.com/making-an-audio-waveform-visualizer-with-vanilla-javascript/
来自文章:
const filterData = audioBuffer => {
const rawData = audioBuffer.getChannelData(0); // We only need to work with one channel of data
const samples = 70; // Number of samples we want to have in our final data set
const blockSize = Math.floor(rawData.length / samples); // the number of samples in each subdivision
const filteredData = [];
for (let i = 0; i < samples; i++) {
let blockStart = blockSize * i; // the location of the first sample in the block
let sum = 0;
for (let j = 0; j < blockSize; j++) {
sum = sum + Math.abs(rawData[blockStart + j]) // find the sum of all the samples in the block
}
filteredData.push(sum / blockSize); // divide the sum by the block size to get the average
}
return filteredData;
}
解决方案
您在主题中说您拥有整个文件。如果是这种情况,您可以对文件 ( decodeAudioData
) 进行解码,您将获得一个包含所有样本的 AudioBuffer,然后您可以将其用于可视化。
如果您无法将整个文件解码到内存中,则必须使用不同的方法,并且AnalyserNode
可能无法正常工作,因为它是异步的,因此您可能无法获得所有样本,或者您可能会得到重复。如果这是不可接受的,您可能必须使用MediaStreamAudioSourceNode
连接到 a 的 aScriptProcessorNode
或AudioWorkletNode
来获取所需的时域数据。
推荐阅读
- powershell - 使用变量列表列出文件夹?
- loops - 删除 Rust 循环中的边界检查以尝试达到最佳编译器输出
- php - php openssl_csr_new():错误签名请求调试
- sequelize.js - Sequelize 查询由 $and 连接的 2 个函数
- javascript - 文本被截断时的 CSS 选择
- kotlin - 变换单
- >> 转单
- >
- tensorflow - 根据点的数量和大小,使用哪种神经网络将点状图像分类为一个数字?
- c# - cryptuiapi.h 和 wincrypt.h 是否有 C# 包装器?
- autodesk-forge - 重命名 Bucket 或转移所有模型
- vue.js - VueJS - 本地主机 API 请求在 Android Chromium 上不起作用