Loading samples with Web Csound?

I’m starting to think we might need a sub category for web based questions :laughing:

@stevenyi, @hlolli sorry to ping you, but I can’t seem to play back samples using the same code I used in earlier versions of Csound wasm. The following sketch just can’t find the files I’m trying to load? Any ideas?

https://editor.p5js.org/rorywalsh/sketches/rHcZrmhDq

I’ve tried with XMLHttpRequest() and fetch() but no luck. You can probably spot straight away what I’m doing wrong?

That’d be great! I might be asking a few more questions on that topic…

This is what I’m getting. I can’t anwer for this version of cound hosted on @kunstmusik

Strange, although I should have said that you need to click the canvas to start the instrument. I get the following in the output console:

midi channel 64 using instr 1 
displays suppressed 
0dBFS level = 1.0 
orch now loaded 
audio buffered in 256 sample-frame blocks 
SECTION 1: 
Helloe[mINIT ERROR in instr 1 (opcode diskin2) line 10: e[mdiskin2: https://assets.editor.p5js.org/5dbbe32aa9d4bc00178410d4/441f7099-bc5a-443f-9ad2-5cc24e4cc10b.ogg: failed to open file (No Error.)e[m 
e[maSiren	diskin2	"https://assets.editor.p5js.org/5dbbe32aa9d4bc00178410d4/441f7099-bc5a-443f-9ad2-5cc24e4cc10b.ogg"	1	0	1	0	0	0	0	0	 
		   T  4.891 - note deleted.  i1 had 1 init errorse[m

There seems to be a bug in the filename for sure.

csound.writeToFS(name, data);

you pass in “siren.ogg” but perhaps it’s getting modified somewhere? The absolute url is coming from the csound logs, so it must be passed into csound incorrectly. Also there’s a chance this csound web module has the http request built in, not sure.

Looks to me that p5’s web editor is doing some magic text replacement, preprocessing and replacing file names in code with the URL where they are served. Debug tools show that when the siren.ogg was fetched it was requested with the URL of “https://assets.editor.p5js.org/5dbbe32aa9d4bc00178410d4/441f7099-bc5a-443f-9ad2-5cc24e4cc10b.ogg” rather than just siren.ogg.

I modified the code to write to a different file name than what I wanted to fetch and it worked here so it’s an issue with p5.js’s online editor. The code I used is:

const orcCode = `
; Initialize the global variables.
; Initialize the global variables.
ksmps = 32
nchnls = 2
0dbfs = 1

instr 1
prints “Hello”
aSiren, a2 diskin2 “myfile.ogg”, 1, 0, 1
outs aSiren, aSiren
endin
`;

let csound;
let isPlaying = false;

//imports files for use with Csound
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);
}

CsoundObj.initialize().then(() => {
console.log(“loading… loaded!”);
csound = new CsoundObj();
csound.setOption(’-m0d’);
csound.compileOrc(orcCode);
CopyUrlToLocal(“myfile.ogg”, “siren.ogg”);
csound.start();
});

function mousePressed() {
if (csound && isPlaying === false) {
csound.audioContext.resume();
csound.readScore(‘i1 0 3600’);
isPlaying = true;
} else {
csound.stop();
csound.reset();
isPlaying = false;
}
}

var waves = [];
var car;
var people;
//based on p5.js Web Editor

function setup() {
createCanvas(windowWidth, windowHeight);
}

function draw() {
background(0);

}

Wow, thanks Steven. I should have tried running the code locally first. My bad. :frowning:

And now I have a nice example of doppler shift to show my students, albeit a completely crude implementation! I updated it to use a proper doppler implementation curtesy of some code Victor posted to the list.

Source code here