def test_multiple():
    am.enable_module()
    filename = 'test.wav'
    write_audio_file(filename, 20.0)
    tolerance = 2.0**(-15)
    ntests = 100
    for lib in am.installed_modules('fileio'):
        if lib in ['scipy.io.wavfile', 'pydub']:
            continue
        print('')
        print('multiple indices access for module %s' % lib)
        am.select_module(lib)
        full_data, rate = al.load_audio(filename, verbose=4)
        with al.AudioLoader(filename, 5.0, 2.0, verbose=4) as data:
            for time in [0.1, 1.5, 2.0, 5.5, 8.0, 20.0]:
                nframes = int(time * data.samplerate)
                if nframes > 0.9 * len(data):
                    nframes = len(data)
                for n in [1, 2, 4, 8, 16]:  # number of indices
                    offs = 0
                    if len(data) > nframes:
                        offs = np.random.randint(len(data) - nframes)
                    failed = -1
                    for k in range(ntests):
                        inx = np.random.randint(0, nframes, n) + offs
                        if np.any(
                                np.abs(full_data[inx] -
                                       data[inx]) > tolerance):
                            failed = 1
                            break
                    assert_equal(failed, -1, (
                        'multiple random frame access failed with %s module at indices '
                        % lib) + str(inx))
    os.remove(filename)
    am.enable_module()
def test_negative():
    am.enable_module()
    filename = 'test.wav'
    write_audio_file(filename, 40.0)
    tolerance = 2.0**(-15)
    nsteps = 200
    for lib in am.installed_modules('fileio'):
        if lib in ['scipy.io.wavfile', 'pydub']:
            continue
        print('')
        print('negative slice access for module %s' % lib)
        am.select_module(lib)
        full_data, rate = al.load_audio(filename, verbose=4)
        with al.AudioLoader(filename, 5.0, 2.0, verbose=4) as data:
            for time in [0.1, 1.5, 2.0, 5.5, 8.0]:
                nframes = int(time * data.samplerate)
                step = int(len(data) / nsteps)
                failed = -1
                for inx in range(0, len(data) - nframes, step):
                    if np.any(
                            np.abs(full_data[-inx:-inx + nframes] -
                                   data[-inx:-inx + nframes]) > tolerance):
                        failed = -inx
                        break
                assert_true(
                    failed < 0,
                    'frame slice access backward with negative indices failed at index %d with nframes=%d and %s module'
                    % (failed, nframes, lib))
    os.remove(filename)
    am.enable_module()
Exemple #3
0
def check_writing(md, start, sep, end):
    if md:
        print(
            'writing %s files: _maximum number of channels (quantization error)_'
            % format)
    else:
        print(
            'writing %s files: maximum number of channels (quantization error)'
            % format)
    print('')
    print(start + sep.join(['%-16s' % 'module\encoding'] +
                           ['%-13s' % e for e in encodings]) + end)
    if md:
        print(start + sep.join(['-' * 16] + ['-' * 13
                                             for e in encodings]) + end)
    for lib, write_file, load_file, encodings_func in audio_funcs:
        if not aw.audio_modules[lib]:
            continue
        results = start + '%-16s' % lib
        no_result = sep + '-' + ' ' * 12
        for encoding in encodings:
            encoding = encoding.upper()
            if encoding == '' or encoding in encodings_func(format):
                max_error = 0.0
                max_channel = 0
                channels = 1
                while channels <= max_channels:
                    try:
                        write_file(filename,
                                   data[:, 1:1 + channels],
                                   samplerate,
                                   format=format,
                                   encoding=encoding)
                    except:
                        break
                    try:
                        data_read, samplerate_read = al.load_audio(filename)
                        error = check(samplerate, data[:, 1:1 + channels],
                                      samplerate_read, data_read, lib,
                                      encoding)
                        if error < -1e-8:
                            break
                        if max_error < error:
                            max_error = error
                        max_channel = channels
                    except:
                        break
                    channels *= 2
                if max_channel > 0:
                    results += sep + '%3d (%7.1e)' % (max_channel, max_error)
                else:
                    results += no_result
            else:
                results += no_result
        print(results + end)
    print('')
def test_iter():
    am.enable_module()
    filename = 'test.wav'
    write_audio_file(filename, 1.0)
    full_data, rate = al.load_audio(filename)
    tolerance = 2.0**(-15)
    with al.AudioLoader(filename, 0.2) as data:
        for k, x in enumerate(data):
            assert_false(np.any(np.abs(x - full_data[k]) > tolerance),
                         'iteration %d does not match' % k)
Exemple #5
0
def check_writing(md, start, sep, end):
    if md:
        print('writing %s files: _maximum number of channels (quantization error)_' % format)
    else:
        print('writing %s files: maximum number of channels (quantization error)' % format)
    print('')
    print(start + sep.join(['%-16s' % 'module\encoding'] + ['%-13s' % e for e in encodings]) + end)
    if md:
        print(start + sep.join(['-' * 16] + ['-' * 13 for e in encodings]) + end)
    for lib, write_file, load_file, encodings_func in audio_funcs:
        if not aw.audio_modules[lib]:
            continue
        results = start + '%-16s' % lib
        no_result = sep + '-' + ' ' * 12
        for encoding in encodings:
            encoding = encoding.upper()
            if encoding == '' or encoding in encodings_func(format):
                max_error = 0.0
                max_channel = 0
                channels = 1
                while channels <= max_channels:
                    try:
                        write_file(filename, data[:, 1:1+channels], samplerate, format=format, encoding=encoding)
                    except:
                        break
                    try:
                        data_read, samplerate_read = al.load_audio(filename)
                        error = check(samplerate, data[:, 1:1+channels], samplerate_read, data_read, lib, encoding)
                        if error < -1e-8:
                            break
                        if max_error < error:
                            max_error = error
                        max_channel = channels
                    except:
                        break
                    channels *= 2
                if max_channel > 0:
                    results += sep + '%3d (%7.1e)' % (max_channel, max_error)
                else:
                    results += no_result
            else:
                results += no_result
        print(results + end)
    print('')
def test_slice():
    am.enable_module()
    filename = 'test.wav'
    write_audio_file(filename, 20.0)
    tolerance = 2.0**(-15)
    ntests = 100
    for lib in am.installed_modules('fileio'):
        if lib in ['scipy.io.wavfile', 'pydub']:
            continue
        print('')
        print('random frame slice access for module %s' % lib)
        am.select_module(lib)
        full_data, rate = al.load_audio(filename, verbose=4)
        with al.AudioLoader(filename, 5.0, 2.0, verbose=4) as data:
            for n in range(5):
                assert_false(
                    np.any(np.abs(data[:n] - full_data[:n]) > tolerance),
                    'zero slice up to %d does not match' % n)
            for n in range(1, 5):
                assert_false(
                    np.any(np.abs(data[:50:n] - full_data[:50:n]) > tolerance),
                    'step slice with step=%d does not match' % n)
            for time in [0.1, 1.5, 2.0, 5.5, 8.0]:
                nframes = int(time * data.samplerate)
                failed = -1
                for inx in np.random.randint(0, len(data) - nframes, ntests):
                    if np.any(
                            np.abs(full_data[inx:inx + nframes] -
                                   data[inx:inx + nframes]) > tolerance):
                        failed = inx
                        break
                assert_true(
                    failed < 0,
                    'random frame slice access failed at index %d with nframes=%d and %s module'
                    % (failed, nframes, lib))
    os.remove(filename)
    am.enable_module()
def test_write_read():
    def check(samplerate_write, data_write, samplerate_read, data_read, lib,
              encoding):
        assert_almost_equal(
            samplerate_write, samplerate_read,
            'samplerates differ for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            len(data_write), len(data_read),
            'frames %d %d differ for module %s with encoding %s' %
            (len(data_write), len(data_read), lib, encoding))
        assert_equal(
            len(data_write.shape), len(data_read.shape),
            'shape len differs for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            len(data_read.shape), 2,
            'shape differs from 2 for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            data_write.shape[0], data_read.shape[0],
            'shape[0] differs for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            data_write.shape[1], data_read.shape[1],
            'shape[1] differs for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            data_read.dtype, np.float64,
            'read in data are not doubles for module %s with encoding %s' %
            (lib, encoding))
        n = min([len(data_write), len(data_read)])
        max_error = np.max(np.abs(data_write[:n] - data_read[:n]))
        print('maximum error = %g' % max_error)
        assert_less(
            max_error, 0.05,
            'values differ for module %s with encoding %s by up to %g' %
            (lib, encoding, max_error))

    # generate data:
    samplerate = 44100.0
    duration = 10.0
    t = np.arange(int(duration * samplerate)) / samplerate
    data = np.sin(2.0 * np.pi * 880.0 * t) * t / duration
    data = data.reshape((-1, 1))

    # parameter for wav file:
    filename = 'test.wav'
    format = 'wav'
    encodings = [
        'PCM_16', 'PCM_24', 'PCM_32', 'PCM_64', 'FLOAT', 'DOUBLE', 'PCM_U8',
        'ALAW', 'ULAW', ''
    ]
    encodings_with_read_error = [
        'G721_32', 'GSM610', ''
    ]  # soundfile: raise ValueError("frames must be specified for non-seekable files") in sf.read()
    encodings_with_seek_error = [
        'IMA_ADPCM', 'MS_ADPCM', ''
    ]  # soundfile: RuntimeError: Internal psf_fseek() failed.

    # parameter for ogg file:
    ## filename = 'test.ogg'
    ## format = 'OGG'
    ## encodings = ['VORBIS']

    # fix parameter:
    format = format.upper()

    for channels in [1, 2, 4, 8, 16]:

        # generate data:
        if channels > 1:
            for k in range(data.shape[1], channels):
                data = np.hstack((data, data[:, 0].reshape((-1, 1)) / k))
        print('channels = %d' % channels)

        # write, read, and check:
        audio_funcs = [
            [
                'soundfile', aw.write_soundfile, al.load_soundfile,
                aw.encodings_soundfile
            ],
            [
                'scikits.audiolab', aw.write_audiolab, al.load_audiolab,
                aw.encodings_audiolab
            ],
            [
                'wavefile', aw.write_wavefile, al.load_wavefile,
                aw.encodings_wavefile
            ], ['wave', aw.write_wave, al.load_wave, aw.encodings_wave],
            ['ewave', aw.write_ewave, al.load_ewave, aw.encodings_ewave],
            [
                'scipy.io.wavfile', aw.write_wavfile, al.load_wavfile,
                aw.encodings_wavfile
            ]
        ]

        for lib, write_file, load_file, encodings_func in audio_funcs:
            if not aw.audio_modules[lib]:
                continue
            print('')
            print(lib)
            for encoding in encodings:
                encoding = encoding.upper()
                if encoding == '' or encoding in encodings_func(format):
                    print(encoding)
                    write_file(filename,
                               data,
                               samplerate,
                               format=format,
                               encoding=encoding)
                    data_read, samplerate_read = load_file(filename)
                    check(samplerate, data, samplerate_read, data_read, lib,
                          encoding)

        print('')
        print('audioio')
        for encoding in encodings:
            encoding = encoding.upper()
            if encoding == '' or encoding in aw.available_encodings(format):
                print(encoding)
                aw.write_audio(filename,
                               data,
                               samplerate,
                               format=format,
                               encoding=encoding)
                data_read, samplerate_read = al.load_audio(filename)
                check(samplerate, data, samplerate_read, data_read, 'audioio',
                      encoding)
Exemple #8
0
def test_write_read():

    def check(samplerate_write, data_write, samplerate_read, data_read, lib, encoding):
        assert_almost_equal(samplerate_write, samplerate_read, 'samplerates differ for module %s with encoding %s' % (lib, encoding))
        assert_equal(len(data_write), len(data_read), 'frames %d %d differ for module %s with encoding %s' % (len(data_write), len(data_read), lib, encoding))
        assert_equal(len(data_write.shape), len(data_read.shape), 'shape len differs for module %s with encoding %s' % (lib, encoding))
        assert_equal(len(data_read.shape), 2, 'shape differs from 2 for module %s with encoding %s' % (lib, encoding))
        assert_equal(data_write.shape[0], data_read.shape[0], 'shape[0] differs for module %s with encoding %s' % (lib, encoding))
        assert_equal(data_write.shape[1], data_read.shape[1], 'shape[1] differs for module %s with encoding %s' % (lib, encoding))
        assert_equal(data_read.dtype, np.float64, 'read in data are not doubles for module %s with encoding %s' % (lib, encoding))
        n = min([len(data_write), len(data_read)])
        max_error = np.max(np.abs(data_write[:n] - data_read[:n]))
        print('maximum error = %g' % max_error)
        assert_less(max_error, 0.05, 'values differ for module %s with encoding %s by up to %g' % (lib, encoding, max_error))
        
    # generate data:
    samplerate = 44100.0
    duration = 10.0
    t = np.arange(int(duration*samplerate))/samplerate
    data = np.sin(2.0*np.pi*880.0*t) * t/duration
    data = data.reshape((-1, 1))

    # parameter for wav file:
    filename = 'test.wav'
    format = 'wav'
    encodings = ['PCM_16', 'PCM_24', 'PCM_32', 'PCM_64', 'FLOAT', 'DOUBLE', 'PCM_U8', 'ALAW', 'ULAW', '']
    encodings_with_read_error = ['G721_32', 'GSM610', ''] # soundfile: raise ValueError("frames must be specified for non-seekable files") in sf.read()
    encodings_with_seek_error = ['IMA_ADPCM', 'MS_ADPCM', ''] # soundfile: RuntimeError: Internal psf_fseek() failed.

    # parameter for ogg file:
    ## filename = 'test.ogg'
    ## format = 'OGG'
    ## encodings = ['VORBIS']

    # fix parameter:
    format = format.upper()

    for channels in [1, 2, 4, 8, 16]:

        # generate data:
        if channels > 1:
            for k in range(data.shape[1], channels):
                data = np.hstack((data, data[:,0].reshape((-1, 1))/k))
        print('channels = %d' % channels)

        # write, read, and check:
        audio_funcs = [
            ['soundfile', aw.write_soundfile, al.load_soundfile, aw.encodings_soundfile],
            ['scikits.audiolab', aw.write_audiolab, al.load_audiolab, aw.encodings_audiolab],
            ['wavefile', aw.write_wavefile, al.load_wavefile, aw.encodings_wavefile],
            ['wave', aw.write_wave, al.load_wave, aw.encodings_wave],
            ['ewave', aw.write_ewave, al.load_ewave, aw.encodings_ewave],
            ['scipy.io.wavfile', aw.write_wavfile, al.load_wavfile, aw.encodings_wavfile]
            ]
            
        for lib, write_file, load_file, encodings_func in audio_funcs:
            if not aw.audio_modules[lib]:
                continue
            print('')
            print(lib)
            for encoding in encodings:
                encoding = encoding.upper()
                if encoding == '' or encoding in encodings_func(format):
                    print(encoding)
                    write_file(filename, data, samplerate, format=format, encoding=encoding)
                    data_read, samplerate_read = load_file(filename)
                    check(samplerate, data, samplerate_read, data_read, lib, encoding)

        print('')
        print('audioio')
        for encoding in encodings:
            encoding = encoding.upper()
            if encoding == '' or encoding in aw.available_encodings(format):
                print(encoding)
                aw.write_audio(filename, data, samplerate, format=format, encoding=encoding)
                data_read, samplerate_read = al.load_audio(filename)
                check(samplerate, data, samplerate_read, data_read, 'audioio', encoding)
Exemple #9
0
def test_write_read():
    def check(samplerate_write, data_write, samplerate_read, data_read, lib,
              encoding):
        assert_almost_equal(
            samplerate_write, samplerate_read,
            'samplerates differ for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            len(data_write), len(data_read),
            'frames %d %d differ for module %s with encoding %s' %
            (len(data_write), len(data_read), lib, encoding))
        assert_equal(
            len(data_write.shape), len(data_read.shape),
            'shape len differs for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            len(data_read.shape), 2,
            'shape differs from 2 for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            data_write.shape[0], data_read.shape[0],
            'shape[0] differs for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            data_write.shape[1], data_read.shape[1],
            'shape[1] differs for module %s with encoding %s' %
            (lib, encoding))
        assert_equal(
            data_read.dtype, np.float64,
            'read in data are not doubles for module %s with encoding %s' %
            (lib, encoding))
        n = min([len(data_write), len(data_read)])
        max_error = np.max(np.abs(data_write[:n] - data_read[:n]))
        print('maximum error = %g' % max_error)
        assert_less(
            max_error, 0.05,
            'values differ for module %s with encoding %s by up to %g' %
            (lib, encoding, max_error))

    am.enable_module()
    # generate data:
    samplerate = 44100.0
    duration = 10.0
    t = np.arange(0.0, duration, 1.0 / samplerate)
    data = np.sin(2.0 * np.pi * 880.0 * t) * t / duration
    data = data.reshape((-1, 1))

    # parameter for wav file:
    filename = 'test.wav'
    format = 'wav'
    encodings = [
        'PCM_16', 'PCM_24', 'PCM_32', 'PCM_64', 'FLOAT', 'DOUBLE', 'PCM_U8',
        'ALAW', 'ULAW', ''
    ]
    encodings_with_read_error = [
        'G721_32', 'GSM610', ''
    ]  # soundfile: raise ValueError("frames must be specified for non-seekable files") in sf.read()
    encodings_with_seek_error = [
        'IMA_ADPCM', 'MS_ADPCM', ''
    ]  # soundfile: RuntimeError: Internal psf_fseek() failed.

    # parameter for ogg file:
    ## filename = 'test.ogg'
    ## format = 'OGG'
    ## encodings = ['VORBIS']

    mpeg_filename = 'test.mp3'

    # fix parameter:
    format = format.upper()

    for channels in [1, 2, 4, 8, 16]:

        # generate data:
        if channels > 1:
            for k in range(data.shape[1], channels):
                data = np.hstack((data, data[:, 0].reshape((-1, 1)) / k))
        print('channels = %d' % channels)

        # write, read, and check:
        for lib in am.installed_modules('fileio'):
            if lib in ['audioread', 'pydub']:
                continue
            print('')
            print('%s module:' % lib)
            am.select_module(lib)
            for encoding in encodings:
                encoding = encoding.upper()
                if encoding == '' or encoding in aw.available_encodings(
                        format):
                    print(encoding)
                    aw.write_audio(filename,
                                   data,
                                   samplerate,
                                   format=format,
                                   encoding=encoding,
                                   verbose=2)
                    data_read, samplerate_read = al.load_audio(filename,
                                                               verbose=2)
                    check(samplerate, data, samplerate_read, data_read, lib,
                          encoding)
        """
        if 'audioread' in am.installed_modules('fileio') and 'pydub' in am.installed_modules('fileio'):
            am.select_module('pydub')
            aw.write_audio(mpeg_filename, data, samplerate)
            am.select_module('audioread')
            data_read, samplerate_read = al.load_audio(mpeg_filename, verbose=2)
            check(samplerate, data, samplerate_read, data_read, 'pydub', '')
        """

        am.enable_module()
        print('')
        print('audioio')
        for encoding in encodings:
            encoding = encoding.upper()
            if encoding == '' or encoding in aw.available_encodings(format):
                print(encoding)
                aw.write_audio(filename,
                               data,
                               samplerate,
                               format=format,
                               encoding=encoding)
                data_read, samplerate_read = al.load_audio(filename, verbose=0)
                check(samplerate, data, samplerate_read, data_read, 'audioio',
                      encoding)
    if os.path.isfile(filename):
        os.remove(filename)
    if os.path.isfile(mpeg_filename):
        os.remove(mpeg_filename)
Exemple #10
0
def test_dimensions():
    am.enable_module()
    print('1-D data')
    filename = 'test.wav'
    samplerate = 44100.0
    duration = 10.0
    t = np.arange(0.0, duration, 1.0 / samplerate)
    data = np.sin(2.0 * np.pi * 880.0 * t) * t / duration
    for lib in am.installed_modules('fileio'):
        if lib == 'audioread' or (lib == 'pydub' and 'audioread'
                                  not in am.installed_modules('fileio')):
            continue
        print('%s module...' % lib)
        am.select_module(lib)
        aw.write_audio(filename, data, samplerate)
        if lib == 'pydub':
            am.select_module('audioread')
        data_read, samplerate_read = al.load_audio(filename)
        assert_equal(len(data_read.shape), 2,
                     'read in data must be a 2-D array')
        assert_equal(data_read.shape[1], 1,
                     'read in data must be a 2-D array with one column')

    print('2-D data one channel')
    filename = 'test.wav'
    samplerate = 44100.0
    duration = 10.0
    t = np.arange(0.0, duration, 1.0 / samplerate)
    data = np.sin(2.0 * np.pi * 880.0 * t) * t / duration
    data = data.reshape((-1, 1))
    for lib in am.installed_modules('fileio'):
        if lib == 'audioread' or (lib == 'pydub' and 'audioread'
                                  not in am.installed_modules('fileio')):
            continue
        print('%s module...' % lib)
        am.select_module(lib)
        aw.write_audio(filename, data, samplerate)
        if lib == 'pydub':
            am.select_module('audioread')
        data_read, samplerate_read = al.load_audio(filename)
        assert_equal(len(data_read.shape), 2,
                     'read in data must be a 2-D array')
        assert_equal(data_read.shape[1], 1,
                     'read in data must be a 2-D array with one column')
        assert_equal(data_read.shape, data.shape,
                     'input and output data must have same shape')

    print('2-D data two channel')
    filename = 'test.wav'
    samplerate = 44100.0
    duration = 10.0
    t = np.arange(0.0, duration, 1.0 / samplerate)
    data = np.sin(2.0 * np.pi * 880.0 * t) * t / duration
    data = data.reshape((-1, 2))
    for lib in am.installed_modules('fileio'):
        if lib == 'audioread' or (lib == 'pydub' and 'audioread'
                                  not in am.installed_modules('fileio')):
            continue
        print('%s module...' % lib)
        am.select_module(lib)
        aw.write_audio(filename, data, samplerate)
        if lib == 'pydub':
            am.select_module('audioread')
        data_read, samplerate_read = al.load_audio(filename)
        assert_equal(len(data_read.shape), 2,
                     'read in data must be a 2-D array')
        assert_equal(data_read.shape[1], 2,
                     'read in data must be a 2-D array with two columns')
        assert_equal(data_read.shape, data.shape,
                     'input and output data must have same shape')
    am.enable_module()