models.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. // Import as different name `coreEmit`, so we don't conflict
  6. // with the global `window` listener itself.
  7. const { emit: coreEmit } = require("sdk/event/core");
  8. /**
  9. * Representational wrapper around AudioNodeActors. Adding and destroying
  10. * AudioNodes should be performed through the AudioNodes collection.
  11. *
  12. * Events:
  13. * - `connect`: node, destinationNode, parameter
  14. * - `disconnect`: node
  15. */
  16. const AudioNodeModel = Class({
  17. extends: EventTarget,
  18. // Will be added via AudioNodes `add`
  19. collection: null,
  20. initialize: function (actor) {
  21. this.actor = actor;
  22. this.id = actor.actorID;
  23. this.type = actor.type;
  24. this.bypassable = actor.bypassable;
  25. this._bypassed = false;
  26. this.connections = [];
  27. },
  28. /**
  29. * Stores connection data inside this instance of this audio node connecting
  30. * to another node (destination). If connecting to another node's AudioParam,
  31. * the second argument (param) must be populated with a string.
  32. *
  33. * Connecting nodes is idempotent. Upon new connection, emits "connect" event.
  34. *
  35. * @param AudioNodeModel destination
  36. * @param String param
  37. */
  38. connect: function (destination, param) {
  39. let edge = findWhere(this.connections, { destination: destination.id, param: param });
  40. if (!edge) {
  41. this.connections.push({ source: this.id, destination: destination.id, param: param });
  42. coreEmit(this, "connect", this, destination, param);
  43. }
  44. },
  45. /**
  46. * Clears out all internal connection data. Emits "disconnect" event.
  47. */
  48. disconnect: function () {
  49. this.connections.length = 0;
  50. coreEmit(this, "disconnect", this);
  51. },
  52. /**
  53. * Gets the bypass status of the audio node.
  54. *
  55. * @return Boolean
  56. */
  57. isBypassed: function () {
  58. return this._bypassed;
  59. },
  60. /**
  61. * Sets the bypass value of an AudioNode.
  62. *
  63. * @param Boolean enable
  64. * @return Promise
  65. */
  66. bypass: function (enable) {
  67. this._bypassed = enable;
  68. return this.actor.bypass(enable).then(() => coreEmit(this, "bypass", this, enable));
  69. },
  70. /**
  71. * Returns a promise that resolves to an array of objects containing
  72. * both a `param` name property and a `value` property.
  73. *
  74. * @return Promise->Object
  75. */
  76. getParams: function () {
  77. return this.actor.getParams();
  78. },
  79. /**
  80. * Returns a promise that resolves to an object containing an
  81. * array of event information and an array of automation data.
  82. *
  83. * @param String paramName
  84. * @return Promise->Array
  85. */
  86. getAutomationData: function (paramName) {
  87. return this.actor.getAutomationData(paramName);
  88. },
  89. /**
  90. * Takes a `dagreD3.Digraph` object and adds this node to
  91. * the graph to be rendered.
  92. *
  93. * @param dagreD3.Digraph
  94. */
  95. addToGraph: function (graph) {
  96. graph.addNode(this.id, {
  97. type: this.type,
  98. label: this.type.replace(/Node$/, ""),
  99. id: this.id,
  100. bypassed: this._bypassed
  101. });
  102. },
  103. /**
  104. * Takes a `dagreD3.Digraph` object and adds edges to
  105. * the graph to be rendered. Separate from `addToGraph`,
  106. * as while we depend on D3/Dagre's constraints, we cannot
  107. * add edges for nodes that have not yet been added to the graph.
  108. *
  109. * @param dagreD3.Digraph
  110. */
  111. addEdgesToGraph: function (graph) {
  112. for (let edge of this.connections) {
  113. let options = {
  114. source: this.id,
  115. target: edge.destination
  116. };
  117. // Only add `label` if `param` specified, as this is an AudioParam
  118. // connection then. `label` adds the magic to render with dagre-d3,
  119. // and `param` is just more explicitly the param, ignoring
  120. // implementation details.
  121. if (edge.param) {
  122. options.label = options.param = edge.param;
  123. }
  124. graph.addEdge(null, this.id, edge.destination, options);
  125. }
  126. },
  127. toString: () => "[object AudioNodeModel]",
  128. });
  129. /**
  130. * Constructor for a Collection of `AudioNodeModel` models.
  131. *
  132. * Events:
  133. * - `add`: node
  134. * - `remove`: node
  135. * - `connect`: node, destinationNode, parameter
  136. * - `disconnect`: node
  137. */
  138. const AudioNodesCollection = Class({
  139. extends: EventTarget,
  140. model: AudioNodeModel,
  141. initialize: function () {
  142. this.models = new Set();
  143. this._onModelEvent = this._onModelEvent.bind(this);
  144. },
  145. /**
  146. * Iterates over all models within the collection, calling `fn` with the
  147. * model as the first argument.
  148. *
  149. * @param Function fn
  150. */
  151. forEach: function (fn) {
  152. this.models.forEach(fn);
  153. },
  154. /**
  155. * Creates a new AudioNodeModel, passing through arguments into the AudioNodeModel
  156. * constructor, and adds the model to the internal collection store of this
  157. * instance.
  158. *
  159. * Emits "add" event on instance when completed.
  160. *
  161. * @param Object obj
  162. * @return AudioNodeModel
  163. */
  164. add: function (obj) {
  165. let node = new this.model(obj);
  166. node.collection = this;
  167. this.models.add(node);
  168. node.on("*", this._onModelEvent);
  169. coreEmit(this, "add", node);
  170. return node;
  171. },
  172. /**
  173. * Removes an AudioNodeModel from the internal collection. Calls `delete` method
  174. * on the model, and emits "remove" on this instance.
  175. *
  176. * @param AudioNodeModel node
  177. */
  178. remove: function (node) {
  179. this.models.delete(node);
  180. coreEmit(this, "remove", node);
  181. },
  182. /**
  183. * Empties out the internal collection of all AudioNodeModels.
  184. */
  185. reset: function () {
  186. this.models.clear();
  187. },
  188. /**
  189. * Takes an `id` from an AudioNodeModel and returns the corresponding
  190. * AudioNodeModel within the collection that matches that id. Returns `null`
  191. * if not found.
  192. *
  193. * @param Number id
  194. * @return AudioNodeModel|null
  195. */
  196. get: function (id) {
  197. return findWhere(this.models, { id: id });
  198. },
  199. /**
  200. * Returns the count for how many models are a part of this collection.
  201. *
  202. * @return Number
  203. */
  204. get length() {
  205. return this.models.size;
  206. },
  207. /**
  208. * Returns detailed information about the collection. used during tests
  209. * to query state. Returns an object with information on node count,
  210. * how many edges are within the data graph, as well as how many of those edges
  211. * are for AudioParams.
  212. *
  213. * @return Object
  214. */
  215. getInfo: function () {
  216. let info = {
  217. nodes: this.length,
  218. edges: 0,
  219. paramEdges: 0
  220. };
  221. this.models.forEach(node => {
  222. let paramEdgeCount = node.connections.filter(edge => edge.param).length;
  223. info.edges += node.connections.length - paramEdgeCount;
  224. info.paramEdges += paramEdgeCount;
  225. });
  226. return info;
  227. },
  228. /**
  229. * Adds all nodes within the collection to the passed in graph,
  230. * as well as their corresponding edges.
  231. *
  232. * @param dagreD3.Digraph
  233. */
  234. populateGraph: function (graph) {
  235. this.models.forEach(node => node.addToGraph(graph));
  236. this.models.forEach(node => node.addEdgesToGraph(graph));
  237. },
  238. /**
  239. * Called when a stored model emits any event. Used to manage
  240. * event propagation, or listening to model events to react, like
  241. * removing a model from the collection when it's destroyed.
  242. */
  243. _onModelEvent: function (eventName, node, ...args) {
  244. if (eventName === "remove") {
  245. // If a `remove` event from the model, remove it
  246. // from the collection, and let the method handle the emitting on
  247. // the collection
  248. this.remove(node);
  249. } else {
  250. // Pipe the event to the collection
  251. coreEmit(this, eventName, node, ...args);
  252. }
  253. },
  254. toString: () => "[object AudioNodeCollection]",
  255. });