def test00_invalidtype(): if os.path.exists(test_file): os.remove(test_file) try: ewave.open(test_file,'w',dtype='S2') except ewave.Error: # verify that file was not created assert not os.path.exists(test_file), "file was created for invalid type" return raise Exception("Exception was not raised for invalid type")
def write_file(fname,tgt_type=None): fp1 = ewave.open(fname,"r") with ewave.open(test_file,"w", sampling_rate=fp1.sampling_rate, dtype=tgt_type or fp1.dtype, nchannels=fp1.nchannels) as fp2: assert fp2.filename == test_file, fname assert fp2.sampling_rate == fp1.sampling_rate, fname assert fp2.nchannels == fp1.nchannels, fname if not tgt_type: assert fp2.dtype == fp1.dtype, fname fp2.write(fp1.read()) assert fp2.nframes == fp1.nframes, fname os.remove(test_file)
def test02_modify(): from numpy.random import randn data = randn(10000,nchan) with ewave.open(test_file,"w+",sampling_rate=Fs,dtype='f',nchannels=nchan) as fp: fp.write(data) d2 = fp.read(memmap='r+') compare_arrays(data,d2,"written data") d2 *= 2 compare_arrays(data*2,d2,"modified data") with ewave.open(test_file,"r") as fp: d2 = fp.read(memmap='r') compare_arrays(data*2,d2,"written data")
def plotwav2(fn, bw=1, size=8192, fix=False, smoother=smoothfft2, **kwargs): s, srate = wav_read(fn) s, rms = normalize(s, srate) sm = monoize(s) if s.shape[1] == 2: ss = monoize(s*np.array((1, -1))) else: ss = np.zeros(len(s)) xs, ys_m, ys_s = plotwavinternal(sm, ss, srate, bw, size, smoother) side_gain = np.average(ys_s) - np.average(ys_m) if fix: fno = fn[:-4]+"-proc.wav" fir_m = firize(xs, -ys_m, srate=srate) fir_s = firize(xs, -ys_s, srate=srate) smf = convolve_each(sm/8, fir_m, mode='same') ssf = convolve_each(ss/8, fir_s, mode='same') ssf *= 10**(side_gain/20) sf = np.array((smf + ssf, smf - ssf)).T import ewave with ewave.open(fno, 'w', sampling_rate=srate, nchannels=count_channels(sf)) as f: f.write(sf) print('wrote '+fno) return xs, ys_m, ys_s
def load_stimulus(path, window, step, f_min=0.5, f_max=8.0, f_count=30, compress=1, **kwargs): """Load sound stimulus and calculate spectrotemporal representation. Parameters: path: location of wave file window: duration of window (in ms) step: window step (in ms) f_min: minimum frequency (in kHz) f_max: maximum frequency (in kHz) f_count: number of frequency bins gammatone: if True, use gammatone filterbank Returns spectrogram, duration (ms) """ fp = ewave.open(path, "r") Fs = fp.sampling_rate / 1000. osc = ewave.rescale(fp.read(), 'h') Pxx = gtgram(osc, Fs * 1000, window / 1000, step / 1000, f_count, f_min * 1000, f_max * 1000) Pxx = np.log10(Pxx + compress) - np.log10(compress) return Pxx, Pxx.shape[1] * step, step
def createtemparf(filename, datatype=0): root, ext = os.path.splitext(filename) arffile = arf.open_file(tempfile.mktemp()) if ext == '.lbl': lbl_rec = lbl.read(filename) print(lbl_rec) dset = arf.create_dataset(arffile, os.path.split(filename)[-1], lbl_rec, units=['','s','s'], datatype=2002) dset.attrs['units'] = 's' elif ext == '.wav': wavfile = ewave.open(filename) arf.create_dataset(arffile, os.path.split(filename)[-1], wavfile.read(), sampling_rate=wavfile.sampling_rate, datatype=1) elif ext =='.pcm': from arfx import pcmio pcmfile = pcmio.open(filename) arf.create_dataset(arffile, os.path.split(filename)[-1], pcmfile.read(), sampling_rate=pcmfile.sampling_rate, datatype=datatype) elif ext == '.pcm_seq2': from arfx import io pcmseqfile = io.open(filename) dataset_basename = os.path.split(filename)[-1] for i in xrange(pcmseqfile.nentries): dataset_name = '_'.join([dataset_basename, str(i)]) arf.create_dataset(arffile, dataset_name, pcmseqfile.read(), sampling_rate=pcmseqfile.sampling_rate, timestamp=pcmseqfile.timestamp, datatype=datatype) #try block added because pcmseqfile.nentries doesn't seem to always be accurate try: pcmseqfile.entry += 1 except ValueError: continue return arffile['/']
def get_wav_info(wav_file): wav = ewave.open(wav_file, 'r') frames = wav.read() #sound_info = pylab.fromstring(frames, 'Int16') sound_info = frames frame_rate = wav.sampling_rate #wav.close() return sound_info, frame_rate
def __missing__(self, key): fname, fs = key fp = ewave.open(os.path.join(self.dir, fname + ".wav")) data = fp.read() if fp.sampling_rate != fs: data = src_simple(data, 1.0 * fs / fp.sampling_rate) ret = self[key] = data return ret
def test02_append(): from numpy import random, concatenate d1 = random.randn(100,nchan) d2 = random.randn(100,nchan) with ewave.open(test_file,"w+",sampling_rate=Fs,dtype='f',nchannels=nchan) as fp: fp.write(d1) compare_arrays(d1,fp.read(),"first data") fp.write(d2) compare_arrays(concatenate(d1,d2),fp.read(),"second data")
def read_file(fname, memmap="r"): fp = ewave.open(fname,"r") assert fp.mode == "r" data = fp.read(memmap=memmap) if fp.nchannels == 1: assert data.ndim == 1 assert data.size == fp.nframes else: assert data.shape[0] == fp.nframes assert data.shape[1] == fp.nchannels
def wav_read(fn): with ewave.open(fn) as f: s = f.read() srate = f.sampling_rate if s.dtype == np.float32: s = np.asfarray(s) elif s.dtype != np.float64: bits = s.dtype.itemsize*8 s = np.asfarray(s)/2**(bits - 1) return s, srate
def load_raw_responses(*path, unit, stimname, offset=-2): r = [] t = None for i, f in enumerate( glob.glob(os.path.join(*path, "%s_%s*.wav" % (unit, stimname)))): with ewave.open(f) as fp: r.append(fp.read()) if i == 0: t = np.arange(0, fp.nframes / fp.sampling_rate, 1. / fp.sampling_rate) + offset # assumes that all the responses are the same duration return r, t
def readwrite_file(fname): """ test files in r+ mode """ fp1 = ewave.open(fname,"r") d1 = fp1.read() with ewave.open(test_file,"w", sampling_rate=fp1.sampling_rate, dtype=fp1.dtype, nchannels=fp1.nchannels) as fp2: fp2.write(d1, scale=False).flush() with ewave.open(test_file,"r+") as fp3: assert fp3.filename == test_file, fname assert fp3.sampling_rate == fp1.sampling_rate, fname assert fp3.dtype == fp1.dtype, fname assert fp3.nchannels == fp1.nchannels, fname assert fp3.nframes == fp1.nframes, fname d2 = fp3.read(memmap='r+') compare_arrays(d1,d2,fname) os.remove(test_file)
def load_stimulus(path, window, step, f_min=0.5, f_max=8.0, f_count=30, compress=1, gammatone=False, **kwargs): """Load sound stimulus and calculate spectrotemporal representation. Parameters: path: location of wave file window: duration of window (in ms) step: window step (in ms) f_min: minimum frequency (in kHz) f_max: maximum frequency (in kHz) f_count: number of frequency bins gammatone: if True, use gammatone filterbank Returns spectrogram, duration (ms) """ import ewave fp = ewave.open(path, "r") Fs = fp.sampling_rate / 1000. osc = ewave.rescale(fp.read(), 'h') if gammatone: import gammatone.gtgram as gg Pxx = gg.gtgram(osc, Fs * 1000, window / 1000, step / 1000, f_count, f_min * 1000) else: import libtfr # nfft based on desired number of channels btw f_min and f_max nfft = int(f_count / (f_max - f_min) * Fs) npoints = int(Fs * window) if nfft < npoints: raise ValueError( "window size {} ({} points) too large for desired freq resolution {}. " "Decrease to {} ms or increase f_count.".format( window, f_count, npoints, nfft / Fs)) nstep = int(Fs * step) taper = np.hanning(npoints) mfft = libtfr.mfft_precalc(nfft, taper) Pxx = mfft.mtspec(osc, nstep) freqs, ind = libtfr.fgrid(Fs, nfft, [f_min, f_max]) Pxx = Pxx[ind, :] if compress is not None: Pxx = np.log10(Pxx + compress) - np.log10(compress) return Pxx, Pxx.shape[1] * step
def dat2wav(datfile, wavfile=None): if wavfile is None: wavfile = datfile + ".wav" data, params = load_dat(datfile) rate = params["sampling_rate"] nchannels = params["n_channels"] dtype = params["dtype"] with ewave.open(wavfile, "w+", sampling_rate=rate, dtype=dtype, nchannels=nchannels) as wavfp: for i in range(0, len(data), nchannels * BUFFER_SIZE): buffer = data[i:i + nchannels * BUFFER_SIZE] wavfp.write(buffer)
def createtemparf(filename, datatype=0): root, ext = os.path.splitext(filename) arffile = arf.open_file(tempfile.mktemp()) if ext == '.lbl': lbl_rec = lbl.read(filename) print(lbl_rec) dset = arf.create_dataset(arffile, os.path.split(filename)[-1], lbl_rec, units=['','s','s'], datatype=2002) dset.attrs['units'] = 's' elif ext == '.wav': wavfile = ewave.open(filename) arf.create_dataset(arffile, os.path.split(filename)[-1], wavfile.read(), sampling_rate=wavfile.sampling_rate, datatype=1) elif ext =='.pcm': from arfx import pcmio pcmfile = pcmio.open(filename) arf.create_dataset(arffile, os.path.split(filename)[-1], pcmfile.read(), sampling_rate=pcmfile.sampling_rate, datatype=datatype) return arffile['/']
} archive = "/home/data/starlings" if __name__ == "__main__": import argparse p = argparse.ArgumentParser(description="extract raw extracellular recordings from arf files""") p.add_argument("unit", help="the name of the unit") p.add_argument("channel", help="the name of the channel to extract") args = p.parse_args() location = nbank.get(args.unit, local_only=True) stim_counter = collections.defaultdict(int) with h5.File(location, "r") as fp: for entry_name in arf.keys_by_creation(fp): entry = fp[entry_name] stim = entry['stimuli']['name', 0].decode('utf-8') if stim not in stimuli: print("%s -> skipping (%s)" % (entry_name, stim)) else: stim_counter[stim] += 1 outfile = "%s_%s_%d.wav" % (args.unit, stimuli[stim], stim_counter[stim]) print("%s -> %s" % (entry_name, outfile)) dset = entry[args.channel] sampling_rate = dset.attrs['sampling_rate'] with ewave.open(outfile, mode='w', sampling_rate=sampling_rate, dtype=dset.dtype) as ofp: ofp.write(dset[:])
def wav_write(fn, s, srate, dtype='h'): if dtype in ('b', 'h', 'i', 'l') and np.max(np.abs(s)) > 1: lament('wav_write(): WARNING; clipping') with ewave.open(fn, 'w', srate, dtype, count_channels(s)) as f: f.write(s)
def load_timeseries(path): wavfile = ewave.open(path, "r") return wavfile.read(memmap=False), wavfile.sampling_rate
def load_timeseries(*path): wavfile = ewave.open(os.path.join(*path), "r") return wavfile.read(memmap=False), wavfile.sampling_rate
def test00_handle(): fp = open(test_file,'wb') with ewave.open(fp,sampling_rate=Fs,nchannels=nchan) as wfp: assert wfp.sampling_rate == Fs assert wfp.filename == test_file os.remove(test_file)
def test00_mode(): ewave.open(test_file,'a')