html5鼠标讯听,HTML5 用鼠标控制的低频音量震荡

这篇博客介绍了如何使用JavaScript和Web Audio API来创建声音控制。通过初始化音频上下文,设置振荡器和增益节点,实现了音量和声音的开关控制。此外,还展示了如何根据鼠标移动事件动态调整声音频率,为用户提供交互式的音频体验。
摘要由CSDN通过智能技术生成

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

var audioContext;

var soundOscillator;

var gainOscillator;

var gainNode;

var isPlaying = false;

function initSound() {

// This is not needed any more

//var AudioContext = window.AudioContext || window.webkitAudioContext;

// https://developers.google.com/web/updates/2014/07/Web-Audio-Changes-in-m36?hl=en

audioContext = new AudioContext();

// Oscillator for sound

soundOscillator = audioContext.createOscillator();

soundOscillator.type = "square";

soundOscillator.frequency.value = 0;

soundOscillator.start(0);

// Oscillator for volume/gain

gainOscillator = audioContext.createOscillator();

gainOscillator.type = "sine";

gainOscillator.frequency.value = 0;

gainOscillator.start(0);

// Controls the volume

gainNode = audioContext.createGain();

// Wire them up

soundOscillator.connect(gainNode);

gainOscillator.connect(gainNode.gain);

gainNode.connect(audioContext.destination);

}

function toggleSound(toggleButton) {

isPlaying = !isPlaying;

if (isPlaying) {

gainNode.gain.value = 1;

toggleButton.innerHTML = "

Stop

"

} else {

soundOscillator.frequency.value = 0;

gainOscillator.frequency.value = 0;

gainNode.gain.value = 0;

toggleButton.innerHTML = "

Start

"

}

}

function onMousemove(event) {

if (isPlaying) {

var lfoFrequency = Math.round(event.clientX / window.innerWidth * 30);

gainOscillator.frequency.value = lfoFrequency;

lfoFrequencySpan.innerHTML = lfoFrequency;

var soundFrequency = Math.round(event.clientY / window.innerHeight * 1000);

soundOscillator.frequency.value = soundFrequency;

soundFrequencySpan.innerHTML = soundFrequency;

}

}

function initControls() {

soundFrequencySpan = document.getElementById("soundFrequency");

lfoFrequencySpan = document.getElementById("lfoFrequency");

document.addEventListener("mousemove", onMousemove, false);

}

function init() {

initSound();

initControls();

}

init();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值