从wav文件中读取样本 [英] Read samples from wav-file

查看:65
本文介绍了从wav文件中读取样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用html5制作一个网页,该网页将wav文件中的样本数据存储在数组中.有没有办法用javascript获取样本数据?我正在使用文件输入来选择wav文件.

I'm trying to make a webpage in html5 which stores sample-data from a wav-file in an array. Is there any way to get the sample-data with javascript? I'm using a file-input to select the wav-file.

在我已经添加的javascript中:

In the javascript I already added:

document.getElementById('fileinput').addEventListener('change', readFile, false);

但是我不知道在readFile中做什么.

but I have no idea what to do in readFile.

我试图在ArrayBuffer中获取该文件,将其传递给encodeAudioData方法,并从中获取一个typedArraybuffer.这是我的代码:

I tried to get the file in an ArrayBuffer, pass it to the decodeAudioData method and get a typedArraybuffer out of it. This is my code:

var openFile = function(event) {
var input = event.target;
var audioContext = new AudioContext();

var reader = new FileReader();
reader.onload = function(){
var arrayBuffer = reader.result;
  console.log("arrayBuffer:");
  console.log(arrayBuffer);
  audioContext.decodeAudioData(arrayBuffer, decodedDone);

};
reader.readAsArrayBuffer(input.files[0]);
};
function decodedDone(decoded) {
var typedArray = new Uint32Array(decoded, 1, decoded.length);
  console.log("decoded");
  console.log(decoded);
  console.log("typedArray");
  console.log(typedArray);

for (i=0; i<10; i++)
{
    console.log(typedArray[i]);
}

}

typedArray的元素全为0.我创建typedArray的方式是否错误或者我在做其他事情吗?

The elements of typedArray are all 0. Is my way of creating the typedArray wrong or did I do something else wrong on?

我终于明白了.这是我的代码:

I finally got it. This is my code:

var openFile = function(event) {
var input = event.target;
var audioContext = new AudioContext();

var reader = new FileReader();
reader.onload = function(){
var arrayBuffer = reader.result;
  console.log("arrayBuffer:");
  console.log(arrayBuffer);
  audioContext.decodeAudioData(arrayBuffer, decodedDone);

};
reader.readAsArrayBuffer(input.files[0]);
};
function decodedDone(decoded) {

 var typedArray = new Float32Array(decoded.length);

typedArray=decoded.getChannelData(0);
console.log("typedArray:");
console.log(typedArray);

}

感谢您的回答!

推荐答案

您需要学习很多有关Web API的知识,以实现此目标,但最后非常简单.

You'll need to learn a lot about Web APIs to accomplish that, but in the end it's quite simple.

  1. 使用文件API 在ArrayBuffer中获取文件内容.
  2. 将其传递到 Web音频API
  1. Get the file contents in an ArrayBuffer with the File API
  2. Pass it to the Web Audio API's decodeAudioData method.
  3. Get a typed ArrayBuffer with the raw samples you wanted.

如果要实现均衡器,那是在浪费时间,音频API中有一个本机均衡器节点.视波形文件的长度而定,最好不将其全部加载到内存中,而只是创建一个从中读取数据的源并将该源连接到均衡器节点.

If you want to implement an equalizer, you're wasting your time, there's a native equalizer node in the Audio API. Depending on the length of your wave file it might be better not to load it all in memory and instead to just create a source that reads from it and connect that source to an equalizer node.

这篇关于从wav文件中读取样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆