Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // TODO Group audio files so we can control volume seperately
  4.  
  5. const context = new AudioContext();
  6. const soundDatas = {};
  7. const soundFiles = {
  8. clear: '/audio/clear.wav',
  9. fall: '/audio/fall.wav',
  10. land: '/audio/land.wav',
  11. lock: '/audio/lock.wav',
  12. irs: '/audio/irs.wav',
  13. };
  14.  
  15. for (let key in soundFiles) {
  16. const request = new Request(soundFiles[key]);
  17. fetch(request).then(body => {
  18. return body.arrayBuffer();
  19. }).then(buffer => {
  20. context.decodeAudioData(buffer, data => {
  21. soundDatas[key] = data;
  22. });
  23. });
  24. }
  25.  
  26. function play(key) {
  27. // TODO Check if loaded.
  28. const source = context.createBufferSource();
  29. source.buffer = soundDatas[key];
  30. source.connect(context.destination);
  31. source.start(0);
  32. }
  33.  
  34. // e.g.,
  35. // play('fall');
  36.  
  37. // TODO Promise.all for onload
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement