コード例 #1
0
ファイル: test_wav.py プロジェクト: zhangpengpengpeng/kaldiio
def test_read_write_wav(tmpdir, func, dtype):
    path = tmpdir.mkdir('test')
    ark = path.join('a.ark').strpath
    scp = path.join('a.scp').strpath

    # Write as pcm16
    array = np.random.randint(0, 10, 10, dtype=dtype)
    array2 = np.random.randint(0, 10, 10, dtype=dtype)
    d = {'utt': (8000, array), 'utt2': (8000, array2)}
    save_ark(ark, d, scp=scp)

    d = dict(func(scp))
    rate, test = d['utt']
    assert rate == 8000
    np.testing.assert_array_equal(array, test)

    rate, test = d['utt2']
    assert rate == 8000
    np.testing.assert_array_equal(array2, test)

    d = dict(load_ark(ark))
    rate, test = d['utt']
    assert rate == 8000
    np.testing.assert_array_equal(array, test)

    rate, test = d['utt2']
    assert rate == 8000
    np.testing.assert_array_equal(array2, test)
コード例 #2
0
    def __iter__(self):
        if self.scp:
            while True:
                try:
                    k, v = next(self.generator)
                except StopIteration:
                    break
                except Exception:
                    if self.permissive:
                        # Stop if error happen
                        break
                    else:
                        raise
                yield k, v

        else:
            with self.file as f:
                it = load_ark(f)
                while True:
                    try:
                        k, v = next(it)
                    except StopIteration:
                        break
                    except Exception:
                        if self.permissive:
                            # Stop if error happen
                            break
                        else:
                            raise
                    yield k, v
            self.closed = True
コード例 #3
0
def test_wavark_stream(tmpdir, dtype, write_function):
    path = tmpdir.mkdir("test")
    ark = path.join("a.ark").strpath

    # Write as pcm16
    array = np.random.randint(-1000, 1000, 100).astype(np.double) / abs(
        np.iinfo(np.int16).min)
    array2 = np.random.randint(-1000, 1000, 100).astype(np.double) / abs(
        np.iinfo(np.int16).min)
    if write_function == "numpy":
        d = {"utt": array, "utt2": array2}
    else:
        d = {"utt": (8000, array), "utt2": (8000, array2)}
    save_ark(ark, d, write_function=write_function)

    with open_like_kaldi("cat {}|".format(ark), "rb") as f:
        d = dict(load_ark(f))
        if write_function == "numpy":
            test = d["utt"]
        else:
            rate, test = d["utt"]
            assert rate == 8000
        np.testing.assert_allclose(array, test)

        if write_function == "numpy":
            test = d["utt2"]
        else:
            rate, test = d["utt2"]
            assert rate == 8000
        np.testing.assert_allclose(array2, test)
コード例 #4
0
def test_read_write_wav(tmpdir, func, dtype):
    path = tmpdir.mkdir("test")
    ark = path.join("a.ark").strpath
    scp = path.join("a.scp").strpath

    # Write as pcm16
    array = np.random.randint(0, 10, 10, dtype=dtype)
    array2 = np.random.randint(0, 10, 10, dtype=dtype)
    d = {"utt": (8000, array), "utt2": (8000, array2)}
    save_ark(ark, d, scp=scp)

    d = dict(func(scp))
    rate, test = d["utt"]
    assert rate == 8000
    np.testing.assert_array_equal(array, test)

    rate, test = d["utt2"]
    assert rate == 8000
    np.testing.assert_array_equal(array2, test)

    d = dict(load_ark(ark))
    rate, test = d["utt"]
    assert rate == 8000
    np.testing.assert_array_equal(array, test)

    rate, test = d["utt2"]
    assert rate == 8000
    np.testing.assert_array_equal(array2, test)
コード例 #5
0
def test_write_helper_scp_ark(tmpdir):
    path = tmpdir.strpath
    d = {"foo": numpy.random.randn(10, 10), "bar": numpy.random.randn(10, 10)}

    with WriteHelper("scp,f,ark:{p}/out.scp,{p}/out.ark".format(p=path)) as w:
        for k, v in d.items():
            w(k, v)
    from_ark = dict(load_ark("{p}/out.ark".format(p=path)))
    from_scp = load_scp("{p}/out.scp".format(p=path))
    _compare(from_ark, d)
    _compare(from_scp, d)
コード例 #6
0
def test_write_helper(tmpdir):
    path = tmpdir.strpath
    d = {'foo': numpy.random.randn(10, 10), 'bar': numpy.random.randn(10, 10)}

    with WriteHelper('ark,f,scp:{p}/out.ark,{p}/out.scp'.format(p=path)) as w:
        for k, v in d.items():
            w(k, v)
    from_ark = dict(load_ark('{p}/out.ark'.format(p=path)))
    from_scp = load_scp('{p}/out.scp'.format(p=path))
    _compare(from_ark, d)
    _compare(from_scp, d)
コード例 #7
0
def test_read_write(tmpdir, func, dtype, write_function):
    path = tmpdir.mkdir("test")
    ark = path.join("a.ark").strpath
    scp = path.join("a.scp").strpath

    # Write as pcm16
    array = np.random.randint(-1000, 1000, 100).astype(np.double) / float(
        abs(np.iinfo(np.int16).min))
    array2 = np.random.randint(-1000, 1000, 100).astype(np.double) / abs(
        np.iinfo(np.int16).min)

    if write_function == "numpy":
        d = {"utt": array, "utt2": array2}
    else:
        d = {"utt": (8000, array), "utt2": (8000, array2)}
    save_ark(ark, d, scp=scp, write_function=write_function)

    d = dict(func(scp))
    if write_function == "numpy":
        test = d["utt"]
    else:
        rate, test = d["utt"]
        assert rate == 8000
    np.testing.assert_allclose(array, test)

    if write_function == "numpy":
        test = d["utt2"]
    else:
        rate, test = d["utt2"]
        assert rate == 8000
    np.testing.assert_allclose(array2, test)

    d = dict(load_ark(ark))
    if write_function == "numpy":
        test = d["utt"]
    else:
        rate, test = d["utt"]
        assert rate == 8000
    np.testing.assert_allclose(array, test)

    if write_function == "numpy":
        test = d["utt2"]
    else:
        rate, test = d["utt2"]
        assert rate == 8000
    np.testing.assert_allclose(array2, test)
コード例 #8
0
ファイル: test_wav.py プロジェクト: zhangpengpengpeng/kaldiio
def test_wavark_stream(tmpdir, dtype):
    path = tmpdir.mkdir('test')
    ark = path.join('a.ark').strpath

    # Write as pcm16
    array = np.random.randint(0, 10, 10, dtype=dtype)
    array2 = np.random.randint(0, 10, 10, dtype=dtype)
    d = {'utt': (8000, array), 'utt2': (8000, array2)}
    save_ark(ark, d)

    with open_like_kaldi('cat {}|'.format(ark), 'rb') as f:
        d = dict(load_ark(f))
        rate, test = d['utt']
        assert rate == 8000
        np.testing.assert_array_equal(array, test)

        rate, test = d['utt2']
        assert rate == 8000
        np.testing.assert_array_equal(array2, test)
コード例 #9
0
def test_wavark_stream(tmpdir, dtype):
    path = tmpdir.mkdir("test")
    ark = path.join("a.ark").strpath

    # Write as pcm16
    array = np.random.randint(0, 10, 10, dtype=dtype)
    array2 = np.random.randint(0, 10, 10, dtype=dtype)
    d = {"utt": (8000, array), "utt2": (8000, array2)}
    save_ark(ark, d)

    with open_like_kaldi("cat {}|".format(ark), "rb") as f:
        d = dict(load_ark(f))
        rate, test = d["utt"]
        assert rate == 8000
        np.testing.assert_array_equal(array, test)

        rate, test = d["utt2"]
        assert rate == 8000
        np.testing.assert_array_equal(array2, test)