tone.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. isSynthDefinition: 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/synth.vwf");
  64. }
  65. }
  66. return found;
  67. },
  68. isMembraneSynthDefinition: 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/membraneSynth.vwf");
  73. }
  74. }
  75. return found;
  76. },
  77. isNoiseSynthDefinition: 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/noiseSynth.vwf");
  82. }
  83. }
  84. return found;
  85. },
  86. createToneObject: function (node, config) {
  87. var protos = node.prototypes;
  88. var toneObj = undefined;
  89. if (this.isToneClass(protos, "proxy/tonejs/synth.vwf")) {
  90. toneObj = new Tone.PolySynth(Tone.synth);
  91. }
  92. if (this.isToneClass(protos, "proxy/tonejs/membraneSynth.vwf")) {
  93. toneObj = new Tone.PolySynth(Tone.MembraneSynth);
  94. // toneObj.set({
  95. // envelope: {
  96. // release: 0.1
  97. // }
  98. // });
  99. }
  100. if (this.isToneClass(protos, "proxy/tonejs/noiseSynth.vwf")) {
  101. toneObj = new Tone.NoiseSynth();
  102. }
  103. if (this.isToneClass(protos, "proxy/tonejs/pluckSynth.vwf")) {
  104. toneObj = new Tone.PolySynth(Tone.PluckSynth);
  105. }
  106. return toneObj
  107. }
  108. };
  109. this.state.kernel = this.kernel.kernel.kernel;
  110. //this.Tone = Tone;
  111. //this.state.kernel = this.kernel.kernel.kernel;
  112. },
  113. // == Model API ============================================================================
  114. // -- creatingNode -------------------------------------------------------------------------
  115. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  116. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  117. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  118. // the scene or a prototype. In either of those cases, save the uri of the new node
  119. var childURI = (nodeID === 0 ? childIndex : undefined);
  120. var appID = this.kernel.application();
  121. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  122. // and then return
  123. var prototypeID = _self_.utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  124. if (prototypeID !== undefined) {
  125. this.state.prototypes[prototypeID] = {
  126. parentID: nodeID,
  127. ID: childID,
  128. extendsID: childExtendsID,
  129. implementsID: childImplementsIDs,
  130. source: childSource,
  131. type: childType,
  132. name: childName
  133. };
  134. return;
  135. }
  136. var protos = _self_.getPrototypes(this.kernel, childExtendsID);
  137. //var kernel = this.kernel.kernel.kernel;
  138. var node;
  139. if (this.state.isToneNodeComponent(protos)) {
  140. // Create the local copy of the node properties
  141. if (this.state.nodes[childID] === undefined) {
  142. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  143. childSource, childType, childIndex, childName, callback);
  144. }
  145. node = this.state.nodes[childID];
  146. node.prototypes = protos;
  147. //this.state.createToneObject(node);
  148. let aframeDriver = vwf.views["/drivers/view/aframe"];
  149. if(aframeDriver){
  150. let parentNode = aframeDriver.state.nodes[nodeID];
  151. if(parentNode.aframeObj){
  152. let sceneEl = aframeDriver.state.nodes[nodeID].scene.sceneEl;
  153. if (!sceneEl.audioListener) {
  154. sceneEl.audioListener = new THREE.AudioListener();
  155. sceneEl.camera && sceneEl.camera.add(sceneEl.audioListener);
  156. // sceneEl.addEventListener('camera-set-active', function (evt) {
  157. // evt.detail.cameraEl.getObject3D('camera').add(sceneEl.audioListener);
  158. // });
  159. }
  160. node.sound = new THREE.PositionalAudio( sceneEl.audioListener );
  161. Tone.setContext(node.sound.context);
  162. //node.sound.context.resume();
  163. node.toneObj = this.state.createToneObject(node);
  164. node.sound.setNodeSource(node.toneObj);
  165. parentNode.aframeObj.object3D.add(node.sound);
  166. }
  167. }
  168. //addNodeToHierarchy(node);
  169. //notifyDriverOfPrototypeAndBehaviorProps();
  170. }
  171. },
  172. // -- initializingNode -------------------------------------------------------------------------
  173. // initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  174. // childSource, childType, childIndex, childName ) {
  175. // },
  176. // -- deletingNode -------------------------------------------------------------------------
  177. //deletingNode: function( nodeID ) {
  178. //},
  179. // -- initializingProperty -----------------------------------------------------------------
  180. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  181. var value = undefined;
  182. var node = this.state.nodes[nodeID];
  183. if (node !== undefined) {
  184. value = this.settingProperty(nodeID, propertyName, propertyValue);
  185. }
  186. return value;
  187. },
  188. // -- creatingProperty ---------------------------------------------------------------------
  189. creatingProperty: function (nodeID, propertyName, propertyValue) {
  190. return this.initializingProperty(nodeID, propertyName, propertyValue);
  191. },
  192. // -- settingProperty ----------------------------------------------------------------------
  193. settingProperty: function( nodeID, propertyName, propertyValue ) {
  194. let self = this;
  195. let node = this.state.nodes[nodeID];
  196. var value = undefined;
  197. if (node && node.toneObj && _self_.utility.validObject(propertyValue)) {
  198. let toneObject = node.toneObj;
  199. // if (self.state.isComponentNodeDefinition(node.prototypes)) {
  200. // value = propertyValue;
  201. // switch (propertyName) {
  202. // default:
  203. // value = undefined;
  204. // break;
  205. // }
  206. // }
  207. if (value === undefined && self.state.isSynthDefinition(node.prototypes)) {
  208. value = propertyValue;
  209. switch (propertyName) {
  210. case "type":
  211. toneObject.set({oscillator:{type: propertyValue}})
  212. //"sine"; "square"; "triangle"; "sawtooth";
  213. break;
  214. default:
  215. value = undefined;
  216. break;
  217. }
  218. }
  219. }
  220. return value;
  221. },
  222. // -- gettingProperty ----------------------------------------------------------------------
  223. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  224. let self = this;
  225. let node = this.state.nodes[nodeID];
  226. let value = undefined;
  227. if (node && node.toneObj) {
  228. let toneObject = node.toneObj;
  229. // if (self.state.isComponentNodeDefinition(node.prototypes)) {
  230. // switch (propertyName) {
  231. // }
  232. // }
  233. if (value === undefined && self.state.isSynthDefinition(node.prototypes)) {
  234. switch (propertyName) {
  235. case "type":
  236. value = toneObject.options.oscillator.type
  237. break;
  238. }
  239. }
  240. }
  241. if ( value !== undefined ) {
  242. propertyValue = value;
  243. }
  244. return value;
  245. }
  246. } );
  247. }
  248. getPrototypes(kernel, extendsID) {
  249. var prototypes = [];
  250. var id = extendsID;
  251. while (id !== undefined) {
  252. prototypes.push(id);
  253. id = kernel.prototype(id);
  254. }
  255. return prototypes;
  256. }
  257. }
  258. export {
  259. ToneModel as default
  260. }