📜  p5.js | SoundFile 对象安装和方法

📅  最后修改于: 2022-05-13 01:56:55.392000             🧑  作者: Mango

p5.js | SoundFile 对象安装和方法

SoundFile 对象是 p5.sound 的一部分,它是一个 JavaScript 库。该对象包含如此多的功能,其中有近 30 多个功能。它们中的每一个对于设计一个健全的网站都很有用。通过使用此对象,您可以轻松地将音频添加到您的网站并播放、暂停、停止任何您想要的内容。为此,您只需在索引文件中附加声音库。为此,我们将遵循下面一一提到的一些程序。

  • 第 1 步:首先,您必须从此链接下载 p5.Sound 库,打开此链接后转到 p5.js 完整部分,如下所示。

  • 第 2 步:下载文件后,您必须解压缩文件夹,然后您将在 p5.js 文件夹中看到一些文件,例如index.htmlsketch.js通过打开 index.html 文件,您将看到 p5.js 已经链接到它您可以通过用以下脚本替换该脚本来更改它以获得更快的性能。

示例 HTML 页面可能如下所示:

HTML


    
        
        
    
     
    
    


Javascript
function setup() {
 
}
 
function draw() {
  ellipse(50, 50, 80, 80);
}


  • 第 3 步:在这一步中,我们将为 p5.js 设置代码编辑器。在我看来,最好的代码编辑器是BracketsAtom 。但是你也可以使用其他的,比如 Notepad++、Sublime 等。
  • 第 4 步:现在打开您的编辑器并跟踪到您的 p5.js 文件夹位置并打开sketch.js文件。在这个文件中,我们将执行任务,在这里我们将编写脚本来测试这个库并在我们的网站上使用它们。
  • 第5步:在这一步中,您将了解如何使用它。

Javascript

function setup() {
 
}
 
function draw() {
  ellipse(50, 50, 80, 80);
}

你会得到这样的东西:

p5.SoundFile 的所有函数列表:

FunctionDescription
loadSound()This function is used to load the sound.
isLoaded()This function is used to check the sound is loaded or not.
play()This function is use to play the loaded sound.
playMode()This function is used to play the sound in two playing mode restart and sustain.
pause()This function is used to pause the played sound.
loop()This function is used to create the loop of that sound.
islooping()This function is used to check the loop function called or not.
setLoop()This function is used to create loop define number of times.
isPlaying()This function is used to check audio is playing or not.
isPaused()This function is used to check audio is paused or not.
stop()This function is used to stop the played audio.
setVolume()This function is used to set the volume of the audio range is 0 to 1.
pan()This function is used to set the left right pan of the audio range is -1(left) to 1(right).
getPan()This function is used to check the pan side.
rate()This function is used to set the playing speed of the audio.
duration()This function is used to get the duration of the audio.
currentTime()This function is used to get the current time of the playing audio.
jump()This function is used to jump at some specific point of the audio.
channels()This function is used to get the channels Mono means 1 Stereo means 2.
sampleRate()This function is used to get the samplerate per second of the audio.
frames()This function is used to get the frames of the audio, where frames= samplesrate * duration.
getPeaks()This function is used to get the array of amplitude peaksof the audio.
reverseBuffer()This function is used to play the audio in reverse way.
onended()This function is used to call event after ending the audio.
connect()This function is used to connects the output of a p5sound object to input of another p5.sound object.
disconnect()This function is used to disconnects the output of this p5sound object.
setPath()This function is used to set the path for the audio.
setBuffer()This function is used to set the buffer for the audio.
processPeaks()This function is used to get the beats of the audio.
addCue()This function is used to add events after specified time.
removeCue()This function is used to remove specific event that was schedule by the addCue function.
clearCues()This function is to clear all the event that was scheduled by the addCue function.
save()This function is used to save the audio in locally.
getBlob()This function is used to upload the audio in the server.