xxxxxxxxxx
117
const csoundjs = 'https://cdn.jsdelivr.net/npm/@csound/browser@6.18.5/dist/csound.js';
let csound = null;
let code = `
0dbfs = 1
chn_k "pitch",3
chn_k "time",3
chn_k "amp",3
instr 1
ifw ftgen 0,0,p4*sr,2,0
ain inch 1
aph phasor sr/(ftlen(ifw))
tablew ain,aph,ifw,1
ktime chnget "time"
kpitch chnget "pitch"
kamp chnget "amp"
kamp port kamp, 0.1
asig temposcal ktime,kamp,kpitch,ifw,1
out asig
endin
schedule(1,0,-1,2)
`;
async function start() {
// if the Csound object is not initialised
if(csound == null) {
// import the Csound method from csound.js
const { Csound } = await import(csoundjs);
// create a Csound engine object
csound = await Csound();
// set realtime audio (dac) output
await csound.setOption("-odac");
await csound.setOption("-iadc");
// compile csound code
await csound.compileOrc(code);
// start the engine
await csound.start();
}
}
const width = 400;
const height = 400;
const mpoint = {
on: false,
x: 0,
y: 0
};
let txt = null;
function setup() {
start().then( () => {
const cnv = createCanvas(width, height);
cnv.mousePressed(pressed);
cnv.mouseReleased(released);
cnv.mouseMoved(moved);
});
}
function moved() {
if(mpoint.on){
mpoint.x = mouseX;
mpoint.y = mouseY;
const t = (4.0*mpoint.x/width)-2;
const p = -(4.0*mouseY/height)+2;
csound.setControlChannel('time', t).then(
() =>{ csound.setControlChannel('pitch', p);});
txt = "timescale: " + t.toFixed(3) + "\npitchscale: " + p.toFixed(3);
}
}
function pressed() {
mpoint.x = mouseX;
mpoint.y = mouseY;
mpoint.on = true;
const t = (4.0*mpoint.x/width)-2;
const p = -(4.0*mouseY/height)+2;
csound.setControlChannel('amp',1).then(() => {
csound.setControlChannel('time', t).then(
() =>{ csound.setControlChannel('pitch', p);});});
txt = "timescale: " + t.toFixed(3) + "\npitchscale: " + p.toFixed(3);
}
function released() {
mpoint.on = false;
txt = null;
if(csound){
csound.setControlChannel('amp',0);
}
}
function draw() {
background('gold');
if(mpoint.on){
strokeWeight(1);
stroke(0);
line(0, mpoint.y, width, mpoint.y);
line(mpoint.x,0,mpoint.x,height);
if(txt) {
let ofx = 5;
let ofy = 12;
if(mpoint.x > width - 100) ofx = -100;
if(mpoint.y > height - 24) ofy = -24;
text(txt, mpoint.x+ofx,mpoint.y+ofy);
}
}
strokeWeight(5);
stroke('green');
line(0, height/2, width, height/2);
line(width/2,0,width/2,height);
}