xxxxxxxxxx
76
const orcCode = `
; Initialize the global variables.
; Initialize the global variables.
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
kFreq randi 1000, 1, 1
kHrms randh 15, 1, 1
a1 buzz 1, kFreq, abs(kHrms)+1, -1
chnset abs(kFreq)/1000, "freq"
outs a1*.1, a1*.1
endin
`;
let csound;
let isPlaying = false;
let numberOfPlays = 0;
let fft = 0, freq = 0;
function startCsound() {
console.log("loading... loaded!");
csound = new CsoundObj();
fft = csound.audioContext.createAnalyser();
csound.getNode().connect(fft);
csound.setOption('-m0d');
}
CsoundObj.initialize().then(() => {
startCsound();
});
function setup() {
createCanvas(400, 400);
}
function draw() {
background(0, 0, 0, 10);
if (isPlaying === false) {
textAlign(CENTER, CENTER);
fill(255);
text("Click to start audio", width / 2, height / 2);
}
let freqData = new Uint8Array(fft.frequencyBinCount);
fft.getByteFrequencyData(freqData);
noStroke();
if(csound) {
csound.requestControlChannel("freq", () => freq = csound.getControlChannel("freq"));
}
fill(random(100, 255)*freq, random(100, 255)*freq, random(100, 255)*freq);
for (let i = 0; i < freqData.length; i++) {
let x = map(i, 0, freqData.length / 2, 0, width);
let h = -height + map(freqData[i], 0, 255, height, 0);
rect(x, height, width / freqData.length, h)
}
}
function mousePressed() {
if (csound && isPlaying === false) {
csound.compileOrc(orcCode);
csound.start();
csound.audioContext.resume();
csound.readScore('i1 0 3600');
isPlaying = true;
}
else{
csound.stop();
csound.reset();
isPlaying = false;
}
}