def read(name, end=None, start=0, dtype=np.float64, return_format=False) : """read samples from arbitrary sound files. return data, samplerate and encoding string returns subset of samples as specified by start and end arguments (Def all samples) normalizes samples to [-1,1] if the datatype is a floating point type """ sf = PySndfile(name) enc = sf.encoding_str() nf = sf.seek(start, 0) if not nf == start: raise IOError("sndio.read::error:: while seeking at starting position") if end == None: ff = sf.read_frames(dtype=dtype) else: ff = sf.read_frames(end-start, dtype=dtype) # if norm and (enc not in ["float32" , "float64"]) : # if enc in enc_norm_map : # ff = ff / enc_norm_map[sf.encoding_str()] # else : # raise IOError("sndio.read::error::normalization of compressed pcm data is not supported") if return_format: return ff, sf.samplerate(), enc, sf.major_format_str() return ff, sf.samplerate(), enc
def synthesize_trial(wavFileMatrix, indexes): ''' Using the matrix of alternative words and the selected words for each column, generate samples from audio files Returns an array of samples generated by concatenating the selected audio files ''' columnNames = ['a', 'b', 'c', 'd', 'e'] indexes = np.pad(indexes, ((0, 1)), 'constant', constant_values=0) indexes = rolling_window_lastaxis(indexes, 2) offset = 10 y = np.array([]) filenames = [] for name, ind in zip(columnNames, indexes): if name == 'e': offset = 1 wavFilename, wavFilepath = wavFileMatrix[name][(ind[0] * offset) + ind[1]] wav = PySndfile(wavFilepath) fs = wav.samplerate() x = wav.read_frames() y = np.append(y, x) filenames.append(wavFilename) return (y, { 'rate': fs, 'format': wav.major_format_str(), 'enc': wav.encoding_str() }, filenames)
def get_info(name) : """ retrieve samplerate, encoding (str) and format informationfor sndfile name """ sf = PySndfile(name) return sf.samplerate(), sf.encoding_str(), sf.major_format_str()