2017-02-11 5 views
0

나는 이것이 쉽지만 분명한 것처럼 들리지만이 작업을 할 수는 없다는 것을 알고있다. 웹 오디오 API를 사용하여 oscillatorNodes를 만드는 기능을 만드는 방법 Api

이 조각을 참조하십시오

ctx = new AudioContext(); 
 
gain = ctx.createGain(); 
 
gain.connect(ctx.destination); 
 
gain.gain.value = 0.5; 
 

 

 
osc1 = ctx.createOscillator(); 
 
osc1.connect(gain); 
 
osc1.frequency.value = 450; 
 
osc1.start(); 
 
osc1.stop(2); 
 

 
osc2 = ctx.createOscillator(); 
 
osc2.connect(gain); 
 
osc2.frequency.value = 500; 
 
osc2.start(); 
 
osc2.stop(2); 
 
console.log("playing");

그것은 문제없이 한 번에 두 개의 발진기를 재생하지만이 코드를 반복합니다. 중복 코드를 함수에 넣으려고해도 함수가 작동하지 않습니다.

ctx = new AudioContext(); 
 
gain = ctx.createGain(); 
 
createAndPlayOsc(450); 
 
createAndPlayOsc(500); 
 

 

 
function createAndPlayOsc(freq){ 
 
    console.log("creating osc with freq " + freq); 
 
    var osc = ctx.createOscillator(); 
 
    osc.connect(gain); 
 
    osc.frequency.value = freq; 
 
    osc.start(); 
 
    osc.stop(2); 
 
    console.log("playing osc with freq " + freq); 
 
}

나는 AudioContext

ctx = new AudioContext(); 
 
gain = ctx.createGain(); 
 
createAndPlayOsc(450, ctx); 
 
createAndPlayOsc(500, ctx); 
 

 

 
function createAndPlayOsc(freq, context){ 
 
    console.log("creating osc with freq " + freq); 
 
    var osc = context.createOscillator(); 
 
    osc.connect(gain); 
 
    osc.frequency.value = freq; 
 
    osc.start(); 
 
    osc.stop(2); 
 
    console.log("playing osc with freq " + freq); 
 
}

또는 모두 gainNode 문맥을 보낼 경우에도

ctx = new AudioContext(); 
 
gain = ctx.createGain(); 
 
createAndPlayOsc(450, ctx, gain); 
 
createAndPlayOsc(500, ctx, gain); 
 

 

 
function createAndPlayOsc(freq, context, gainNode){ 
 
    console.log("creating osc with freq " + freq); 
 
    var osc = context.createOscillator(); 
 
    osc.connect(gainNode); 
 
    osc.frequency.value = freq; 
 
    osc.start(); 
 
    osc.stop(2); 
 
    console.log("playing osc with freq " + freq); 
 
}

나는 무엇을 놓치고?

gain.connect(ctx.destination); 

답변

1

당신은 문맥에 이득을 연결하는 것을 잊었다. 나는 그것들이 생성자에서 생성되었지만 메소드에서 생성되지 않는다면 '오스'를 재생하는 Ionic2 App의 제공자 클래스에 문제가있다. 나는 단지이 것이이 질문과 관련이 있기를 원한다고 생각한다. (
+0

아 ... 너무 쉽게 ... 감사 : – distante