问题
I am trying to loop through a bunch of sound files in SoundManager2 and play them at various points in time. The code below takes a songArray that looks like this, with the "note" consisting of a sound file and a duration for how long to play that sound file:
var songArray = [[null],[["clarinet_e4.mp3",1],["clarinet_a4.mp3",1]],[null],[null],[null],[null],[["clarinet_c4.mp3",1]],[["clarinet_c5.mp3",1]],[null],[["clarinet_ab4.mp3",1]],[["clarinet_f3.mp3",1]],[null],[["clarinet_g4.mp3",1]],[["clarinet_d5.mp3",1]],[null],[null]]
It goes through each X in the array and plays the sounds found for the given X. It then stops the sound after a time given by the duration. The code runs perfectly except that when a sound is stopped, it gives a popping noise just at the end of the song.
function playSong(X) {
playSongNotesAtXValue(X);
window.setTimeout(function() {
X++;
if (X >= theSong.songArray.length) {
X = 0;
}
playSong(X);
}, (30/theSong.tempo)*1000)
}
function playSongNotesAtXValue(X) {
var columnToPlay = theSong.songArray[X];
if (columnToPlay[0] != null) {
for (i = 0; i < columnToPlay.length; i++) {
var note = columnToPlay[i];
soundManager.play(note[0],{volume:30});
window.setTimeout(function() {
soundManager.stop(note[0]);
}, (note[1]*30/theSong.tempo)*1000);
}
}
}
Any ideas for what could be wrong?
P.S. We have tried it on the latest version of Safari, Chrome, and Firefox.
回答1:
If I am following you correctly, you are playing audio files and then stopping at an arbitrary point in the middle of the file. This will often lead to a discontinuity in the output which will result in a popping or clicking noise because the signal is jumping from a high level to zero in one sample. To eliminate this, you must smoothly reduce the volume of the input from the initial volume to zero, rather than changing it abruptly from the initial volume to zero.
You can usually use linear interpolation to accomplish this "smoothing" task: http://blog.bjornroche.com/2010/10/linear-interpolation-for-audio-in-c-c.html
Sorry I don't know SoundManager2 well enough to understand if you are already doing this.
来源:https://stackoverflow.com/questions/11927302/soundmanager2-distortion-when-stopping-sound