예제 #1
0
def test_schedule_context_manager(mocker, func, cm_func, args, kwargs):
    mocker.patch.object(transport, "send")

    osc = Oscillator()

    def clb(time):
        osc.start(time).stop(time + 1)

    eid = func(clb, *args, **kwargs)
    (expected, ) = transport.send.call_args[0]

    with cm_func(*args, **kwargs) as (time, cm_eid):
        osc.start(time).stop(time + 1)

    (actual, ) = transport.send.call_args[0]
    assert eid != cm_eid
    actual["id"] = expected["id"]

    assert actual == expected

    with pytest.raises(RuntimeError, match=r".*used outside of its context."):
        time.items
예제 #2
0
def test_pattern_callback(mocker):
    osc = Oscillator()

    def clb(time, value):
        osc.frequency.exp_ramp_to(value.note, 0.2, start_time=time)

    pattern = Pattern(values=["C2", "D4", "E5", "A6"])
    mocker.patch.object(pattern, "send")
    pattern.callback = clb

    expected = {
        "event":
        "set_callback",
        "op":
        "",
        "items": [{
            "method": "exponentialRampTo",
            "callee": osc.frequency.model_id,
            "args": {
                "value": {
                    "value": "value.note",
                    "eval": True
                },
                "ramp_time": {
                    "value": 0.2,
                    "eval": False
                },
                "start_time": {
                    "value": "time",
                    "eval": True
                },
            },
            "arg_keys": ["value", "ramp_time", "start_time"],
        }],
    }

    pattern.send.assert_called_with(expected)

    assert pattern.values == ["C2", "D4", "E5", "A6"]
예제 #3
0
def test_event_callback(mocker):
    osc = Oscillator()

    def clb(time, value):
        osc.frequency.exp_ramp_to(value, 0.2, start_time=time)

    event = Event(value="C4")
    mocker.patch.object(event, "send")
    event.callback = clb

    expected = {
        "event":
        "set_callback",
        "op":
        "",
        "items": [{
            "method": "exponentialRampTo",
            "callee": osc.frequency.model_id,
            "args": {
                "value": {
                    "value": "value",
                    "eval": True
                },
                "ramp_time": {
                    "value": 0.2,
                    "eval": False
                },
                "start_time": {
                    "value": "time",
                    "eval": True
                },
            },
            "arg_keys": ["value", "ramp_time", "start_time"],
        }],
    }

    event.send.assert_called_with(expected)

    assert event.value == "C4"
예제 #4
0
def test_sequence_callback(mocker):
    osc = Oscillator()

    def clb(time, value):
        osc.frequency.exp_ramp_to(value.note, 0.2, start_time=time)

    seq = Sequence(events=["C4", ["E4", "D4", "E4"], "G4", ["A4", "G4"]])
    mocker.patch.object(seq, "send")
    seq.callback = clb

    expected = {
        "event":
        "set_callback",
        "op":
        "",
        "items": [{
            "method": "exponentialRampTo",
            "callee": osc.frequency.model_id,
            "args": {
                "value": {
                    "value": "value.note",
                    "eval": True
                },
                "ramp_time": {
                    "value": 0.2,
                    "eval": False
                },
                "start_time": {
                    "value": "time",
                    "eval": True
                },
            },
            "arg_keys": ["value", "ramp_time", "start_time"],
        }],
    }

    seq.send.assert_called_with(expected)

    assert seq.events == ["C4", ["E4", "D4", "E4"], "G4", ["A4", "G4"]]
예제 #5
0
def test_transport_schedule(mocker, op, func, expected_id, args, kwargs):
    mocker.patch.object(transport, "send")

    osc = Oscillator()

    def clb(time):
        osc.start(time).stop(time + 1)

    eid = func(clb, *args.values(), **kwargs)
    assert eid == expected_id

    expected = {
        "event":
        "schedule",
        "op":
        op,
        "id":
        expected_id,
        "items": [
            {
                "method": "start",
                "callee": osc.model_id,
                "args": {
                    "time": {
                        "value": "time",
                        "eval": True
                    },
                    "offset": {
                        "value": None,
                        "eval": False
                    },
                    "duration": {
                        "value": None,
                        "eval": False
                    },
                },
                "arg_keys": ["time", "offset", "duration"],
            },
            {
                "method": "stop",
                "callee": osc.model_id,
                "args": {
                    "time": {
                        "value": "time + 1",
                        "eval": True
                    }
                },
                "arg_keys": ["time"],
            },
        ],
    }
    expected.update(args)
    expected.update(kwargs)

    transport.send.assert_called_with(expected)

    if op != "once":
        t = transport.clear(eid)
        assert t is transport
        transport.send.assert_called_with({"event": "clear", "id": eid})

    with pytest.raises(ValueError, match=".*event ID not found.*"):
        transport.clear(eid)
예제 #6
0
def test_part_callback(mocker):
    osc = Oscillator()

    def clb(time, value):
        osc.frequency.exp_ramp_to(value.note, 0.2, start_time=time)

    part = Part(events=[
        {
            "time": "0:0",
            "note": "C3"
        },
        Note(time="0:2", note="D3", velocity=0.5),
    ])
    mocker.patch.object(part, "send")
    part.callback = clb

    expected = {
        "event":
        "set_callback",
        "op":
        "",
        "items": [{
            "method": "exponentialRampTo",
            "callee": osc.frequency.model_id,
            "args": {
                "value": {
                    "value": "value.note",
                    "eval": True
                },
                "ramp_time": {
                    "value": 0.2,
                    "eval": False
                },
                "start_time": {
                    "value": "time",
                    "eval": True
                },
            },
            "arg_keys": ["value", "ramp_time", "start_time"],
        }],
    }

    part.send.assert_called_with(expected)

    assert part._events == [
        {
            "time": "0:0",
            "note": "C3",
            "velocity": 1,
            "duration": 0.1
        },
        {
            "time": "0:2",
            "note": "D3",
            "velocity": 0.5,
            "duration": 0.1
        },
    ]

    with pytest.raises(ValueError,
                       match="cannot interpret this value as a Note"):
        Part(callback=clb, events=["invalid"])
예제 #7
0
 def __init__(self):
     self.oscillator = Oscillator()
     self.envelope = AmplitudeEnvelope()
     super().__init__()