xxxxxxxxxx
106
let decoder;
let latentRange = [-3, 3];
let numSteps = 10; // one step is 2048 samples (~ 42ms @ 44.1 kHz)
let curStep = 0; // 0..numSteps-1
let latents = [];
let selSlider;
async function preload() {
decoder = await ort.InferenceSession.create(
"https://sukzessiv.net/~gohai/rave/organ_pca8_dec.onnx"
//"https://sukzessiv.net/~gohai/rave/apollo_pca8_dec.onnx"
);
}
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 8; i++) {
for (let step = 0; step < numSteps; step++) {
latents.push(0);
}
}
}
function draw() {
background(220);
let oldStep = curStep;
curStep = floor(millis() / (1000 / (44100 / 2048))) % numSteps;
if (curStep < oldStep) {
background(255, 0, 0);
requestAudio();
}
if (selSlider != null) {
updateSlider(selSlider, mouseY);
}
for (let i = 0; i < 8; i++) {
drawSlider(i);
}
}
async function requestAudio() {
if (!audioCtx) return;
let inputTensor = new ort.Tensor("float32", latents, [1, 8, numSteps]);
let audio_out = (await decoder.run({ latent: inputTensor })).audio_out;
audio_out = tensorToBuffer(audio_out);
playBuffer(audio_out);
}
function drawSlider(n) {
push();
noStroke();
translate(width / 16 + (n * width) / 8, height / 2);
let w = width / 16;
let h = height / 3;
rect(-w / 2, -h / 2, w, h);
stroke(0, 0, 255);
let y = map(
latents[n * numSteps + curStep],
latentRange[0],
latentRange[1],
h / 2,
-h / 2
);
line(-w / 2, y, w / 2, y);
pop();
}
function sliderFromCoords(x, y) {
if (y < height / 3 || y > (2 * height) / 3) {
return null;
}
for (let i = 0; i < 8; i++) {
if (
x >= width / 16 + (i * width) / 8 - width / 32 &&
x <= width / 16 + (i * width) / 8 + width / 32
) {
return i;
}
}
return null;
}
function updateSlider(slider, y) {
let val = map(
y,
height / 2 - height / 3 / 2,
height / 2 + height / 3 / 2,
latentRange[1],
latentRange[0]
);
val = constrain(val, latentRange[0], latentRange[1]);
latents[slider * numSteps + curStep] = val;
}
function mousePressed() {
selSlider = sliderFromCoords(mouseX, mouseY);
if (!audioCtx) {
enableAudioCtx();
}
}
function mouseReleased() {
selSlider = null;
}