1

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 "{}"

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
  • What do you understand by "Stringify audio node object" ? What kind of result do you expect if not `{}` ? (I'm not specialist in this domain, but may help) – NatNgs Dec 20 '17 at 13:49
  • I think he refers to something that casts the object state into a unique string, like Java's toString() method. – StarShine Dec 20 '17 at 13:50
  • i'd expect something like:
    {gain: AudioParam, context: AudioContext, numberOfInputs: 1, numberOfOutputs: 1, channelCount: 2, …}
    – Pedro Infantilo Dec 20 '17 at 14:13

1 Answers1

0

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).

Thomas McGuire
  • 5,308
  • 26
  • 45