test.py 1.87 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
15
16
17
import os

import nose.tools
import pkg_resources

André Anjos's avatar
André Anjos committed
18
19
20
import scipy.io.wavfile
import numpy

21
22
23
import bob.io.base
import bob.io.matlab
from . import reader, writer
André Anjos's avatar
André Anjos committed
24
25


26
27
28
def F(f):
  """Returns the test file on the "data" subdirectory"""
  return pkg_resources.resource_filename(__name__, os.path.join('data', f))
André Anjos's avatar
André Anjos committed
29
30


31
def run_for(fname):
André Anjos's avatar
André Anjos committed
32

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

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

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

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

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

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

54
55
    # verify if the nchannels are the same
    nose.tools.eq_(data.shape[0], scipy_data.shape[1])
André Anjos's avatar
André Anjos committed
56

57
58
59
    # 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
60
61


62
63
def test_read_mono_wav():
  run_for('sample1.wav')
André Anjos's avatar
André Anjos committed
64
65


66
67
def test_read_stereo_wav():
  run_for('sample4.wav')
André Anjos's avatar
André Anjos committed
68
69


70
def test_read_mono_wav_matlab():
André Anjos's avatar
André Anjos committed
71

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

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

79
80
81
82
  # 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)
André Anjos's avatar
André Anjos committed
83
84


85
# TODO: could add test for writing