Is there an easier way to get a buffer's sampling rate in JS?

Roald Baudoux's icon

Hello,

My way to get a buffer's SR in JS is to have an info~ object related to this buffer in the patcher, give it a scripting name and connect its leftmost output to a pattr object. Then I send a bang to the info~ object from the JS and use the "getvalueof" on the pattr object.

let sr = 44100;

function get_sr() {
	let info_object = thispatcher.getnamed("testbuf_info");
        let val = info_object.message("bang");
	let grabber = thispatcher.getnamed("testbuf_sr");
	sr = grabber.getvalueof();
	post("Buffer's samplerate is "+sr+" fps\n");
}

This gives me what I want but isn't there an easier way to achieve this? Maybe some methods could be added to JS buffer's object in a future update?

Joshua Kit Clayton's icon

I've bumped up the incident count on this very old ticket for accessing samplerate directly from js, which I would like to see in a future update. There is, however, a very tricky way that you can get this implicitly in the other attributes that are available.

function getsamplerate(buf) {
    if(buf.length()*buf.framecount() == 0) {
        return undefined;
    } else {
        return Math.floor(buf.framecount() / buf.length()*1000.);
    }
}

post("samplerate: " + getsamplerate(buf) + "\n");
Roald Baudoux's icon

Thank you Joshua, this is a nice trick!