a munch adventure
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

146 satır
2.9 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) => {
  46. if (xhr.status == 200)
  47. cacheAndParse(name, xhr.data);
  48. else
  49. console.log("Couldn't load " + name);
  50. }
  51. xhr.onerror = (xhr) => console.log("Couldn't load " + name);
  52. xhr.send();
  53. }
  54. // check if the content is cached
  55. function checkCache(type, name, hit, miss) {
  56. const req = window.indexedDB.open("cache", 1);
  57. req.onsuccess = () => {
  58. const db = req.result;
  59. const tx = db.transaction([type], "readonly");
  60. const audio = tx.objectStore(type);
  61. const read = audio.get(name);
  62. read.onsuccess = (event) => {
  63. const res = event.target.result;
  64. if (res) {
  65. console.log("cache hit on " + name);
  66. hit(res.content);
  67. } else {
  68. console.log("cache miss on " + name);
  69. miss();
  70. }
  71. }
  72. tx.oncomplete = () => {
  73. db.close();
  74. }
  75. }
  76. }
  77. function initAudio() {
  78. audioContext = new (window.AudioContext || window.webkitAudioContext)();
  79. console.log("Initialized audio context");
  80. console.log(audioContext);
  81. }
  82. // caching stuff here
  83. function storeCache(type, name, blob) {
  84. const req = window.indexedDB.open("cache", 1);
  85. req.onsuccess = () => {
  86. const db = req.result;
  87. const tx = db.transaction([type], "readwrite");
  88. const audio = tx.objectStore(type);
  89. const update = audio.put({
  90. name: name,
  91. content: blob
  92. });
  93. tx.oncomplete = () => {
  94. db.close();
  95. }
  96. }
  97. }
  98. // if the indexedDB table doesn't exist at all, make it
  99. function createCache() {
  100. let idb = window.indexedDB;
  101. let req = idb.open("cache", 1);
  102. req.onupgradeneeded = event => {
  103. const db = event.target.result;
  104. const audio = db.createObjectStore("audio", { keyPath: "name" });
  105. }
  106. req.onerror = event => {
  107. alert("Couldn't open the database?");
  108. }
  109. }