a munch adventure
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

140 lignes
2.8 KiB

  1. let audioDict = {};
  2. // play some sound
  3. function playAudio(name) {
  4. if (audioDict[name] == undefined) {
  5. console.log(name + " is not loaded yet, dingus");
  6. return;
  7. }
  8. let src = audioContext.createBufferSource();
  9. src.buffer = audioDict[name];
  10. src.connect(audioContext.destination);
  11. src.start(0);
  12. }
  13. // asynchronously load an audio file
  14. function loadAudio(name, flush=false) {
  15. // do we already have the audio?
  16. if (audioDict[name] && !flush) {
  17. return;
  18. }
  19. // is the audio already stored locally?
  20. if (!flush) {
  21. checkCache(
  22. "audio",
  23. name,
  24. (data) => parseAudioData(name, data),
  25. () => loadRemoteAudio(name)
  26. );
  27. } else {
  28. loadRemoteAudio(name);
  29. }
  30. }
  31. function cacheAndParse(name, data) {
  32. storeCache("audio", name, data.slice(0));
  33. parseAudioData(name, data);
  34. }
  35. function parseAudioData(name, data) {
  36. console.log(data);
  37. audioContext.decodeAudioData(data, function(buffer) {
  38. audioDict[name] = buffer;
  39. }, function(e){ console.log("Error with decoding audio data" + e.err);});
  40. }
  41. function loadRemoteAudio(name) {
  42. let xhr = new XMLHttpRequest();
  43. xhr.open("GET", audioBaseUrl + name, true);
  44. xhr.responseType = "arraybuffer";
  45. xhr.onload = (xhr) => cacheAndParse(name, xhr.data);
  46. xhr.send();
  47. }
  48. // check if the content is cached
  49. function checkCache(type, name, hit, miss) {
  50. const req = window.indexedDB.open("cache", 1);
  51. req.onsuccess = () => {
  52. const db = req.result;
  53. const tx = db.transaction([type], "readonly");
  54. const audio = tx.objectStore(type);
  55. const read = audio.get(name);
  56. read.onsuccess = (event) => {
  57. const res = event.target.result;
  58. if (res) {
  59. console.log("cache hit on " + name);
  60. hit(res.content);
  61. } else {
  62. console.log("cache miss on " + name);
  63. miss();
  64. }
  65. }
  66. tx.oncomplete = () => {
  67. db.close();
  68. }
  69. }
  70. }
  71. function initAudio() {
  72. audioContext = new (window.AudioContext || window.webkitAudioContext)();
  73. console.log("Initialized audio context");
  74. console.log(audioContext);
  75. }
  76. // caching stuff here
  77. function storeCache(type, name, blob) {
  78. const req = window.indexedDB.open("cache", 1);
  79. req.onsuccess = () => {
  80. const db = req.result;
  81. const tx = db.transaction([type], "readwrite");
  82. const audio = tx.objectStore(type);
  83. const update = audio.put({
  84. name: name,
  85. content: blob
  86. });
  87. tx.oncomplete = () => {
  88. db.close();
  89. }
  90. }
  91. }
  92. // if the indexedDB table doesn't exist at all, make it
  93. function createCache() {
  94. let idb = window.indexedDB;
  95. let req = idb.open("cache", 1);
  96. req.onupgradeneeded = event => {
  97. const db = event.target.result;
  98. const audio = db.createObjectStore("audio", { keyPath: "name" });
  99. }
  100. req.onerror = event => {
  101. alert("Couldn't open the database?");
  102. }
  103. }