Exemplo n.º 1
0
def test_check_wavs_bad(wav_file, wav_file_8k, tmpdir, capsys):
    def fun(utts):
        c = pipeline._init_config(
            pipeline.get_default_config('mfcc', with_cmvn=False))
        u = pipeline._init_utterances(utts)
        pipeline._Manager(c, u)
        return u

    # build a stereo file and make sure it is not supported by the
    # pipeline
    audio = Audio.load(wav_file)
    stereo = Audio(np.asarray((audio.data, audio.data)).T,
                   sample_rate=audio.sample_rate)
    assert stereo.nchannels == 2
    wav_file_2 = str(tmpdir.join('stereo.wav'))
    stereo.save(wav_file_2)
    with pytest.raises(ValueError) as err:
        fun([(wav_file_2, )])
    assert 'all wav files are not mono' in str(err)

    # ensure we catch differences in sample rates
    capsys.readouterr()  # clear buffer
    w = [(wav_file, ), (wav_file_8k, )]
    out = fun(w)
    err = capsys.readouterr().err
    assert 'several sample rates found in wav files' in err
    assert sorted(out.keys()) == ['utt_1', 'utt_2']

    # make sure timestamps are ordered
    with pytest.raises(ValueError) as err:
        fun([('1', wav_file, 1, 0)])
    assert 'timestamps are not in increasing order for' in str(err)
Exemplo n.º 2
0
def test_save(tmpdir, audio):
    p = str(tmpdir.join('test.wav'))
    audio.save(p)

    # cannot overwrite an existing file
    with pytest.raises(ValueError) as err:
        audio.save(p)
    assert 'file already exist' in str(err)

    audio2 = Audio.load(p)
    assert audio == audio2

    # test with float32 wav
    signal = np.zeros((1000,), dtype=np.float32)
    signal[10] = 1.0
    signal[20] = -1.0
    p = str(tmpdir.join('test2.wav'))
    audio = Audio(signal, 1000)
    audio.save(p)
    meta = Audio.scan(p)
    assert meta.nsamples == 1000
    assert meta.nchannels == 1

    audio2 = Audio.load(p)
    assert audio2 == audio
    assert audio2.data.min() == -1.0
    assert audio2.data.max() == 1.0