weixin_33716941 2016-01-15 18:17 采纳率: 0%
浏览 157

使用ajax加载wav文件

I have a list of audio files. What I want to accomplish is that every time when I click on a file from the list, javascript loads the waveform of that audio file.

This is a function that draws the waveform:

function drawBuffer( width, height, context, data ) {
    var step = Math.ceil( data.length / width );
    var amp = height / 2;
    context.fillStyle = "silver";
    context.clearRect(0,0,width,height);
    for(var i=0; i < width; i++){
        var min = 1.0;
        var max = -1.0;
        for (j=0; j<step; j++) {
            var datum = data[(i*step)+j]; 
            if (datum < min)
                min = datum;
            if (datum > max)
                max = datum;
        }
        context.fillRect(i,(1+min)*amp,1,Math.max(1,(max-min)*amp));
    }
}

On click I get the value selected from the list and request the php page that should return the file with that name:

function getAudioClip() {
        var x = document.getElementById("clips").value;
        $.get( "get_audio.php", { filename: x } )
          .done(function( data ) {
            loadAudioClip(data);
          });
    }

In the end the function loadAudioClip(data) should change the waveform:

function loadAudioClip(data) {
        var canvas = document.getElementById( "wavedisplay" );
        drawBuffer( canvas.width, canvas.height, canvas.getContext('2d'), data );
}

How should my get_audio.php look like. How should I get data. The files are in the folder called audio/.

  • 写回答

1条回答 默认 最新

  • weixin_33681778 2016-01-15 18:51
    关注

    HTML5 and includes an API to perform these operations in a simpler form.

    I suggest you review the documentation for the plugin wavesurfer.js using all the features you are looking for.

    Wavesurfer on Github: https://github.com/katspaugh/wavesurfer.js

    I hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog