Example #1
0
    def test_bad_wavread(self):
        """ Check wavread on bad file"""
        # Create a tmp audio file with non wav format, write some random data into it,
        # and check it can not be opened by wavread
        rfd, fd, cfilename   = open_tmp_file('pysndfiletest.wav')
        try:
            nbuff = 22050
            noise = 0.1 * N.random.randn(nbuff)

            # Open the copy file for writing
            format = audio_format('aiff', 'pcm16')
            b = Sndfile(cfilename, 'w', format, 1, nbuff)

            b.write_frames(noise)

            b.close()

            b = Sndfile(cfilename, 'r')
            rcnoise = b.read_frames(nbuff)
            b.close()

            try:
                rnoise  = wavread(cfilename)[0]
                raise Exception("wavread on non wav file succeded, expected to fail")
            except ValueError as e:
                pass
                #print str(e) + ", as expected"

        finally:
            close_tmp_file(rfd, cfilename)
Example #2
0
    def _test_int_io(self, dt):
        # TODO: check if neg or pos value is the highest in abs
        rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav')
        try:
            # Use almost full possible range possible for the given data-type
            nb = 2 ** (8 * np.dtype(dt).itemsize - 3)
            fs = 22050
            nbuff = fs
            a = np.random.random_integers(-nb, nb, nbuff)
            a = a.astype(dt)

            # Open the file for writing
            format = Format('wav', _DTYPE_TO_ENC[dt])
            b = Sndfile(fd, 'w', format, 1, fs)

            b.write_frames(a)
            b.close()

            b = Sndfile(cfilename, 'r')

            read_a  = b.read_frames(nbuff, dtype=dt)
            b.close()

            assert_array_equal(a, read_a)

        finally:
            close_tmp_file(rfd, cfilename)
Example #3
0
 def test_bigframes(self):
     """ Try to seek really far."""
     rawname = join(TEST_DATA_DIR, 'test.wav')
     a = Sndfile(rawname, 'r')
     try:
         try:
             a.seek(2 ** 60)
             raise Exception(
                   "Seek really succeded ! This should not happen")
         except IOError as e:
             pass
     finally:
         a.close()
Example #4
0
    def test_float_frames(self):
        """ Check nframes can be a float"""
        rfd, fd, cfilename   = open_tmp_file('pysndfiletest.wav')
        try:
            # Open the file for writing
            format = Format('wav', 'pcm16')
            a = Sndfile(fd, 'rw', format, channels=1, samplerate=22050)
            tmp = np.random.random_integers(-100, 100, 1000)
            tmp = tmp.astype(np.short)
            a.write_frames(tmp)
            a.seek(0)
            a.sync()
            ctmp = a.read_frames(1e2, dtype=np.short)
            a.close()

        finally:
            close_tmp_file(rfd, cfilename)
Example #5
0
    def _test_read(self, func, format, filext):
        # Create a tmp audio file, write some random data into it, and check it
        # is the expected data when read from a function from the matapi.
        rfd, fd, cfilename   = open_tmp_file('pySndfiletest.' + filext)
        try:
            nbuff = 22050
            noise = 0.1 * N.random.randn(nbuff)

            # Open the copy file for writing
            b = Sndfile(cfilename, 'w', format, 1, nbuff)
            b.write_frames(noise)
            b.close()

            # Reread the data
            b = Sndfile(cfilename, 'r')
            rcnoise = b.read_frames(nbuff)
            b.close()

            rnoise  = func(cfilename)[0]

            assert_array_equal(rnoise, rcnoise)
        finally:
            close_tmp_file(rfd, cfilename)
Example #6
0
    def test_rw(self):
        """Test read/write pointers for seek."""
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        rfd, fd, cfilename   = open_tmp_file('rwseektest.wav')
        try:
            ref = Sndfile(ofilename, 'r')
            test = Sndfile(fd, 'rw', format=ref.format,
                           channels=ref.channels, samplerate=ref.samplerate)
            n = 1024

            rbuff = ref.read_frames(n, dtype = np.int16)
            test.write_frames(rbuff)
            tbuff = test.read_frames(n, dtype = np.int16)

            assert_array_equal(rbuff, tbuff)

            # Test seeking both read and write pointers
            test.seek(0, 0)
            test.write_frames(rbuff)
            tbuff = test.read_frames(n, dtype = np.int16)
            assert_array_equal(rbuff, tbuff)

            # Test seeking only read pointer
            rbuff1 = rbuff.copy()
            rbuff2 = rbuff1 * 2 + 1
            rbuff2.clip(-30000, 30000)
            test.seek(0, 0, 'r')
            test.write_frames(rbuff2)
            tbuff1 = test.read_frames(n, dtype = np.int16)
            try:
                tbuff2 = test.read_frames(n, dtype = np.int16)
            except IOError as e:
                msg = "write pointer was updated in read seek !"
                msg += "\n(msg is %s)" % e
                raise AssertionError(msg)

            assert_array_equal(rbuff1, tbuff1)
            assert_array_equal(rbuff2, tbuff2)
            if np.all(rbuff2 == tbuff1):
                raise AssertionError("write pointer was updated"\
                        " in read seek !")

            # Test seeking only write pointer
            rbuff3 = rbuff1 * 2 - 1
            rbuff3.clip(-30000, 30000)
            test.seek(0, 0, 'rw')
            test.seek(n, 0, 'w')
            test.write_frames(rbuff3)
            tbuff1 = test.read_frames(n, np.int16)
            try:
                assert_array_equal(tbuff1, rbuff1)
            except AssertionError:
                raise AssertionError("read pointer was updated in write seek !")

            try:
                tbuff3 = test.read_frames(n, np.int16)
            except IOError as e:
                msg = "read pointer was updated in write seek !"
                msg += "\n(msg is %s)" % e
                raise AssertionError(msg)

            assert_array_equal(tbuff3, rbuff3)
            test.close()

        finally:
            close_tmp_file(rfd, cfilename)
Example #7
0
    def test_simple(self):
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        # Open the test file for reading
        a = Sndfile(ofilename, 'r')
        nframes = a.nframes

        buffsize = 1024
        buffsize = min(nframes, buffsize)

        # First, read some frames, go back, and compare buffers
        buff = a.read_frames(buffsize)
        a.seek(0)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff, buff2)

        a.close()

        # Now, read some frames, go back, and compare buffers
        # (check whence == 1 == SEEK_CUR)
        a = Sndfile(ofilename, 'r')
        a.read_frames(buffsize)
        buff = a.read_frames(buffsize)
        a.seek(-buffsize, 1)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff, buff2)

        a.close()

        # Now, read some frames, go back, and compare buffers
        # (check whence == 2 == SEEK_END)
        a = Sndfile(ofilename, 'r')
        buff = a.read_frames(nframes)
        a.seek(-buffsize, 2)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff[-buffsize:], buff2)
Example #8
0
    def test_basic_io(self):
        """ Check open, close and basic read/write"""
        # dirty !
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav')
        try:
            nbuff = 22050

            # Open the test file for reading
            a = Sndfile(ofilename, 'r')
            nframes = a.nframes

            # Open the copy file for writing
            format = Format('wav', 'pcm16')
            b = Sndfile(fd, 'w', format, a.channels, a.samplerate)

            # Copy the data
            for i in range(nframes / nbuff):
                tmpa    = a.read_frames(nbuff)
                assert tmpa.dtype == np.float
                b.write_frames(tmpa)
            nrem    = nframes % nbuff
            tmpa    = a.read_frames(nrem)
            assert tmpa.dtype == np.float
            b.write_frames(tmpa)

            a.close()
            b.close()
        finally:
            close_tmp_file(rfd, cfilename)
Example #9
0
    def _test_read(self, func, format, filext):
        # Create a tmp audio file, write some random data into it, and check it
        # is the expected data when read from a function from the matapi.
        rfd, fd, cfilename   = open_tmp_file('pySndfiletest.' + filext)
        try:
            nbuff = 22050
            noise = 0.1 * N.random.randn(nbuff)

            # Open the copy file for writing
            b = Sndfile(cfilename, 'w', format, 1, nbuff)
            b.write_frames(noise)
            b.close()

            # Reread the data
            b = Sndfile(cfilename, 'r')
            rcnoise = b.read_frames(nbuff)
            b.close()

            rnoise  = func(cfilename)[0]

            assert_array_equal(rnoise, rcnoise)
        finally:
            close_tmp_file(rfd, cfilename)
Example #10
0
    def _test_read_write(self, dtype):
        # dirty !
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav')
        try:
            nbuff = 22050

            # Open the test file for reading
            a = Sndfile(ofilename, 'r')
            nframes = a.nframes

            # Open the copy file for writing
            format = Format('wav', _DTYPE_TO_ENC[dtype])
            b = Sndfile(fd, 'w', format, a.channels, a.samplerate)

            # Copy the data in the wav file
            for i in range(nframes / nbuff):
                tmpa    = a.read_frames(nbuff, dtype=dtype)
                assert tmpa.dtype == dtype
                b.write_frames(tmpa)
            nrem = nframes % nbuff
            tmpa = a.read_frames(nrem)
            b.write_frames(tmpa)

            a.close()
            b.close()

            # Now, reopen both files in for reading, and check data are
            # the same
            a = Sndfile(ofilename, 'r')
            b = Sndfile(cfilename, 'r')
            for i in range(nframes / nbuff):
                tmpa = a.read_frames(nbuff, dtype=dtype)
                tmpb = b.read_frames(nbuff, dtype=dtype)
                assert_array_equal(tmpa, tmpb)

            a.close()
            b.close()

        finally:
            close_tmp_file(rfd, cfilename)
Example #11
0
import numpy as np
from audiolab import Format, Sndfile

filename = 'foo.wav'

# Create some data to save as audio data: one second of stereo white noise
data = np.random.randn(48000, 2)

# Create a Sndfile instance for writing wav files @ 48000 Hz
format = Format('wav')
f = Sndfile(filename, 'w', format, 2, 48000)

# Write the first 500 frames of the signal. Note that the write_frames method
# uses tmp's numpy dtype to determine how to write to the file; sndfile also
# converts the data on the fly if necessary
f.write_frames(data[:500])

f.close()
Example #12
0
import numpy as np
from audiolab import Sndfile

f = Sndfile("test.wav", "r")

# Sndfile instances can be queried for the audio file meta-data
fs = f.samplerate
nc = f.channels
enc = f.encoding

# Reading is straightfoward
data = f.read_frames(1000)

# This reads the next 1000 frames, e.g. from 1000 to 2000, but as single precision
data_float = f.read_frames(1000, dtype=np.float32)
Example #13
0
    def _test_write(self, func, format, filext):
        """ Check *write functions from matpi """
        rfd1, fd1, cfilename1  = open_tmp_file('matapi_test.' + filext)
        rfd2, fd2, cfilename2  = open_tmp_file('matapi_test.' + filext)
        try:
            nbuff = 22050
            fs = nbuff
            noise = 0.1 * N.random.randn(nbuff)

            # Open the first file for writing with Sndfile
            b = Sndfile(cfilename1, 'w', format, 1, fs)

            b.write_frames(noise)

            b.close()

            # Write same data with wavwrite
            func(noise, cfilename2, fs)

            # Compare if both files have both same audio data and same
            # meta-data
            f1  = Sndfile(cfilename1)
            f2  = Sndfile(cfilename2)

            assert_array_equal(f1.read_frames(f1.nframes), f2.read_frames(f2.nframes))
            assert_equal(f1.format, f2.format)
            assert_equal(f1.samplerate, f2.samplerate)
            assert_equal(f1.channels, f2.channels)
            f1.close()
            f2.close()
        finally:
            close_tmp_file(rfd1, cfilename1)
            close_tmp_file(rfd2, cfilename2)
Example #14
0
    def test_bad_wavread(self):
        """ Check wavread on bad file"""
        # Create a tmp audio file with non wav format, write some random data into it,
        # and check it can not be opened by wavread
        rfd, fd, cfilename   = open_tmp_file('pysndfiletest.wav')
        try:
            nbuff = 22050
            noise = 0.1 * N.random.randn(nbuff)

            # Open the copy file for writing
            format = audio_format('aiff', 'pcm16')
            b = Sndfile(cfilename, 'w', format, 1, nbuff)

            b.write_frames(noise)

            b.close()

            b = Sndfile(cfilename, 'r')
            rcnoise = b.read_frames(nbuff)
            b.close()

            try:
                rnoise  = wavread(cfilename)[0]
                raise Exception("wavread on non wav file succeded, expected to fail")
            except ValueError, e:
                pass
                #print str(e) + ", as expected"

        finally:
            close_tmp_file(rfd, cfilename)
Example #15
0
 def test_basic_io_fd(self):
     """ Check open from fd works"""
     ofilename = join(TEST_DATA_DIR, 'test.wav')
     fd = os.open(ofilename, os.O_RDONLY)
     hdl = Sndfile(fd, 'r')
     hdl.close()
Example #16
0
 def test_raw(self):
     rawname = join(TEST_DATA_DIR, 'test.raw')
     format = Format('raw', 'pcm16', 'little')
     a = Sndfile(rawname, 'r', format, 1, 11025)
     assert a.nframes == 11290
     a.close()
Example #17
0
import numpy as np
from audiolab import Sndfile

f = Sndfile('test.wav', 'r')

# Sndfile instances can be queried for the audio file meta-data
fs = f.samplerate
nc = f.channels
enc = f.encoding

# Reading is straightfoward
data = f.read_frames(1000)

# This reads the next 1000 frames, e.g. from 1000 to 2000, but as single precision
data_float = f.read_frames(1000, dtype=np.float32)
Example #18
0
from audiolab import Format, Sndfile

# Use 24 bits encoding, big endian
format = Format('wav', 'pcm24', 'big')
f = Sndfile('foo.wav', 'w', format, 2, 48000)
Example #19
0
    def _test_write(self, func, format, filext):
        """ Check *write functions from matpi """
        rfd1, fd1, cfilename1  = open_tmp_file('matapi_test.' + filext)
        rfd2, fd2, cfilename2  = open_tmp_file('matapi_test.' + filext)
        try:
            nbuff = 22050
            fs = nbuff
            noise = 0.1 * N.random.randn(nbuff)

            # Open the first file for writing with Sndfile
            b = Sndfile(cfilename1, 'w', format, 1, fs)

            b.write_frames(noise)

            b.close()

            # Write same data with wavwrite
            func(noise, cfilename2, fs)

            # Compare if both files have both same audio data and same
            # meta-data
            f1  = Sndfile(cfilename1)
            f2  = Sndfile(cfilename2)

            assert_array_equal(f1.read_frames(f1.nframes), f2.read_frames(f2.nframes))
            assert_equal(f1.format, f2.format)
            assert_equal(f1.samplerate, f2.samplerate)
            assert_equal(f1.channels, f2.channels)
            f1.close()
            f2.close()
        finally:
            close_tmp_file(rfd1, cfilename1)
            close_tmp_file(rfd2, cfilename2)