2017-04-13 23 views
1

JavaScriptで次のAudioContext()サウンドオブジェクトがあります。
ボリュームは100%です。私は10%でボリュームを再生したい(ボリューム= 0.1の場合)。
どのようにボリュームを10%に減らすことができますか?私たちは、音量をコントロールするGainNodesを使用(オーディオバッファ)AudioContext()の音量を制御するには?

const aCtx = new AudioContext(); 
 
let source = aCtx.createBufferSource(); 
 
let buf; 
 
fetch('https://dl.dropboxusercontent.com/s/knpo4d2yooe2u4h/tank_driven.wav') // can be XHR as well 
 
    .then(resp => resp.arrayBuffer()) 
 
    .then(buf => aCtx.decodeAudioData(buf)) // can be callback as well 
 
    .then(decoded => { 
 
    source.buffer = buf = decoded; 
 
    source.loop = true; 
 
    source.connect(aCtx.destination); 
 
    check.disabled = false; 
 
    }); 
 

 
check.onchange = e => { 
 
    if (check.checked) { 
 
    source.start(0); // start our bufferSource 
 
    } else { 
 
    source.stop(0); // this destroys the buffer source 
 
    source = aCtx.createBufferSource(); // so we need to create a new one 
 
    source.buffer = buf; 
 
    source.loop = true; 
 
    source.connect(aCtx.destination); 
 
    } 
 
};
<label>Start Playing</label> 
 
<input type="checkbox" id="check" disabled><br> 
 
<br>Its volume is 100%. Please help me to reduce it to 10%.

+0

の詳細情報については、チェックアウト

以下に示すように、

だから私は[私のコード](http://stackoverflow.com/a/42970609/3702797を認識)ここで、答え全体を読むと、2番目のスニペットで[gainNode](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext)を使用しています/ createGain)を使用すると、出力ボリュームを制御できます。 – Kaiido

答えて

2

var gainNode = aCtx.createGain() 
gainNode.gain.value = 0.1 // 10 % 
gainNode.connect(aCtx.destination) 

// now instead of connecting to aCtx.destination, connect to the gainNode 
source.connect(gainNode) 

ソリューション

const aCtx = new AudioContext(); 
 

 
const gainNode = aCtx.createGain(); 
 
gainNode.gain.value = 0.1; // setting it to 10% 
 
gainNode.connect(aCtx.destination); 
 

 
let source = aCtx.createBufferSource(); 
 
let buf; 
 
fetch('https://dl.dropboxusercontent.com/s/knpo4d2yooe2u4h/tank_driven.wav') // can be XHR as well 
 
    .then(resp => resp.arrayBuffer()) 
 
    .then(buf => aCtx.decodeAudioData(buf)) // can be callback as well 
 
    .then(decoded => { 
 
    source.buffer = buf = decoded; 
 
    source.loop = true; 
 
    source.connect(gainNode); 
 

 
    check.disabled = false; 
 
    }); 
 

 
check.onchange = e => { 
 
    if (check.checked) { 
 
    source.start(0); // start our bufferSource 
 
    } else { 
 
    source.stop(0); // this destroys the buffer source 
 
    source = aCtx.createBufferSource(); // so we need to create a new one 
 
    source.buffer = buf; 
 
    source.loop = true; 
 
    source.connect(gainNode); 
 
    
 
    } 
 
};
<label>Start Playing</label> 
 
<input type="checkbox" id="check" disabled><br> 
 
<br>Its volume is 100%. Please help me to reduce it to 10%.

+1

@Kaiidoありがとうございます。それを見たことがあります。それも私を傷つける...今私はそれを見る。 – arcs

0

あなたはそのpuporseためAudioContextのcreateGainを使用することができます。 createGain

https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain

const aCtx = new AudioContext(); 
    let source = aCtx.createBufferSource(); 
    let buf; 

    var gainNode = aCtx.createGain(); // Create a gainNode reference. 
    gainNode.connect(aCtx.destination); // Add context to gainNode 

fetch('https://dl.dropboxusercontent.com/s/knpo4d2yooe2u4h/tank_driven.wav') // can be XHR as well 
     .then(resp => resp.arrayBuffer()) 
     .then(buf => aCtx.decodeAudioData(buf)) // can be callback as well 
     .then(decoded => { 
     source.buffer = buf = decoded; 
     source.loop = true; 

     source.connect(gainNode); //Connecting gain to source 
     gainNode.gain.value = 1; // 100% VOLUME RANGE OF VALUE IS 0-1 

     check.disabled = false; 
     }); 


    check.onchange = e => { 
     if (check.checked) { 
     source.start(0); // start our bufferSource 
     } else { 
     source.stop(0); // this destroys the buffer source 
     source = aCtx.createBufferSource(); // so we need to create a new one 
     source.buffer = buf; 
     source.loop = true; 

     source.connect(gainNode); //Connecting gain to source 
     gainNode.gain.value = 0.1; // 10% VOLUME RANGE OF VALUE IS 0-1 
     } 
    }; 
+2

バッファソースをcheck.onchangeのgainNodeに接続する必要があります。そうしないと、2回目の変更後にもう動作しません。 – Kaiido

+0

はい、正しく更新されました。 –

+1

gainNodeをコンテキストに再接続する必要はありません一度やり直してください。例えば、gloablスコープで。あなたはまだバッファをコンテキストの宛先に直接接続していますが、これはまあまあです。 – Kaiido

関連する問題