[Csnd] Csound wasm hrtfmove loading files

Hi!

Inspired by the work on Csound-wasm and Vicor’s examples on https://github.com/vlazzarini/vanilla/ I wanted to try a web solution of binaural audio and hrtfmove opcode.

Following the examples I was able to load an audio file and play it with diskin2 but for the hrtf analysis files Csound complained:

cannot load hrtf-44100-left.dat, or SADIR undefined

hrtf-44100-left.dat and -right.dat are in the same folder as the html file and csd.

I tried also setting SADIR:

–env:SADIR=“./”

But not sure what it should actually be.

Is it supposed to work? How can I debug this problem? Can I check somehow which files are actually loaded to the filesystem?

I am using csound.js v6.18.1 form Victor’s vanilla repo.

The code for loading the files that I used:

const csd = “test.csd”;
let filename = “sound.wav”;
const hrftL = “hrtf-44100-left.dat”, hrftR = “hrtf-44100-right.dat”;

There’s a difference between diskin and hrtfmove. Diskin opens files for reading and reads blocks from it,
whereas hrtfmove uses memory files, where the whole file is put into memory and data is read from there.

Looking at Engine/memfiles.c: Load_File_() , I see three possibilities for failure:

1) The file cannot be opened (e.g. it is not found, or permissions are not given).

2) The file length is zero: fseek and ftell are used to find how much data is there, and if
it is less than 1 byte, it fails.

3) The file reading (fread) fails to read all the data.

Any of these can lead to the error message you got (the code does not distinguish between them).

Given that files are being copied OK, I would think 1 and possibly 2 are unlikely to be the cause.

In any case, it seems that there may be a problem loading memory files. You can try to see if Csound
can open the dat files using another open (fopen? diskin perhaps) and if that is the case, then you know
the problem is not with copying the data into the local fs.

Hi!

Thanks for the suggestions!

The results are strange. Using a simple test instument:

instr 1

Sfile = “nofile.dat” ; “hrtf-44100-left.dat”

prints Sfile

ihandle fiopen Sfile, 3

print ihandle

endin

on desktop I get ann error as expected:
nofile.datINIT ERROR in instr 1 (opcode fiopen) line 21: error opening file ‘nofile.dat’
(and with Sfile = “hrtf-44100-left.dat” it opens as expected)

But on the web it reports no error on both cases, even if there is no file with that name. So I don’t know…

Another test:

instr 1

kvalue readk “hrtf-44100-left.dat”, 6, 0.001

printk2 kvalue

endin

Prints the same values on desktop and on the web browser, so the file is copied to memory correctly, just hrtfmove2 cannot read it.

I attach the csd for any case.

Tarmo

Kontakt Victor Lazzarini (<Victor.Lazzarini@mu.ie>) kirjutas kuupäeval T, 29. november 2022 kell 10:43:

There’s a difference between diskin and hrtfmove. Diskin opens files for reading and reads blocks from it,
whereas hrtfmove uses memory files, where the whole file is put into memory and data is read from there.

Looking at Engine/memfiles.c: Load_File_() , I see three possibilities for failure:

  1. The file cannot be opened (e.g. it is not found, or permissions are not given).

  2. The file length is zero: fseek and ftell are used to find how much data is there, and if
    it is less than 1 byte, it fails.

  3. The file reading (fread) fails to read all the data.

Any of these can lead to the error message you got (the code does not distinguish between them).

Given that files are being copied OK, I would think 1 and possibly 2 are unlikely to be the cause.

In any case, it seems that there may be a problem loading memory files. You can try to see if Csound
can open the dat files using another open (fopen? diskin perhaps) and if that is the case, then you know
the problem is not with copying the data into the local fs.

========================
Prof. Victor Lazzarini
Maynooth University
Ireland

WARNINGThis email originated from outside of Maynooth University’s Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
Hi!

Inspired by the work on Csound-wasm and Vicor’s examples on https://github.com/vlazzarini/vanilla/ I wanted to try a web solution of binaural audio and hrtfmove opcode.

Following the examples I was able to load an audio file and play it with diskin2 but for the hrtf analysis files Csound complained:

cannot load hrtf-44100-left.dat, or SADIR undefined

hrtf-44100-left.dat and -right.dat are in the same folder as the html file and csd.

I tried also setting SADIR:

–env:SADIR=“./”

But not sure what it should actually be.

Is it supposed to work? How can I debug this problem? Can I check somehow which files are actually loaded to the filesystem?

I am using csound.js v6.18.1 form Victor’s vanilla repo.

The code for loading the files that I used:

const csd = “test.csd”;
let filename = “sound.wav”;
const hrftL = “hrtf-44100-left.dat”, hrftR = “hrtf-44100-right.dat”;

await copyUrlToLocal(csd, csd)
await copyUrlToLocal(filename, filename);
await copyUrlToLocal(hrftL, hrftL);
await copyUrlToLocal(hrftR, hrftR);

async function copyUrlToLocal(src, dest) {
let srcfile = await fetch(src, {cache: “no-store”})
let dat = await srcfile.arrayBuffer();
await csound.fs.writeFile(dest, new Uint8Array(dat));
console.log("Finished loading: ", dest);
}

Thanks!
tarmo
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

agemove.csd (816 Bytes)

Thanks

Now that you confirmed with readk that the file is found and opened, we can confirm the problem is in
memory files, because readk uses csound->fileOpen2() and reads data directly from file.

It’s not hrtfmove that can’t read it. The message comes from memfiles.c, so the problem is there.

Other opcodes that use memory files in the same way are the atsa opcodes. They should have
the same problem.

Perhaps Hlodver or Steven could have a look to see what’s going on. I would need a local build of
wasm, which I don’t have the tools for.

I should note that the hrtf opcodes used to work with the Emscripten build of Csound WASM, we used
them here in 2019 for a research project, so this is some form of regression.

My first instinct was that it must use a different file open handler. I just never bumped into this issue until now. I don’t think it will be too difficult to fix. Sounds like it will need to be fixed on binary level and not with the bindings. I’ll update here Tarmo when I have a new patch release.

It doesn’t look different to me: it uses fopen() and fread(). See memfiles.c:Load_File_()

It’s fixed in @csound/browser 6.18.4

Splendid!

I can confirm that hrtfmove works now with @csound/browser 6.18.4.

Thank you, Hlödver for so quick fix!

tarmo

Kontakt Hlöðver Sigurðsson (<hlolli@gmail.com>) kirjutas kuupäeval T, 29. november 2022 kell 18:06:

It’s fixed in @csound/browser 6.18.4

Hi!

Inspired by the work on Csound-wasm and Vicor’s examples on https://github.com/vlazzarini/vanilla/ I wanted to try a web solution of binaural audio and hrtfmove opcode.

Following the examples I was able to load an audio file and play it with diskin2 but for the hrtf analysis files Csound complained:

cannot load hrtf-44100-left.dat, or SADIR undefined

hrtf-44100-left.dat and -right.dat are in the same folder as the html file and csd.

I tried also setting SADIR:

–env:SADIR=“./”

But not sure what it should actually be.

Is it supposed to work? How can I debug this problem? Can I check somehow which files are actually loaded to the filesystem?

I am using csound.js v6.18.1 form Victor’s vanilla repo.

The code for loading the files that I used:

const csd = “test.csd”;
let filename = “sound.wav”;
const hrftL = “hrtf-44100-left.dat”, hrftR = “hrtf-44100-right.dat”;


await copyUrlToLocal(csd, csd)

await copyUrlToLocal(filename, filename);
await copyUrlToLocal(hrftL, hrftL);
await copyUrlToLocal(hrftR, hrftR);

async function copyUrlToLocal(src, dest) {
let srcfile = await fetch(src, {cache: “no-store”})
let dat = await srcfile.arrayBuffer();
await csound.fs.writeFile(dest, new Uint8Array(dat));
console.log("Finished loading: ", dest);
}

Thanks!
tarmo

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Great, thanks Hlodver!