Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docs/api/loaders/AudioLoader.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="../../" />
<script src="list.js"></script>
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
<h1>[name]</h1>

<div class="desc">Class for loading an [page:String AudioBuffer].</div>


<h2>Constructor</h2>

<h3>[name]( [page:String context], [page:LoadingManager manager] )</h3>
<div>
[page:String context] — The [page:String AudioContext] for the loader to use. Default is [page:String window.AudioContext].<br />
[page:LoadingManager manager] — The [page:LoadingManager loadingManager] for the loader to use. Default is [page:LoadingManager THREE.DefaultLoadingManager].
</div>
<div>
Creates a new [name].
</div>


<h2>Methods</h2>

<h3>[method:null load]( [page:String url], [page:Function onLoad], [page:Function onProgress], [page:Function onError] )</h3>
<div>
[page:String url] — required<br />
[page:Function onLoad] — Will be called when load completes. The argument will be the loaded text response.<br />
[page:Function onProgress] — Will be called while load progresses. The argument will be the XmlHttpRequest instance, that contain .[page:Integer total] and .[page:Integer loaded] bytes.<br />
[page:Function onError] — Will be called when load errors.<br />
</div>
<div>
Begin loading from url and pass the loaded [page:String AudioBuffer] to onLoad.
</div>



<h2>Example</h2>

<code>
// instantiate a listener
var audioListener = new THREE.AudioListener();

// add the listener to the camera
camera.add( audioListener );

// instantiate audio object
var oceanAmbientSound = new THREE.Audio( audioListener );

// add the audio object to the scene
scene.add( oceanAmbientSound );

// instantiate a loader
var loader = new THREE.AudioLoader();

// load a resource
loader.load(
// resource URL
'audio/ambient_ocean.ogg',
// Function when resource is loaded
function ( audioBuffer ) {
// set the audio object buffer to the loaded object
oceanAmbientSound.setBuffer( audioBuffer );

// play the audio
oceanAmbientSound.play();
},
// Function called when download progresses
function ( xhr ) {
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
},
// Function called when download errors
function ( xhr ) {
console.log( 'An error happened' );
}
);
</code>



<h2>Source</h2>

[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
</body>
</html>
1 change: 1 addition & 0 deletions docs/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var list = {


"Loaders": [
[ "AudioLoader", "api/loaders/AudioLoader" ],
[ "BabylonLoader", "api/loaders/BabylonLoader" ],
[ "BufferGeometryLoader", "api/loaders/BufferGeometryLoader" ],
[ "Cache", "api/loaders/Cache" ],
Expand Down
28 changes: 18 additions & 10 deletions examples/misc_sound.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
var listener = new THREE.AudioListener();
camera.add( listener );

var audioLoader = new THREE.AudioLoader(listener.context);


scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000000, 0.0025 );
Expand All @@ -99,9 +101,11 @@
scene.add( mesh1 );

var sound1 = new THREE.PositionalAudio( listener );
sound1.load( 'sounds/358232_j_s_song.ogg' );
sound1.setRefDistance( 20 );
sound1.autoplay = true;
audioLoader.load( 'sounds/358232_j_s_song.ogg', function( buffer ) {
sound1.setBuffer( buffer );
sound1.setRefDistance( 20 );
sound1.play();
});
mesh1.add( sound1 );

//
Expand All @@ -111,9 +115,11 @@
scene.add( mesh2 );

var sound2 = new THREE.PositionalAudio( listener );
sound2.load( 'sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg' );
sound2.setRefDistance( 20 );
sound2.autoplay = true;
audioLoader.load( 'sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg', function( buffer ) {
sound2.setBuffer( buffer );
sound2.setRefDistance( 20 );
sound2.play();
});
mesh2.add( sound2 );

//
Expand Down Expand Up @@ -142,10 +148,12 @@
// global ambient audio

var sound4 = new THREE.Audio( listener );
sound4.load( 'sounds/Project_Utopia.ogg' );
sound4.autoplay = true;
sound4.setLoop(true);
sound4.setVolume(0.5);
audioLoader.load( 'sounds/Project_Utopia.ogg', function( buffer ) {
sound4.setBuffer( buffer );
sound4.setLoop(true);
sound4.setVolume(0.5);
sound4.play();
});

// ground

Expand Down
22 changes: 11 additions & 11 deletions src/audio/Audio.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @author mrdoob / http://mrdoob.com/
* @author Reece Aaron Lecrivain / http://reecenotes.com/
*/

THREE.Audio = function ( listener ) {
Expand Down Expand Up @@ -38,10 +39,13 @@ THREE.Audio.prototype.getOutput = function () {

THREE.Audio.prototype.load = function ( file ) {

var buffer = new THREE.AudioBuffer( this.context );
buffer.load( file );
console.warn( 'THREE.Audio: .load has been deprecated. Please use THREE.AudioLoader.' );

this.setBuffer( buffer );
var audioLoader = new THREE.AudioLoader( this.context );

audioLoader.load( file, function ( buffer ) {
this.setBuffer( buffer );
});

return this;

Expand All @@ -62,13 +66,9 @@ THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {

var scope = this;

audioBuffer.onReady( function( buffer ) {

scope.source.buffer = buffer;
scope.sourceType = 'buffer';
if ( scope.autoplay ) scope.play();

} );
scope.source.buffer = audioBuffer;
scope.sourceType = 'buffer';
if ( scope.autoplay ) scope.play();

return this;

Expand Down Expand Up @@ -213,7 +213,7 @@ THREE.Audio.prototype.getPlaybackRate = function () {

};

THREE.Audio.prototype.onEnded = function() {
THREE.Audio.prototype.onEnded = function () {

this.isPlaying = false;

Expand Down
56 changes: 0 additions & 56 deletions src/audio/AudioBuffer.js

This file was deleted.

35 changes: 35 additions & 0 deletions src/loaders/AudioLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @author Reece Aaron Lecrivain / http://reecenotes.com/
*/

THREE.AudioLoader = function ( context, manager ) {

this.context = ( context !== undefined ) ? context : new ( window.AudioContext || window.webkitAudioContext )();

this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;

};

THREE.AudioLoader.prototype = {

constructor: THREE.AudioLoader,

load: function ( url, onLoad, onProgress, onError ) {

var scope = this;

var loader = new THREE.XHRLoader( this.manager );
loader.setResponseType( 'arraybuffer' );
loader.load( url, function ( buffer ) {

scope.context.decodeAudioData( buffer, function ( audioBuffer ) {

onLoad( audioBuffer );

} );

}, onProgress, onError );

}

};
2 changes: 1 addition & 1 deletion utils/build/includes/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"src/animation/tracks/VectorKeyframeTrack.js",
"src/audio/Audio.js",
"src/audio/AudioAnalyser.js",
"src/audio/AudioBuffer.js",
"src/audio/PositionalAudio.js",
"src/audio/AudioListener.js",
"src/cameras/Camera.js",
Expand All @@ -68,6 +67,7 @@
"src/lights/HemisphereLight.js",
"src/lights/PointLight.js",
"src/lights/SpotLight.js",
"src/loaders/AudioLoader.js",
"src/loaders/Cache.js",
"src/loaders/Loader.js",
"src/loaders/XHRLoader.js",
Expand Down