使用JavaScript和HTML 5创建音量控制

时间:2014-11-20 11:30:04

标签: javascript html5

我无法理解讲师给我的作业。他为我们提供了一个应该从计算机麦克风录制的程序并告诉我们,为它构建一个控制扬声器数据库的音量控制。这让我很困惑,因为据我所知,这个节目中没有扬声器。

<html>
<head>  
    <title>Audio Renderer -chrome</title>

    <style>
    </style>
</head>
<body>
    <h1>HTML5 webmic-Renderer </h1>
    <h4>Chrome</h4>
    <pre id="preLog">Access to micro</pre>


    <p>
        <input type="button" id="buttonStart" value="Start" onclick="start()" />
        <input type="button" id="buttonStop" value="Stop" onclick="stop()"  />
    </p>

<script>
var audioContext = new webkitAudioContext();
var realAudioInput = null;
var preLog ;
var zeroGain;
var channel = 2;
var bufferSize =1024;

function log(text){
            preLog = document.getElementById('preLog');
            if (preLog) preLog.textContent += ('\n' + text);
            else alert(text);
}
function start() {
        log('Get user media..');

        if (navigator.webkitGetUserMedia) navigator.webkitGetUserMedia({audio:true}, gotStream, noStream);
        else log('getUserMedia() not available from your Web browser!');
}

function noStream() {
            log('Access to Micro was denied!');
}

function gotStream(stream) {
    log('Access to Micro was started');
    // Create an AudioNode from the stream.
    realAudioInput = audioContext.createMediaStreamSource(stream);

    // Create an GainNode .
    zeroGain = audioContext.createGain();
    zeroGain.gain.value = 1.0;


        // create an audio node with 2 input and 1 output channels, and 1024 byte buffer size per audio frame
    jsNode = audioContext.createScriptProcessor(bufferSize, channel, channel-1);
    jsNode.onaudioprocess = audioProcess;

    // Signal Graph
    realAudioInput.connect( jsNode );
         // zeroGain.connect(??);
    jsNode.connect( audioContext.destination ); 
}

function stop() {
log('Access to Micro stopped');
    realAudioInput.disconnect(0);
}

  // this function is called every audio frame
  function audioProcess(event) {  
    var sampleIn_l  = event.inputBuffer.getChannelData(channel-2); // Stereo: 0 = left channel, 1 = right channel 
    var sampleIn_r  = event.inputBuffer.getChannelData(channel-1);
    var sampleOut = event.outputBuffer.getChannelData(channel-2); 

      // loop through every sample and add sample values to out buffer
      for(i = 0; i < event.inputBuffer.length; i++) {
        var sample_l = sampleIn_l[i]  ;
        var sample_r = sampleIn_r[i]  ;
        sampleOut[i] = ( sample_l );
      }
  } 

</script>
</body>
</html>

在他的作业中说:为节目级别的音频渲染器创建一个交互式音量控制器,用于节点级别的扬声器:zeroGain.gain.value = 1.0; 我只是不明白他想要什么。如果有人能提供帮助,我会很高兴。)

非常感谢您的阅读!

0 个答案:

没有答案