Skip to content

Commit

Permalink
Add support to PositionalAudio
Browse files Browse the repository at this point in the history
  • Loading branch information
fedegratti committed Nov 16, 2021
1 parent 029cb73 commit b1d9ce0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v5.16.10
v5.16.11
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ohzi-core",
"version": "5.16.10",
"version": "5.16.11",
"description": "OHZI Core Library",
"module": "build/index.module.js",
"source": "src/index.js",
Expand Down
20 changes: 17 additions & 3 deletions src/components/AudioClip.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { Audio as THREEAudio } from 'three';
import { Audio as TAudio } from 'three';
import { PositionalAudio as TPositionalAudio } from 'three';

export default class AudioClip
{
constructor(buffer, loop = true, volume = 1)
constructor(buffer, loop = true, volume = 1, positional = false)
{
this.buffer = buffer;
this.loop = loop;
this.volume = volume;
this.positional = positional;

this.audio = undefined;
}

init(audio_listener)
{
this.audio = new THREEAudio(audio_listener);
if (this.positional)
{
this.audio = new TPositionalAudio(audio_listener);
}
else
{
this.audio = new TAudio(audio_listener);
}

this.audio.setBuffer(this.buffer);
this.audio.setLoop(this.loop);

// This prevents undesired volume at the beginning of playing
this.audio.gain.gain.value = 0;

this.audio.setVolume(this.loop ? 0 : this.volume);
}

Expand Down
47 changes: 20 additions & 27 deletions src/resource_loader/AudioLoader.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,47 @@
import AbstractLoader from './AbstractLoader';

import AudioClip from '../components/AudioClip';
import { AudioLoader as THREEAudioLoader } from 'three';

import { AudioContext } from 'three';

export default class AudioLoader extends AbstractLoader
{
constructor(resource_id, url, loop = true, volume = 1, size)
constructor(resource_id, url, loop = true, volume = 0, size, positional = false)
{
super(resource_id, url, size);
this.loader = new THREEAudioLoader();
this.loop = loop;
this.volume = volume;
this.positional = positional;
}

on_preloaded_finished(resource_container)
on_preloaded_finished(resource_container, response)
{
if (!window.user_interaction_for_audio)
{
setTimeout(this.on_preloaded_finished.bind(this, resource_container), 100);
setTimeout(this.on_preloaded_finished.bind(this, resource_container, response), 100);
}
else
{
this.load_with_three_loader(resource_container);
this.instantiate_audio(resource_container, response);
}
}

load_with_three_loader(resource_container)
instantiate_audio(resource_container, response)
{
let ctx = this;

this.loader.load(this.url, (buffer) =>
response.arrayBuffer().then((array_buffer) =>
{
resource_container.set_resource(ctx.resource_id, ctx.url, new AudioClip(buffer, this.loop, this.volume));
const context = AudioContext.getContext();
context.decodeAudioData(array_buffer, (audio_buffer) =>
{
resource_container.set_resource(
this.resource_id,
this.url,
new AudioClip(audio_buffer, this.loop, this.volume, this.positional)
);

ctx.__update_downloaded_bytes(1, 1);
ctx.__loading_ended();
},
(xhr) =>
{
// if (xhr)
// {
// let total = xhr.total || this.total_bytes;

// ctx.__update_downloaded_bytes(xhr.loaded, total);
// }
},
(error) =>
{
ctx.__set_error('Audio could not be loaded. Maybe wrong name or path, I don\'t know' + '¯\\_(ツ)_/¯', error);
ctx.__loading_ended();
this.__update_downloaded_bytes(1, 1);
this.__loading_ended();
});
});
}
}
4 changes: 2 additions & 2 deletions src/resource_loader/ResourceBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export default class ResourceBatch
this.resource_loaders.push(new CubemapLoader(resource_id, url, size));
}

add_audio(resource_id, url, loop, volume, size)
add_audio(resource_id, url, loop, volume, size, positional)
{
this.resource_loaders.push(new AudioLoader(resource_id, url, loop, volume, size));
this.resource_loaders.push(new AudioLoader(resource_id, url, loop, volume, size, positional));
}

add_video(resource_id, url, size)
Expand Down
9 changes: 5 additions & 4 deletions src/resource_loader/TextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ export default class TextureLoader extends AbstractLoader

const image = new Image();
image.src = url;

image.onload = () =>
{
texture.image = image;
texture.needsUpdate = true;
};

resource_container.set_resource(this.resource_id, this.url, texture);
resource_container.set_resource(this.resource_id, this.url, texture);

this.__update_downloaded_bytes(1, 1);
this.__loading_ended();
this.__update_downloaded_bytes(1, 1);
this.__loading_ended();
};
});
}
}

0 comments on commit b1d9ce0

Please sign in to comment.