示例#1
0
def test_alsa():
    interface = alsa.Interface(config=config.fastest())
    interface.launch = mock.Mock()
    with interface:
        r = interface.recorder()
        r.read(2)
        r.close()

    p = mock.call(
        args='arecord -f S16_LE -c 1 -r 32000 -T 100 -q -'.split(),
        stdout=-1)
    assert interface.launch.mock_calls == [p, p.stdout.read(2), p.kill()]

    interface.launch = mock.Mock()
    with interface:
        p = interface.player()
        p.write('\x00\x00')
        p.close()

    p = mock.call(
        args='aplay -f S16_LE -c 1 -r 32000 -T 100 -q -'.split(),
        stdin=-1)
    assert interface.launch.mock_calls == [
        p, p.stdin.write('\x00\x00'), p.stdin.close(), p.wait()
    ]
示例#2
0
def run(size, chan=None, df=0, success=True, reader=None, cfg=None):
    if cfg is None:
        cfg = config.fastest()
    tx_data = os.urandom(size)
    tx_audio = BytesIO()
    main.send(config=cfg, src=BytesIO(tx_data), dst=tx_audio, gain=0.5)

    data = tx_audio.getvalue()
    data = common.loads(data)
    if chan is not None:
        data = chan(data)
    if df:
        sampler = sampling.Sampler(data, sampling.Interpolator())
        sampler.freq += df
        data = sampler.take(len(data))

    data = common.dumps(data)
    rx_audio = BytesIO(data)
    rx_data = BytesIO()
    dump = BytesIO()

    if reader:
        rx_audio = reader(rx_audio)
    try:
        result = main.recv(config=cfg, src=rx_audio, dst=rx_data,
                           dump_audio=dump, pylab=None)
    finally:
        rx_audio.close()

    rx_data = rx_data.getvalue()
    assert data.startswith(dump.getvalue())

    assert result == success
    if success:
        assert rx_data == tx_data
示例#3
0
def test():
    length = 1024
    data = b'\x12\x34' * length
    with mock.patch('ctypes.CDLL') as cdll:
        lib = mock.Mock()
        lib.Pa_GetErrorText = lambda code: b'Error' if code else b'Success'
        lib.Pa_GetDefaultOutputDevice.return_value = 1
        lib.Pa_GetDefaultInputDevice.return_value = 2
        lib.Pa_OpenStream.return_value = 0
        cdll.return_value = lib
        interface = audio.Interface(config=config.fastest(), debug=True)
        assert interface.load(name='portaudio') is interface
        with interface:
            s = interface.player()
            assert s.params.device == 1
            s.stream = 1  # simulate non-zero output stream handle
            s.write(data=data)
            s.close()

        with interface:
            s = interface.recorder()
            assert s.params.device == 2
            s.stream = 2  # simulate non-zero input stream handle
            s.read(len(data))
            s.close()

        with pytest.raises(Exception):
            interface._error_check(1)
示例#4
0
def test_alsa():
    interface = alsa.Interface(config=config.fastest())
    interface.launch = mock.Mock()
    with interface:
        r = interface.recorder()
        r.read(2)
        r.close()

    p = mock.call(args='arecord -f S16_LE -c 1 -r 32000 -T 100 -q -'.split(),
                  stdout=-1)
    assert interface.launch.mock_calls == [p, p.stdout.read(2), p.kill()]

    interface.launch = mock.Mock()
    with interface:
        p = interface.player()
        p.write('\x00\x00')
        p.close()

    p = mock.call(args='aplay -f S16_LE -c 1 -r 32000 -T 100 -q -'.split(),
                  stdin=-1)
    assert interface.launch.mock_calls == [
        p, p.stdin.write('\x00\x00'),
        p.stdin.close(),
        p.wait()
    ]
示例#5
0
def test_alsa_subprocess():
    interface = alsa.Interface(config=config.fastest())
    with mock.patch('subprocess.Popen') as popen:
        with interface:
            p = interface.launch(args=['foobar'])
            p.wait.side_effect = OSError('invalid command')
            assert interface.processes == [p]
            assert popen.mock_calls == [mock.call(args=['foobar'])]
示例#6
0
def test_alsa_subprocess():
    interface = alsa.Interface(config=config.fastest())
    with mock.patch('subprocess.Popen') as popen:
        with interface:
            p = interface.launch(args=['foobar'])
            p.wait.side_effect = OSError('invalid command')
            assert interface.processes == [p]
            assert popen.mock_calls == [mock.call(args=['foobar'])]
示例#7
0
def run(size, chan=None, df=0, success=True, cfg=None):
    if cfg is None:
        cfg = config.fastest()
    tx_data = os.urandom(size)
    tx_audio = BytesIO()
    main.send(config=cfg, src=BytesIO(tx_data), dst=tx_audio, gain=0.5)

    data = tx_audio.getvalue()
    data = common.loads(data)
    if chan is not None:
        data = chan(data)
    if df:
        sampler = sampling.Sampler(data, sampling.Interpolator())
        sampler.freq += df
        data = sampler.take(len(data))

    data = common.dumps(data)
    rx_audio = BytesIO(data)
    rx_data = BytesIO()
    dump = BytesIO()

    try:
        result = main.recv(config=cfg,
                           src=rx_audio,
                           dst=rx_data,
                           dump_audio=dump,
                           pylab=None)
    finally:
        rx_audio.close()

    rx_data = rx_data.getvalue()
    assert data.startswith(dump.getvalue())

    assert result == success
    if success:
        assert rx_data == tx_data
示例#8
0
import numpy as np
import pytest

from amodem import dsp
from amodem import recv
from amodem import detect
from amodem import equalizer
from amodem import sampling
from amodem import config
from amodem import common
config = config.fastest()


def test_detect():
    P = sum(equalizer.prefix)
    t = np.arange(P * config.Nsym) * config.Ts
    x = np.cos(2 * np.pi * config.Fc * t)

    detector = detect.Detector(config, pylab=common.Dummy())
    samples, amp, freq_err = detector.run(x)
    assert abs(1 - amp) < 1e-12
    assert abs(freq_err) < 1e-12

    x = np.cos(2 * np.pi * (2 * config.Fc) * t)
    with pytest.raises(ValueError):
        detector.run(x)

    with pytest.raises(ValueError):
        detector.max_offset = 0
        detector.run(x)
示例#9
0
def test_configs():
    default = config.Configuration()
    fastest = config.fastest()
    slowest = config.slowest()
    assert slowest.modem_bps <= default.modem_bps
    assert fastest.modem_bps >= default.modem_bps
示例#10
0
def test_configs():
    default = config.Configuration()
    fastest = config.fastest()
    slowest = config.slowest()
    assert slowest.modem_bps <= default.modem_bps
    assert fastest.modem_bps >= default.modem_bps
示例#11
0
from amodem import calib
from amodem import common
from amodem import config

from io import BytesIO

import numpy as np
import random
import pytest
import mock

config = config.fastest()


class ProcessMock(object):
    def __init__(self):
        self.buf = BytesIO()
        self.stdin = self
        self.stdout = self
        self.bytes_per_sample = 2

    def write(self, data):
        assert self.buf.tell() < 10e6
        self.buf.write(data)

    def read(self, n):
        return self.buf.read(n)


def test_success():
    p = ProcessMock()