# HG changeset patch # User Nicholas Jillings # Date 1450268118 0 # Node ID 261d92ea87e1ffaa73c0ee66b37ce2f9d2ee4f1e # Parent 7b522c1455160025d0909d4dcc6da9fe014ab878 Automatic Loudness normalisation to -23 LUFS diff -r 7b522c145516 -r 261d92ea87e1 core.js --- a/core.js Tue Dec 15 16:02:17 2015 +0000 +++ b/core.js Wed Dec 16 12:15:18 2015 +0000 @@ -19,6 +19,10 @@ // Add a prototype to the bufferSourceNode to reference to the audioObject holding it AudioBufferSourceNode.prototype.owner = undefined; +// Add a prototype to the bufferNode to hold the desired LINEAR gain +AudioBuffer.prototype.gain = undefined; +// Add a prototype to the bufferNode to hold the computed LUFS loudness +AudioBuffer.prototype.lufs = undefined; window.onload = function() { // Function called once the browser has loaded all files. @@ -113,7 +117,8 @@ } if (buffer == null) { - buffer = new audioEngineContext.bufferObj(URL); + buffer = new audioEngineContext.bufferObj(); + buffer.getMedia(URL); audioEngineContext.buffers.push(buffer); } }); @@ -757,45 +762,49 @@ this.audioObjects = []; this.buffers = []; - this.bufferObj = function(url) + this.bufferObj = function() { - this.url = url; + this.url = null; this.buffer = null; this.xmlRequest = new XMLHttpRequest(); this.users = []; - this.xmlRequest.open('GET',this.url,true); - this.xmlRequest.responseType = 'arraybuffer'; - - var bufferObj = this; - - // Create callback to decode the data asynchronously - this.xmlRequest.onloadend = function() { - audioContext.decodeAudioData(bufferObj.xmlRequest.response, function(decodedData) { - bufferObj.buffer = decodedData; - for (var i=0; i Web Audio API Buffer object // timescale -> M or Momentary (returns Array), S or Short (returns Array), // I or Integrated (default, returns number) + // target -> default is -23 LUFS but can be any LUFS measurement. - // Create the required filters - if (buffer == undefined || result == undefined) + if (buffer == undefined) { return 0; } @@ -22,10 +24,15 @@ { timescale = "I"; } + if (target == undefined) + { + target = -23; + } if (offlineContext == undefined) { offlineContext = new OfflineAudioContext(buffer.numberOfChannels, buffer.length, buffer.sampleRate); } + // Create the required filters var KFilter = offlineContext.createBiquadFilter(); KFilter.type = "highshelf"; KFilter.gain.value = 4; @@ -45,7 +52,6 @@ processSource.start(); offlineContext.startRendering().then(function(renderedBuffer) { // Have the renderedBuffer information, now continue processing - console.log(renderedBuffer); switch(timescale) { case "I": @@ -87,11 +93,13 @@ } } var overallRelLoudness = calculateOverallLoudnessFromChannelBlocks(relgateEnergy); - result[0] = overallRelLoudness; + buffer.lufs = overallRelLoudness; + var diff = -23 -overallRelLoudness; + buffer.gain = decibelToLinear(diff); } }).catch(function(err) { console.log(err); - result[0] = 1; + buffer.lufs = 1; }); }