2016-10-27 1 views
0

웹 오디오 API으로 음향 파를 생성하는 다양한 발진기를 만들기 시작했습니다.오실레이터 주파수 변경

저는 전 과정에 다소 새로운 것이므로, 음색을 재생하는 방법이 있는지, 일정한 주파수가 아닌 중간 단계로 변경하는지 궁금합니다.

예 : I가 3초 대해 300 Hz에서 시작 톤을 원하고 레이즈 직선 다음 4초 위에 400 Hz에서 한다.

나는이 세례반하지만 내가 무엇을 찾고 있다면 난 정말 확실하지 않다 : 지금까지

osc.setPeriodicWave(wave); 

내 자바 스크립트 :

$(document).ready(function() { 

// Function that plays the tone. 
var playTone = function(duration, frequency) { 
var context = new(window.AudioContext || window.webkitAudioContext)(); 
var osc = context.createOscillator(); 
// Sine is the default type. Also available: square, sawtooth and triangle waveforms. 
osc.type = 'sine'; 
// Frequency in Hz. 
osc.frequency.value = frequency; // Frequency in Hz 
osc.connect(context.destination); 
osc.start(context.currentTime); 
osc.stop(context.currentTime + duration); 
} 

// Button and input functionality. 
$("button").on("click", function() { 

var whichButton = $(this).text() 
var duration = document.getElementById("durationInput").value; 
var frequency = document.getElementById("f1Input").value; 
if (whichButton == " Play") { 
    playTone(duration, frequency); 
} 

}); 

}); 

내 작업과 codepen 지금까지 :Codepen.io - Acoustic wave generator

+0

[mcve]에 관련 코드를 포함 시키십시오. 질문 자체는 제 3 자 사이트가 아니라. –

+0

요청하신대로 완료하십시오! 그 죄송합니다. – Konsal

답변

0

고조파 (additive) 합성을위한 PeriodicWave는 원하지 않습니다.

이것은 매우 간단합니다. 주파수 AudioParam의 스케줄링 방법 인 "setValueAtTime"과 "linearRampToValueAtTime"을 사용해야합니다. 이것을 시도하십시오 :

// Function that plays the tone. 
var playTone = function(duration, frequency) { 
    var context = new(window.AudioContext || window.webkitAudioContext)(); 
    var osc = context.createOscillator(); 
    // Sine is the default type. Also available: square, sawtooth and triangle waveforms. 
    osc.type = 'sine'; 
    var now = context.currentTime; 
    // Frequency in Hz. 
    // Set initial value. (you can use .value=freq if you want) 
    osc.frequency.setValueAtTime(frequency, now); 
    // set a "checkpoint" in 3 seconds - that will be the starting point of the ramp. 
    osc.frequency.setValueAtTime(frequency, now+3); 
    // set a ramp to freq+100Hz over the next 4 seconds. 
    osc.frequency.linearRampToValueAtTime(frequency+100,now+7) 
    osc.connect(context.destination); 
    osc.start(now); 
    osc.stop(now + duration); 
} 
+0

정보 및 도움에 감사드립니다! 그것은 이상하게 행동하고 있습니다. ** 주파수를 낮추면 큰 효과가 있습니다 **하지만 주파수를 높이려고하면 (예 : ** 3 <시간 <7 초 **), 처음 3 초 동안 원래의 톤을 재생 한 다음 ** 0.5 초 동안 고음을 내고 ** 멈춤 ** – Konsal

+0

이 문제의 최소한의 예를 들어 보라. 문제가 무엇인지 알지 못한다. –

+0

분명히 jQuery를 없애면 .linearRampToValueAtTime 주파수를 올리거나 낮추면 잘 동작합니다. @cwilso의 코드를 사용하여 주파수를 높이는 jQuery에 문제가 있습니다. – Konsal