2023-01-04 20:28:36 +03:00
|
|
|
export const createDummyMediaStream = (audioContext: AudioContext) => {
|
|
|
|
const dummyOutputNode = audioContext.createMediaStreamDestination();
|
|
|
|
|
|
|
|
const gainNode = audioContext.createGain();
|
|
|
|
gainNode.gain.value = 0.0;
|
|
|
|
gainNode.connect(dummyOutputNode);
|
|
|
|
const oscillatorNode = audioContext.createOscillator();
|
|
|
|
oscillatorNode.frequency.value = 440;
|
|
|
|
oscillatorNode.connect(gainNode);
|
|
|
|
oscillatorNode.start();
|
|
|
|
return dummyOutputNode.stream;
|
|
|
|
};
|
2023-01-05 12:35:56 +03:00
|
|
|
|
|
|
|
export const fileSelector = async (regex: string) => {
|
|
|
|
const fileInput = document.createElement("input");
|
|
|
|
fileInput.type = "file";
|
|
|
|
const p = new Promise<File>((resolve, reject) => {
|
|
|
|
fileInput.onchange = (e) => {
|
|
|
|
if (e.target instanceof HTMLInputElement == false) {
|
|
|
|
console.log("invalid target!", e.target)
|
|
|
|
reject("invalid target")
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
const target = e.target as HTMLInputElement
|
|
|
|
if (!target.files || target.files.length == 0) {
|
|
|
|
reject("no file selected")
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (regex != "" && target.files[0].type.match(regex)) {
|
|
|
|
reject(`not target file type ${target.files[0].type}`);
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
resolve(target.files[0])
|
|
|
|
return null
|
|
|
|
};
|
|
|
|
fileInput.click();
|
|
|
|
});
|
|
|
|
return await p
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fileSelectorAsDataURL = async (regex: string) => {
|
|
|
|
const f = await fileSelector(regex)
|
|
|
|
if (!f) {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = await new Promise<string>((resolve) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => {
|
|
|
|
console.log("load data", reader.result as string);
|
|
|
|
resolve(reader.result as string);
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(f);
|
|
|
|
})
|
|
|
|
return url
|
|
|
|
}
|
2023-01-07 14:07:39 +03:00
|
|
|
|
|
|
|
|
2023-01-08 03:22:22 +03:00
|
|
|
export const validateUrl = (url: string) => {
|
|
|
|
|
2023-02-21 11:54:02 +03:00
|
|
|
if (url?.endsWith("/")) {
|
2023-01-08 03:22:22 +03:00
|
|
|
return url.substring(0, url.length - 1)
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|