sound.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * ===========================================================================
  3. *
  4. * Wolf3D Browser Version GPL Source Code
  5. * Copyright (C) 2012 id Software LLC, a ZeniMax Media company.
  6. *
  7. * This file is part of the Wolf3D Browser Version GPL Source Code ("Wolf3D Browser Source Code").
  8. *
  9. * Wolf3D Browser Source Code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Wolf3D Browser Source Code is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License version 2
  20. * along with Wolf3D Browser Source Code. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * If you have questions concerning this license, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  23. *
  24. * ===========================================================================
  25. */
  26. Wolf.Sound = (function() {
  27. Wolf.setConsts({
  28. // Sound channels
  29. // Channel 0 never willingly overrides
  30. // Other channels (1-7) always override a playing sound on that channel
  31. CHAN_AUTO : 0,
  32. CHAN_WEAPON : 1,
  33. CHAN_VOICE : 2,
  34. CHAN_ITEM : 3,
  35. CHAN_BODY : 4,
  36. // Modifier flags
  37. CHAN_NO_PHS_ADD : 8, // Send to all clients, not just ones in PHS (ATTN 0 will also do this)
  38. CHAN_RELIABLE : 16, // Send by reliable message, not datagram
  39. // Sound attenuation values
  40. ATTN_NONE : 0, // Full volume the entire level
  41. ATTN_NORM : 1,
  42. ATTN_IDLE : 2,
  43. ATTN_STATIC : 3, // Diminish very rapidly with distance
  44. MAX_PLAYSOUNDS : 128,
  45. MAX_CHANNELS : 64,
  46. MUSIC_VOLUME : 0.8,
  47. MASTER_VOLUME : 0.6
  48. });
  49. var sounds = {},
  50. audioElements = [],
  51. currentMusic,
  52. soundEnabled = true,
  53. musicEnabled = true,
  54. music,
  55. ext,
  56. exts = ["ogg", "mp3"];
  57. function getFileName(file) {
  58. if (!ext) {
  59. // look for a probably
  60. for (var i=0;i<exts.length;i++) {
  61. if (Modernizr.audio[exts[i]] == "probably") {
  62. ext = exts[i];
  63. break;
  64. }
  65. }
  66. // look for a maybe
  67. if (!ext) {
  68. for (var i=0;i<exts.length;i++) {
  69. if (Modernizr.audio[exts[i]] == "maybe") {
  70. ext = exts[i];
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. return file.split(".")[0] + "." + ext
  77. }
  78. function createAudioElement() {
  79. var audio = new Audio();
  80. audioElements.push(audio);
  81. return audio;
  82. }
  83. function startSound(posPlayer, posSound, entNum, entChannel, file, volume, attenuation, timeOfs) {
  84. var audio, dx, dy, dist;
  85. if (!sounds[file]) {
  86. sounds[file] = [];
  87. }
  88. for (var i=0;i<sounds[file].length;i++) {
  89. if (sounds[file][i].ended || sounds[file][i].paused) {
  90. audio = sounds[file][i];
  91. break;
  92. }
  93. }
  94. if (!audio) {
  95. audio = createAudioElement();
  96. audio.src = getFileName(file);
  97. sounds[file].push(audio);
  98. }
  99. if (posPlayer && posSound) {
  100. dx = (posPlayer.x - posSound.x) / Wolf.TILEGLOBAL;
  101. dy = (posPlayer.y - posSound.y) / Wolf.TILEGLOBAL;
  102. dist = dx * dx + dy * dy;
  103. volume *= 1 / (1 + dist / 50);
  104. }
  105. audio.volume = volume * Wolf.MASTER_VOLUME * (soundEnabled ? 1 : 0);
  106. audio.play();
  107. }
  108. function startMusic(file) {
  109. if (!music) {
  110. music = createAudioElement();
  111. music.loop = true;
  112. }
  113. var filename = getFileName(file);
  114. if (currentMusic != filename) {
  115. music.src = currentMusic = filename;
  116. music.volume = Wolf.MUSIC_VOLUME * Wolf.MASTER_VOLUME * (musicEnabled ? 1 : 0);
  117. music.play();
  118. }
  119. }
  120. function stopAllSounds() {
  121. for (var i=0;i<audioElements.length;i++) {
  122. if (audioElements[i].currentTime > 0) {
  123. audioElements[i].currentTime = 0;
  124. audioElements[i].pause();
  125. }
  126. }
  127. }
  128. function init() {
  129. }
  130. function isMusicEnabled() {
  131. return musicEnabled
  132. }
  133. function isSoundEnabled() {
  134. return soundEnabled;
  135. }
  136. function toggleMusic(enable) {
  137. if (typeof enable != "undefined") {
  138. musicEnabled = enable;
  139. } else {
  140. musicEnabled = !musicEnabled;
  141. }
  142. if (music) {
  143. music.volume = Wolf.MUSIC_VOLUME * Wolf.MASTER_VOLUME * (musicEnabled ? 1 : 0);
  144. }
  145. }
  146. function pauseMusic(enable) {
  147. if (music) {
  148. if (enable) {
  149. music.pause();
  150. } else if (music.paused) {
  151. music.play();
  152. }
  153. }
  154. }
  155. function toggleSound(enable) {
  156. if (typeof enable != "undefined") {
  157. soundEnabled = enable;
  158. } else {
  159. soundEnabled = !soundEnabled;
  160. }
  161. }
  162. if (Modernizr.audio) {
  163. return {
  164. startSound : startSound,
  165. startMusic : startMusic,
  166. stopAllSounds : stopAllSounds,
  167. isMusicEnabled : isMusicEnabled,
  168. isSoundEnabled : isSoundEnabled,
  169. toggleMusic : toggleMusic,
  170. toggleSound : toggleSound,
  171. pauseMusic : pauseMusic,
  172. init : init
  173. }
  174. } else {
  175. return {
  176. startSound : Wolf.noop,
  177. startMusic : Wolf.noop,
  178. stopAllSounds : Wolf.noop,
  179. isMusicEnabled : Wolf.noop,
  180. isSoundEnabled : Wolf.noop,
  181. toggleMusic : Wolf.noop,
  182. toggleSound : Wolf.noop,
  183. pauseMusic : Wolf.noop,
  184. init : Wolf.noop
  185. }
  186. }
  187. })();