audio.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Blocky - audio.js
  3. All sounds are synthesised with the Web Audio API from filtered noise
  4. and simple oscillators, so the game ships no audio files.
  5. */
  6. const MATERIAL = {
  7. stone: { type: "bandpass", freq: 2200, q: 1.1, dur: 0.11, gain: 0.55 },
  8. wood: { type: "bandpass", freq: 850, q: 2.5, dur: 0.12, gain: 0.7, thump: 170 },
  9. gravel: { type: "lowpass", freq: 1300, q: 0.7, dur: 0.15, gain: 0.7, crunch: true },
  10. grass: { type: "highpass", freq: 1800, q: 0.6, dur: 0.12, gain: 0.35 },
  11. sand: { type: "highpass", freq: 2800, q: 0.5, dur: 0.16, gain: 0.32 },
  12. glass: { type: "bandpass", freq: 3200, q: 4, dur: 0.1, gain: 0.45, ping: true },
  13. metal: { type: "bandpass", freq: 2600, q: 7, dur: 0.14, gain: 0.45, ping: true },
  14. cloth: { type: "lowpass", freq: 900, q: 0.7, dur: 0.12, gain: 0.4 },
  15. water: { type: "lowpass", freq: 900, q: 0.7, dur: 0.25, gain: 0.4 }
  16. };
  17. export class Sound {
  18. constructor() {
  19. this.ctx = null;
  20. this.volume = 0.8;
  21. this.rainGain = null;
  22. }
  23. //Must be called from a user gesture before anything plays
  24. ensure() {
  25. if (this.ctx) {
  26. if (this.ctx.state === "suspended") {
  27. this.ctx.resume();
  28. }
  29. return;
  30. }
  31. const AC = window.AudioContext || window.webkitAudioContext;
  32. if (!AC) {
  33. return;
  34. }
  35. this.ctx = new AC();
  36. this.master = this.ctx.createGain();
  37. this.master.gain.value = this.volume;
  38. this.master.connect(this.ctx.destination);
  39. const len = this.ctx.sampleRate * 2;
  40. this.noise = this.ctx.createBuffer(1, len, this.ctx.sampleRate);
  41. const d = this.noise.getChannelData(0);
  42. for (let i = 0; i < len; i++) {
  43. d[i] = Math.random() * 2 - 1;
  44. }
  45. //Brown noise for thunder
  46. this.brown = this.ctx.createBuffer(1, len, this.ctx.sampleRate);
  47. const b = this.brown.getChannelData(0);
  48. let last = 0;
  49. for (let i = 0; i < len; i++) {
  50. last = (last + 0.02 * (Math.random() * 2 - 1)) / 1.02;
  51. b[i] = last * 3.5;
  52. }
  53. this.startRain();
  54. }
  55. setVolume(v) {
  56. this.volume = v;
  57. if (this.master) {
  58. this.master.gain.value = v;
  59. }
  60. }
  61. noiseBurst(filterType, freq, q, dur, gain, when = 0, buffer = null) {
  62. const ctx = this.ctx;
  63. const t = ctx.currentTime + when;
  64. const src = ctx.createBufferSource();
  65. src.buffer = buffer || this.noise;
  66. src.loop = true;
  67. const f = ctx.createBiquadFilter();
  68. f.type = filterType;
  69. f.frequency.value = freq;
  70. f.Q.value = q;
  71. const g = ctx.createGain();
  72. g.gain.setValueAtTime(0.0001, t);
  73. g.gain.exponentialRampToValueAtTime(gain, t + 0.008);
  74. g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
  75. src.connect(f).connect(g).connect(this.master);
  76. src.start(t, Math.random() * 1.5);
  77. src.stop(t + dur + 0.05);
  78. return f;
  79. }
  80. tone(freq, dur, gain, type = "sine", when = 0, endFreq = null) {
  81. const ctx = this.ctx;
  82. const t = ctx.currentTime + when;
  83. const o = ctx.createOscillator();
  84. o.type = type;
  85. o.frequency.setValueAtTime(freq, t);
  86. if (endFreq) {
  87. o.frequency.exponentialRampToValueAtTime(endFreq, t + dur);
  88. }
  89. const g = ctx.createGain();
  90. g.gain.setValueAtTime(0.0001, t);
  91. g.gain.exponentialRampToValueAtTime(gain, t + 0.01);
  92. g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
  93. o.connect(g).connect(this.master);
  94. o.start(t);
  95. o.stop(t + dur + 0.05);
  96. }
  97. //kind: dig, break, place, step
  98. block(kind, material) {
  99. if (!this.ctx) {
  100. return;
  101. }
  102. const m = MATERIAL[material] || MATERIAL.stone;
  103. const pitch = 0.85 + Math.random() * 0.3;
  104. let gain = m.gain, dur = m.dur;
  105. if (kind === "step") {
  106. gain *= 0.32;
  107. dur *= 0.8;
  108. } else if (kind === "dig") {
  109. gain *= 0.45;
  110. dur *= 0.7;
  111. } else if (kind === "break") {
  112. gain *= 1.1;
  113. dur *= 1.6;
  114. }
  115. this.noiseBurst(m.type, m.freq * pitch, m.q, dur, gain);
  116. if (m.crunch && kind !== "step") {
  117. this.noiseBurst("bandpass", 600 * pitch, 2, dur * 0.6, gain * 0.6, 0.04);
  118. }
  119. if (m.thump) {
  120. this.tone(m.thump * pitch, dur, gain * 0.5, "sine");
  121. }
  122. if (m.ping && kind === "break") {
  123. for (let i = 0; i < 4; i++) {
  124. this.tone(2000 + Math.random() * 2500, 0.18, 0.08, "sine", i * 0.03);
  125. }
  126. }
  127. }
  128. play(name, opts = {}) {
  129. if (!this.ctx) {
  130. return;
  131. }
  132. switch (name) {
  133. case "pop":
  134. this.tone(700 + Math.random() * 400, 0.07, 0.12, "sine", 0, 1300 + Math.random() * 300);
  135. break;
  136. case "click":
  137. this.tone(1100, 0.03, 0.1, "square");
  138. break;
  139. case "hurt":
  140. this.tone(240, 0.18, 0.35, "triangle", 0, 110);
  141. this.noiseBurst("lowpass", 500, 0.7, 0.12, 0.4);
  142. break;
  143. case "eat":
  144. this.noiseBurst("bandpass", 1300 + Math.random() * 600, 1.5, 0.08, 0.45);
  145. this.noiseBurst("bandpass", 900, 1.2, 0.06, 0.3, 0.05);
  146. break;
  147. case "burp":
  148. this.tone(160, 0.22, 0.3, "sawtooth", 0, 90);
  149. break;
  150. case "splash": {
  151. const f = this.noiseBurst("lowpass", 2400, 0.6, 0.45, 0.5);
  152. f.frequency.exponentialRampToValueAtTime(300, this.ctx.currentTime + 0.4);
  153. break;
  154. }
  155. case "toolBreak":
  156. this.tone(900, 0.2, 0.2, "square", 0, 300);
  157. this.noiseBurst("highpass", 3000, 0.5, 0.2, 0.3);
  158. break;
  159. case "thunder": {
  160. const dist = opts.distance || 0;
  161. const near = Math.max(0.15, 1 - dist / 80);
  162. const delay = Math.min(3, dist / 40);
  163. if (dist < 24) {
  164. this.noiseBurst("highpass", 1500, 0.4, 0.25, 0.6 * near, delay);
  165. }
  166. const len = 3 + Math.random() * 2;
  167. const f = this.noiseBurst("lowpass", 260 + near * 300, 0.5, len, 1.2 * near, delay, this.brown);
  168. f.frequency.exponentialRampToValueAtTime(90, this.ctx.currentTime + delay + len);
  169. this.noiseBurst("lowpass", 160, 0.5, len * 0.8, 0.9 * near, delay + 0.4 + Math.random() * 0.5, this.brown);
  170. break;
  171. }
  172. case "fizz":
  173. this.noiseBurst("highpass", 4000, 0.5, 0.3, 0.2);
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. startRain() {
  180. const ctx = this.ctx;
  181. const src = ctx.createBufferSource();
  182. src.buffer = this.noise;
  183. src.loop = true;
  184. const hp = ctx.createBiquadFilter();
  185. hp.type = "highpass";
  186. hp.frequency.value = 500;
  187. this.rainLp = ctx.createBiquadFilter();
  188. this.rainLp.type = "lowpass";
  189. this.rainLp.frequency.value = 6000;
  190. this.rainGain = ctx.createGain();
  191. this.rainGain.gain.value = 0;
  192. src.connect(hp).connect(this.rainLp).connect(this.rainGain).connect(this.master);
  193. src.start();
  194. }
  195. //level 0-1, muffled when the player is under a roof
  196. setRain(level, muffled) {
  197. if (!this.rainGain) {
  198. return;
  199. }
  200. const t = this.ctx.currentTime;
  201. this.rainGain.gain.setTargetAtTime(level * 0.22, t, 0.4);
  202. this.rainLp.frequency.setTargetAtTime(muffled ? 900 : 6000, t, 0.4);
  203. }
  204. }