Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aimogen-pro/scripts/audio.js

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + "use strict";
2 + jQuery(document).ready(function(){
3 + (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Recorder = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
4 + "use strict";
5 +
6 + module.exports = require("./recorder").Recorder;
7 +
8 + },{"./recorder":2}],2:[function(require,module,exports){
9 + 'use strict';
10 +
11 + var _createClass = (function () {
12 + function defineProperties(target, props) {
13 + for (var i = 0; i < props.length; i++) {
14 + var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
15 + }
16 + }return function (Constructor, protoProps, staticProps) {
17 + if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;
18 + };
19 + })();
20 +
21 + Object.defineProperty(exports, "__esModule", {
22 + value: true
23 + });
24 + exports.Recorder = undefined;
25 +
26 + var _inlineWorker = require('inline-worker');
27 +
28 + var _inlineWorker2 = _interopRequireDefault(_inlineWorker);
29 +
30 + function _interopRequireDefault(obj) {
31 + return obj && obj.__esModule ? obj : { default: obj };
32 + }
33 +
34 + function _classCallCheck(instance, Constructor) {
35 + if (!(instance instanceof Constructor)) {
36 + throw new TypeError("Cannot call a class as a function");
37 + }
38 + }
39 +
40 + var Recorder = exports.Recorder = (function () {
41 + function Recorder(source, cfg) {
42 + var _this = this;
43 +
44 + _classCallCheck(this, Recorder);
45 +
46 + this.config = {
47 + bufferLen: 4096,
48 + numChannels: 2,
49 + mimeType: 'audio/wav'
50 + };
51 + this.recording = false;
52 + this.callbacks = {
53 + getBuffer: [],
54 + exportWAV: []
55 + };
56 +
57 + Object.assign(this.config, cfg);
58 + this.context = source.context;
59 + this.node = (this.context.createScriptProcessor || this.context.createJavaScriptNode).call(this.context, this.config.bufferLen, this.config.numChannels, this.config.numChannels);
60 +
61 + this.node.onaudioprocess = function (e) {
62 + if (!_this.recording) return;
63 +
64 + var buffer = [];
65 + for (var channel = 0; channel < _this.config.numChannels; channel++) {
66 + buffer.push(e.inputBuffer.getChannelData(channel));
67 + }
68 + _this.worker.postMessage({
69 + command: 'record',
70 + buffer: buffer
71 + });
72 + };
73 +
74 + source.connect(this.node);
75 + this.node.connect(this.context.destination); //this should not be necessary
76 +
77 + var self = {};
78 + this.worker = new _inlineWorker2.default(function () {
79 + var recLength = 0,
80 + recBuffers = [],
81 + sampleRate = undefined,
82 + numChannels = undefined;
83 +
84 + self.onmessage = function (e) {
85 + switch (e.data.command) {
86 + case 'init':
87 + init(e.data.config);
88 + break;
89 + case 'record':
90 + record(e.data.buffer);
91 + break;
92 + case 'exportWAV':
93 + exportWAV(e.data.type);
94 + break;
95 + case 'getBuffer':
96 + getBuffer();
97 + break;
98 + case 'clear':
99 + clear();
100 + break;
101 + }
102 + };
103 +
104 + function init(config) {
105 + sampleRate = config.sampleRate;
106 + numChannels = config.numChannels;
107 + initBuffers();
108 + }
109 +
110 + function record(inputBuffer) {
111 + for (var channel = 0; channel < numChannels; channel++) {
112 + recBuffers[channel].push(inputBuffer[channel]);
113 + }
114 + recLength += inputBuffer[0].length;
115 + }
116 +
117 + function exportWAV(type) {
118 + var buffers = [];
119 + for (var channel = 0; channel < numChannels; channel++) {
120 + buffers.push(mergeBuffers(recBuffers[channel], recLength));
121 + }
122 + var interleaved = undefined;
123 + if (numChannels === 2) {
124 + interleaved = interleave(buffers[0], buffers[1]);
125 + } else {
126 + interleaved = buffers[0];
127 + }
128 + var dataview = encodeWAV(interleaved);
129 + var audioBlob = new Blob([dataview], { type: type });
130 +
131 + self.postMessage({ command: 'exportWAV', data: audioBlob });
132 + }
133 +
134 + function getBuffer() {
135 + var buffers = [];
136 + for (var channel = 0; channel < numChannels; channel++) {
137 + buffers.push(mergeBuffers(recBuffers[channel], recLength));
138 + }
139 + self.postMessage({ command: 'getBuffer', data: buffers });
140 + }
141 +
142 + function clear() {
143 + recLength = 0;
144 + recBuffers = [];
145 + initBuffers();
146 + }
147 +
148 + function initBuffers() {
149 + for (var channel = 0; channel < numChannels; channel++) {
150 + recBuffers[channel] = [];
151 + }
152 + }
153 +
154 + function mergeBuffers(recBuffers, recLength) {
155 + var result = new Float32Array(recLength);
156 + var offset = 0;
157 + for (var i = 0; i < recBuffers.length; i++) {
158 + result.set(recBuffers[i], offset);
159 + offset += recBuffers[i].length;
160 + }
161 + return result;
162 + }
163 +
164 + function interleave(inputL, inputR) {
165 + var length = inputL.length + inputR.length;
166 + var result = new Float32Array(length);
167 +
168 + var index = 0,
169 + inputIndex = 0;
170 +
171 + while (index < length) {
172 + result[index++] = inputL[inputIndex];
173 + result[index++] = inputR[inputIndex];
174 + inputIndex++;
175 + }
176 + return result;
177 + }
178 +
179 + function floatTo16BitPCM(output, offset, input) {
180 + for (var i = 0; i < input.length; i++, offset += 2) {
181 + var s = Math.max(-1, Math.min(1, input[i]));
182 + output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
183 + }
184 + }
185 +
186 + function writeString(view, offset, string) {
187 + for (var i = 0; i < string.length; i++) {
188 + view.setUint8(offset + i, string.charCodeAt(i));
189 + }
190 + }
191 +
192 + function encodeWAV(samples) {
193 + var buffer = new ArrayBuffer(44 + samples.length * 2);
194 + var view = new DataView(buffer);
195 +
196 + /* RIFF identifier */
197 + writeString(view, 0, 'RIFF');
198 + /* RIFF chunk length */
199 + view.setUint32(4, 36 + samples.length * 2, true);
200 + /* RIFF type */
201 + writeString(view, 8, 'WAVE');
202 + /* format chunk identifier */
203 + writeString(view, 12, 'fmt ');
204 + /* format chunk length */
205 + view.setUint32(16, 16, true);
206 + /* sample format (raw) */
207 + view.setUint16(20, 1, true);
208 + /* channel count */
209 + view.setUint16(22, numChannels, true);
210 + /* sample rate */
211 + view.setUint32(24, sampleRate, true);
212 + /* byte rate (sample rate * block align) */
213 + view.setUint32(28, sampleRate * 4, true);
214 + /* block align (channel count * bytes per sample) */
215 + view.setUint16(32, numChannels * 2, true);
216 + /* bits per sample */
217 + view.setUint16(34, 16, true);
218 + /* data chunk identifier */
219 + writeString(view, 36, 'data');
220 + /* data chunk length */
221 + view.setUint32(40, samples.length * 2, true);
222 +
223 + floatTo16BitPCM(view, 44, samples);
224 +
225 + return view;
226 + }
227 + }, self);
228 +
229 + this.worker.postMessage({
230 + command: 'init',
231 + config: {
232 + sampleRate: this.context.sampleRate,
233 + numChannels: this.config.numChannels
234 + }
235 + });
236 +
237 + this.worker.onmessage = function (e) {
238 + var cb = _this.callbacks[e.data.command].pop();
239 + if (typeof cb == 'function') {
240 + cb(e.data.data);
241 + }
242 + };
243 + }
244 +
245 + _createClass(Recorder, [{
246 + key: 'record',
247 + value: function record() {
248 + this.recording = true;
249 + }
250 + }, {
251 + key: 'stop',
252 + value: function stop() {
253 + this.recording = false;
254 + }
255 + }, {
256 + key: 'clear',
257 + value: function clear() {
258 + this.worker.postMessage({ command: 'clear' });
259 + }
260 + }, {
261 + key: 'getBuffer',
262 + value: function getBuffer(cb) {
263 + cb = cb || this.config.callback;
264 + if (!cb) throw new Error('Callback not set');
265 +
266 + this.callbacks.getBuffer.push(cb);
267 +
268 + this.worker.postMessage({ command: 'getBuffer' });
269 + }
270 + }, {
271 + key: 'exportWAV',
272 + value: function exportWAV(cb, mimeType) {
273 + mimeType = mimeType || this.config.mimeType;
274 + cb = cb || this.config.callback;
275 + if (!cb) throw new Error('Callback not set');
276 +
277 + this.callbacks.exportWAV.push(cb);
278 +
279 + this.worker.postMessage({
280 + command: 'exportWAV',
281 + type: mimeType
282 + });
283 + }
284 + }], [{
285 + key: 'forceDownload',
286 + value: function forceDownload(blob, filename) {
287 + var url = (window.URL || window.webkitURL).createObjectURL(blob);
288 + var link = window.document.createElement('a');
289 + link.href = url;
290 + link.download = filename || 'output.wav';
291 + var click = document.createEvent("Event");
292 + click.initEvent("click", true, true);
293 + link.dispatchEvent(click);
294 + }
295 + }]);
296 +
297 + return Recorder;
298 + })();
299 +
300 + exports.default = Recorder;
301 +
302 + },{"inline-worker":3}],3:[function(require,module,exports){
303 + "use strict";
304 +
305 + module.exports = require("./inline-worker");
306 + },{"./inline-worker":4}],4:[function(require,module,exports){
307 + (function (global){
308 + "use strict";
309 +
310 + var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
311 +
312 + var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
313 +
314 + var WORKER_ENABLED = !!(global === global.window && global.URL && global.Blob && global.Worker);
315 +
316 + var InlineWorker = (function () {
317 + function InlineWorker(func, self) {
318 + var _this = this;
319 +
320 + _classCallCheck(this, InlineWorker);
321 +
322 + if (WORKER_ENABLED) {
323 + var functionBody = func.toString().trim().match(/^function\s*\w*\s*\([\w\s,]*\)\s*{([\w\W]*?)}$/)[1];
324 + var url = global.URL.createObjectURL(new global.Blob([functionBody], { type: "text/javascript" }));
325 +
326 + return new global.Worker(url);
327 + }
328 +
329 + this.self = self;
330 + this.self.postMessage = function (data) {
331 + setTimeout(function () {
332 + _this.onmessage({ data: data });
333 + }, 0);
334 + };
335 +
336 + setTimeout(function () {
337 + func.call(self);
338 + }, 0);
339 + }
340 +
341 + _createClass(InlineWorker, {
342 + postMessage: {
343 + value: function postMessage(data) {
344 + var _this = this;
345 +
346 + setTimeout(function () {
347 + _this.self.onmessage({ data: data });
348 + }, 0);
349 + }
350 + }
351 + });
352 +
353 + return InlineWorker;
354 + })();
355 +
356 + module.exports = InlineWorker;
357 + }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
358 + },{}]},{},[1])(1)
359 + });
360 + function aiomaticLoading(btn){
361 + btn.attr('disabled','disabled');
362 + if(!btn.find('spinner').length){
363 + btn.append('<span class="spinner"></span>');
364 + }
365 + btn.find('.spinner').css('visibility','unset');
366 + }
367 + function aiomaticRmLoading(btn){
368 + btn.removeAttr('disabled');
369 + btn.find('.spinner').remove();
370 + }
371 + jQuery('.aiomatic-audio-select').on('click', function (){
372 + var type = jQuery(this).val();
373 + jQuery('.aiomatic-audio-type').hide();
374 + jQuery('.aiomatic-audio-'+type).show();
375 + jQuery('.aiomatic-audio-'+type).css('visibility','visible');
376 + });
377 + jQuery('#button-start-converter').on('click', function (e){
378 + e.preventDefault();
379 + var type = jQuery('.aiomatic-audio-select:checked').val();
380 + var error_message = false;
381 + var response = 'text';
382 + if(type === 'upload'){
383 + if(jQuery('.aiomatic-audio-upload input')[0].files.length === 0){
384 + error_message = 'An audio file is mandatory.';
385 + }
386 + else{
387 + var file = jQuery('.aiomatic-audio-upload input')[0].files[0];
388 + if(jQuery.inArray(file.type, aiomatic_audio_mime_types) < 0){
389 + error_message = 'Accepted file types are mp3, mp4, mpeg, mpga, m4a, wav, or webm'
390 + }
391 + else if(file.size > 26214400){
392 + error_message = 'Audio file maximum 25MB';
393 + }
394 + }
395 + }
396 + if(!error_message && type === 'url' && jQuery('.aiomatic-audio-url input').val() === ''){
397 + error_message = 'Please insert audio URL';
398 + }
399 + if(!error_message && (response === 'post' || response === 'page') && jQuery('.aiomatic-audio-title').val() === ''){
400 + error_message = 'The title field is required'
401 + }
402 + if(type === 'record' && aiomaticAudioBlob.size > (10 * Math.pow(1024, 25))){
403 + error_message = 'Audio file maximum 25MB';
404 + }
405 + if(error_message){
406 + alert(error_message)
407 + }
408 + else{
409 + var data = new FormData(jQuery('.aiomatic-audio-form')[0]);
410 + data.append('action', 'aiomatic_audio_converter');
411 + data.append('nonce', aiomatic_audio_object.nonce);
412 + if(type === 'record'){
413 + data.append('recorded_audio', aiomaticAudioBlob, 'aiomatic_recording.wav');
414 + aiomaticUploadConverter(data);
415 + }
416 + else {
417 + aiomaticUploadConverter(data);
418 + }
419 + }
420 + return false;
421 + });
422 +
423 + function aiomaticUploadConverter(data){
424 + var btn = jQuery('#button-start-converter');
425 + jQuery.ajax({
426 + url: aiomatic_audio_object.ajax_url,
427 + data: data,
428 + type: 'POST',
429 + dataType: 'JSON',
430 + cache: false,
431 + contentType: false,
432 + processData: false,
433 + xhr: function () {
434 + var xhr = jQuery.ajaxSettings.xhr();
435 + xhr.upload.addEventListener("progress", function (evt) {
436 + if (evt.lengthComputable) {
437 + var percentComplete = evt.loaded / evt.total;
438 + aiomatic_progress.find('span').css('width', (Math.round(percentComplete * 100)) + '%');
439 + }
440 + }, false);
441 + return xhr;
442 + },
443 + beforeSend: function () {
444 + jQuery('.button-link-delete').show();
445 + jQuery('.button-link-delete').css('visibility','visible');
446 + aiomatic_progress.find('span').css('width', '0');
447 + aiomatic_progress.show();
448 + aiomatic_progress.css('visibility','visible');
449 + aiomaticLoading(btn);
450 + aiomatic_error_message.hide();
451 + aiomatic_upload_success.hide();
452 + },
453 + success: function (res) {
454 + if (res.status === 'success') {
455 + aiomaticRmLoading(btn);
456 + jQuery('.button-link-delete').hide();
457 + jQuery('.aiomatic-audio-upload input').val('');
458 + aiomatic_progress.hide();
459 + aiomatic_upload_success.show();
460 + aiomatic_upload_success.css('visibility','visible');
461 + jQuery('#aiomatic_audio_result').text(res.data);
462 + } else {
463 + aiomaticRmLoading(btn);
464 + jQuery('.button-link-delete').hide();
465 + aiomatic_progress.find('small').html('Error');
466 + aiomatic_progress.addClass('aiomatic_error');
467 + aiomatic_error_message.html(res.msg);
468 + aiomatic_error_message.show();
469 + aiomatic_error_message.css('visibility','visible');
470 + }
471 + },
472 + error: function () {
473 + aiomaticRmLoading(btn);
474 + jQuery('.button-link-delete').hide();
475 + aiomatic_progress.addClass('aiomatic_error');
476 + aiomatic_progress.find('small').html('Error');
477 + aiomatic_error_message.html('Please try again');
478 + aiomatic_error_message.show();
479 + aiomatic_error_message.css('visibility','visible');
480 + }
481 + });
482 + }
483 + var aiomatic_audio_mime_types = ['audio/mpeg','video/mp4','video/mpeg','audio/m4a','audio/wav','video/webm'];
484 + var aiomatic_progress = jQuery('.aiomatic_progress');
485 + var aiomatic_error_message = jQuery('.aiomatic-error-msg');
486 + var aiomatic_upload_success = jQuery('.aiomatic_upload_success');
487 + /*Start Record*/
488 + var aiomatic_btn_record = jQuery('#btn-audio-record');
489 + var aiomatic_btn_record_pause = jQuery('#btn-audio-record-pause');
490 + var aiomatic_btn_record_stop = jQuery('#btn-audio-record-stop');
491 + var aiomatic_audio_record_result = jQuery('#aiomatic-audio-record-result');
492 + var aiomaticStream;
493 + var aiomaticRec;
494 + var input;
495 + var aiomaticAudioContext = window.AudioContext || window.webkitAudioContext;
496 + var audioContext;
497 + var aiomaticAudioBlob;
498 + function aiomaticstartRecording() {
499 + var constraints = { audio: true, video:false }
500 + navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
501 + audioContext = new aiomaticAudioContext();
502 + aiomaticStream = stream;
503 + input = audioContext.createMediaStreamSource(stream);
504 + aiomaticRec = new Recorder(input,{numChannels:1});
505 + aiomaticRec.record();
506 + })
507 + }
508 + function aiomaticpauseRecording(){
509 + if (aiomaticRec.recording){
510 + aiomaticRec.stop();
511 + }
512 + else{
513 + aiomaticRec.record()
514 + }
515 + }
516 + function aiomaticstopRecording() {
517 + aiomaticRec.stop();
518 + aiomaticStream.getAudioTracks()[0].stop();
519 + aiomaticRec.exportWAV(aiomaticcreateDownloadLink);
520 + }
521 + function aiomaticcreateDownloadLink(blob) {
522 + aiomaticAudioBlob = blob;
523 + var url = URL.createObjectURL(blob);
524 + aiomatic_audio_record_result.html('<audio controls="true" src="'+url+'"></audio>');
525 + }
526 + aiomatic_btn_record_pause.on('click', function (){
527 + if(aiomatic_btn_record_pause.hasClass('aiomatic-paused')){
528 + aiomatic_btn_record_pause.html('Pause');
529 + aiomatic_btn_record_pause.removeClass('aiomatic-paused');
530 + }
531 + else{
532 + aiomatic_btn_record_pause.html('Continue');
533 + aiomatic_btn_record_pause.addClass('aiomatic-paused');
534 + }
535 + aiomaticpauseRecording();
536 + })
537 + aiomatic_btn_record.on('click', function (){
538 + aiomaticstartRecording();
539 + aiomatic_btn_record.hide();
540 + aiomatic_audio_record_result.empty();
541 + aiomatic_audio_record_result.hide();
542 + aiomatic_btn_record_pause.show();
543 + aiomatic_btn_record_pause.css('visibility','visible');
544 + aiomatic_btn_record_stop.show();
545 + aiomatic_btn_record_stop.css('visibility','visible');
546 + });
547 + aiomatic_btn_record_stop.on('click', function () {
548 + aiomaticstopRecording();
549 + aiomatic_btn_record_pause.hide();
550 + aiomatic_btn_record_stop.hide();
551 + aiomatic_btn_record.html('Re-Record');
552 + aiomatic_btn_record.show();
553 + aiomatic_btn_record.css('visibility','visible');
554 + aiomatic_audio_record_result.show();
555 + aiomatic_audio_record_result.css('visibility','visible');
556 + })
557 + jQuery('.aiomatic-audio-purpose').on('change', function (){
558 + if(jQuery(this).val() === 'translations'){
559 + jQuery('.aiomatic_languages').hide();
560 + }
561 + else{
562 + jQuery('.aiomatic_languages').show();
563 + jQuery('.aiomatic_languages').css('visibility','visible');
564 + }
565 + });
566 + var aiomaticAudioWorking = false;
567 + jQuery('.aiomatic-btn-cancel').on('click', function (){
568 + var btn = jQuery('#button-start-converter');
569 + jQuery(this).hide();
570 + aiomaticRmLoading(btn);
571 + aiomatic_progress.hide();
572 + if(aiomaticAudioWorking){
573 + aiomaticAudioWorking.abort();
574 + }
575 + });
576 + });