surikov / webaudiofont

Use full GM set of musical instruments to play MIDI and single sounds or effects. Support for reverberation and equaliser. No plugins, no Flash. Pure HTML5 implementation compatible with desktop and mobile browser. See live examples.
https://surikov.github.io/webaudiofont/
GNU General Public License v3.0
891 stars 92 forks source link

I have difficulties to understand from the examples #65

Closed mohammadalmalt closed 4 years ago

mohammadalmalt commented 4 years ago

Hello, I have written a function to play an array of notes, for example: [0, 4, 7] , and i added an interval but i seem to don't understand how to make a pause between the notes. All three notes get played at the same time, like a chord. I would appreciate any hint to point me in the right direction.

function playmulti(octave, notes, duration) { for (let i = 0; i < notes.length; i++) {

    player.queueWaveTable(audioContext, audioContext.destination
        , instr, 0, 12 * octave + notes[i], duration);
        interval = setInterval(function () {
          console.log("waiting");
          clearInterval(interval);
        }, 3500);

}

return false;

}

surikov commented 4 years ago

Use https://jsbin.com/?html to create working code. Place link to your code here.

mohammadalmalt commented 4 years ago

Hello,

Here is the code: https://jsbin.com/zizovex/edit?html

surikov commented 4 years ago

read wiki at https://github.com/surikov/webaudiofont/wiki/2.1.-queueWaveTable fixed code

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
   <script src='https://surikov.github.io/webaudiofont/npm/dist/WebAudioFontPlayer.js'></script>
    <script src='https://surikov.github.io/webaudiofontdata/sound/0000_Chaos_sf2_file.js'></script>
   <script>
    var instr = _tone_0000_Chaos_sf2_file;
var AudioContextFunc = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContextFunc();
var player = new WebAudioFontPlayer();
player.loader.decodeAfterLoading(audioContext, '_tone_0000_Chaos_sf2_file');
var interval;

function playmulti(octave, notes, duration) {
var startTime=audioContext.currentTime+0.1;
for (let i = 0; i < notes.length; i++) {

    player.queueWaveTable(audioContext, audioContext.destination
        , instr, startTime+i/2, 12 * octave + notes[i], duration);
        interval = setInterval(function () {
          console.log("waiting");
          clearInterval(interval);
        }, duration);

}

return false;
}

  </script>
</head>
<body>
<p><a href="#" onmousedown="playmulti(4, [0,4,7], 1);">Play three notes</a></p>
</body>
</html>
mohammadalmalt commented 4 years ago

большое спасибо. я твой должник