Skip to content

Commit

Permalink
PatchSpec: Round json output to 8 decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Jan 9, 2024
1 parent b7d87ba commit efc50fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion source/lib/json11/json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion tests/test_patch.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"}]}'

0 comments on commit efc50fe

Please sign in to comment.