From efc50fe5714e0bc42f257eaaa8680163c11a4649 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Tue, 9 Jan 2024 21:13:33 +0000 Subject: [PATCH] PatchSpec: Round json output to 8 decimal places --- source/lib/json11/json11.cpp | 3 ++- tests/test_patch.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/source/lib/json11/json11.cpp b/source/lib/json11/json11.cpp index 3e5af5d4..456822c2 100644 --- a/source/lib/json11/json11.cpp +++ b/source/lib/json11/json11.cpp @@ -62,7 +62,8 @@ static void dump(double value, string &out) if (std::isfinite(value)) { char buf[32]; - snprintf(buf, sizeof buf, "%.17g", value); + // DJJ: Modified to round doubles to 8 d.p. precision. + snprintf(buf, sizeof buf, "%.8g", value); out += buf; } else diff --git a/tests/test_patch.py b/tests/test_patch.py index d2b4e948..29511e7a 100644 --- a/tests/test_patch.py +++ b/tests/test_patch.py @@ -1,5 +1,5 @@ from signalflow import PatchSpec, Patch, Buffer, BufferPlayer -from signalflow import Multiply, SineOscillator, ASREnvelope, SquareOscillator, Sum, Add, AzimuthPanner +from signalflow import Multiply, SineOscillator, ASREnvelope, SquareOscillator, Sum, Add, AzimuthPanner, StereoPanner from . import graph import numpy as np import json @@ -89,3 +89,16 @@ def test_patch_property_serialisation_json(graph): spec_new.from_json(spec_json) patch = Patch(spec_new) assert patch.output.num_output_channels == 3 + +def test_patch_to_json(graph): + class TestPatch (Patch): + def __init__(self): + super().__init__() + sin = SineOscillator(440) * ASREnvelope() + stereo = StereoPanner(sin) + self.set_output(stereo) + + patch = TestPatch() + spec = patch.to_spec() + json = spec.to_json() + assert json == '{"buffer_inputs": [], "inputs": [], "name": "", "nodes": [{"id": 4, "inputs": {"attack": 0.1, "curve": 1, "release": 0.1, "sustain": 0.5}, "node": "asr-envelope"}, {"id": 2, "inputs": {"frequency": 440}, "node": "sine"}, {"id": 1, "inputs": {"input0": {"id": 2}, "input1": {"id": 4}}, "node": "multiply"}, {"id": 0, "inputs": {"input": {"id": 1}, "pan": 0}, "is_output": true, "node": "stereo-panner"}]}' \ No newline at end of file