Best JavaScript code snippet using wpt
audio-encoder.https.any.js
Source:audio-encoder.https.any.js
...79 let timestamp_us = 0;80 let data_duration_s = total_duration_s / data_count;81 let data_length = data_duration_s * config.sampleRate;82 for (let i = 0; i < data_count; i++) {83 let data = make_audio_data(timestamp_us, config.numberOfChannels,84 config.sampleRate, data_length);85 encoder.encode(data);86 data.close();87 timestamp_us += data_duration_s * 1_000_000;88 }89 await encoder.flush();90 encoder.close();91 assert_greater_than_equal(outputs.length, data_count);92 assert_equals(outputs[0].timestamp, 0, "first chunk timestamp");93 let total_encoded_duration = 094 for (chunk of outputs) {95 assert_greater_than(chunk.byteLength, 0);96 assert_greater_than_equal(timestamp_us, chunk.timestamp);97 assert_greater_than(chunk.duration, 0);98 total_encoded_duration += chunk.duration;99 }100 // The total duration might be padded with silence.101 assert_greater_than_equal(102 total_encoded_duration, total_duration_s * 1_000_000);103}, 'Simple audio encoding');104promise_test(async t => {105 let sample_rate = 48000;106 let total_duration_s = 1;107 let data_count = 10;108 let outputs = [];109 let init = {110 error: e => {111 assert_unreached('error: ' + e);112 },113 output: chunk => {114 outputs.push(chunk);115 }116 };117 let encoder = new AudioEncoder(init);118 assert_equals(encoder.state, 'unconfigured');119 let config = {120 codec: 'opus',121 sampleRate: sample_rate,122 numberOfChannels: 2,123 bitrate: 256000 // 256kbit124 };125 encoder.configure(config);126 let timestamp_us = -10000;127 let data = make_audio_data(128 timestamp_us, config.numberOfChannels, config.sampleRate, 10000);129 encoder.encode(data);130 data.close();131 await encoder.flush();132 encoder.close();133 assert_greater_than_equal(outputs.length, 1);134 assert_equals(outputs[0].timestamp, -10000, 'first chunk timestamp');135 for (chunk of outputs) {136 assert_greater_than(chunk.byteLength, 0);137 assert_greater_than_equal(chunk.timestamp, timestamp_us);138 }139}, 'Encode audio with negative timestamp');140async function checkEncodingError(config, good_data, bad_data) {141 let error = null;142 let outputs = 0;143 let init = {144 error: e => {145 error = e;146 },147 output: chunk => {148 outputs++;149 }150 };151 let encoder = new AudioEncoder(init);152 let support = await AudioEncoder.isConfigSupported(config);153 assert_true(support.supported)154 config = support.config;155 encoder.configure(config);156 for (let data of good_data) {157 encoder.encode(data);158 data.close();159 }160 await encoder.flush();161 let txt_config = "sampleRate: " + config.sampleRate162 + " numberOfChannels: " + config.numberOfChannels;163 assert_equals(error, null, txt_config);164 assert_greater_than(outputs, 0);165 encoder.encode(bad_data);166 await encoder.flush().catch(() => {});167 assert_not_equals(error, null, txt_config);168}169function channelNumberVariationTests() {170 let sample_rate = 48000;171 for (let channels = 1; channels <= 2; channels++) {172 let config = {173 codec: 'opus',174 sampleRate: sample_rate,175 numberOfChannels: channels,176 bitrate: 128000177 };178 let ts = 0;179 let length = sample_rate / 10;180 let data1 = make_audio_data(ts, channels, sample_rate, length);181 ts += Math.floor(data1.duration / 1000000);182 let data2 = make_audio_data(ts, channels, sample_rate, length);183 ts += Math.floor(data2.duration / 1000000);184 let bad_data = make_audio_data(ts, channels + 1, sample_rate, length);185 promise_test(async t =>186 checkEncodingError(config, [data1, data2], bad_data),187 "Channel number variation: " + channels);188 }189}190channelNumberVariationTests();191function sampleRateVariationTests() {192 let channels = 1193 for (let sample_rate = 3000; sample_rate < 96000; sample_rate += 10000) {194 let config = {195 codec: 'opus',196 sampleRate: sample_rate,197 numberOfChannels: channels,198 bitrate: 128000199 };200 let ts = 0;201 let length = sample_rate / 10;202 let data1 = make_audio_data(ts, channels, sample_rate, length);203 ts += Math.floor(data1.duration / 1000000);204 let data2 = make_audio_data(ts, channels, sample_rate, length);205 ts += Math.floor(data2.duration / 1000000);206 let bad_data = make_audio_data(ts, channels, sample_rate + 333, length);207 promise_test(async t =>208 checkEncodingError(config, [data1, data2], bad_data),209 "Sample rate variation: " + sample_rate);210 }211}212sampleRateVariationTests();213promise_test(async t => {214 let sample_rate = 48000;215 let total_duration_s = 1;216 let data_count = 10;217 let input_data = [];218 let output_data = [];219 let decoder_init = {220 error: t.unreached_func("Decode error"),221 output: data => {222 output_data.push(data);223 }224 };225 let decoder = new AudioDecoder(decoder_init);226 let encoder_init = {227 error: t.unreached_func("Encoder error"),228 output: (chunk, metadata) => {229 let config = metadata.decoderConfig;230 if (config)231 decoder.configure(config);232 decoder.decode(chunk);233 }234 };235 let encoder = new AudioEncoder(encoder_init);236 let config = {237 codec: 'opus',238 sampleRate: sample_rate,239 numberOfChannels: 2,240 bitrate: 256000, //256kbit241 };242 encoder.configure(config);243 let timestamp_us = 0;244 const data_duration_s = total_duration_s / data_count;245 const data_length = data_duration_s * config.sampleRate;246 for (let i = 0; i < data_count; i++) {247 let data = make_audio_data(timestamp_us, config.numberOfChannels,248 config.sampleRate, data_length);249 input_data.push(data);250 encoder.encode(data);251 timestamp_us += data_duration_s * 1_000_000;252 }253 await encoder.flush();254 encoder.close();255 await decoder.flush();256 decoder.close();257 let total_input = join_audio_data(input_data);258 let frames_per_plane = total_input.length / config.numberOfChannels;259 let total_output = join_audio_data(output_data);260 let base_input = input_data[0];261 let base_output = output_data[0];262 // TODO: Convert formats to simplify conversions, once263 // https://github.com/w3c/webcodecs/issues/232 is resolved.264 assert_equals(base_input.format, "f32-planar");265 assert_equals(base_output.format, "f32");266 assert_equals(base_output.numberOfChannels, config.numberOfChannels);267 assert_equals(base_output.sampleRate, sample_rate);268 // Output can be slightly longer that the input due to padding269 assert_greater_than_equal(total_output.length, total_input.length);270 // Compare waveform before and after encoding271 for (let channel = 0; channel < base_input.numberOfChannels; channel++) {272 let plane_start = channel * frames_per_plane;273 let input_plane = total_input.slice(274 plane_start, plane_start + frames_per_plane);275 for (let i = 0; i < base_input.numberOfFrames; i += 10) {276 // Instead of de-interleaving the data, directly look into |total_output|277 // for the sample we are interested in.278 let ouput_index = i * base_input.numberOfChannels + channel;279 // Checking only every 10th sample to save test time in slow280 // configurations like MSAN etc.281 assert_approx_equals(282 input_plane[i], total_output[ouput_index], 0.5,283 'Difference between input and output is too large.' +284 ' index: ' + i + ' channel: ' + channel +285 ' input: ' + input_plane[i] +286 ' output: ' + total_output[ouput_index]);287 }288 }289}, 'Encoding and decoding');290promise_test(async t => {291 let output_count = 0;292 let encoder_config = {293 codec: 'opus',294 sampleRate: 24000,295 numberOfChannels: 1,296 bitrate: 96000297 };298 let decoder_config = null;299 let init = {300 error: t.unreached_func("Encoder error"),301 output: (chunk, metadata) => {302 let config = metadata.decoderConfig;303 // Only the first invocation of the output callback is supposed to have304 // a |config| in it.305 output_count++;306 if (output_count == 1) {307 assert_equals(typeof config, "object");308 decoder_config = config;309 } else {310 assert_equals(config, undefined);311 }312 }313 };314 let encoder = new AudioEncoder(init);315 encoder.configure(encoder_config);316 let large_data = make_audio_data(0, encoder_config.numberOfChannels,317 encoder_config.sampleRate, encoder_config.sampleRate);318 encoder.encode(large_data);319 await encoder.flush();320 // Large data produced more than one output, and we've got decoder_config321 assert_greater_than(output_count, 1);322 assert_not_equals(decoder_config, null);323 assert_equals(decoder_config.codec, encoder_config.codec);324 assert_equals(decoder_config.sampleRate, encoder_config.sampleRate);325 assert_equals(decoder_config.numberOfChannels, encoder_config.numberOfChannels);326 // Check that description start with 'Opus'327 let extra_data = new Uint8Array(decoder_config.description);328 assert_equals(extra_data[0], 0x4f);329 assert_equals(extra_data[1], 0x70);330 assert_equals(extra_data[2], 0x75);...
audio-encoder.any.js
Source:audio-encoder.any.js
...52 let timestamp_us = 0;53 let data_duration_s = total_duration_s / data_count;54 let data_length = data_duration_s * config.sampleRate;55 for (let i = 0; i < data_count; i++) {56 let data = make_audio_data(timestamp_us, config.numberOfChannels,57 config.sampleRate, data_length);58 encoder.encode(data);59 data.close();60 timestamp_us += data_duration_s * 1_000_000;61 }62 await encoder.flush();63 encoder.close();64 assert_greater_than_equal(outputs.length, data_count);65 assert_equals(outputs[0].timestamp, 0, "first chunk timestamp");66 for (chunk of outputs) {67 assert_greater_than(chunk.byteLength, 0);68 assert_greater_than_equal(timestamp_us, chunk.timestamp);69 }70}, 'Simple audio encoding');71promise_test(async t => {72 let sample_rate = 48000;73 let total_duration_s = 1;74 let data_count = 10;75 let outputs = [];76 let init = {77 error: e => {78 assert_unreached('error: ' + e);79 },80 output: chunk => {81 outputs.push(chunk);82 }83 };84 let encoder = new AudioEncoder(init);85 assert_equals(encoder.state, 'unconfigured');86 let config = {87 codec: 'opus',88 sampleRate: sample_rate,89 numberOfChannels: 2,90 bitrate: 256000 // 256kbit91 };92 encoder.configure(config);93 let timestamp_us = -10000;94 let data = make_audio_data(95 timestamp_us, config.numberOfChannels, config.sampleRate, 10000);96 encoder.encode(data);97 data.close();98 await encoder.flush();99 encoder.close();100 assert_greater_than_equal(outputs.length, 1);101 assert_equals(outputs[0].timestamp, -10000, 'first chunk timestamp');102 for (chunk of outputs) {103 assert_greater_than(chunk.byteLength, 0);104 assert_greater_than_equal(chunk.timestamp, timestamp_us);105 }106}, 'Encode audio with negative timestamp');107async function checkEncodingError(config, good_data, bad_data) {108 let error = null;109 let outputs = 0;110 let init = {111 error: e => {112 error = e;113 },114 output: chunk => {115 outputs++;116 }117 };118 let encoder = new AudioEncoder(init);119 let support = await AudioEncoder.isConfigSupported(config);120 assert_true(support.supported)121 config = support.config;122 encoder.configure(config);123 for (let data of good_data) {124 encoder.encode(data);125 data.close();126 }127 await encoder.flush();128 let txt_config = "sampleRate: " + config.sampleRate129 + " numberOfChannels: " + config.numberOfChannels;130 assert_equals(error, null, txt_config);131 assert_greater_than(outputs, 0);132 encoder.encode(bad_data);133 await encoder.flush().catch(() => {});134 assert_not_equals(error, null, txt_config);135}136function channelNumberVariationTests() {137 let sample_rate = 48000;138 for (let channels = 1; channels <= 2; channels++) {139 let config = {140 codec: 'opus',141 sampleRate: sample_rate,142 numberOfChannels: channels,143 bitrate: 128000144 };145 let ts = 0;146 let length = sample_rate / 10;147 let data1 = make_audio_data(ts, channels, sample_rate, length);148 ts += Math.floor(data1.buffer.duration / 1000000);149 let data2 = make_audio_data(ts, channels, sample_rate, length);150 ts += Math.floor(data2.buffer.duration / 1000000);151 let bad_data = make_audio_data(ts, channels + 1, sample_rate, length);152 promise_test(async t =>153 checkEncodingError(config, [data1, data2], bad_data),154 "Channel number variation: " + channels);155 }156}157channelNumberVariationTests();158function sampleRateVariationTests() {159 let channels = 1160 for (let sample_rate = 3000; sample_rate < 96000; sample_rate += 10000) {161 let config = {162 codec: 'opus',163 sampleRate: sample_rate,164 numberOfChannels: channels,165 bitrate: 128000166 };167 let ts = 0;168 let length = sample_rate / 10;169 let data1 = make_audio_data(ts, channels, sample_rate, length);170 ts += Math.floor(data1.buffer.duration / 1000000);171 let data2 = make_audio_data(ts, channels, sample_rate, length);172 ts += Math.floor(data2.buffer.duration / 1000000);173 let bad_data = make_audio_data(ts, channels, sample_rate + 333, length);174 promise_test(async t =>175 checkEncodingError(config, [data1, data2], bad_data),176 "Sample rate variation: " + sample_rate);177 }178}179sampleRateVariationTests();180promise_test(async t => {181 let sample_rate = 48000;182 let total_duration_s = 1;183 let data_count = 10;184 let input_data = [];185 let output_data = [];186 let decoder_init = {187 error: t.unreached_func("Decode error"),188 output: data => {189 output_data.push(data);190 }191 };192 let decoder = new AudioDecoder(decoder_init);193 let encoder_init = {194 error: t.unreached_func("Encoder error"),195 output: (chunk, metadata) => {196 let config = metadata.decoderConfig;197 if (config)198 decoder.configure(config);199 decoder.decode(chunk);200 }201 };202 let encoder = new AudioEncoder(encoder_init);203 let config = {204 codec: 'opus',205 sampleRate: sample_rate,206 numberOfChannels: 2,207 bitrate: 256000, //256kbit208 };209 encoder.configure(config);210 let timestamp_us = 0;211 const data_duration_s = total_duration_s / data_count;212 const data_length = data_duration_s * config.sampleRate;213 for (let i = 0; i < data_count; i++) {214 let data = make_audio_data(timestamp_us, config.numberOfChannels,215 config.sampleRate, data_length);216 input_data.push(data);217 encoder.encode(data);218 timestamp_us += data_duration_s * 1_000_000;219 }220 await encoder.flush();221 encoder.close();222 await decoder.flush();223 decoder.close();224 let total_input = join_buffers(input_data.map(f => f.buffer));225 let total_output = join_buffers(output_data.map(f => f.buffer));226 assert_equals(total_output.numberOfChannels, 2);227 assert_equals(total_output.sampleRate, sample_rate);228 // Output can be slightly longer that the input due to padding229 assert_greater_than_equal(total_output.length, total_input.length);230 assert_greater_than_equal(total_output.duration, total_duration_s);231 assert_approx_equals(total_output.duration, total_duration_s, 0.1);232 // Compare waveform before and after encoding233 for (let channel = 0; channel < total_input.numberOfChannels; channel++) {234 let input_data = total_input.getChannelData(channel);235 let output_data = total_output.getChannelData(channel);236 for (let i = 0; i < total_input.length; i += 10) {237 // Checking only every 10th sample to save test time in slow238 // configurations like MSAN etc.239 assert_approx_equals(input_data[i], output_data[i], 0.5,240 "Difference between input and output is too large."241 + " index: " + i242 + " input: " + input_data[i]243 + " output: " + output_data[i]);244 }245 }246}, 'Encoding and decoding');247promise_test(async t => {248 let output_count = 0;249 let encoder_config = {250 codec: 'opus',251 sampleRate: 24000,252 numberOfChannels: 1,253 bitrate: 96000254 };255 let decoder_config = null;256 let init = {257 error: t.unreached_func("Encoder error"),258 output: (chunk, metadata) => {259 let config = metadata.decoderConfig;260 // Only the first invocation of the output callback is supposed to have261 // a |config| in it.262 output_count++;263 if (output_count == 1) {264 assert_equals(typeof config, "object");265 decoder_config = config;266 } else {267 assert_equals(config, undefined);268 }269 }270 };271 let encoder = new AudioEncoder(init);272 encoder.configure(encoder_config);273 let large_data = make_audio_data(0, encoder_config.numberOfChannels,274 encoder_config.sampleRate, encoder_config.sampleRate);275 encoder.encode(large_data);276 await encoder.flush();277 // Large data produced more than one output, and we've got decoder_config278 assert_greater_than(output_count, 1);279 assert_not_equals(decoder_config, null);280 assert_equals(decoder_config.codec, encoder_config.codec);281 assert_equals(decoder_config.sampleRate, encoder_config.sampleRate);282 assert_equals(decoder_config.numberOfChannels, encoder_config.numberOfChannels);283 // Check that description start with 'Opus'284 let extra_data = new Uint8Array(decoder_config.description);285 assert_equals(extra_data[0], 0x4f);286 assert_equals(extra_data[1], 0x70);287 assert_equals(extra_data[2], 0x75);...
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var page = wptools.page('wikipedia');4page.make_audio_data(function(err, res) {5 if (err) {6 console.log(err);7 } else {8 fs.writeFileSync('audio_data.json', JSON.stringify(res, null, 2));9 }10});11{12 {13 }14}15var wptools = require('wptools');16var fs = require('fs');17var page = wptools.page('wikipedia');18page.make_image_data(function(err, res) {19 if (err) {20 console.log(err);21 } else {22 fs.writeFileSync('image_data.json', JSON.stringify(res, null, 2));23 }24});25{26 {27 }28}29var wptools = require('wptools');30var fs = require('fs');31var page = wptools.page('wikipedia');32page.make_image_data(function(err, res
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var path = require('path');3var fs = require('fs');4var input_audio = path.join(__dirname, 'input_audio.wav');5var output_audio = path.join(__dirname, 'output_audio.wav');6var input_audio_file = fs.readFileSync(input_audio);7var output_audio_file = wptoolkit.make_audio_data(input_audio_file);8fs.writeFileSync(output_audio, output_audio_file);9var wptoolkit = require('wptoolkit');10var path = require('path');11var fs = require('fs');12var input_audio = path.join(__dirname, 'input_audio.wav');13var output_audio = path.join(__dirname, 'output_audio.wav');14var input_audio_file = fs.readFileSync(input_audio);15var output_audio_file = wptoolkit.make_audio_data(input_audio_file);16fs.writeFileSync(output_audio, output_audio_file);17var wptoolkit = require('wptoolkit');18var path = require('path');19var fs = require('fs');20var input_audio = path.join(__dirname, 'input_audio.wav');21var output_audio = path.join(__dirname, 'output_audio.wav');22var input_audio_file = fs.readFileSync(input_audio);23var output_audio_file = wptoolkit.make_audio(input_audio_file);24fs.writeFileSync(output_audio, output_audio_file);25var wptoolkit = require('wptoolkit');
Using AI Code Generation
1var audio = new wptb_audio();2audio.make_audio_data( 'audio' );3wptb_audio.prototype.make_audio_data = function( audio ) {4 var audio = document.getElementById( audio );5 var audio_data = audio.getAttribute( 'data-wptb-audio' );6 audio_data = JSON.parse( audio_data );7 console.log( audio_data );8}
Using AI Code Generation
1var audio_data = wptb_audio.make_audio_data( audio_url, audio_title );2wptb_audio.make_audio_data = function( audio_url, audio_title ) {3 var audio_data = {4 };5 var audio_data_parts = audio_url.split( '.' );6 var audio_data_parts_count = audio_data_parts.length;7 if( audio_data_parts_count > 1 ) {8 audio_data.type = audio_data_parts[ audio_data_parts_count - 1 ];9 if( audio_data.type == 'mp3' ) {10 audio_data.mime_type = 'audio/mpeg';11 } else if( audio_data.type == 'ogg' ) {12 audio_data.mime_type = 'audio/ogg';13 } else if( audio_data.type == 'wav' ) {14 audio_data.mime_type = 'audio/wav';15 } else if( audio_data.type == 'm4a' ) {16 audio_data.mime_type = 'audio/mp4';17 } else if( audio_data.type == 'wma' ) {18 audio_data.mime_type = 'audio/x-ms-wma';19 } else if( audio_data.type == 'aac' ) {20 audio_data.mime_type = 'audio/aac';21 } else if( audio_data.type == 'flac' ) {22 audio_data.mime_type = 'audio/flac';23 }24 }25 var audio_data_parts = audio_url.split( '/' );26 var audio_data_parts_count = audio_data_parts.length;27 if( audio_data_parts_count > 1 ) {28 audio_data.id = audio_data_parts[ audio_data_parts_count - 1 ];29 }
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!