nickjillings@1429: /** nickjillings@1429: * loundess.js nickjillings@1429: * Loudness module for the Web Audio Evaluation Toolbox nickjillings@1429: * Allows for automatic calculation of loudness of Web Audio API Buffer objects, nickjillings@1429: * return gain values to correct for a target loudness or match loudness between nickjillings@1429: * multiple objects nickjillings@1429: */ nickjillings@1429: nickjillings@1430: var interval_cal_loudness_event = null; nickjillings@1430: nickjillings@1389: if (typeof OfflineAudioContext == "undefined"){ nickjillings@1389: var OfflineAudioContext = webkitOfflineAudioContext; nickjillings@1389: } nickjillings@1389: nickjillings@1430: function calculateLoudness(buffer, timescale, target, offlineContext) nickjillings@1429: { nickjillings@1430: // This function returns the EBU R 128 specification loudness model and sets the linear gain required to match -23 LUFS nickjillings@1429: // buffer -> Web Audio API Buffer object nickjillings@1429: // timescale -> M or Momentary (returns Array), S or Short (returns Array), nickjillings@1429: // I or Integrated (default, returns number) nickjillings@1430: // target -> default is -23 LUFS but can be any LUFS measurement. nickjillings@1429: nickjillings@1430: if (buffer == undefined) nickjillings@1429: { nickjillings@1429: return 0; nickjillings@1429: } nickjillings@1429: if (timescale == undefined) nickjillings@1429: { nickjillings@1429: timescale = "I"; nickjillings@1429: } nickjillings@1430: if (target == undefined) nickjillings@1430: { nickjillings@1430: target = -23; nickjillings@1430: } nickjillings@1429: if (offlineContext == undefined) nickjillings@1429: { nickjillings@1358: offlineContext = new OfflineAudioContext(audioContext.destination.channelCount, buffer.buffer.duration*audioContext.sampleRate, audioContext.sampleRate); nickjillings@1429: } nickjillings@1430: // Create the required filters nickjillings@1429: var KFilter = offlineContext.createBiquadFilter(); nickjillings@1429: KFilter.type = "highshelf"; nickjillings@1429: KFilter.gain.value = 4; nickjillings@1366: KFilter.frequency.value = 1500; nickjillings@1429: nickjillings@1429: var HPFilter = offlineContext.createBiquadFilter(); nickjillings@1429: HPFilter.type = "highpass"; nickjillings@1366: HPFilter.Q.value = 0.5; nickjillings@1366: HPFilter.frequency.value = 38; nickjillings@1429: // copy Data into the process buffer nickjillings@1429: var processSource = offlineContext.createBufferSource(); nickjillings@1342: processSource.buffer = buffer.buffer; nickjillings@1429: nickjillings@1429: processSource.connect(KFilter); nickjillings@1429: KFilter.connect(HPFilter); nickjillings@1429: HPFilter.connect(offlineContext.destination); nickjillings@1389: offlineContext.oncomplete = function(renderedBuffer) { nickjillings@1429: // Have the renderedBuffer information, now continue processing nickjillings@1389: if (typeof renderedBuffer.renderedBuffer == 'object') { nickjillings@1389: renderedBuffer = renderedBuffer.renderedBuffer; nickjillings@1389: } nickjillings@1429: switch(timescale) nickjillings@1429: { nickjillings@1429: case "I": nickjillings@1357: // Calculate the Mean Squared of a signal nickjillings@1357: var MS = calculateMeanSquared(renderedBuffer,0.4,0.75); nickjillings@1357: // Calculate the Loudness of each block nickjillings@1357: var MSL = calculateLoudnessFromBlocks(MS); nickjillings@1357: // Get blocks from Absolute Gate nickjillings@1357: var LK = loudnessGate(MSL,MS,-70); nickjillings@1357: // Calculate Loudness nickjillings@1357: var LK_gate = loudnessOfBlocks(LK); nickjillings@1357: // Get blocks from Relative Gate nickjillings@1357: var RK = loudnessGate(MSL,MS,LK_gate-10); nickjillings@1357: var RK_gate = loudnessOfBlocks(RK); nickjillings@1357: buffer.buffer.lufs = RK_gate; nickjillings@1429: } nickjillings@1359: buffer.ready(); nickjillings@1389: }; nickjillings@1852: processSource.start(0); nickjillings@1389: offlineContext.startRendering(); nickjillings@1429: } nickjillings@1429: nickjillings@1357: function calculateMeanSquared(buffer,frame_dur,frame_overlap) nickjillings@1429: { nickjillings@1357: frame_size = Math.floor(buffer.sampleRate*frame_dur); nickjillings@1357: step_size = Math.floor(frame_size*(1.0-frame_overlap)); nickjillings@1357: num_frames = Math.floor((buffer.length-frame_size)/step_size); nickjillings@1357: nickjillings@1357: MS = Array(buffer.numberOfChannels); nickjillings@1357: for (var c=0; c= 3){G = 1.41;} nickjillings@1357: sum += blocks[c][n]*G; nickjillings@1357: } nickjillings@1357: MSL[n] = -0.691 + 10*Math.log10(sum); nickjillings@1357: } nickjillings@1357: return MSL; nickjillings@1429: } nickjillings@1357: nickjillings@1357: function loudnessGate(blocks,source,threshold) nickjillings@1429: { nickjillings@1357: var num_frames = source[0].length; nickjillings@1357: var num_channels = source.length; nickjillings@1357: var LK = Array(num_channels); nickjillings@1357: for (var c=0; c threshold) nickjillings@1357: { nickjillings@1357: for (var c=0; c= 3){G = 1.41;} nickjillings@1357: sum += blocks[c][n]*G; nickjillings@1357: } nickjillings@1357: sum /= num_frames; nickjillings@1357: loudness += sum; nickjillings@1357: } nickjillings@1357: loudness = -0.691 + 10 * Math.log10(loudness); nickjillings@1357: return loudness; nickjillings@1357: }