2014-02-16 4 views
0

WebAudio API와 HTML5/Javascript를 학습 과정으로 연구 한 결과 일부 학습 프로젝트의 코드를 살펴보고 일종의 학습 곡선으로 분류하려고했지만 무언가 저를 혼란스럽게합니다. 아래의 코드는 LOWPASS 필터 및 품질 슬라이더와 함께 재생/정지 버튼에있는 오디오의 일부 JS입니다. 재생/정지 버튼 대신 자동 재생으로 어떻게 변경됩니까? 미친 듯이 혼란 스럽습니다. 사전에어디서/어떻게 자동 재생으로 변경하겠습니까?

var QUAL_MUL = 30; 

function FilterSample() { 
    this.isPlaying = false; 
    loadSounds(this, {buffer: '02.mp3'}); 
}; 


FilterSample.prototype.play = function() { 
    // Create the source. 
    var source = context.createBufferSource(); 
    source.buffer = this.buffer; 
    // Create the filter. 
    var filter = context.createBiquadFilter(); 
    filter.type = filter.LOWPASS; 
    filter.frequency.value = 5000; 
    // Connect source to filter, filter to destination. 
    source.connect(filter); 
    filter.connect(context.destination); 
    // Play! 
    source.start(0); 
    source.loop = true; 
    //!-- THIS DOESN'T WORK source.autoplay = true; 
    // Save source and filterNode for later access. 
    this.source = source; 
    this.filter = filter; 
}; 


// PAUSE Button Function 
    FilterSample.prototype.stop = function() { 
    this.source.stop(0); 
}; 

// Play Button Toggle Function 
FilterSample.prototype.toggle = function() { 
    this.isPlaying ? this.stop() : this.play(); 
    this.isPlaying = !this.isPlaying; 
}; 

FilterSample.prototype.changeFrequency = function(element) { 

    var minValue = 40; 
    var maxValue = context.sampleRate/2; 

    var numberOfOctaves = Math.log(maxValue/minValue)/Math.LN2; 

    var multiplier = Math.pow(2, numberOfOctaves * (element.value - 1.0)); 

    this.filter.frequency.value = maxValue * multiplier; 
}; 

FilterSample.prototype.changeQuality = function(element) { 
    this.filter.Q.value = element.value * QUAL_MUL; 
}; 

FilterSample.prototype.toggleFilter = function(element) { 
    this.source.disconnect(0); 
    this.filter.disconnect(0); 

    if (element.checked) { 

    this.source.connect(this.filter); 
    this.filter.connect(context.destination); 
    } else { 

    this.source.connect(context.destination); 
    } 
}; 

건배 당신이 날 당신은/어떤로드 된 모든 소리가 인출되었을 때 실행됩니다 콜백 함수를/받아들이 loadSounds을 수정해야이

답변

0

을 이해하는 데 도움이 될 수 있다면.

이 그런 다음에 생성자를 변경 : 내가 제공 한 코드로 전체 FilterSample 기능을 대체 할 경우 다음

function FilterSample() { 
    this.isPlaying = false; 
    loadSounds(this, {buffer: '02.mp3'}, function() { 
    this.play(); 
    }.bind(this)); 
}; 
+0

놀이의 스크립트 요소를 빼앗아/버튼을 중지합니다. loadSounds 섹션을 수정하면 어떻게 작동합니까? 임 진짜로 이것에 새로운 그러나 당신의 도움은 빛난다! – ryananthonyyy

+0

'loadSounds'가 이미 콜백을 수락하도록 설정되었다고 생각합니다. 다행 이군. –