0

我正在尝试测试一个未连接到场景中任何世界对象的简单振荡器。只是一个音调,但按照振荡器节点的示例似乎没有播放任何内容。是否需要先进行某种用户交互才能被允许播放?

我还有一个在场景中旋转的立方体,但它不应该影响任何东西。剧本:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

var geometry = new THREE.BoxGeometry();
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );

camera.position.z = 5;

function animate() {
    requestAnimationFrame( animate );

    cube.rotation.x += 0.05;
    cube.rotation.y += 0.01;

    renderer.render( scene, camera );
}
animate();


// create web audio api context
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();

// create Oscillator node
var oscillator = audioCtx.createOscillator();

oscillator.type = 'square';
oscillator.frequency.setValueAtTime(440, audioCtx.currentTime); // value in hertz
var gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
oscillator.connect(audioCtx.destination);

var initialFreq = 3000;
var initialVol = 0.500;

gainNode.gain.value = initialVol;
gainNode.gain.minValue = initialVol;
gainNode.gain.maxValue = initialVol;

oscillator.frequency.value = 3000;

oscillator.start();
4

0 回答 0