html5鼠标讯听,HTML5 随鼠标移动而变化的低频信号发生器

这篇博客介绍了如何利用JavaScript和Web Audio API来创建声音效果。通过初始化音频上下文,设置振荡器和低频振荡器(LFO),控制音量增益,以及响应鼠标移动事件改变频率,实现音乐播放和停止的功能。此外,还涉及到LFO的幅度控制,允许用户通过滑块调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

var audioContext;

var soundOscillator;

var lfo;

var lfoGain;

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 = "sine";

soundOscillator.frequency.value = 0;

soundOscillator.start(0);

// LFO

// Used for changing the frequency of the sound

// oscillator.

lfo = audioContext.createOscillator();

lfo.type = "sine";

lfo.frequency.value = 0;

lfo.start(0);

// Controls the amplitude of LFO applied.

// An oscillator node produces values

// in the 0 - 1 range. By applying a

// gain of 200 to the LFO we can make

// it produce values in the 0 - 200

// range.

lfoGain = audioContext.createGain();

lfoGain.gain.value = 200;

// Controls the volume

gainNode = audioContext.createGain();

// Wire them up

lfo.connect(lfoGain);

lfoGain.connect(soundOscillator.frequency);

soundOscillator.connect(gainNode);

gainNode.connect(audioContext.destination);

}

function toggleSound(toggleButton) {

isPlaying = !isPlaying;

if (isPlaying) {

gainNode.gain.value = 1;

toggleButton.innerHTML = "

Stop

"

} else {

soundOscillator.frequency.value = 0;

lfo.frequency.value = 0;

gainNode.gain.value = 0;

toggleButton.innerHTML = "

Start

"

}

}

function onMousemove(event) {

if (isPlaying) {

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

lfo.frequency.value = lfoFrequency;

lfoFrequencySpan.innerHTML = lfoFrequency;

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

soundOscillator.frequency.value = soundFrequency;

soundFrequencySpan.innerHTML = soundFrequency;

}

}

function initControls() {

soundFrequencySpan = document.getElementById("soundFrequency");

lfoFrequencySpan = document.getElementById("lfoFrequency");

var lfoAmplitudeSlider = document.getElementById("lfoAmplitudeSlider");

var lfoAmplitudeSpan = document.getElementById("lfoAmplitude");

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

lfoAmplitudeSlider.addEventListener("change", function() {

lfoGain.gain.value = this.value;

lfoAmplitudeSpan.innerHTML = this.value;

});

lfoAmplitudeSlider.addEventListener("mousemove", function() {

lfoGain.gain.value = this.value;

lfoAmplitudeSpan.innerHTML = this.value;

});

}

function init() {

initSound();

initControls();

}

init();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值