test.py 3.14 KB
Newer Older
André Anjos's avatar
André Anjos committed
1 2 3 4 5 6 7
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :


"""Tests audio reader and writer based on sox for bob and python
"""

8 9
import nose.tools

André Anjos's avatar
André Anjos committed
10 11 12
import scipy.io.wavfile
import numpy

13 14
import bob.io.base
from . import reader, writer
André Anjos's avatar
André Anjos committed
15

André Anjos's avatar
André Anjos committed
16 17
from bob.io.base.test_utils import datafile, temporary_filename

André Anjos's avatar
André Anjos committed
18

19 20
def F(f):
  """Returns the test file on the "data" subdirectory"""
André Anjos's avatar
André Anjos committed
21
  return datafile(f, __name__)
André Anjos's avatar
André Anjos committed
22 23


André Anjos's avatar
André Anjos committed
24
def run_wav(fname):
André Anjos's avatar
André Anjos committed
25

26 27
  # audio file name
  infile = F(fname)
André Anjos's avatar
André Anjos committed
28

29 30 31
  # reading the file using our SoX bindings
  f = reader(infile)
  data = f.load()
André Anjos's avatar
André Anjos committed
32

André Anjos's avatar
André Anjos committed
33 34 35
  # check bob.io.base interface
  assert numpy.array_equal(data, bob.io.base.load(infile))

36 37
  # reading the file using scipy.io.wavfile
  (scipy_rate, scipy_data) = scipy.io.wavfile.read(infile)
André Anjos's avatar
André Anjos committed
38

39 40
  # verify if the rates are the same
  nose.tools.eq_(int(f.rate), scipy_rate)
André Anjos's avatar
André Anjos committed
41

42 43
  # If one channel
  if data.shape[0] == 1:
André Anjos's avatar
André Anjos committed
44
    # verify if the data is the same (x 2^15)
45
    assert numpy.array_equal((data[0]*pow(2,15)).astype(int), scipy_data)
André Anjos's avatar
André Anjos committed
46

47 48
  # If more than one channel
  elif data.shape[0] > 1:
André Anjos's avatar
André Anjos committed
49

André Anjos's avatar
André Anjos committed
50
    # verify if the number of channels is the same
51
    nose.tools.eq_(data.shape[0], scipy_data.shape[1])
André Anjos's avatar
André Anjos committed
52

53 54 55
    # verify if the data are the same (x 2^15)
    for i in range(data.shape[0]):
      assert numpy.array_equal((data[i]*pow(2,15)).astype(int), scipy_data[:,i])
André Anjos's avatar
André Anjos committed
56 57


58
def test_read_mono_wav():
André Anjos's avatar
André Anjos committed
59
  run_wav('sample1.wav')
André Anjos's avatar
André Anjos committed
60 61


62
def test_read_stereo_wav():
André Anjos's avatar
André Anjos committed
63
  run_wav('sample4.wav')
André Anjos's avatar
André Anjos committed
64 65


André Anjos's avatar
André Anjos committed
66
def test_compare_wav_matlab():
André Anjos's avatar
André Anjos committed
67

68 69
  # audio file name
  infile = F('sample3.wav')
André Anjos's avatar
André Anjos committed
70

71 72 73
  # reading the file using xbob.sox
  f = reader(infile)
  data = f.load()
André Anjos's avatar
André Anjos committed
74

75 76
  # reading the .mat file that contains the data generated by the Matlab
  # wavread function
77
  mat_data = bob.io.base.load(F('sample3_matlab.hdf5'))
André Anjos's avatar
André Anjos committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  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')


André Anjos's avatar
André Anjos committed
102
def read_write(stem, fmt1, fmt2, encoding='UNKNOWN', bits_per_sample=16):
André Anjos's avatar
André Anjos committed
103 104 105 106 107

  f1 = reader(F(stem + fmt1))
  data = f1.load()

  f2_filename = temporary_filename(suffix=fmt2)
André Anjos's avatar
André Anjos committed
108 109
  f2 = writer(f2_filename, rate=f1.rate, encoding=encoding,
      bits_per_sample=bits_per_sample)
André Anjos's avatar
André Anjos committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  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')
André Anjos's avatar
André Anjos committed
129

André Anjos's avatar
André Anjos committed
130 131
def test_wav_writing_timit_1():
  read_write('timit-sa1', '.sph', '.wav')
André Anjos's avatar
André Anjos committed
132

André Anjos's avatar
André Anjos committed
133 134
def test_wav_writing_timit_2():
  read_write('timit-sa2', '.sph', '.wav')
André Anjos's avatar
André Anjos committed
135 136 137 138 139 140

def test_au_writing_timit_1():
  read_write('timit-sa1', '.sph', '.au')

def test_au_writing_timit_2():
  read_write('timit-sa2', '.sph', '.au')