gpt4 book ai didi

javascript - 玩弄 audioContext

转载 作者:行者123 更新时间:2023-11-29 15:33:57 27 4
gpt4 key购买 nike

受此启发SO answer ,我有这段代码,它可以工作,但它会弹出并且听起来不太流畅。

$('.btn').each(initDraggable);
function initDraggable() {
var param = {}
param.drag = drag
param.stop = stopper
$(this).draggable(param)
}
var Variables = {}
Variables.Frequency = 0
function drag(myEvent,myUI) {
var Frequency = myUI.position.left + 100
var Duration = 5000
if (Math.abs(Variables.Frequency-Frequency) > 25) {
if (typeof(Variables.osc) === 'undefined') {
} else {
Variables.osc.stop(0)
}
Variables.Frequency = Frequency
Oscillator(Frequency,Duration)
}
}
function stopper() {
Variables.osc.stop(0)
}


Variables.ctx = new(window.audioContext || window.webkitAudioContext)
function Oscillator(argFrequency,argDuration) {
Variables.osc = Variables.ctx.createOscillator()
Variables.osc.type = 0
Variables.osc.connect(Variables.ctx.destination)
Variables.osc.frequency.value = argFrequency
Variables.osc.start(0)
setTimeout(myTimeout,argDuration)
function myTimeout() {
Variables.osc.stop(0)
}
}
<link rel="stylesheet" href="//cdn.jsdelivr.net/bootstrap/latest/css/bootstrap.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.css">
<span class="btn btn-primary">Drag me</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/bootstrap/latest/js/bootstrap.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui.touch-punch/latest/jquery.ui.touch-punch.js"></script>

问:有什么简单的方法可以让它听起来更流畅一点?

最佳答案

无需在拖动 时重新创建振荡器,我认为这就是导致爆裂声的原因。但是您可以更改已启动的振荡器频率值,因此您可以通过在拖动start时创建振荡器来实现您的结果,将其停止停止,然后在拖动 时简单地更改频率值。像这样:

$('.btn').each(initDraggable);

function initDraggable() {
var param = {}
param.drag = drag
param.stop = stopper
param.start = startOsc
$(this).draggable(param)
}
var Variables = {}
Variables.Frequency = 0

function drag(myEvent, myUI) {
var Frequency = myUI.position.left + 100
var Duration = 5000
if (Math.abs(Variables.Frequency - Frequency) > 25) {
//You could put some validation on creation, but since you're not creating it, it's not necessary here
/*if (typeof(Variables.osc) === 'undefined') {
} else {
Variables.osc.stop(0)
}*/

Variables.Frequency = Frequency;
//Oscillator(Frequency,Duration)
Variables.osc.frequency.value = Frequency;
}
}

function startOsc(myEvent, myUI) {
var Frequency = myUI.position.left + 100
Oscillator(Frequency) //Since it plays in continue you don't need duration
}

function stopper() {
Variables.osc.stop(0)
}


Variables.ctx = new(window.AudioContext || window.webkitAudioContext)

function Oscillator(argFrequency /*,argDuration*/ ) {
Variables.osc = Variables.ctx.createOscillator()
Variables.osc.type = 0
Variables.osc.connect(Variables.ctx.destination)
Variables.osc.frequency.value = argFrequency
Variables.osc.start(0)
/*setTimeout(myTimeout,argDuration)
function myTimeout() {
Variables.osc.stop(0)
}*/
}
<link rel="stylesheet" href="//cdn.jsdelivr.net/bootstrap/latest/css/bootstrap.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.css">
<span class="btn btn-primary">Drag me</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/bootstrap/latest/js/bootstrap.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui.touch-punch/latest/jquery.ui.touch-punch.js"></script>

关于javascript - 玩弄 audioContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com