-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
135 lines (125 loc) · 3.64 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title></title>
</head>
<script type="text/javascript">
// csound is the Csound engine object (null as we start)
let csound = null;
let filename = "audio.ogg"
// instrument on/off state
let isOn = false;
// CSD file name
const csd = './hommage.csd'
// this is the JS function to start Csound
// and resume performance if needed
async function start() {
// if the Csound object is not initialised
if(csound == null) {
// import the Csound method from csound.js
const { Csound } = await import('https://www.unpkg.com/@csound/[email protected]/dist/csound.js');
// Csound engine object running on a worker thread
csound = await Csound({useWorker: true});
// copy the CSD file to the Csound local filesystem
await copyUrlToLocal(csd,csd)
// compile the code in the CSD file
await csound.compileCsd(csd)
// set the output file name
await csound.setOption("-o"+filename);
// set ogg encoding
await csound.setOption("--ogg");
// handle Csound messages
await csound.on("message", handleMessage);
// start the engine
await csound.start();
// on render end, call finish()
await csound.on("renderEnded", finish);
}
}
let sf = null;
// this is called on render end
async function finish() {
// stop Csound and close output file
await csound.cleanup();
// copy output file and get a URL
sf = await copyUrlFromLocal(filename, 'audio/ogg');
// notify the console
handleMessage("Complete: " + filename + " ready");
// enable the download button
document.getElementById('download').disabled = false;
// enable the open button
document.getElementById('open').disabled = false;
}
// this is called by the download button
async function download() {
// create an anchor element
let a = document.createElement('a');
// append it to html body
document.body.appendChild(a);
// set the anchor URL
a.href = sf;
// set the download name
a.download = filename;
// click on the element
a.click();
}
// this is called by the open button
async function openf() {
// create an anchor element
let a = document.createElement('a');
// append it to html body
document.body.appendChild(a);
// set the anchor URL
a.href = sf;
// open in a different tab
a.target = "_blank";
// click on the element
a.click();
}
// copy file from local and return a URL for it
async function copyUrlFromLocal(src,t) {
// get the file as a Uint8Array
let data = await csound.fs.readFile(src);
// create a data blob
let destfile = new Blob([data.buffer], { type: t});
// create a URL for it
return window.URL.createObjectURL(destfile);
}
async function copyUrlToLocal(src, dest) {
// fetch the file
let srcfile = await fetch(src, {cache: "no-store"})
// get the file data as an array
let dat = await srcfile.arrayBuffer();
// write the data as a new file in the filesystem
await csound.fs.writeFile(dest, new Uint8Array(dat));
}
let count = 0;
function handleMessage(message) {
// get the display element (called console in the page)
let element = document.getElementById('console');
// add the message to HTML content (plus a newline)
element.innerHTML += message + '\n';
// focus on bottom, new messages make the display scroll down
element.scrollTop = 99999;
// clear display every 1000 lines
if(count == 1000) {
count = 0;
element.innerHTML == "";
}
count += 1;
};
</script>
<body onload="start()">
<H1>Render</H1>
<input type="button" id="open" onclick="openf()" value="open" disabled>
<input type="button" id="download" onclick="download()"
value="download" disabled>
</input>
</p>
<p>
<textarea class="console" cols="80" rows="20" id="console">
</textarea>
<p>
<hr>
<p><a href="./readme.html">README</a></p>
<!-- hhmts start -->Last modified: Sat Sep 28 18:24:35 IST 2024 <!-- hhmts end -->
</body> </html>