I am trying to learn p5.sound by loading a sound, and panning it with a slider.
When I move the slider, especially if I move it too far too suddenly, the sound begins to crackle. Crackling while panning does not happen with DAWs on my computer, but only on browsers.
How can I eliminate the popping?
var sound;
var sliderPan
var button;
function preload() {
sound = loadSound("formsoflife.mp3");
}
function setup() {
createCanvas(400, 400);
button = createButton("play");
button.mousePressed(togglePlaying);
sliderPan = createSlider(-1, 1, 0, 0.1);
}
function draw() {
background(0);
background (sound.currentTime(), 0, 255)
sound.pan(sliderPan.value());
}
function togglePlaying() {
if (!sound.isPlaying()) {
sound.play();
button.html("pause");
} else if (sound.isPlaying()) {
sound.pause();
button.html("play");
}
}
I’m guessing that the popping is less noticeable if you move the slider gently, right? If you move the slider fast back and forth, again and again, the popping is probably present all the time.
Think about what is actually happening in the physical world when you move that digital slider. You’re controlling electrons! Speakers produce sound by converting electricity to mechanical movement, after all.
In the extreme case, when panning from all left to all right very fast, distortions, in the form of the popping you’re describing, can be heard. The trick is to slowly ramp the value up and down instead of changing it right away.
Luckily, there’s built-in support for that. pan() has a second argument, timeFromNow, that lets you control the ramp time. Experiment with different values, and I’m sure you’ll find something that makes the popping less noticeable:
The linearRampToValueAtTime() method of the AudioParam Interface schedules a gradual linear change in the value of the AudioParam . The change starts at the time specified for the previous event, follows a linear ramp to the new value given in the value parameter, and reaches the new value at the time given in the endTime parameter.
Hmm, using that code causes excessive popping during the 3-second pan… Might need to write a function that splits the 3 seconds into increments of 0.1 seconds.
I suspect that’s caused by the panning being done in draw(). draw() is called 60 times per second on most machines. Just as a test, what if you do the pan everytime the slider value is changed instead:
It does kind-of work! The only problem is that if you change slider before current the 3-second ramp is completed, it starts immediately from the end of the ramp.
Glad to hear that. That makes sense, the line sound.pan(sound.getPan()) causes this.
I’m afraid what you want is not directly achievable via p5.js-sound API:s. You either have to look one level below, working directly with the Web Audio API:s or create a workaround.
Maybe by splitting up the increments, as you suggested earlier, or by disabling the slider while panning is in progress. (The user has to wait until it is finished.)