tone.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 & Tone driver
  6. import {Fabric} from '/core/vwf/fabric.js';
  7. //import * as Tone from '/drivers/view/tonejs/dist/Tone.js';
  8. class ToneViewDriver extends Fabric {
  9. constructor(module) {
  10. console.log("ToneViewDriver constructor");
  11. super(module, 'View');
  12. }
  13. factory() {
  14. let _self_ = this;
  15. return this.load(this.module,
  16. {
  17. initialize: function() {
  18. let self = this;
  19. this.fabric = _self_;
  20. this.nodes = {};
  21. // document.querySelector("button").addEventListener("click", async () => {
  22. // await Tone.start();
  23. // console.log("context started");
  24. // });
  25. //window._Tone = Tone.default;
  26. // this.osc = osc;
  27. // this.portValue = '8081';
  28. // this.hostValue = 'localhost';
  29. // this.port = null;
  30. //window._osc = this.osc;
  31. // window._OSCManager = this;
  32. },
  33. createdNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  34. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  35. let self = this;
  36. var node = this.state.nodes[childID];
  37. // If the "nodes" object does not have this object in it, it must not be one that
  38. // this driver cares about
  39. if (!node) {
  40. return;
  41. }
  42. this.nodes[childID] = {
  43. id: childID,
  44. extends: childExtendsID,
  45. parent: nodeID,
  46. toneObj: node.toneObj
  47. };
  48. //parent: this.state.nodes[childID].aframeObj
  49. },
  50. firedEvent: function (nodeID, eventName, eventParameters) {
  51. let self = this;
  52. // if (eventName == 'sendOSC'){
  53. // var clientThatSatProperty = self.kernel.client();
  54. // var me = self.kernel.moniker();
  55. // // If the transform property was initially updated by this view....
  56. // if (clientThatSatProperty == me) {
  57. // if (self.osc !== null) {
  58. // if (self.getStatus() == 1) {
  59. // self.port.send(eventParameters[0]);
  60. // console.log('send: ' + eventParameters[0]);
  61. // }
  62. // }
  63. // }
  64. // }
  65. },
  66. /*
  67. * Receives incoming messages
  68. */
  69. calledMethod: function( nodeID, methodName, methodParameters, methodValue ) {
  70. let self = this;
  71. let node = this.state.nodes[nodeID];
  72. // If the "nodes" object does not have this object in it, it must not be one that
  73. // this driver cares about
  74. if (!node) {
  75. return;
  76. }
  77. if (methodName == "triggerAttackRelease") {
  78. if(node.toneObj){
  79. const now = Tone.now()
  80. let notes = methodParameters[0];
  81. // let notes = methodParameters[0].map(el=>{
  82. // return Tone.Frequency(el).toNote();
  83. // })
  84. if (self.state.isMembraneSynthDefinition(node.prototypes)) {
  85. node.toneObj.triggerAttackRelease(notes[0], methodParameters[1][0], now);
  86. } else if(self.state.isNoiseSynthDefinition(node.prototypes)) {
  87. node.toneObj.triggerAttackRelease("16n", now)
  88. }
  89. else {
  90. node.toneObj.triggerAttackRelease(notes, methodParameters[1], now, methodParameters[2])
  91. }
  92. }
  93. }
  94. if (methodName == "triggerAttack") {
  95. if(node.toneObj){
  96. const now = Tone.now()
  97. node.toneObj.triggerAttack(methodParameters[0], now, methodParameters[1])
  98. }
  99. }
  100. if (methodName == "triggerRelease") {
  101. if(node.toneObj){
  102. node.toneObj.triggerRelease(methodParameters[0], "+0.1")
  103. }
  104. }
  105. },
  106. });
  107. }
  108. }
  109. export { ToneViewDriver as default }