test.py 3.15 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
15
import bob.io.base
import bob.io.matlab
from . import reader, writer
André Anjos's avatar
André Anjos committed
16

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

André Anjos's avatar
André Anjos committed
19

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


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

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

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

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

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

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

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

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

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

54
55
56
    # 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
57
58


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


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


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

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

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

76
77
78
  # reading the .mat file that contains the data generated by the Matlab
  # wavread function
  mat_data = bob.io.base.load(F('sample3.mat'))
André Anjos's avatar
André Anjos committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  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
103
def read_write(stem, fmt1, fmt2, encoding='UNKNOWN', bits_per_sample=16):
André Anjos's avatar
André Anjos committed
104
105
106
107
108

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

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

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

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

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

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