test_wavfile.py :  » Math » SciPy » scipy » scipy » io » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Math » SciPy 
SciPy » scipy » scipy » io » tests » test_wavfile.py
import os
import tempfile
import numpy as np

from numpy.testing import *
from scipy.io import wavfile

def datafile(fn):
    return os.path.join(os.path.dirname(__file__), 'data', fn)

def test_read_1():
    rate, data = wavfile.read(datafile('test-44100-le-1ch-4bytes.wav'))
    assert rate == 44100
    assert np.issubdtype(data.dtype, np.int32)
    assert data.shape == (4410,)

def test_read_2():
    rate, data = wavfile.read(datafile('test-8000-le-2ch-1byteu.wav'))
    assert rate == 8000
    assert np.issubdtype(data.dtype, np.uint8)
    assert data.shape == (800, 2)

def test_read_fail():
    assert_raises(ValueError, wavfile.read, datafile('example_1.nc'))

def _check_roundtrip(rate, dtype, channels):
    fd, tmpfile = tempfile.mkstemp(suffix='.wav')
    try:
        os.close(fd)

        data = np.random.rand(100, channels)
        if channels == 1:
            data = data[:,0]
        data = (data*128).astype(dtype)

        wavfile.write(tmpfile, rate, data)
        rate2, data2 = wavfile.read(tmpfile)

        assert rate == rate2
        assert data2.dtype.byteorder in ('<', '=', '|'), data2.dtype
        assert_array_equal(data, data2)
    finally:
        os.unlink(tmpfile)

def test_write_roundtrip():
    for signed in ('i', 'u'):
        for size in (1, 2, 4, 8):
            if size == 1 and signed == 'i':
                # signed 8-bit integer PCM is not allowed
                continue
            for endianness in ('>', '<'):
                if size == 1 and endianness == '<':
                    continue
                for rate in (8000, 32000):
                    for channels in (1, 2, 5):
                        dt = np.dtype('%s%s%d' % (endianness, signed, size))
                        yield _check_roundtrip, rate, dt, channels
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.