use AudioContext to play recorded sounds

pull/89/head
jmoenig 2019-04-02 10:33:34 +02:00
rodzic 514ec3a1bf
commit 91f4391509
2 zmienionych plików z 18 dodań i 6 usunięć

Wyświetl plik

@ -55,6 +55,7 @@
### 2019-04-02
* Objects, Threads: lazily initialize volume property
* Objects: use AudioContext to play recorded sounds
### 2019-04-01
* Objects: let the Microphone share the Note prototype's AudioContext

Wyświetl plik

@ -3237,16 +3237,25 @@ SpriteMorph.prototype.playSound = function (name) {
this.sounds.asArray(),
function (s) {return s.name === name.toString(); }
),
active;
ctx = this.audioContext(),
gain = this.getGainNode(),
aud,
source;
if (sound) {
active = sound.play();
aud = document.createElement('audio');
aud.src = sound.audio.src;
source = ctx.createMediaElementSource(aud);
source.connect(gain);
gain.connect(ctx.destination); // perhaps redundant
this.setVolume(this.volume); // probably redundant as well
aud.play();
if (stage) {
stage.activeSounds.push(active);
stage.activeSounds = stage.activeSounds.filter(function (aud) {
return !aud.ended && !aud.terminated;
stage.activeSounds.push(aud);
stage.activeSounds = stage.activeSounds.filter(function (snd) {
return !snd.ended && !snd.terminated;
});
}
return active;
return aud;
}
};
@ -8848,6 +8857,8 @@ function Sound(audio, name) {
Sound.prototype.play = function () {
// return an instance of an audio element which can be terminated
// externally (i.e. by the stage)
// Note: only to be used by the GUI, not by scripts,
// because no effects like volume or panning are applied
var aud = document.createElement('audio');
aud.src = this.audio.src;
aud.play();