Skip to content

Commit 608ffa9

Browse files
committed
loadSound support both preload and promises
1 parent 115c96a commit 608ffa9

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/SoundFile.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,27 @@ import { Player as TonePlayer } from "tone/build/esm/source/buffer/Player.js";
4848
* </code></div>
4949
*/
5050
function loadSound (path) {
51-
let player = new p5.SoundFile(
52-
path,
53-
function () {
54-
self._decrementPreload();
55-
}
56-
);
57-
return player;
51+
if(self._incrementPreload && self._decrementPreload){
52+
self._incrementPreload();
53+
54+
let player = new p5.SoundFile(
55+
path,
56+
function () {
57+
self._decrementPreload();
58+
}
59+
);
60+
return player;
61+
62+
}else{
63+
return new Promise((resolve) => {
64+
let player = new p5.SoundFile(
65+
path,
66+
function () {
67+
resolve(player);
68+
}
69+
);
70+
});
71+
}
5872
}
5973

6074
/**

src/app.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ p5.Panner = Panner;
5151
import Panner3D from './Panner3D';
5252
p5.Panner3D = Panner3D;
5353

54-
p5.prototype.loadSound = loadSound;
55-
p5.prototype.registerPreloadMethod('loadSound', p5.prototype);
56-
5754
import SoundFile, { loadSound } from './SoundFile';
5855
p5.SoundFile = SoundFile;
56+
p5.prototype.loadSound = loadSound;
5957

6058
import AudioIn from './AudioIn';
6159
p5.AudioIn = AudioIn;

0 commit comments

Comments
 (0)