Example #1
0
def test_ports_and_connections_when_deactivated():
    """tests the creation of connections and ports while the client is deactivated."""
    with _create_client() as client:
        client.inports.register("input")
        client.outports.register("output")
        # test creating and connecting ports, when the client is deactivated
        signal1 = sumpf.MergeSignals([
            sumpf.SineWave(length=2**12),
            sumpf.HannWindow(length=2**12),
            sumpf.ExponentialSweep(length=2**12)
        ]).output()
        xruns = _XRunHandler()
        sumpf_jack = sumpf.Jack(name="port_creation", input_signal=signal1)
        sumpf_jack.xruns.connect(xruns.xrun)
        sumpf_jack.add_input_port("index1")
        sumpf_jack.add_input_port("shortname")
        sumpf_jack.add_input_port("name")
        sumpf_jack.add_input_port("index2")
        sumpf_jack.connect(0, 0)
        sumpf_jack.connect("Hann window", "shortname")
        sumpf_jack.connect("port_creation:Sweep", "port_creation:name")
        sumpf_jack.connect(0, "Testclient:input")
        sumpf_jack.connect("Testclient:output", 3)
        sumpf_jack.start()
        reference = sumpf.MergeSignals([
            signal1, signal1[0, 0:-client.blocksize].shift(client.blocksize)
        ]).output()
        assert sumpf_jack.output().channels() == pytest.approx(
            reference.channels())
        # test that the connections remain established, when the output ports change
        signal2 = sumpf.MergeSignals([
            sumpf.BartlettWindow(length=2**12),
            sumpf.ExponentialSweep(length=2**12),
            sumpf.SineWave(length=2**12)
        ]).output()
        sumpf_jack.input(signal2)
        sumpf_jack.start()
        reference = sumpf.MergeSignals([
            signal2, signal2[0, 0:-client.blocksize].shift(client.blocksize)
        ]).output()
        assert sumpf_jack.output().channels() == pytest.approx(
            reference.channels())
        # test that the connections are broken, when the input ports are changed
        sumpf_jack.remove_input_port("index1")
        sumpf_jack.remove_input_port("shortname")
        sumpf_jack.remove_input_port("name")
        sumpf_jack.remove_input_port("index2")
        sumpf_jack.add_input_port("a")
        sumpf_jack.add_input_port("b")
        sumpf_jack.add_input_port("c")
        sumpf_jack.add_input_port("d")
        sumpf_jack.start()
        assert (sumpf_jack.output().channels() == numpy.zeros(
            shape=sumpf_jack.output().shape())).all()
Example #2
0
def test_playback_and_recording():
    """Tests connecting ports in JACK, playing back and recording a signal."""
    with _create_client(
    ):  # skip this test if the JACK server is not running or the JACK client library is not available
        excitation = sumpf.MergeSignals([
            sumpf.GaussianNoise(length=2**15).shift(-5),
            sumpf.SineWave(length=2**15),
            sumpf.HannWindow(length=2**15)
        ]).output()
        assert excitation.offset(
        ) % 2 != 0  # check that the offset and length are odd values, that
        assert excitation.length(
        ) % 2 != 0  # do not coincide with the blocks of the JACK server
        xruns = _XRunHandler()
        jack = sumpf.Jack(input_signal=excitation,
                          input_ports=["capture_1", "channel_2", "record_3"])
        jack.xruns.connect(xruns.xrun)
        jack.connect("Gaussian noise", "SuMPF:capture_1")
        jack.connect(1, "channel_2")
        jack.connect("SuMPF:Hann window", 2)
        jack.start()
        response = jack.output()
        assert response.channels() == pytest.approx(excitation.channels())
        assert response.offset() == excitation.offset()
        assert response.sampling_rate() == jack.sampling_rate()  # pylint: disable=comparison-with-callable; pylint got confused by the connectors.MacroOutput
        assert response.labels() == ("capture_1", "channel_2", "record_3")
        assert xruns.xruns == []
Example #3
0
def test_manual_activation():
    """Tests the methods for activating and deactivating the JACK client manually."""
    with _create_client() as client:
        import jack  # pylint: disable=import-outside-toplevel; this test is skipped, if the JACK client library is not available
        signal = sumpf.SineWave(length=2**12)
        xruns = _XRunHandler()
        sumpf_jack = sumpf.Jack("manual",
                                input_signal=signal,
                                input_ports=["capture"],
                                auto_deactivate=False)
        sumpf_jack.xruns.connect(xruns.xrun)
        client.connect(
            "manual:Sine",
            "manual:capture")  # the client should be activated by default
        sumpf_jack.deactivate()
        with pytest.raises(jack.JackError):
            client.disconnect(
                "manual:Sine", "manual:capture"
            )  # this should fail, because the JACK client is deactivated
        sumpf_jack.activate()
        client.disconnect(
            "manual:Sine", "manual:capture"
        )  # this should not fail, because the connection should have been restored
        client.connect("manual:Sine", "manual:capture")
        sumpf_jack.start()
        response = sumpf_jack.output()
        assert response[0].channels() == pytest.approx(signal.channels())
        client.disconnect(
            "manual:Sine", "manual:capture"
        )  # this should not fail, because the JACK client should remain active after the recording
        sumpf_jack.deactivate()
Example #4
0
def test_automatic_deactivation():
    """Tests the functionality for activating and deactivating the JACK client automatically."""
    with _create_client() as client:
        import jack  # pylint: disable=import-outside-toplevel; this test is skipped, if the JACK client library is not available
        signal = sumpf.SineWave(length=2**12)
        xruns = _XRunHandler()
        sumpf_jack = sumpf.Jack(
            "auto", input_signal=signal,
            input_ports=["capture"
                         ])  # auto_deactivate should be enabled by default
        sumpf_jack.xruns.connect(xruns.xrun)
        with pytest.raises(jack.JackError):
            client.connect(
                "auto:Sine", "auto:capture"
            )  # this should fail, because the JACK client is deactivated
        sumpf_jack.connect(0, 0)
        with pytest.raises(jack.JackError):
            client.disconnect(
                "auto:Sine", "auto:capture"
            )  # this should fail, because the JACK client is still deactivated
        sumpf_jack.start()
        response = sumpf_jack.output()
        with pytest.raises(jack.JackError):
            client.disconnect(
                "auto:Sine", "auto:capture"
            )  # this should fail, because the JACK client is automatically deactivated after recording
        assert response[0].channels() == pytest.approx(signal.channels())
Example #5
0
def test_cosine(frequency, phase, sampling_rate, length):
    """Tests if the phase term is implemented correctly by comparing the sine wave to a cosine."""
    sine = sumpf.SineWave(frequency, phase + math.pi / 2.0, sampling_rate,
                          length)
    # test the channels
    if frequency / sampling_rate < 100.0:  # if the ratio is too large, the limited floating point precision can lead to very different result values
        omega = 2.0 * math.pi * frequency
        t = sine.time_samples()
        reference = numpy.cos(omega * t + phase)
        assert sine.channels()[0] == pytest.approx(reference)
Example #6
0
def test_sine(frequency, phase, sampling_rate, length):
    """Tests the general properties of the sine wave class."""
    sine = sumpf.SineWave(frequency, phase, sampling_rate, length)
    # test the metadata
    assert sine.sampling_rate() == sampling_rate
    assert sine.length() == length
    assert sine.shape() == (1, length)
    assert sine.labels() == ("Sine", )
    # test the periods method
    assert sine.periods() == frequency * sine.duration()
    # test the channels
    if frequency / sampling_rate < 100.0:  # if the ratio is too large, the limited floating point precision can lead to very different result values
        omega = 2.0 * math.pi * frequency
        t = sine.time_samples()
        reference = numpy.sin(omega * t + phase)
        assert sine.channels()[0] == pytest.approx(reference)
Example #7
0
def test_multiple_starts():
    """Tests if the playback and the recording can be started multiple times"""
    with _create_client(
    ):  # skip this test if the JACK server is not running or the JACK client library is not available
        excitation = sumpf.SineWave(length=2**15)
        xruns = _XRunHandler()
        jack = sumpf.Jack(input_signal=excitation, input_ports=["capture_1"])
        jack.xruns.connect(xruns.xrun)
        jack.connect(0, 0)
        jack.start()
        response = jack.output()
        assert response.channels() == pytest.approx(excitation.channels())
        jack.add_input_port("capture_2")
        jack.connect(0, 1)
        jack.start()
        response = jack.output()
        assert response[0].channels() == pytest.approx(excitation.channels())
        assert response[1].channels() == pytest.approx(excitation.channels())
        assert xruns.xruns == []
Example #8
0
from lad import lad_training
from lad import lad_testing
import numpy as np
import sumpf
import sumpf_staging
import utilities
import matplotlib.pyplot as plt

inputList = []
snrArray = []

bound = 0.6
signal = sumpf.SineWave(frequency=0.0, sampling_rate=2048, length=16384)

for f in range(10, 1000, 10):
    straight = 0
    for i in range(f, 2049, f):
        signal = signal + sumpf.SineWave(
            frequency=i, sampling_rate=2048, length=16384)
    spectrogram = signal.short_time_fourier_transform()
    straight = lad_training(spectrogram.magnitude())
    while bound < 5:
        noise = sumpf.GaussianNoise(mean=0.0,
                                    standard_deviation=bound,
                                    sampling_rate=2048,
                                    length=16384)
        signal_noise = signal + noise
        spectrogram_noise = signal_noise.short_time_fourier_transform()
        response = lad_training(spectrogram_noise.magnitude())
        if response < straight * 0.95:
            level_1 = pow(signal.level(True), 2)
from lad import lad_training
from lad import lad_testing
import numpy as np
import sumpf
import sumpf_staging
import utilities
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

signal = sumpf.SineWave(frequency=0.0, sampling_rate=2048, length=16384)
signal1 = sumpf.SineWave(frequency=0.0, sampling_rate=2048, length=16384)
signal2 = sumpf.SineWave(frequency=0.0, sampling_rate=2048, length=16384)
signal3 = sumpf.SineWave(frequency=0.0, sampling_rate=2048, length=16384)
signal4 = sumpf.SineWave(frequency=0.0, sampling_rate=2048, length=16384)
result = []
result1 = []
result2 = []
result3 = []
result4 = []
inputList = []
snrArray = []
snrArray1 = []
snrArray2 = []
snrArray3 = []
snrArray4 = []
bound = 0.001

for i in range(100, 2049, 100):
    signal = signal + sumpf.SineWave(
        frequency=i, sampling_rate=2048, length=16384)