audio.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * WebRTC.js : Behaves as a wrapper for vwf/view/rtcObject
  3. * Maps simple 1:1 signal model to a broadcast model using target and sender ids
  4. */
  5. define( [ "module", "vwf/view", "vwf/view/buzz/buzz.min" ], function( module, view, buzz ) {
  6. var self = this;
  7. //a simple structure to hold the BUZZ sound reference and position data
  8. function SoundSource() {
  9. this.id = null;
  10. this.sound = null;
  11. this.position = null;
  12. this.volume = 1;
  13. this.endrange = 100;
  14. this.startrange = 1;
  15. this.looping = false;
  16. this.playing = false;
  17. this.play = function() {
  18. if ( !this.playing ) {
  19. this.sound.play();
  20. }
  21. if ( this.playing && this.sound.getPercent() == 100 ) {
  22. this.sound.stop();
  23. this.sound.play();
  24. }
  25. this.playing = true;
  26. };
  27. this.pause = function() {
  28. if ( this.playing ) {
  29. this.sound.pause();
  30. }
  31. this.playing = false;
  32. };
  33. this.stop = function() {
  34. if ( this.playing ) {
  35. this.sound.stop();
  36. }
  37. this.playing = false;
  38. };
  39. }
  40. SoundSource.prototype.loop = function() {
  41. if ( !this.looping ) {
  42. this.looping = true;
  43. this.sound.loop();
  44. }
  45. }
  46. SoundSource.prototype.unloop = function() {
  47. if ( this.looping ) {
  48. this.looping = false;
  49. this.sound.unloop();
  50. }
  51. }
  52. //Get the position of your source object
  53. //note: the 3D driver must keep track of this
  54. SoundSource.prototype.updateSourcePosition = function() {
  55. this.position = vwf.getProperty( this.id, 'worldPosition' );
  56. }
  57. //use inverse falloff, adjust the range parameters of the falloff curve by the "volume"
  58. //since HTML cant actually play it louder, but we can make it 'carry' farther
  59. SoundSource.prototype.updateVolume = function( camerapos ) {
  60. var x = Vec3.distance( camerapos, this.position );
  61. x = Math.max( 0, x );
  62. var v = this.volume;
  63. var vol = ( ( -x + v ) / ( v || 1 ) ) * ( ( -x + v ) / ( v || 1 ) );
  64. if( x > v ) {
  65. vol = 0;
  66. }
  67. this.sound.setVolume( Math.max( Math.min( vol, 1 ), 0 ) * 100 );
  68. }
  69. //the driver
  70. return view.load( module, {
  71. initialize: function( options ) {
  72. this.buzz = require( "buzz" );
  73. window._buzz = this.buzz;
  74. this.sounds = {};
  75. this.soundSources = {};
  76. //set this up as a global, so that we can play a click to indicate GUI actions
  77. window._SoundManager = this;
  78. },
  79. //simple function for gui elements to play sounds
  80. playSound: function( url, volume ) {
  81. this.calledMethod( this.kernel.application(), 'playSound', [ url, false, volume ] );
  82. },
  83. calledMethod : function( nodeID, methodName, methodParameters ) {
  84. //if the scene played the sound, it has no position and just plays at full volume
  85. if ( nodeID === this.kernel.application() && methodsName === "playSound" ) {
  86. var url = params[0];
  87. var loop = params[1] || false;
  88. //cache the sound - can only be played simultainously by different nodes
  89. if( this.sounds[ url ] ) {
  90. if( this.sounds[ url ].getPercent() == 100 ) {
  91. this.sounds[ url ].stop();
  92. }
  93. this.sounds[ url ].play();
  94. if ( loop ) {
  95. this.sounds[ url ].loop();
  96. } else {
  97. this.sounds[ url ].unloop();
  98. }
  99. } else {
  100. var mySound = new this.buzz.sound( url, {
  101. autoplay: false,
  102. loop: false
  103. } );
  104. this.sounds[ url ] = mySound;
  105. mySound.play();
  106. }
  107. } else {
  108. var url, loop, vol, soundID, soundSrc;
  109. switch ( methodName ) {
  110. case 'playSound':
  111. url = params[0];
  112. loop = params[1] || false;
  113. vol = params[2] || 1;
  114. soundID = id + url;
  115. soundSrc = this.soundSources[ soundID ];
  116. //cache the sound - can only be played simultainously by different nodes
  117. if ( !soundSrc ) {
  118. soundSrc = this.soundSources[ soundID ] = new SoundSource();
  119. soundSrc.id = id;
  120. soundSrc.url = url;
  121. soundSrc.volume = vol;
  122. soundSrc.sound = new this.buzz.sound( url, {
  123. autoplay: true,
  124. loop: loop
  125. } );
  126. soundSrc.looping = loop;
  127. soundSrc.position = [ 0, 0, 0 ];
  128. window._dSound = Sound;
  129. } else {
  130. if( soundSrc.sound.getPercent() == 100 ) {
  131. soundSrc.stop();
  132. }
  133. soundSrc.sound.setPercent( 0 );
  134. soundSrc.play();
  135. if( loop ) {
  136. soundSrc.loop();
  137. } else {
  138. soundSrc.unloop();
  139. }
  140. soundSrc.volume = vol;
  141. }
  142. break;
  143. case 'pauseSound':
  144. url = params[ 0 ];
  145. soundID = id + url;
  146. soundSrc = this.soundSources[ soundID ];
  147. if ( soundSrc ) {
  148. soundSrc.pause();
  149. }
  150. break;
  151. case 'stopSound':
  152. url = params[0];
  153. soundID = id + url;
  154. soundSrc = this.soundSources[ soundID ];
  155. if ( soundSrc ) {
  156. soundSrc.stop();
  157. }
  158. break;
  159. case 'deleteSound':
  160. url = params[0];
  161. soundID = id + url;
  162. soundSrc = this.soundSources[ soundID ];
  163. if ( soundSrc ) {
  164. soundSrc.stop();
  165. soundSrc.sound = null;
  166. }
  167. delete this.soundSources[ soundID ];
  168. break;
  169. }
  170. }
  171. },
  172. //Update the sound volume based on the position of the camera and the position of the object
  173. ticked : function()
  174. {
  175. try {
  176. var campos = [
  177. _dView.getCamera().matrixWorld.elements[12],
  178. _dView.getCamera().matrixWorld.elements[13],
  179. _dView.getCamera().matrixWorld.elements[14]
  180. ];
  181. for ( var i in this.soundSources )
  182. {
  183. this.soundSources[ i ].updateSourcePosition();
  184. this.soundSources[ i ].updateVolume( campos );
  185. }
  186. } catch( e ) {
  187. }
  188. },
  189. deletedNode: function( nodeID ) {
  190. for ( var id in this.soundSources ) {
  191. if( this.soundSources[ id ].id === nodeID )
  192. delete this.soundSources[ id ];
  193. }
  194. }
  195. } )
  196. } );