tone.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. */
  5. // VWF & ToneJS model driver
  6. import { Fabric } from '/core/vwf/fabric.js';
  7. class ToneModel extends Fabric {
  8. constructor(module) {
  9. console.log("ToneModel constructor");
  10. super(module, "Model");
  11. }
  12. factory() {
  13. let _self_ = this;
  14. return this.load(this.module,
  15. {
  16. // == Module Definition ====================================================================
  17. // -- pipeline -----------------------------------------------------------------------------
  18. // pipeline: [ log ], // vwf <=> log <=> scene
  19. // -- initialize ---------------------------------------------------------------------------
  20. initialize: function () {
  21. var self = this;
  22. this.state = {
  23. nodes: {},
  24. scenes: {},
  25. prototypes: {},
  26. createLocalNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  27. childSource, childType, childIndex, childName, callback) {
  28. return {
  29. "parentID": nodeID,
  30. "ID": childID,
  31. "extendsID": childExtendsID,
  32. "implementsIDs": childImplementsIDs,
  33. "source": childSource,
  34. "type": childType,
  35. "name": childName,
  36. "prototypes": undefined
  37. };
  38. },
  39. isToneNodeComponent: function (prototypes) {
  40. var found = false;
  41. if (prototypes) {
  42. for (var i = 0; i < prototypes.length && !found; i++) {
  43. found = (prototypes[i] === "proxy/tonejs/node.vwf");
  44. }
  45. }
  46. return found;
  47. },
  48. isToneClass: function (prototypes, classID) {
  49. if (prototypes) {
  50. for (var i = 0; i < prototypes.length; i++) {
  51. if (prototypes[i] === classID) {
  52. //console.info( "prototypes[ i ]: " + prototypes[ i ] );
  53. return true;
  54. }
  55. }
  56. }
  57. return false;
  58. },
  59. isTransportDefinition: function (prototypes) {
  60. var found = false;
  61. if (prototypes) {
  62. for (var i = 0; i < prototypes.length && !found; i++) {
  63. found = (prototypes[i] == "proxy/tonejs/transport.vwf");
  64. }
  65. }
  66. return found;
  67. },
  68. isPlayerDefinition: function (prototypes) {
  69. var found = false;
  70. if (prototypes) {
  71. for (var i = 0; i < prototypes.length && !found; i++) {
  72. found = (prototypes[i] == "proxy/tonejs/player.vwf");
  73. }
  74. }
  75. return found;
  76. },
  77. isSynthDefinition: function (prototypes) {
  78. var found = false;
  79. if (prototypes) {
  80. for (var i = 0; i < prototypes.length && !found; i++) {
  81. found = (prototypes[i] == "proxy/tonejs/synth.vwf");
  82. }
  83. }
  84. return found;
  85. },
  86. isMembraneSynthDefinition: function (prototypes) {
  87. var found = false;
  88. if (prototypes) {
  89. for (var i = 0; i < prototypes.length && !found; i++) {
  90. found = (prototypes[i] == "proxy/tonejs/membraneSynth.vwf");
  91. }
  92. }
  93. return found;
  94. },
  95. isNoiseSynthDefinition: function (prototypes) {
  96. var found = false;
  97. if (prototypes) {
  98. for (var i = 0; i < prototypes.length && !found; i++) {
  99. found = (prototypes[i] == "proxy/tonejs/noiseSynth.vwf");
  100. }
  101. }
  102. return found;
  103. },
  104. createToneObject: function (node, config) {
  105. var protos = node.prototypes;
  106. var toneObj = undefined;
  107. if (this.isToneClass(protos, "proxy/tonejs/transport.vwf")) {
  108. // global transport for now
  109. toneObj = Tone.Transport;
  110. }
  111. if (this.isToneClass(protos, "proxy/tonejs/player.vwf")) {
  112. toneObj = new Tone.Player(); //GrainPlayer
  113. toneObj.autostart = false;
  114. }
  115. if (this.isToneClass(protos, "proxy/tonejs/synth.vwf")) {
  116. toneObj = new Tone.PolySynth(Tone.synth);
  117. }
  118. if (this.isToneClass(protos, "proxy/tonejs/membraneSynth.vwf")) {
  119. toneObj = new Tone.PolySynth(Tone.MembraneSynth);
  120. // toneObj.set({
  121. // envelope: {
  122. // release: 0.1
  123. // }
  124. // });
  125. }
  126. if (this.isToneClass(protos, "proxy/tonejs/noiseSynth.vwf")) {
  127. toneObj = new Tone.NoiseSynth();
  128. }
  129. if (this.isToneClass(protos, "proxy/tonejs/pluckSynth.vwf")) {
  130. toneObj = new Tone.PolySynth(Tone.PluckSynth);
  131. }
  132. return toneObj
  133. }
  134. };
  135. this.state.kernel = this.kernel.kernel.kernel;
  136. //this.Tone = Tone;
  137. //this.state.kernel = this.kernel.kernel.kernel;
  138. },
  139. // == Model API ============================================================================
  140. // -- creatingNode -------------------------------------------------------------------------
  141. creatingNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  142. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  143. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  144. // the scene or a prototype. In either of those cases, save the uri of the new node
  145. var childURI = (nodeID === 0 ? childIndex : undefined);
  146. var appID = this.kernel.application();
  147. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  148. // and then return
  149. var prototypeID = _self_.utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  150. if (prototypeID !== undefined) {
  151. this.state.prototypes[prototypeID] = {
  152. parentID: nodeID,
  153. ID: childID,
  154. extendsID: childExtendsID,
  155. implementsID: childImplementsIDs,
  156. source: childSource,
  157. type: childType,
  158. name: childName
  159. };
  160. return;
  161. }
  162. var protos = _self_.getPrototypes(this.kernel, childExtendsID);
  163. //var kernel = this.kernel.kernel.kernel;
  164. var node;
  165. if (this.state.isToneNodeComponent(protos)) {
  166. // Create the local copy of the node properties
  167. if (this.state.nodes[childID] === undefined) {
  168. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  169. childSource, childType, childIndex, childName, callback);
  170. }
  171. node = this.state.nodes[childID];
  172. node.prototypes = protos;
  173. //this.state.createToneObject(node);
  174. let aframeDriver = vwf.views["/drivers/view/aframe"];
  175. if (aframeDriver) {
  176. let parentNode = aframeDriver.state.nodes[nodeID];
  177. if (parentNode.aframeObj) {
  178. let sceneEl = aframeDriver.state.nodes[nodeID].scene.sceneEl;
  179. if (!sceneEl.audioListener) {
  180. sceneEl.audioListener = new THREE.AudioListener();
  181. sceneEl.camera && sceneEl.camera.add(sceneEl.audioListener);
  182. // sceneEl.addEventListener('camera-set-active', function (evt) {
  183. // evt.detail.cameraEl.getObject3D('camera').add(sceneEl.audioListener);
  184. // });
  185. }
  186. node.sound = new THREE.PositionalAudio(sceneEl.audioListener);
  187. Tone.setContext(node.sound.context);
  188. //node.sound.context.resume();
  189. node.toneObj = this.state.createToneObject(node);
  190. if (!this.state.isTransportDefinition(node.prototypes)) {
  191. node.sound.setNodeSource(node.toneObj);
  192. parentNode.aframeObj.object3D.add(node.sound);
  193. }
  194. }
  195. } else {
  196. node.toneObj = this.state.createToneObject(node);
  197. if (!this.state.isTransportDefinition(node.prototypes)) {
  198. node.toneObj.toDestination();
  199. }
  200. //node.sound.setNodeSource(node.toneObj);
  201. }
  202. //addNodeToHierarchy(node);
  203. //notifyDriverOfPrototypeAndBehaviorProps();
  204. }
  205. },
  206. // -- initializingNode -------------------------------------------------------------------------
  207. // initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  208. // childSource, childType, childIndex, childName ) {
  209. // },
  210. // -- deletingNode -------------------------------------------------------------------------
  211. //deletingNode: function( nodeID ) {
  212. //},
  213. // -- initializingProperty -----------------------------------------------------------------
  214. initializingProperty: function (nodeID, propertyName, propertyValue) {
  215. var value = undefined;
  216. var node = this.state.nodes[nodeID];
  217. if (node !== undefined) {
  218. value = this.settingProperty(nodeID, propertyName, propertyValue);
  219. }
  220. return value;
  221. },
  222. // -- creatingProperty ---------------------------------------------------------------------
  223. creatingProperty: function (nodeID, propertyName, propertyValue) {
  224. return this.initializingProperty(nodeID, propertyName, propertyValue);
  225. },
  226. // -- settingProperty ----------------------------------------------------------------------
  227. settingProperty: function (nodeID, propertyName, propertyValue) {
  228. let self = this;
  229. let node = this.state.nodes[nodeID];
  230. var value = undefined;
  231. if (node && node.toneObj && _self_.utility.validObject(propertyValue)) {
  232. let toneObject = node.toneObj;
  233. // if (self.state.isComponentNodeDefinition(node.prototypes)) {
  234. // value = propertyValue;
  235. // switch (propertyName) {
  236. // default:
  237. // value = undefined;
  238. // break;
  239. // }
  240. // }
  241. if (value === undefined && self.state.isTransportDefinition(node.prototypes)) {
  242. value = propertyValue;
  243. switch (propertyName) {
  244. case "bpm":
  245. toneObject.bpm.value = propertyValue
  246. toneObject.initbpm = propertyValue
  247. break;
  248. // case "state":
  249. // toneObject.state = propertyValue
  250. // break;
  251. case "position":
  252. toneObject.position = propertyValue
  253. break;
  254. case "loop":
  255. toneObject.loop = propertyValue
  256. break;
  257. case "loopStart":
  258. if (typeof propertyValue == "string") {
  259. propertyValue = Tone.Time(propertyValue).toSeconds();
  260. }
  261. toneObject.loopStart = propertyValue
  262. break;
  263. case "loopEnd":
  264. if (typeof propertyValue == "string") {
  265. propertyValue = Tone.Time(propertyValue).toSeconds();
  266. }
  267. toneObject.loopEnd = propertyValue
  268. break;
  269. case "duration":
  270. if (typeof propertyValue == "string") {
  271. propertyValue = Tone.Time(propertyValue).toSeconds();
  272. }
  273. toneObject.duration = propertyValue
  274. break;
  275. default:
  276. value = undefined;
  277. break;
  278. }
  279. }
  280. if (value === undefined && self.state.isPlayerDefinition(node.prototypes)) {
  281. value = propertyValue;
  282. switch (propertyName) {
  283. // case "url":
  284. // toneObject.load(propertyValue)
  285. // break;
  286. // case "state":
  287. // toneObject.state = propertyValue
  288. // break;
  289. case "startTime":
  290. toneObject.startTime = propertyValue
  291. break;
  292. case "volume":
  293. toneObject.volume.value = propertyValue
  294. break;
  295. case "mute":
  296. toneObject.mute = propertyValue
  297. break;
  298. case "autostart":
  299. toneObject.autostart = propertyValue
  300. break;
  301. case "loop":
  302. toneObject.loop = propertyValue
  303. break;
  304. case "loopStart":
  305. toneObject.loopStart = propertyValue
  306. break;
  307. case "loopEnd":
  308. toneObject.loopEnd = propertyValue
  309. break;
  310. // case "duration":
  311. // toneObject.duration = propertyValue
  312. // break;
  313. default:
  314. value = undefined;
  315. break;
  316. }
  317. }
  318. if (value === undefined && self.state.isSynthDefinition(node.prototypes)) {
  319. value = propertyValue;
  320. switch (propertyName) {
  321. case "type":
  322. toneObject.set({ oscillator: { type: propertyValue } })
  323. //"sine"; "square"; "triangle"; "sawtooth";
  324. break;
  325. default:
  326. value = undefined;
  327. break;
  328. }
  329. }
  330. }
  331. return value;
  332. },
  333. // -- gettingProperty ----------------------------------------------------------------------
  334. gettingProperty: function (nodeID, propertyName, propertyValue) {
  335. let self = this;
  336. let node = this.state.nodes[nodeID];
  337. let value = undefined;
  338. if (node && node.toneObj) {
  339. let toneObject = node.toneObj;
  340. // if (self.state.isComponentNodeDefinition(node.prototypes)) {
  341. // switch (propertyName) {
  342. // }
  343. // }
  344. if (value === undefined && self.state.isTransportDefinition(node.prototypes)) {
  345. switch (propertyName) {
  346. case "bpm":
  347. value = toneObject.bpm.value
  348. break;
  349. // case "state":
  350. // value = toneObject.state
  351. // break;
  352. case "position":
  353. value = toneObject.position
  354. break;
  355. case "loopStart":
  356. value = toneObject.loopStart
  357. break;
  358. case "loopEnd":
  359. value = toneObject.loopEnd
  360. break;
  361. case "duration":
  362. value = toneObject.duration
  363. break;
  364. case "loop":
  365. value = toneObject.loop
  366. break;
  367. }
  368. }
  369. if (value === undefined && self.state.isPlayerDefinition(node.prototypes)) {
  370. switch (propertyName) {
  371. case "autostart":
  372. value = toneObject.autostart
  373. break;
  374. // case "state":
  375. // value = toneObject.state
  376. // break;
  377. case "startTime":
  378. value = toneObject.startTime
  379. break;
  380. case "volume":
  381. value = toneObject.volume.value
  382. break;
  383. case "mute":
  384. value = toneObject.mute
  385. break;
  386. case "loopStart":
  387. value = toneObject.loopStart
  388. break;
  389. case "loopEnd":
  390. value = toneObject.loopEnd
  391. break;
  392. case "duration":
  393. value = toneObject.buffer.duration
  394. break;
  395. case "loop":
  396. value = toneObject.loop
  397. break;
  398. }
  399. }
  400. if (value === undefined && self.state.isSynthDefinition(node.prototypes)) {
  401. switch (propertyName) {
  402. case "type":
  403. value = toneObject.options.oscillator.type
  404. break;
  405. }
  406. }
  407. }
  408. if (value !== undefined) {
  409. propertyValue = value;
  410. }
  411. return value;
  412. }
  413. });
  414. }
  415. getPrototypes(kernel, extendsID) {
  416. var prototypes = [];
  417. var id = extendsID;
  418. while (id !== undefined) {
  419. prototypes.push(id);
  420. id = kernel.prototype(id);
  421. }
  422. return prototypes;
  423. }
  424. }
  425. export {
  426. ToneModel as default
  427. }