test.py 3.09 KB
Newer Older
André Anjos's avatar
André Anjos committed
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Andre Anjos <andre.anjos@idiap.ch>
# Elie Khoury < elie.khoury@idiap.ch>
# Wed 20 Jan 2016 14:35:05 CET
#
# Copyright (C) 2011-2016 Idiap Research Institute, Martigny, Switzerland


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

13
14
import nose.tools

André Anjos's avatar
André Anjos committed
15
16
17
import scipy.io.wavfile
import numpy

18
19
20
import bob.io.base
import bob.io.matlab
from . import reader, writer
André Anjos's avatar
André Anjos committed
21

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

André Anjos's avatar
André Anjos committed
24

25
26
def F(f):
  """Returns the test file on the "data" subdirectory"""
André Anjos's avatar
André Anjos committed
27
  return datafile(f, __name__)
André Anjos's avatar
André Anjos committed
28
29


André Anjos's avatar
André Anjos committed
30
def run_wav(fname):
André Anjos's avatar
André Anjos committed
31

32
33
  # audio file name
  infile = F(fname)
André Anjos's avatar
André Anjos committed
34

35
36
37
  # reading the file using our SoX bindings
  f = reader(infile)
  data = f.load()
André Anjos's avatar
André Anjos committed
38

André Anjos's avatar
André Anjos committed
39
40
41
  # check bob.io.base interface
  assert numpy.array_equal(data, bob.io.base.load(infile))

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

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

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

53
54
  # If more than one channel
  elif data.shape[0] > 1:
André Anjos's avatar
André Anjos committed
55

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

59
60
61
    # 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
62
63


64
def test_read_mono_wav():
André Anjos's avatar
André Anjos committed
65
  run_wav('sample1.wav')
André Anjos's avatar
André Anjos committed
66
67


68
def test_read_stereo_wav():
André Anjos's avatar
André Anjos committed
69
  run_wav('sample4.wav')
André Anjos's avatar
André Anjos committed
70
71


André Anjos's avatar
André Anjos committed
72
def test_compare_wav_matlab():
André Anjos's avatar
André Anjos committed
73

74
75
  # audio file name
  infile = F('sample3.wav')
André Anjos's avatar
André Anjos committed
76

77
78
79
  # reading the file using xbob.sox
  f = reader(infile)
  data = f.load()
André Anjos's avatar
André Anjos committed
80

81
82
83
  # 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  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')
André Anjos's avatar
André Anjos committed
134

André Anjos's avatar
André Anjos committed
135
136
def test_wav_writing_timit_1():
  read_write('timit-sa1', '.sph', '.wav')
André Anjos's avatar
André Anjos committed
137

André Anjos's avatar
André Anjos committed
138
139
def test_wav_writing_timit_2():
  read_write('timit-sa2', '.sph', '.wav')