xxxxxxxxxx
104
var csoundLoaded = false;
let csound;
let csdCode = `
<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
gisample ftgen 1, 0, 0, 1, "TEST.WAV", 0, 0, 0
instr 1
if ftchnls(p1) == 1 then
asigl loscil 0.5, 1, p1, 1, 0
asigr = asigl
elseif ftchnls(p1) == 2 then
asigl, asigr loscil 0.5, 1, p1, 1, 0
endif
; asigr diskin2 "TEST.WAV", 1
asigl = asigr
outs asigr, asigl
endin
</CsInstruments>
<CsScore>
;f0 z
</CsScore>
</CsoundSynthesizer>
`
//imports files for use with Csound
// function CopyUrlToLocal(name, callback = null) {
// var xmlHttpRequest = new XMLHttpRequest();
// xmlHttpRequest.onload = function() {
// var data = new Uint8Array(xmlHttpRequest.response);
// csound.writeToFS(name, data);
// };
// xmlHttpRequest.open("get", name, true);
// xmlHttpRequest.responseType = "arraybuffer";
// xmlHttpRequest.send(null);
// }
//Steven's version
function CopyUrlToLocal(name, file, callback = null) {
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.onload = function() {
var data = new Uint8Array(xmlHttpRequest.response);
csound.writeToFS(name, data);
};
xmlHttpRequest.open("get", file, true);
xmlHttpRequest.responseType = "arraybuffer";
xmlHttpRequest.send(null);
}
async function loadResources(csound, filesArray) {
for (let i = 0; i < filesArray.length; i++) {
const fileUrl = "TEST.WAV"//filesArray[i];
const serverUrl = filesArray[i];//`${process.env.PUBLIC_URL}/${fileUrl}`;
// console.log(serverUrl);
const f = await fetch(serverUrl);
const fName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
// const path = `/${dirToSave}/${fName}`;
const path = `${fName}`;
const buffer = await f.arrayBuffer();
// console.log(path, buffer);
await csound.writeToFS(path, buffer);
}
return true;
}
async function startCsound() {
csound = new CsoundObj();
//use this code to load a .csd file and have Csound compile and start it
// fetch('looper.txt').then((response) => {
// response.arrayBuffer().then((buf) => {
// csound.writeToFS('looper.txt', buf);
// csound.compileCSD('looper.txt');
// CopyUrlToLocal("myFile.WAV", "HC75.WAV");
const resources = ["HC75.WAV"];
console.log(resources);
await loadResources(csound, resources);
csound.compileCSD(csdCode);
csound.start();
csound.readScore("f0 z");
csoundLoaded = true;
// })
// })
//import the four audio samples included with this project
// CopyUrlToLocal("HC75.WAV","HC75.WAV");
}
CsoundObj.initialize().then(() => {
startCsound();
});