def test_forward(): 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('forward 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 forward failed at index %d with nframes=%d and %s module' % (failed, nframes, lib)) os.remove(filename) am.enable_module()
def test_play(): am.enable_module() print() # sine wave: rate = 44100.0 t = np.arange(0.0, 0.5, 1.0/rate) mono_data = np.sin(2.0*np.pi*800.0*t) stereo_data = np.tile(mono_data, (2, 1)).T # fade in and out: ap.fade(mono_data, rate, 0.1) ap.fade(stereo_data, rate, 0.1) print('default module mono...') ap.play(mono_data, rate, blocking=True) ap.play(mono_data, rate, blocking=False) time.sleep(2.0) print('default module stereo...') ap.play(stereo_data, rate, blocking=True) ap.play(stereo_data, rate, blocking=False) time.sleep(2.0) ap.handle.close() for lib in am.installed_modules('device'): print('%s module mono...' % lib) am.select_module(lib) ap.play(mono_data, rate, blocking=True, verbose=2) ap.play(mono_data, rate, blocking=False, verbose=2) time.sleep(2.0) print('%s module stereo...' % lib) ap.play(stereo_data, rate, blocking=True) ap.play(stereo_data, rate, blocking=False) time.sleep(2.0) ap.handle.close() am.enable_module()
def test_single_frame(): am.enable_module() filename = 'test.wav' write_audio_file(filename, 20.0) tolerance = 2.0**(-15) ntests = 500 for lib in am.installed_modules('fileio'): if lib in ['scipy.io.wavfile', 'pydub']: continue print('') print('check single frame 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: assert_false(np.any(np.abs(full_data[0] - data[0]) > tolerance), 'first frame access failed with %s module' % lib) assert_false(np.any(np.abs(full_data[-1] - data[-1]) > tolerance), 'last frame access failed with %s module' % lib) def access_end(n): x = data[len(data) + n] for n in range(10): assert_raises(IndexError, access_end, n) failed = -1 for inx in np.random.randint(-len(data), len(data), ntests): if np.any(np.abs(full_data[inx] - data[inx]) > tolerance): failed = inx break assert_true( failed < 0, 'single random frame access failed at index %d with %s module' % (failed, lib)) os.remove(filename) am.enable_module()
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_modules(): am.enable_module() filename = 'test.wav' write_audio_file(filename, 1.0) for lib, load_file in al.audio_loader_funcs: print(lib) am.disable_module(lib) assert_raises(ImportError, load_file, filename) data = al.AudioLoader(verbose=4) load_funcs = { 'soundfile': data.open_soundfile, 'wavefile': data.open_wavefile, 'audioread': data.open_audioread, 'wave': data.open_wave, 'ewave': data.open_ewave, } if lib not in load_funcs: continue assert_raises(ImportError, load_funcs[lib], filename, 10.0, 2.0) if am.select_module(lib): # check double opening: load_funcs[lib](filename) load_funcs[lib](filename) data.close() os.remove(filename) am.enable_module()
def test_beep(): am.enable_module() print() print('default module...') ap.beep(blocking=True) ap.beep(0.5, 'a4', blocking=True) ap.beep(blocking=False) time.sleep(2.0) ap.handle.close() for lib in am.installed_modules('device'): print('%s module...' % lib) am.select_module(lib) ap.beep(blocking=True, verbose=2) ap.beep(blocking=False, verbose=2) time.sleep(2.0) ap.handle.close() am.enable_module()
def test_audiomodules(): am.enable_module() funcs = ['all', 'fileio', 'device'] for func in funcs: am.enable_module() print('') print('module lists for %s:' % func) inst_mods = am.installed_modules(func) print('installed:', len(inst_mods), inst_mods) assert_greater(len(inst_mods), 0, 'no installed modules for %s' % func) avail_mods = am.available_modules(func) print('available:', len(avail_mods), avail_mods) assert_equal( len(avail_mods), len(inst_mods), 'less available modules than installed modules for %s' % func) unavail_mods = am.unavailable_modules(func) print('unavailable:', len(unavail_mods), unavail_mods) assert_greater_equal( len(unavail_mods), 0, 'wrong number of unavailable modules for %s' % func) missing_mods = am.missing_modules(func) print('missing:', len(missing_mods), missing_mods) assert_greater_equal(len(missing_mods), 0, 'wrong number of missing modules for %s' % func) am.missing_modules_instructions(func) am.list_modules(func, False) am.list_modules(func, True) print() print('single module functions:') for module in am.audio_modules.keys(): am.disable_module(module) am.enable_module(module) am.select_module(module) am.enable_module() inst = am.installation_instruction(module) assert_greater(len(inst), 0, 'no installation instructions for module %s' % module) am.list_modules(module, False) am.list_modules(module, True)
def test_downsample(): def sinewave(rate): t = np.arange(0.0, 0.5, 1.0/rate) mono_data = np.sin(2.0*np.pi*800.0*t) stereo_data = np.tile(mono_data, (2, 1)).T # fade in and out: ap.fade(mono_data, rate, 0.1) ap.fade(stereo_data, rate, 0.1) return mono_data, stereo_data am.enable_module() print() for lib in am.installed_modules('device'): am.select_module(lib) print('%s module ...' % lib) for rate in [45555.0, 100000.0, 600000.0]: print(' rate %.0f Hz ...' % rate) mono_data, stereo_data = sinewave(rate) ap.play(mono_data, rate, verbose=2) ap.play(stereo_data, rate, verbose=2) ap.handle.close() am.enable_module()
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)) 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)
def test_write_read_modules(): am.enable_module() # generate data: filename = 'test.wav' format = 'wav' 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 # test for wrong formats: for lib, write_func in aw.audio_writer_funcs: if not am.select_module(lib): continue assert_raises(ValueError, write_func, '', data, samplerate) assert_raises(ValueError, write_func, filename, data, samplerate, format='xxx') write_func(filename, data, samplerate, format='') os.remove(filename) assert_raises(ValueError, write_func, filename, data, samplerate, encoding='xxx') write_func(filename, data, samplerate, encoding='') os.remove(filename) am.enable_module() assert_raises(ValueError, aw.write_audio, '', data, samplerate) assert_raises(IOError, aw.write_audio, filename, data, samplerate, format='xxx') aw.write_audio(filename, data, samplerate, format='') os.remove(filename) assert_raises(IOError, aw.write_audio, filename, data, samplerate, encoding='xxx') aw.write_audio(filename, data, samplerate, encoding='') os.remove(filename) # test for not available modules: for lib, write_func in aw.audio_writer_funcs: am.disable_module(lib) assert_raises(ImportError, write_func, filename, data, samplerate) am.enable_module(lib) for lib, encodings_func in aw.audio_encodings_funcs: am.disable_module(lib) enc = encodings_func(format) assert_equal( len(enc), 0, 'no encoding should be returned for disabled module %s' % lib) am.enable_module(lib) for lib, formats_func in aw.audio_formats_funcs: am.disable_module(lib) formats = formats_func() assert_equal( len(formats), 0, 'no format should be returned for disabled module %s' % lib) am.enable_module(lib)
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()