brickworks/examples/common/ios/index.html
Stefano D'Angelo 0a586e9764 ios ffs
2023-08-04 15:08:46 +02:00

176 lines
4.3 KiB
HTML

<!DOCTYPE html>
<!--
Brickworks
Copyright (C) 2023 Orastron Srl unipersonale
Brickworks is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
Brickworks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Brickworks. If not, see <http://www.gnu.org/licenses/>.
File author: Stefano D'Angelo, Paolo Marrone
-->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript">
function request(data) {
return window.webkit.messageHandlers.listener.postMessage(data);
}
function needAudioPermission() {
return request({ name: "needAudioPermission" });
}
function requestAudioPermission() {
return request({ name: "requestAudioPermission" });
}
function audioStart() {
return request({ name: "audioStart" });
}
function audioStop() {
return request({ name: "audioStop" });
}
function setParameter(index, value) {
return request({ name: "setParameter", index: index, value: value });
}
function getParameter(index) {
return request({ name: "getParameter", index: index });
}
var hasAudioPermission = true;
var audioStarted = false;
var topButtonElem;
var outParamInterval;
function gotAudioPermission() {
hasAudioPermission = true;
topButtonElem.value = "START";
}
window.onload = async function () {
topButtonElem = document.getElementById("topButton");
var paramsElem = document.getElementById("params");
for (var i = 0; i < buses.length; i++)
if (!buses[i].output) {
hasAudioPermission = !await needAudioPermission();
break;
}
topButtonElem.value = hasAudioPermission ? "START" : "INIT";
topButtonElem.addEventListener("click", async function () {
if (hasAudioPermission) {
if (audioStarted) {
clearInterval(outParamInterval);
await audioStop();
paramsElem.innerHTML = "";
topButtonElem.value = "START";
audioStarted = false;
} else {
if (await audioStart()) {
for (var i = 0; i < parameters.length; i++) {
var div = document.createElement("div");
paramsElem.appendChild(div);
var label = document.createElement("label");
label.innerText = parameters[i].name;
div.appendChild(label);
div.appendChild(document.createElement("br"));
var range = document.createElement("input");
range.classList.add("range");
range.setAttribute("type", "range");
range.setAttribute("id", "p" + i);
range.setAttribute("min", 0);
range.setAttribute("max", 1);
range.setAttribute("step", parameters[i].step ? 1 / parameters[i].step : "any");
range.value = parameters[i].defaultValue;
if (parameters[i].output)
range.setAttribute("readonly", "readonly");
else {
let index = i;
range.addEventListener("input",
async function (ev) {
await setParameter(index, parseFloat(ev.target.value));
});
}
div.appendChild(range);
}
outParamInterval = setInterval(
async function () {
for (var i = 0; i < parameters.length; i++)
if (parameters[i].output)
document.getElementById("p" + i).value = await getParameter(i);
}, 50);
topButtonElem.value = "STOP";
audioStarted = true;
} else
alert("Could not start audio");
}
} else {
await requestAudioPermission();
var interval = setInterval(
async function () {
if (!await needAudioPermission()) {
gotAudioPermission();
clearInterval(interval);
}
}, 50);
}
});
};
</script>
<style>
* {
margin: 0;
padding: 0;
user-select: none;
}
body {
margin: 1em;
}
#topButton {
width: 100%;
border: 0;
background-color: #04aa6d;
color: white;
padding: 0.5em;
text-align: center;
margin-bottom: 1em;
padding: 1em;
}
.range {
width: 90%;
}
</style>
</head>
<body>
<input id="topButton" type="button">
<div id="params"></div>
</body>
</html>