Browse Source

Changed caching to include the story id as well. Probably

tags/v0.1.0
Fen Dweller 6 years ago
parent
commit
58f630924e
1 changed files with 15 additions and 13 deletions
  1. +15
    -13
      audio.js

+ 15
- 13
audio.js View File

@@ -7,6 +7,7 @@ let audioContext;
let gainControl; let gainControl;


let audioBaseUrl; let audioBaseUrl;
let storyName;


let audioDict = {}; let audioDict = {};


@@ -17,7 +18,6 @@ function setVolume(vol) {
// play some sound // play some sound


function playSfx(name) { function playSfx(name) {

if (audioDict[name] == undefined) { if (audioDict[name] == undefined) {


if (waiting[name]) { if (waiting[name]) {
@@ -46,7 +46,6 @@ function playSfx(name) {
} }


function playLoop(name) { function playLoop(name) {

if (audioDict[name] == undefined) { if (audioDict[name] == undefined) {


if (waiting[name]) { if (waiting[name]) {
@@ -159,10 +158,7 @@ function parseAudioData(name, data) {
audioContext.decodeAudioData(data, function(buffer) { audioContext.decodeAudioData(data, function(buffer) {
audioDict[name] = buffer; audioDict[name] = buffer;


console.log(waiting);

waiting[name].forEach(queued => { waiting[name].forEach(queued => {
console.log(queued);
if (queued.type == "sfx") { if (queued.type == "sfx") {
playSfx(name); playSfx(name);
} }
@@ -175,7 +171,7 @@ function parseAudioData(name, data) {


delete waiting[name]; delete waiting[name];
}, function(e){ }, function(e){
console.log("Error with decoding audio data" + e.err);
console.error("Error with decoding audio data" + e.err);
delete waiting[name]; delete waiting[name];
}); });


@@ -190,12 +186,12 @@ function loadRemoteAudio(name) {
if (xhr.status == 200) if (xhr.status == 200)
cacheAndParse(name, xhr.response); cacheAndParse(name, xhr.response);
else { else {
console.log("Couldn't load " + name);
console.error("Couldn't load " + name);
delete waiting[name]; delete waiting[name];
} }
} }
xhr.onerror = (xhr) => { xhr.onerror = (xhr) => {
console.log("Couldn't load " + name);
console.error("Couldn't load " + name);
} }


xhr.send(); xhr.send();
@@ -203,14 +199,14 @@ function loadRemoteAudio(name) {


// check if the content is cached // check if the content is cached
function checkCache(type, name, hit, miss) { function checkCache(type, name, hit, miss) {
const req = window.indexedDB.open("cache", 1);
const req = window.indexedDB.open("cache", 2);
req.onsuccess = () => { req.onsuccess = () => {
const db = req.result; const db = req.result;
const tx = db.transaction([type], "readonly"); const tx = db.transaction([type], "readonly");


const audio = tx.objectStore(type); const audio = tx.objectStore(type);


const read = audio.get(name);
const read = audio.get([storyName, name]);


read.onsuccess = (event) => { read.onsuccess = (event) => {
const res = event.target.result; const res = event.target.result;
@@ -243,6 +239,7 @@ function initAudio(story, state) {
createCache(); createCache();


audioBaseUrl = "./media/" + story.id + "/audio/"; audioBaseUrl = "./media/" + story.id + "/audio/";
storyName = story.id;


story.sounds.forEach(sound => { story.sounds.forEach(sound => {
loadAudio(sound); loadAudio(sound);
@@ -252,7 +249,7 @@ function initAudio(story, state) {
// caching stuff here // caching stuff here


function storeCache(type, name, blob) { function storeCache(type, name, blob) {
const req = window.indexedDB.open("cache", 1);
const req = window.indexedDB.open("cache", 2);
req.onsuccess = () => { req.onsuccess = () => {
const db = req.result; const db = req.result;
const tx = db.transaction([type], "readwrite"); const tx = db.transaction([type], "readwrite");
@@ -260,6 +257,7 @@ function storeCache(type, name, blob) {
const audio = tx.objectStore(type); const audio = tx.objectStore(type);


const update = audio.put({ const update = audio.put({
story: storyName,
name: name, name: name,
content: blob content: blob
}); });
@@ -274,12 +272,16 @@ function storeCache(type, name, blob) {
function createCache() { function createCache() {
let idb = window.indexedDB; let idb = window.indexedDB;


let req = idb.open("cache", 1);
let req = idb.open("cache", 2);


req.onupgradeneeded = event => { req.onupgradeneeded = event => {
const db = event.target.result; const db = event.target.result;


const audio = db.createObjectStore("audio", { keyPath: "name" });
if (event.oldVersion == 1) {
db.deleteObjectStore("audio");
}

const audio = db.createObjectStore("audio", { keyPath: ["story", "name"] });
} }


req.onerror = event => { req.onerror = event => {


Loading…
Cancel
Save