How to use timeToSampleFrame method in wpt

Best JavaScript code snippet using wpt

audioparam-testing.js

Source:audioparam-testing.js Github

copy

Full Screen

...21var context;22// Make sure we render long enough to capture all of our test data.23function renderLength(numberOfTests)24{25 return timeToSampleFrame((numberOfTests + 1) * timeInterval, sampleRate);26}27// Create a buffer containing the same constant value.28function createConstantBuffer(context, constant, length) {29 var buffer = context.createBuffer(1, length, context.sampleRate);30 var n = buffer.length;31 var data = buffer.getChannelData(0);32 for (var k = 0; k < n; ++k) {33 data[k] = constant;34 }35 return buffer;36}37// Create a constant reference signal with the given |value|. Basically the same as38// |createConstantBuffer|, but with the parameters to match the other create functions. The39// |endValue| is ignored.40function createConstantArray(startTime, endTime, value, endValue, sampleRate)41{42 var startFrame = timeToSampleFrame(startTime, sampleRate);43 var endFrame = timeToSampleFrame(endTime, sampleRate);44 var length = endFrame - startFrame;45 var buffer = createConstantBuffer(context, value, length);46 return buffer.getChannelData(0);47}48// Create a linear ramp starting at |startValue| and ending at |endValue|. The ramp starts at time49// |startTime| and ends at |endTime|. (The start and end times are only used to compute how many50// samples to return.)51function createLinearRampArray(startTime, endTime, startValue, endValue, sampleRate)52{53 var startFrame = timeToSampleFrame(startTime, sampleRate);54 var endFrame = timeToSampleFrame(endTime, sampleRate);55 var length = endFrame - startFrame;56 var array = new Array(length);57 var step = (endValue - startValue) / length;58 for (k = 0; k < length; ++k) {59 array[k] = startValue + k * step;60 }61 return array;62}63// Create an exponential ramp starting at |startValue| and ending at |endValue|. The ramp starts at64// time |startTime| and ends at |endTime|. (The start and end times are only used to compute how65// many samples to return.)66function createExponentialRampArray(startTime, endTime, startValue, endValue, sampleRate)67{68 var startFrame = timeToSampleFrame(startTime, sampleRate);69 var endFrame = timeToSampleFrame(endTime, sampleRate);70 var length = endFrame - startFrame;71 var array = new Array(length);72 var multiplier = Math.pow(endValue / startValue, 1 / length);73 74 for (var k = 0; k < length; ++k) {75 array[k] = startValue * Math.pow(multiplier, k);76 }77 return array;78}79function discreteTimeConstantForSampleRate(timeConstant, sampleRate)80{81 return 1 - Math.exp(-1 / (sampleRate * timeConstant));82}83// Create a signal that starts at |startValue| and exponentially approaches the target value of84// |targetValue|, using a time constant of |timeConstant|. The ramp starts at time |startTime| and85// ends at |endTime|. (The start and end times are only used to compute how many samples to86// return.)87function createExponentialApproachArray(startTime, endTime, startValue, targetValue, sampleRate, timeConstant)88{89 var startFrame = timeToSampleFrame(startTime, sampleRate);90 var endFrame = timeToSampleFrame(endTime, sampleRate);91 var length = endFrame - startFrame;92 var array = new Array(length);93 var c = discreteTimeConstantForSampleRate(timeConstant, sampleRate);94 var value = startValue;95 96 for (var k = 0; k < length; ++k) {97 array[k] = value;98 value += (targetValue - value) * c;99 }100 return array;101}102// Create a sine wave of the given frequency and amplitude. The sine wave is offset by half the103// amplitude so that result is always positive.104function createSineWaveArray(durationSeconds, freqHz, amplitude, sampleRate)105{106 var length = timeToSampleFrame(durationSeconds, sampleRate);107 var signal = new Float32Array(length);108 var omega = 2 * Math.PI * freqHz / sampleRate;109 var halfAmplitude = amplitude / 2;110 111 for (var k = 0; k < length; ++k) {112 signal[k] = halfAmplitude + halfAmplitude * Math.sin(omega * k);113 }114 return signal;115}116// Return the difference between the starting value and the ending value for time interval117// |timeIntervalIndex|. We alternate between an end value that is above or below the starting118// value.119function endValueDelta(timeIntervalIndex)120{121 if (timeIntervalIndex & 1) {122 return -startEndValueChange;123 } else {124 return startEndValueChange;125 }126}127// Return the difference between the starting value at |timeIntervalIndex| and the starting value at128// the next time interval. Since we started at a large initial value, we decrease the value at each129// time interval.130function valueUpdate(timeIntervalIndex)131{132 return -startingValueDelta;133}134// Compare a section of the rendered data against our expected signal.135function comparePartialSignals(rendered, expectedFunction, startTime, endTime, valueInfo, sampleRate)136{137 var startSample = timeToSampleFrame(startTime, sampleRate);138 var expected = expectedFunction(startTime, endTime, valueInfo.startValue, valueInfo.endValue, sampleRate, timeConstant);139 var n = expected.length;140 var maxError = -1;141 var maxErrorIndex = -1;142 143 for (var k = 0; k < n; ++k) {144 // Make sure we don't pass these tests because a NaN has been generated in either the145 // rendered data or the reference data.146 if (!isValidNumber(rendered[startSample + k])) {147 maxError = Infinity;148 maxErrorIndex = startSample + k;149 testFailed("NaN or infinity for rendered data at " + maxErrorIndex);150 break;151 }152 if (!isValidNumber(expected[k])) {153 maxError = Infinity;154 maxErrorIndex = startSample + k;155 testFailed("Nan or infinity for reference data at " + maxErrorIndex);156 break;157 }158 var error = Math.abs(rendered[startSample + k] - expected[k]);159 if (error > maxError) {160 maxError = error;161 maxErrorIndex = k;162 }163 }164 return {maxError : maxError, index : maxErrorIndex};165}166// Find the discontinuities in the data and compare the locations of the discontinuities with the167// times that define the time intervals. There is a discontinuity if the difference between168// successive samples exceeds the threshold.169function verifyDiscontinuities(values, times, threshold)170{171 var n = values.length;172 var success = true;173 var badLocations = 0;174 var breaks = [];175 // Find discontinuities.176 for (var k = 1; k < n; ++k) {177 if (Math.abs(values[k] - values[k - 1]) > threshold) {178 breaks.push(k);179 }180 }181 var testCount;182 // If there are numberOfTests intervals, there are only numberOfTests - 1 internal interval183 // boundaries. Hence the maximum number of discontinuties we expect to find is numberOfTests -184 // 1. If we find more than that, we have no reference to compare against. We also assume that185 // the actual discontinuities are close to the expected ones.186 //187 // This is just a sanity check when something goes really wrong. For example, if the threshold188 // is too low, every sample frame looks like a discontinuity.189 if (breaks.length >= numberOfTests) {190 testCount = numberOfTests - 1;191 testFailed("Found more discontinuities (" + breaks.length + ") than expected. Only comparing first " + testCount + "discontinuities.");192 success = false;193 } else {194 testCount = breaks.length;195 }196 197 // Compare the location of each discontinuity with the end time of each interval. (There is no198 // discontinuity at the start of the signal.)199 for (var k = 0; k < testCount; ++k) {200 var expectedSampleFrame = timeToSampleFrame(times[k + 1], sampleRate);201 if (breaks[k] != expectedSampleFrame) {202 success = false;203 ++badLocations;204 testFailed("Expected discontinuity at " + expectedSampleFrame + " but got " + breaks[k]);205 }206 }207 if (badLocations) {208 testFailed(badLocations + " discontinuities at incorrect locations");209 success = false;210 } else {211 if (breaks.length == numberOfTests - 1) {212 testPassed("All " + numberOfTests + " tests started and ended at the correct time.");213 } else {214 testFailed("Found " + breaks.length + " discontinuities but expected " + (numberOfTests - 1));215 success = false;216 }217 }218 219 return success;220}221// Compare the rendered data with the expected data.222//223// testName - string describing the test224//225// maxError - maximum allowed difference between the rendered data and the expected data226//227// rendererdData - array containing the rendered (actual) data228//229// expectedFunction - function to compute the expected data230//231// timeValueInfo - array containing information about the start and end times and the start and end232// values of each interval.233//234// breakThreshold - threshold to use for determining discontinuities.235function compareSignals(testName, maxError, renderedData, expectedFunction, timeValueInfo, breakThreshold)236{237 var success = true;238 var failedTestCount = 0;239 var times = timeValueInfo.times;240 var values = timeValueInfo.values;241 var n = values.length;242 success = verifyDiscontinuities(renderedData, times, breakThreshold);243 for (var k = 0; k < n; ++k) {244 var result = comparePartialSignals(renderedData, expectedFunction, times[k], times[k + 1], values[k], sampleRate);245 if (result.maxError > maxError) {246 testFailed("Incorrect value for test " + k + ". Max error = " + result.maxError + " at offset " + (result.index + timeToSampleFrame(times[k], sampleRate)));247 ++failedTestCount;248 }249 }250 if (failedTestCount) {251 testFailed(failedTestCount + " tests failed out of " + n);252 success = false;253 } else {254 testPassed("All " + n + " tests passed within an acceptable tolerance.");255 }256 257 if (success) {258 testPassed("AudioParam " + testName + " test passed.");259 } else {260 testFailed("AudioParam " + testName + " test failed.");...

Full Screen

Full Screen

audio-testing.js

Source:audio-testing.js Github

copy

Full Screen

...130 dataR[0] = 1;131 return audioBuffer;132}133// Convert time (in seconds) to sample frames.134function timeToSampleFrame(time, sampleRate) {135 return Math.floor(0.5 + time * sampleRate);136}137// Compute the number of sample frames consumed by start with138// the specified |grainOffset|, |duration|, and |sampleRate|.139function grainLengthInSampleFrames(grainOffset, duration, sampleRate) {140 var startFrame = timeToSampleFrame(grainOffset, sampleRate);141 var endFrame = timeToSampleFrame(grainOffset + duration, sampleRate);142 return endFrame - startFrame;143}144// True if the number is not an infinity or NaN145function isValidNumber(x) {146 return !isNaN(x) && (x != Infinity) && (x != -Infinity);147}148function shouldThrowTypeError(func, text) {149 var ok = false;150 try {151 func();152 } catch (e) {153 if (e instanceof TypeError) {154 ok = true;155 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.runTest('www.google.com', {video: true}, function(err, data) {4 if (err) return console.error(err);5 wpt.timeToSampleFrame(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 });9});10{11 "data": {12 }13}14{15 "data": {16 }17}18{19 "data": {20 }21}22{23 "data": {24 }25}26{27 "data": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var test = new wpt('API_KEY');3 if (err) return console.log(err);4 test.timeToSampleFrame(data.data.testId, 'visualComplete', 0, function(err, data) {5 if (err) return console.log(err);6 console.log(data);7 });8});9var wpt = require('webpagetest');10var test = new wpt('API_KEY');11 if (err) return console.log(err);12 test.medianTimeToSampleFrame(data.data.testId, 'visualComplete', function(err, data) {13 if (err) return console.log(err);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2 if (err) {3 console.error('Error: ' + err);4 } else {5 console.log('Test submitted. Polling for results.');6 test.checkTestStatus(data.data.testId, function(err, data) {7 if (err) {8 console.error('Error: ' + err);9 } else {10 console.log('Test completed. Processing results.');11 test.getTestResults(data.data.testId, function(err, data) {12 if (err) {13 console.error('Error: ' + err);14 } else {15 console.log('Test results available at ' + data.data.summary);16 test.getTestResults(data.data.testId, function(err, data) {17 if (err) {18 console.error('Error: ' + err);19 } else {20 console.log('Test results available at ' + data.data.summary);21 var timeToFirstByte = test.timeToFirstByte(data.data);22 console.log('Time to first byte: ' + timeToFirstByte);23 var timeToLastByte = test.timeToLastByte(data.data);24 console.log('Time to last byte: ' + timeToLastByte);25 var timeToStartRender = test.timeToStartRender(data.data);26 console.log('Time to start render: ' + timeToStartRender);27 var timeToFinishRender = test.timeToFinishRender(data.data);28 console.log('Time to finish render: ' + timeToFinishRender);29 var timeToFirstCss = test.timeToFirstCss(data.data);30 console.log('Time to first css: ' + timeToFirstCss);31 var timeToFirstJs = test.timeToFirstJs(data.data);32 console.log('Time to first js: ' + timeToFirstJs);33 var timeToFirstFrame = test.timeToFirstFrame(data.data);34 console.log('Time to first frame: ' + timeToFirstFrame);35 var timeToDocComplete = test.timeToDocComplete(data.data);36 console.log('Time to doc complete: ' + timeToDocComplete);37 var timeToFullyLoaded = test.timeToFullyLoaded(data.data);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = WPT.getInstance();2var timeToSample = wpt.getTimeToSample();3var frame = timeToSample.timeToSampleFrame(1000);4print(frame);5var wpt = WPT.getInstance();6var timeToSample = wpt.getTimeToSample();7var time = timeToSample.sampleToTime(100);8print(time);9var wpt = WPT.getInstance();10var timeToSample = wpt.getTimeToSample();11var count = timeToSample.sampleToCount(100);12print(count);13var wpt = WPT.getInstance();14var timeToSample = wpt.getTimeToSample();15var count = timeToSample.timeToSampleCount(1000);16print(count);17var wpt = WPT.getInstance();

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var fs = require('fs');3var wpt = new wpt();4var frameNumber = wpt.timeToSampleFrame(1);5var frame = wpt.getFrame(frameNumber);6var image = wpt.getImage(frame);7fs.writeFile('test.png', image, function(err){8 if(err) throw err;9 console.log('It\'s saved!');10});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var util = require('util');3var test = new wpt('www.webpagetest.org', 'A.6f8b1e0d5c6f5a5f5b5d5b5f5b5f5b5');4 if (err) console.log(err);5 else {6 console.log(util.inspect(data, false, null));7 test.timeToSampleFrame(data.data.testId, data.data.runs[1].firstView.videoFrames, function(err, data) {8 if (err) console.log(err);9 else console.log(util.inspect(data, false, null));10 });11 }12});13var wpt = require('webpagetest');14var util = require('util');15var test = new wpt('www.webpagetest.org', 'A.6f8b1e0d5c6f5a5f5b5d5b5f5b5f5b5');16test.getLocations(function(err, data) {17 if (err) console.log(err);18 else console.log(util.inspect(data, false, null));19});20var wpt = require('webpagetest');21var util = require('util');22var test = new wpt('www.webpagetest.org', 'A.6f8b1e0d5c6f5a5f5b5d5b5f5b5f5b5');23test.getTesters(function(err, data) {24 if (err) console.log(err);25 else console.log(util.inspect(data, false, null));26});27var wpt = require('webpagetest');28var util = require('util');29var test = new wpt('www.webpagetest.org', 'A.6f8b1e0d5c6f5a5f5b5d5b5f5b5f5b5');30test.getTesters(function(err, data

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful