Skip to content

⬅️ Back to Table of Contents

📄 AudioLoader.js

📊 Analysis Summary

Metric Count
🔧 Functions 2
🧱 Classes 1
📦 Imports 3
📊 Variables & Constants 2

📚 Table of Contents

🛠️ File Location:

📂 src/loaders/AudioLoader.js

📦 Imports

Name Source
AudioContext ../audio/AudioContext.js
FileLoader ./FileLoader.js
Loader ./Loader.js

Variables & Constants

Name Type Kind Value Exported
scope this let/var this
loader FileLoader let/var new FileLoader( this.manager )

Functions

AudioLoader.load(url: string, onLoad: (arg0: AudioBuffer) => any, onProgress: onProgressCallback, onError: onErrorCallback): void

JSDoc:

/**
     * Starts loading from the given URL and passes the loaded audio buffer
     * to the `onLoad()` callback.
     *
     * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
     * @param {function(AudioBuffer)} onLoad - Executed when the loading process has been finished.
     * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
     * @param {onErrorCallback} onError - Executed when errors occur.
     */

Parameters:

  • url string
  • onLoad (arg0: AudioBuffer) => any
  • onProgress onProgressCallback
  • onError onErrorCallback

Returns: void

Calls:

  • loader.setResponseType
  • loader.setPath
  • loader.setRequestHeader
  • loader.setWithCredentials
  • loader.load
  • buffer.slice
  • AudioContext.getContext
  • `context.decodeAudioData( bufferCopy, function ( audioBuffer ) {
                onLoad( audioBuffer );
    
            } ).catch`
    
    • handleError
    • onError
    • console.error
    • scope.manager.itemError

Internal Comments:

// Create a copy of the buffer. The `decodeAudioData` method (x2)
// detaches the buffer when complete, preventing reuse. (x2)

Code
load( url, onLoad, onProgress, onError ) {

        const scope = this;

        const loader = new FileLoader( this.manager );
        loader.setResponseType( 'arraybuffer' );
        loader.setPath( this.path );
        loader.setRequestHeader( this.requestHeader );
        loader.setWithCredentials( this.withCredentials );
        loader.load( url, function ( buffer ) {

            try {

                // Create a copy of the buffer. The `decodeAudioData` method
                // detaches the buffer when complete, preventing reuse.
                const bufferCopy = buffer.slice( 0 );

                const context = AudioContext.getContext();
                context.decodeAudioData( bufferCopy, function ( audioBuffer ) {

                    onLoad( audioBuffer );

                } ).catch( handleError );

            } catch ( e ) {

                handleError( e );

            }

        }, onProgress, onError );

        function handleError( e ) {

            if ( onError ) {

                onError( e );

            } else {

                console.error( e );

            }

            scope.manager.itemError( url );

        }

    }

handleError(e: any): void

Parameters:

  • e any

Returns: void

Calls:

  • onError
  • console.error
  • scope.manager.itemError
Code
function handleError( e ) {

            if ( onError ) {

                onError( e );

            } else {

                console.error( e );

            }

            scope.manager.itemError( url );

        }

Classes

AudioLoader

Class Code
class AudioLoader extends Loader {

    /**
     * Constructs a new audio loader.
     *
     * @param {LoadingManager} [manager] - The loading manager.
     */
    constructor( manager ) {

        super( manager );

    }

    /**
     * Starts loading from the given URL and passes the loaded audio buffer
     * to the `onLoad()` callback.
     *
     * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
     * @param {function(AudioBuffer)} onLoad - Executed when the loading process has been finished.
     * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
     * @param {onErrorCallback} onError - Executed when errors occur.
     */
    load( url, onLoad, onProgress, onError ) {

        const scope = this;

        const loader = new FileLoader( this.manager );
        loader.setResponseType( 'arraybuffer' );
        loader.setPath( this.path );
        loader.setRequestHeader( this.requestHeader );
        loader.setWithCredentials( this.withCredentials );
        loader.load( url, function ( buffer ) {

            try {

                // Create a copy of the buffer. The `decodeAudioData` method
                // detaches the buffer when complete, preventing reuse.
                const bufferCopy = buffer.slice( 0 );

                const context = AudioContext.getContext();
                context.decodeAudioData( bufferCopy, function ( audioBuffer ) {

                    onLoad( audioBuffer );

                } ).catch( handleError );

            } catch ( e ) {

                handleError( e );

            }

        }, onProgress, onError );

        function handleError( e ) {

            if ( onError ) {

                onError( e );

            } else {

                console.error( e );

            }

            scope.manager.itemError( url );

        }

    }

}

Methods

load(url: string, onLoad: (arg0: AudioBuffer) => any, onProgress: onProgressCallback, onError: onErrorCallback): void
Code
load( url, onLoad, onProgress, onError ) {

        const scope = this;

        const loader = new FileLoader( this.manager );
        loader.setResponseType( 'arraybuffer' );
        loader.setPath( this.path );
        loader.setRequestHeader( this.requestHeader );
        loader.setWithCredentials( this.withCredentials );
        loader.load( url, function ( buffer ) {

            try {

                // Create a copy of the buffer. The `decodeAudioData` method
                // detaches the buffer when complete, preventing reuse.
                const bufferCopy = buffer.slice( 0 );

                const context = AudioContext.getContext();
                context.decodeAudioData( bufferCopy, function ( audioBuffer ) {

                    onLoad( audioBuffer );

                } ).catch( handleError );

            } catch ( e ) {

                handleError( e );

            }

        }, onProgress, onError );

        function handleError( e ) {

            if ( onError ) {

                onError( e );

            } else {

                console.error( e );

            }

            scope.manager.itemError( url );

        }

    }