Best JavaScript code snippet using wpt
audio-encoder.https.any.js
Source:audio-encoder.https.any.js
1// META: global=window2// META: script=/webcodecs/utils.js3// Merge all audio buffers into a new big one with all the data.4function join_audio_data(audio_data_array) {5 assert_greater_than_equal(audio_data_array.length, 0);6 let total_frames = 0;7 let base_buffer = audio_data_array[0];8 for (const data of audio_data_array) {9 assert_not_equals(data, null);10 assert_equals(data.sampleRate, base_buffer.sampleRate);11 assert_equals(data.numberOfChannels, base_buffer.numberOfChannels);12 assert_equals(data.format, base_buffer.format);13 total_frames += data.numberOfFrames;14 }15 assert_true(base_buffer.format == 'f32' || base_buffer.format == 'f32-planar');16 if (base_buffer.format == 'f32')17 return join_interleaved_data(audio_data_array, total_frames);18 // The format is 'FLTP'.19 return join_planar_data(audio_data_array, total_frames);20}21function join_interleaved_data(audio_data_array, total_frames) {22 let base_data = audio_data_array[0];23 let channels = base_data.numberOfChannels;24 let total_samples = total_frames * channels;25 let result = new Float32Array(total_samples);26 let copy_dest = new Float32Array(base_data.numberOfFrames * channels);27 // Copy all the interleaved data.28 let position = 0;29 for (const data of audio_data_array) {30 let samples = data.numberOfFrames * channels;31 if (copy_dest.length < samples)32 copy_dest = new Float32Array(samples);33 data.copyTo(copy_dest, {planeIndex: 0});34 result.set(copy_dest, position);35 position += samples;36 }37 assert_equals(position, total_samples);38 return result;39}40function join_planar_data(audio_data_array, total_frames) {41 let base_frames = audio_data_array[0].numberOfFrames;42 let channels = audio_data_array[0].numberOfChannels;43 let result = new Float32Array(total_frames*channels);44 let copyDest = new Float32Array(base_frames);45 // Merge all samples and lay them out according to the FLTP memory layout.46 let position = 0;47 for (let ch = 0; ch < channels; ch++) {48 for (const data of audio_data_array) {49 data.copyTo(copyDest, { planeIndex: ch});50 result.set(copyDest, position);51 position += data.numberOfFrames;52 }53 }54 assert_equals(position, total_frames * channels);55 return result;56}57promise_test(async t => {58 let sample_rate = 48000;59 let total_duration_s = 1;60 let data_count = 10;61 let outputs = [];62 let init = {63 error: e => {64 assert_unreached("error: " + e);65 },66 output: chunk => {67 outputs.push(chunk);68 }69 };70 let encoder = new AudioEncoder(init);71 assert_equals(encoder.state, "unconfigured");72 let config = {73 codec: 'opus',74 sampleRate: sample_rate,75 numberOfChannels: 2,76 bitrate: 256000 //256kbit77 };78 encoder.configure(config);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(...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var fs = require('fs');3var file1 = fs.readFileSync('file1.wav');4var file2 = fs.readFileSync('file2.wav');5var file3 = fs.readFileSync('file3.wav');6var file1_buffer = new Buffer(file1);7var file2_buffer = new Buffer(file2);8var file3_buffer = new Buffer(file3);9var joined_audio = wptoolkit.join_audio_data([file1_buffer, file2_buffer, file3_buffer]);10fs.writeFileSync('joined.wav', joined_audio);11{12 "dependencies": {13 }14}
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var join_audio_data = wptoolkit.join_audio_data;3var audio_data = join_audio_data('audio1.wav', 'audio2.wav');4console.log(audio_data.length);5 var join_audio_data = wptoolkit.join_audio_data;6 var audio_data = join_audio_data('audio1.wav', 'audio2.wav');7 console.log(audio_data.length);8join_audio_data(audio1, audio2)9record_audio()10play_audio(audio_data)11stop_audio()12set_volume(volume)13get_volume()14get_audio_data(audio_file)15save_audio_data(audio_data, audio_file)16get_audio_duration(audio_data)17get_audio_duration(audio_file)18get_audio_duration(audio_data)
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var audio_data = wptoolkit.join_audio_data('audio_1.wav', 'audio_2.wav');3wptoolkit.write_audio_data(audio_data, 'audio_out.wav');4var fs = require('fs');5var player = require('play-sound')(opts = {});6var audio_data = fs.readFileSync('audio_out.wav');7player.play(audio_data, function(err){8 if (err) throw err9});10var fs = require('fs');11var audio_data = fs.readFileSync('audio_out.wav');12fs.writeFileSync('audio_out.mp3', audio_data);13var fs = require('fs');14var request = require('request');15var audio_data = fs.readFileSync('audio_out.wav');16var options = {17 headers: {18 },19};20request(options, function (error, response, body) {21 if (!error && response.statusCode == 200) {22 console.log(body);23 }24});25var fs = require('fs');26var audio_data = fs.readFileSync('audio_out.wav');27var audio_data_base64 = audio_data.toString('base64');28var fs = require('fs');29var audio_data = fs.readFileSync('audio_out.wav');30var audio_data_binary = audio_data.toString('binary');31var fs = require('fs');32var audio_data = fs.readFileSync('audio_out.wav');33var audio_data_hex = audio_data.toString('hex');34var fs = require('fs');35var audio_data = fs.readFileSync('audio
Using AI Code Generation
1var wpt = require('wptoolkit');2var wav1 = './test1.wav';3var wav2 = './test2.wav';4wpt.join_audio_data(wav1, wav2, function (err, data) {5 if (err) {6 console.log(err);7 } else {8 console.log(data);9 }10});11var fs = require('fs');12var wav1 = fs.readFileSync('./test1.wav');13var wav2 = fs.readFileSync('./test2.wav');14fs.writeFileSync('./test.wav', Buffer.concat([wav1, wav2]));
Using AI Code Generation
1var audio_data = wptb.join_audio_data(audio_data1, audio_data2);2var audio_data = wptb.join_audio_data(audio_data1, audio_data2);3var audio_data = wptb.join_audio_data(audio_data1, audio_data2);4var audio_data = wptb.join_audio_data(audio_data1, audio_data2);5var audio_data = wptb.join_audio_data(audio_data1, audio_data2);6var audio_data = wptb.join_audio_data(audio_data1, audio_data2);7var audio_data = wptb.join_audio_data(audio_data1, audio_data2);8var audio_data = wptb.join_audio_data(audio_data1, audio_data2);9var audio_data = wptb.join_audio_data(audio_data1, audio_data2);10var audio_data = wptb.join_audio_data(audio_data1,
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var text = fs.readFileSync('input.txt', 'utf8');4var text_array = text.split('5');6var text_array_length = text_array.length;7var output_file_name = 'output.wav';8var audio_data = [];9for (var i = 0; i < text_array_length; i++) {10 var text_to_speech = wptools.text_to_speech(text_array[i]);11 audio_data.push(text_to_speech);12}13wptools.join_audio_data(audio_data, output_file_name, function (err) {14 if (err) {15 console.log(err);16 } else {17 console.log('done');18 }19});
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!!