-
Notifications
You must be signed in to change notification settings - Fork 146
Getting Started
This guide will teach you how to use the high level API of Aurora.js to extract information about audio files and play them back.
The best way to use Aurora.js, is via Browserify. You can install via npm.
npm install av
You'll also need to install the codecs you want to use. For example, for flac.js:
npm install flac.js
Then, require the module and codecs you need:
// app.js
var AV = require('av');
require('flac.js');
// ... use aurora here
And build this file using browserify:
browserify app.js -o out.js
Aurora supports loading audio files from two sources out of the box: HTTP and local Files. The AV.Player class is used to play back audio.
Out of the box, Aurora supports LPCM, uLaw and aLaw encoded files in a number of common containers such as WAV, AU, AIFF, CAF and M4A. To add support for more codecs, all you need to do is build them and include them on your HTML page. You can find several codecs that we've written on our Github page including FLAC.js, ALAC.js, MP3.js, and AAC.js.
To load an audio file over HTTP, use the AV.Player.fromURL
method. Note that the audio can only be loaded from the same domain as your page, unless you've setup the proper CORS headers.
var player = AV.Player.fromURL('http://mysite.com/audio.wav');
To load an audio file from a local File, use the AV.Player.fromFile
method.
var player = AV.Player.fromFile(file);
Once you have a player, all you have to do to play back the audio file, is call the play
method. Later on, you can use the pause
method, receive events telling you about the playback process, adjust the volume and stereo pan position, and more. Check out the AV.Player documentation for more details on that.
If you don't actually want to play back the audio file, but you only want to get information about it, you can use the AV.Asset class directly. While you could use the AV.Player class to read much of the data you can get from an AV.Asset
, it would be less efficient since the audio data would end up being decoded even if you didn't want it to.
To get information about an audio file, use the get
method. You can ask for format information such as the codec, sampleRate and bit depth of the file, the duration
of the file, and any embedded metadata
in the file.
var asset = AV.Asset.fromURL('http://mysite.com/test.wav');
asset.get('duration', function(duration) {
// do something
});
If you want to decode the audio file for some other purpose than playing it, you can use the AV.Asset
class to do that. Just use the start
and stop
methods to control when the decoding should start and stop and handle the data
events emitted by the Asset.
var asset = AV.Asset.fromURL('http://mysite.com/test.wav');
var list = new AV.BufferList;
asset.on('data', function(buffer) {
list.push(new AV.Buffer(buffer));
});
asset.start();
Alternatively, if you want to decode an entire file to a single buffer to do some processing on, you can use the asset's decodeToBuffer
method.
var asset = AV.Asset.fromURL('http://mysite.com/test.wav');
asset.decodeToBuffer(function(buffer) {
// buffer is now a Float32Array containing the entire decoded audio file
});