Commit d5fe916c authored by André Anjos's avatar André Anjos 💬
Browse files

[test] More tests and samples

parent 7ba54b86
File mode changed from 100755 to 100644
......@@ -10,10 +10,7 @@
"""Tests audio reader and writer based on sox for bob and python
"""
import os
import nose.tools
import pkg_resources
import scipy.io.wavfile
import numpy
......@@ -22,13 +19,15 @@ import bob.io.base
import bob.io.matlab
from . import reader, writer
from bob.io.base.test_utils import datafile, temporary_filename
def F(f):
"""Returns the test file on the "data" subdirectory"""
return pkg_resources.resource_filename(__name__, os.path.join('data', f))
return datafile(f, __name__)
def run_for(fname):
def run_wav(fname):
# audio file name
infile = F(fname)
......@@ -37,6 +36,9 @@ def run_for(fname):
f = reader(infile)
data = f.load()
# check bob.io.base interface
assert numpy.array_equal(data, bob.io.base.load(infile))
# reading the file using scipy.io.wavfile
(scipy_rate, scipy_data) = scipy.io.wavfile.read(infile)
......@@ -45,13 +47,13 @@ def run_for(fname):
# If one channel
if data.shape[0] == 1:
# verify if the data are the same (x 2^15)
# verify if the data is the same (x 2^15)
assert numpy.array_equal((data[0]*pow(2,15)).astype(int), scipy_data)
# If more than one channel
elif data.shape[0] > 1:
# verify if the nchannels are the same
# verify if the number of channels is the same
nose.tools.eq_(data.shape[0], scipy_data.shape[1])
# verify if the data are the same (x 2^15)
......@@ -60,14 +62,14 @@ def run_for(fname):
def test_read_mono_wav():
run_for('sample1.wav')
run_wav('sample1.wav')
def test_read_stereo_wav():
run_for('sample4.wav')
run_wav('sample4.wav')
def test_read_mono_wav_matlab():
def test_compare_wav_matlab():
# audio file name
infile = F('sample3.wav')
......@@ -79,7 +81,59 @@ def test_read_mono_wav_matlab():
# reading the .mat file that contains the data generated by the Matlab
# wavread function
mat_data = bob.io.base.load(F('sample3.mat'))
assert numpy.array_equal(data, mat_data)
assert numpy.array_equal(data, mat_data), '%r != %r' % (data, mat_data)
def run_check(stem, fmt1, fmt2):
f1 = reader(F(stem + fmt1))
f2 = reader(F(stem + fmt2))
nose.tools.eq_(f1.rate, f2.rate)
data_f1 = f1.load()
data_f2 = f2.load()
# verify the data is the same
assert numpy.array_equal(data_f1, data_f2), '%r != %r' % (data_f1, data_f2)
def test_sph_timit_1():
run_check('timit-sa1', '.wav', '.sph')
def test_sph_timit_2():
run_check('timit-sa2', '.wav', '.sph')
def read_write(stem, fmt1, fmt2):
f1 = reader(F(stem + fmt1))
data = f1.load()
f2_filename = temporary_filename(suffix=fmt2)
f2 = writer(f2_filename, rate=f1.rate)
f2.append(data)
f2.close() #forces file closing
f2 = reader(f2_filename)
nose.tools.eq_(f1.rate, f2.rate)
data_f1 = f1.load()
data_f2 = f2.load()
# verify the data is the same
assert numpy.array_equal(data_f1, data_f2), '%r != %r' % (data_f1, data_f2)
def test_sph_writing_timit_1():
read_write('timit-sa1', '.wav', '.sph')
def test_sph_writing_timit_2():
read_write('timit-sa2', '.wav', '.sph')
def test_wav_writing_timit_1():
read_write('timit-sa1', '.sph', '.wav')
# TODO: could add test for writing
def test_wav_writing_timit_2():
read_write('timit-sa2', '.sph', '.wav')
......@@ -21,7 +21,7 @@
PyDoc_STRVAR(s_audiowriter_str, BOB_EXT_MODULE_PREFIX "." AUDIOWRITER_NAME);
PyDoc_STRVAR(s_audiowriter_doc,
"writer(filename, [rate=8000., [encoding='UNSIGNED', [bits_per_sample=16]]]) -> new writer\n\
"writer(filename, [rate=8000., [encoding='UNKNOWN', [bits_per_sample=16]]]) -> new writer\n\
\n\
Use this object to write samples to audio files.\n\
\n\
......@@ -93,7 +93,7 @@ static int PyBobIoAudioWriter_Init(PyBobIoAudioWriterObject* self,
auto filename_ = make_safe(filename);
std::string encoding_str = encoding?encoding:"UNSIGNED";
std::string encoding_str = encoding?encoding:"UNKNOWN";
sox_encoding_t sox_encoding = bob::io::audio::string2encoding(encoding_str.c_str());
#if PY_VERSION_HEX >= 0x03000000
......@@ -311,7 +311,7 @@ static PyObject* PyBobIoAudioWriter_Append(PyBobIoAudioWriterObject* self, PyObj
return 0;
}
if (sample->type_num != NPY_FLOAT32) {
if (sample->type_num != NPY_FLOAT64) {
PyErr_Format(PyExc_TypeError, "input array should have dtype `float64', but you passed an array with dtype == `%s'", PyBlitzArray_TypenumAsString(sample->type_num));
return 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment