예제 #1
0
    def test_wait(self):
        duration = 0.15
        tol = 0.05

        def check(synth):
            self.assertEqual(synth.is_playing, None)
            self.assertEqual(synth.started, True)
            self.assertEqual(synth.freed, False)
            self.assertEqual(synth.group, self.sc.server.default_group.nodeid)
            t_wait_for_notification = time.time()
            while not synth.is_playing:
                if time.time() - t_wait_for_notification > 0.15:
                    self.fail("Waiting for /n_go notification took too long.")
            self.assertEqual(synth.is_playing, True)
            self.assertEqual(synth.started, True)
            self.assertEqual(synth.freed, False)
            synth.wait(timeout=1)
            time_played = time.time() - t0
            self.assertEqual(synth.is_playing, False)
            self.assertEqual(synth.started, False)
            self.assertEqual(synth.freed, True)
            self.assertEqual(synth.group, None)
            self.assertLessEqual(time_played, duration + tol)
            self.assertGreaterEqual(time_played, duration - tol)

        t0 = time.time()
        with self.assertWarnsRegex(
            UserWarning, "SynthDesc 's1' is unknown", msg="SynthDesc seems to be known"
        ):
            s1_synth = Synth("s1", controls={"dur": duration, "amp": 0.0})
        check(s1_synth)
        t0 = time.time()
        s1_synth.new()
        check(s1_synth)
예제 #2
0
 def test_duplicate(self):
     self.assertNotIn("/s_new", self.sc.server.fails)
     with self.assertWarnsRegex(
         UserWarning, "SynthDesc 's2' is unknown", msg="SynthDesc seems to be known"
     ):
         synth1 = Synth("s2", controls={"amp": 0.0})
         wait_t0 = time.time()
         synth1.new(controls={"amp": 0.0})
         while not "/s_new" in self.sc.server.fails:
             self.assertLessEqual(time.time() - wait_t0, 0.5)
         self.assertEqual(self.sc.server.fails["/s_new"].get(), "duplicate node ID")
         synth1.free()
         synth1.wait(timeout=1)
         synth1.new({"amp": 0.0})
         synth1.free()
         synth1.wait(timeout=1)
         with self.assertRaises(Empty):
             self.sc.server.fails["/s_new"].get(timeout=0.5)
예제 #3
0
    def test_fast_wait(self):
        duration = 0.15
        tol = 0.05

        def check(synth):
            self.assertEqual(synth.is_playing, False)
            self.assertEqual(synth.started, False)
            self.assertEqual(synth.freed, True)
            time_played = time.time() - t0
            self.assertLessEqual(time_played, duration + tol)
            self.assertGreaterEqual(time_played, duration - tol)

        t0 = time.time()
        with self.assertWarnsRegex(
            UserWarning, "SynthDesc 's1' is unknown", msg="SynthDesc seems to be known"
        ):
            s1_synth = Synth("s1", controls={"dur": duration, "amp": 0.0})
        s1_synth.wait(timeout=1)
        check(s1_synth)

        t0 = time.time()
        s1_synth.new()
        s1_synth.wait(timeout=1)
        check(s1_synth)
예제 #4
0
class SynthTest(SCBaseTest):

    __test__ = True

    def setUp(self) -> None:
        with self.assertRaises(RuntimeError):
            SynthTest.sc.lang
        self.custom_nodeid = 42
        self.synth_args = {"amp": 0.0, "num": 3}
        warnings.simplefilter("always", UserWarning)
        with self.assertWarnsRegex(UserWarning,
                                   "SynthDesc 's2' is unknown",
                                   msg="SynthDesc seems to be known"):
            self.synth = Synth("s2",
                               controls=self.synth_args,
                               nodeid=self.custom_nodeid)
        self.assertIsNone(self.synth._synth_desc)
        self.sc.server.sync()

    def tearDown(self) -> None:
        nodeid = self.synth.nodeid
        self.assertIn(nodeid, self.sc.server.nodes)
        self.synth.free()
        self.synth.wait()
        del self.synth  # make sure that synth is deleted from registry
        t0 = time.time()
        while nodeid in self.sc.server.nodes:
            time.sleep(0.005)
            if time.time() - t0 > 0.2:
                self.fail("NodeID is still in server.nodes")
        self.assertNotIn(nodeid, self.sc.server.nodes)
        with self.assertRaises(KeyError):
            del self.sc.server.nodes[nodeid]

    def test_node_registry(self):
        copy1 = Synth(nodeid=self.synth.nodeid, new=False)
        copy2 = Synth(nodeid=self.custom_nodeid, new=False)
        self.assertIs(self.synth, copy1)
        self.assertIs(self.synth, copy2)
        self.assertIs(copy1, copy2)
        del copy1, copy2

    def test_set_get(self):
        for name, value in {"amp": 0.0, "num": 1}.items():
            self.synth.__setattr__(name, value)
            self.assertAlmostEqual(self.synth.__getattr__(name), value)

        with self.assertWarnsRegex(UserWarning,
                                   "Setting 'freq' as python attribute"):
            with self.assertRaisesRegex(AttributeError, "no attribute 'freq'"):
                self.synth.__getattribute__(
                    "freq")  # should not have a python attribute named freq
            self.synth.freq = 420  # should warn if setting attribute

        self.assertAlmostEqual(self.synth.get("freq"),
                               400)  # default freq of s2 SynthDef

        with self.assertWarnsRegex(UserWarning,
                                   "recognized as Node Parameter now"):
            self.synth.set("freq", 100)
        self.assertAlmostEqual(self.synth.get("freq"), 100)

        self.synth.freq = 300
        self.assertAlmostEqual(self.synth.get("freq"), 300)

        for name, value in self.synth_args.items():
            self.assertAlmostEqual(self.synth.__getattr__(name), value)

    def test_new_warning(self):
        with self.assertLogs(level="WARNING") as log:
            self.synth.new()
            time.sleep(0.1)
        self.assertTrue("duplicate node ID" in log.output[-1])

    def test_query(self):
        query_result = self.synth.query()
        self.assertIsInstance(query_result, SynthInfo)
        self.assertEqual(query_result.nodeid, self.custom_nodeid)
        self.assertEqual(query_result.group,
                         SynthTest.sc.server.default_group.nodeid)
        self.assertEqual(query_result.prev_nodeid, -1)
        self.assertEqual(query_result.next_nodeid, -1)