Add "Play Audio" Operation
This commit is contained in:
parent
0420aa8edb
commit
dea6f875d3
@ -1065,6 +1065,31 @@ class Utils {
|
||||
}[token];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Turns a dataURI into a byte array.
|
||||
* Credit to sanddune (https://jsfiddle.net/uubnnr0w/380/)
|
||||
*
|
||||
* @param {String} dataURI
|
||||
* @returns {Array}
|
||||
*/
|
||||
static convertDataURIToBinary(dataURI) {
|
||||
const BASE64_MARKER = ";base64,";
|
||||
const base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
|
||||
const base64 = dataURI.substring(base64Index);
|
||||
|
||||
const raw = atob(base64);
|
||||
const rawLength = raw.length;
|
||||
|
||||
const array = new Uint8Array(new ArrayBuffer(rawLength));
|
||||
|
||||
for (let i = 0; i < rawLength; i++) {
|
||||
array[i] = raw.charCodeAt(i);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Utils;
|
||||
|
||||
@ -204,7 +204,8 @@
|
||||
"Escape string",
|
||||
"Unescape string",
|
||||
"Pseudo-Random Number Generator",
|
||||
"Sleep"
|
||||
"Sleep",
|
||||
"Play Audio"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
60
src/core/operations/PlayAudio.mjs
Normal file
60
src/core/operations/PlayAudio.mjs
Normal file
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* @author gchq77703 []
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Utils from "../Utils";
|
||||
|
||||
/**
|
||||
* Play Audio operation
|
||||
*/
|
||||
class PlayAudio extends Operation {
|
||||
|
||||
/**
|
||||
* PlayAudio constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Play Audio";
|
||||
this.module = "Default";
|
||||
this.description = "Plays an audio file from a base 64 encoded data URI. URI must contain a valid audio mediatype.";
|
||||
this.infoURL = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.presentType = "html";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Presents a given Data URI as an audio element.
|
||||
*
|
||||
* @param {String} input
|
||||
* @returns {String}
|
||||
*/
|
||||
present(input) {
|
||||
const type = input.split(";")[0].split(":")[1];
|
||||
const binary = Utils.convertDataURIToBinary(input);
|
||||
const blob = new Blob([binary], { type: type});
|
||||
const src = URL.createObjectURL(blob);
|
||||
|
||||
|
||||
return `<audio id="audio" controls>
|
||||
<source id="audioSource" src="${src}" type="${type}"/>
|
||||
</audio>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} input
|
||||
* @param {Object[]} args
|
||||
* @returns {String}
|
||||
*/
|
||||
run(input, args) {
|
||||
return input;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default PlayAudio;
|
||||
Loading…
x
Reference in New Issue
Block a user