UNPKG

4.01 kBJavaScriptView Raw
1import { Tone } from "../Tone.js";
2import { optionsFromArguments } from "../util/Defaults.js";
3import { noOp } from "../util/Interface.js";
4import { isString } from "../util/TypeCheck.js";
5import { ToneAudioBuffer } from "./ToneAudioBuffer.js";
6import { assert } from "../util/Debug.js";
7/**
8 * A data structure for holding multiple buffers in a Map-like datastructure.
9 *
10 * @example
11 * const pianoSamples = new Tone.ToneAudioBuffers({
12 * A1: "https://tonejs.github.io/audio/casio/A1.mp3",
13 * A2: "https://tonejs.github.io/audio/casio/A2.mp3",
14 * }, () => {
15 * const player = new Tone.Player().toDestination();
16 * // play one of the samples when they all load
17 * player.buffer = pianoSamples.get("A2");
18 * player.start();
19 * });
20 * @example
21 * // To pass in additional parameters in the second parameter
22 * const buffers = new Tone.ToneAudioBuffers({
23 * urls: {
24 * A1: "A1.mp3",
25 * A2: "A2.mp3",
26 * },
27 * onload: () => console.log("loaded"),
28 * baseUrl: "https://tonejs.github.io/audio/casio/"
29 * });
30 * @category Core
31 */
32export class ToneAudioBuffers extends Tone {
33 constructor() {
34 super();
35 this.name = "ToneAudioBuffers";
36 /**
37 * All of the buffers
38 */
39 this._buffers = new Map();
40 /**
41 * Keep track of the number of loaded buffers
42 */
43 this._loadingCount = 0;
44 const options = optionsFromArguments(ToneAudioBuffers.getDefaults(), arguments, ["urls", "onload", "baseUrl"], "urls");
45 this.baseUrl = options.baseUrl;
46 // add each one
47 Object.keys(options.urls).forEach((name) => {
48 this._loadingCount++;
49 const url = options.urls[name];
50 this.add(name, url, this._bufferLoaded.bind(this, options.onload), options.onerror);
51 });
52 }
53 static getDefaults() {
54 return {
55 baseUrl: "",
56 onerror: noOp,
57 onload: noOp,
58 urls: {},
59 };
60 }
61 /**
62 * True if the buffers object has a buffer by that name.
63 * @param name The key or index of the buffer.
64 */
65 has(name) {
66 return this._buffers.has(name.toString());
67 }
68 /**
69 * Get a buffer by name. If an array was loaded,
70 * then use the array index.
71 * @param name The key or index of the buffer.
72 */
73 get(name) {
74 assert(this.has(name), `ToneAudioBuffers has no buffer named: ${name}`);
75 return this._buffers.get(name.toString());
76 }
77 /**
78 * A buffer was loaded. decrement the counter.
79 */
80 _bufferLoaded(callback) {
81 this._loadingCount--;
82 if (this._loadingCount === 0 && callback) {
83 callback();
84 }
85 }
86 /**
87 * If the buffers are loaded or not
88 */
89 get loaded() {
90 return Array.from(this._buffers).every(([_, buffer]) => buffer.loaded);
91 }
92 /**
93 * Add a buffer by name and url to the Buffers
94 * @param name A unique name to give the buffer
95 * @param url Either the url of the bufer, or a buffer which will be added with the given name.
96 * @param callback The callback to invoke when the url is loaded.
97 * @param onerror Invoked if the buffer can't be loaded
98 */
99 add(name, url, callback = noOp, onerror = noOp) {
100 if (isString(url)) {
101 // don't include the baseUrl if the url is a base64 encoded sound
102 if (this.baseUrl &&
103 url.trim().substring(0, 11).toLowerCase() === "data:audio/") {
104 this.baseUrl = "";
105 }
106 this._buffers.set(name.toString(), new ToneAudioBuffer(this.baseUrl + url, callback, onerror));
107 }
108 else {
109 this._buffers.set(name.toString(), new ToneAudioBuffer(url, callback, onerror));
110 }
111 return this;
112 }
113 dispose() {
114 super.dispose();
115 this._buffers.forEach((buffer) => buffer.dispose());
116 this._buffers.clear();
117 return this;
118 }
119}
120//# sourceMappingURL=ToneAudioBuffers.js.map
\No newline at end of file