Foreign Registration

Foreign functions are useful for the end-user if he wants to quickly extend the Prolog system by JavaScript functionality. Since we want to let Dogelog play Mary had a little Lamb, we need at least have some Prolog predicate that can play a music note. We have the music notes as audio files and reference them inside the HTML via audio nodes:

    [...]
<audio id="D" src="notes/D.mp3"></audio>
<audio id="Dis" src="notes/Dis.mp3"></audio>
<audio id="E" src="notes/E.mp3"></audio>
<audio id="F" src="notes/F.mp3"></audio>
[...]

We then register a JavaScript routine, which currently does not return any value. This routine looks up then corresponding audio node and then plays the corresponding audio through the DOM method play(). The setter currentTime is used to seek the audio node to the given time and rewind it to zero:

    function note(key) {
const audio = document.getElementById(key);
audio.currentTime = 0
audio.play();
}

register("note", 1, note, 0);

We experienced that the above doesn’t work on all platforms and in all browsers. The reason is possibly that in newer browsers the DOM method play() returns a promise and we do not yet correctly handle this promise. We will update the example when we have found a more portable solution.

Kommentare