def get_audio(filepath, seg_start, seg_duration, targetfs=None, verbose=True): """ for use only with wav files from rwc database """ # rewriting using scikits.audiolab import scikits.audiolab as audiolab # small hack search for alternate if not op.exists(filepath): filepath = op.splitext(filepath)[0] + '.wav' if not op.exists(filepath): filepath = op.splitext(filepath)[0] + '.WAV' sndfile = audiolab.Sndfile(filepath, 'r') fs = sndfile.samplerate (n, c) = (sndfile.nframes, sndfile.channels) if verbose: print "Reading" # initalize position sndfile.seek(int(seg_start * fs), 0, 'r') audiodata = sndfile.read_frames(int(seg_duration * fs)) sndfile.close() if verbose: print "Done" if targetfs is not None and not (targetfs == fs): if verbose: print "Resampling" sig = Signal(audiodata, fs) sig.resample(targetfs) audiodata = sig.data fs = targetfs if verbose: print "Done" return audiodata, fs