當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js getLevel()用法及代碼示例

getLevel()函數是p5.js庫中的內置函數。此函數用於在調用時返回單個幅度讀數。對於連續的讀數,您可以在繪製循環中運行。

用法:

getLevel(channel)

注意:僅當index.html文件的開頭部分中包含聲音庫時,所有與聲音相關的函數才起作用。


參數:該函數接受如上所述和以下描述的單個參數:

  • channel:此參數用於返回為布爾值的通道,布爾值0表示左,1表示右,它是可選的。

以下示例說明了JavaScript中的p5.getLevel()函數:

function preload(){ 
  sound1 = loadSound('song.mp3'); 
  sound2 = loadSound('pfivesound.mp3'); 
} 
function setup(){ 
  amplitude = new p5.Amplitude(); 
  sound1.play(); 
  sound2.play(); 
  amplitude.setInput(sound2); 
} 
function draw() { 
  background(255); 
  fill(200); 
  let gfg = amplitude.getLevel(); 
  let size = map(gfg, 0, 1, 0, 400); 
  ellipse(width/1, height/1, size*2, size*2); 
} 
function mousePressed(){ 
  sound2.pause(); 
} 
   
function mouseReleased(){ 
  sound2.play(); 
}

在線編輯:
環境設置:

支持的瀏覽器:下麵列出了p5.js getLevel()函數支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • 蘋果瀏覽器
  • Opera


相關用法


注:本文由純淨天空篩選整理自skyridetim大神的英文原創作品 p5.js | getLevel() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。