Is there a way to stringify a web audio node object (such as gain,biquadfilter
) to save/restore its settings?
var gainNode = audioCtx.createGain();
var str = JSON.stringify( gainNode );
console.log( str );
sadly str returns "{}"
Is there a way to stringify a web audio node object (such as gain,biquadfilter
) to save/restore its settings?
var gainNode = audioCtx.createGain();
var str = JSON.stringify( gainNode );
console.log( str );
sadly str returns "{}"
JSON.stringify()
only includes the object's own properties, and not properties inherited from the prototype chain. See How to stringify inherited objects to JSON? for more details.
One way to print all properties, including inherited ones:
function flatten(obj) {
var ret = {};
for (var i in obj) {
ret[i] = obj[i];
}
return ret;
}
var gainNode = audioCtx.createGain();
var str = JSON.stringify(flatten(gainNode));
console.log(str);
While JSON.stringify()
only includes own properties, the for (var i in obj)
syntax iterates over all properties, including inherited ones (except non-enumerable properties, but a GainNode
doesn't have those).