def test_simple(self): ofilename = join(TEST_DATA_DIR, 'test.wav') # Open the test file for reading a = Sndfile(ofilename, 'r') nframes = a.nframes buffsize = 1024 buffsize = min(nframes, buffsize) # First, read some frames, go back, and compare buffers buff = a.read_frames(buffsize) a.seek(0) buff2 = a.read_frames(buffsize) assert_array_equal(buff, buff2) a.close() # Now, read some frames, go back, and compare buffers # (check whence == 1 == SEEK_CUR) a = Sndfile(ofilename, 'r') a.read_frames(buffsize) buff = a.read_frames(buffsize) a.seek(-buffsize, 1) buff2 = a.read_frames(buffsize) assert_array_equal(buff, buff2) a.close() # Now, read some frames, go back, and compare buffers # (check whence == 2 == SEEK_END) a = Sndfile(ofilename, 'r') buff = a.read_frames(nframes) a.seek(-buffsize, 2) buff2 = a.read_frames(buffsize) assert_array_equal(buff[-buffsize:], buff2)
def test_bad_wavread(self): """ Check wavread on bad file""" # Create a tmp audio file with non wav format, write some random data into it, # and check it can not be opened by wavread rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nbuff = 22050 noise = 0.1 * N.random.randn(nbuff) # Open the copy file for writing format = audio_format('aiff', 'pcm16') b = Sndfile(cfilename, 'w', format, 1, nbuff) b.write_frames(noise) b.close() b = Sndfile(cfilename, 'r') rcnoise = b.read_frames(nbuff) b.close() try: rnoise = wavread(cfilename)[0] raise Exception( "wavread on non wav file succeded, expected to fail") except ValueError, e: pass #print str(e) + ", as expected" finally: close_tmp_file(rfd, cfilename)
def _test_int_io(self, dt): # TODO: check if neg or pos value is the highest in abs rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: # Use almost full possible range possible for the given data-type nb = 2**(8 * np.dtype(dt).itemsize - 3) fs = 22050 nbuff = fs a = np.random.random_integers(-nb, nb, nbuff) a = a.astype(dt) # Open the file for writing format = Format('wav', _DTYPE_TO_ENC[dt]) b = Sndfile(fd, 'w', format, 1, fs) b.write_frames(a) b.close() b = Sndfile(cfilename, 'r') read_a = b.read_frames(nbuff, dtype=dt) b.close() assert_array_equal(a, read_a) finally: close_tmp_file(rfd, cfilename)
def _test_write(self, func, format, filext): """ Check *write functions from matpi """ rfd1, fd1, cfilename1 = open_tmp_file('matapi_test.' + filext) rfd2, fd2, cfilename2 = open_tmp_file('matapi_test.' + filext) try: nbuff = 22050 fs = nbuff noise = 0.1 * N.random.randn(nbuff) # Open the first file for writing with Sndfile b = Sndfile(cfilename1, 'w', format, 1, fs) b.write_frames(noise) b.close() # Write same data with wavwrite func(noise, cfilename2, fs) # Compare if both files have both same audio data and same # meta-data f1 = Sndfile(cfilename1) f2 = Sndfile(cfilename2) assert_array_equal(f1.read_frames(f1.nframes), f2.read_frames(f2.nframes)) assert_equal(f1.format, f2.format) assert_equal(f1.samplerate, f2.samplerate) assert_equal(f1.channels, f2.channels) f1.close() f2.close() finally: close_tmp_file(rfd1, cfilename1) close_tmp_file(rfd2, cfilename2)
def downsample(fs, sig): in_file = random_string() + ".wav" out_file = random_string() + ".wav" frame_len = fs * WINDOW_SIZE pad = len(sig) % frame_len if pad > 0: sig = np.append(sig, np.zeros(frame_len - pad)) f = Sndfile(in_file, 'w', Format(type="wav", encoding='pcm16', endianness="file"), 1, fs) f.write_frames(sig) f.close() sox_in = pysox.CSoxStream(in_file) sox_out = pysox.CSoxStream(out_file, 'w', pysox.CSignalInfo(SAMPLE_RATE, 1, 8), fileType='wav') sox_chain = pysox.CEffectsChain(sox_in, sox_out) sox_chain.add_effect(pysox.CEffect("rate", [str(SAMPLE_RATE)])) sox_chain.flow_effects() sox_out.close() f = Sndfile(out_file, 'r') sig = f.read_frames(f.nframes) f.close() os.unlink(in_file) os.unlink(out_file) return sig
def test_basic_io(self): """ Check open, close and basic read/write""" # dirty ! ofilename = join(TEST_DATA_DIR, 'test.wav') rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nbuff = 22050 # Open the test file for reading a = Sndfile(ofilename, 'r') nframes = a.nframes # Open the copy file for writing format = Format('wav', 'pcm16') b = Sndfile(fd, 'w', format, a.channels, a.samplerate) # Copy the data for i in range(nframes / nbuff): tmpa = a.read_frames(nbuff) assert tmpa.dtype == np.float b.write_frames(tmpa) nrem = nframes % nbuff tmpa = a.read_frames(nrem) assert tmpa.dtype == np.float b.write_frames(tmpa) a.close() b.close() finally: close_tmp_file(rfd, cfilename)
def __init__(self, fn, rate=None, pad_start=0, seek=None, duration=None, rotation=None): fp = Sndfile(fn, 'r') if fn.endswith('.wav') else None if fp is None or (rate is not None and fp.samplerate != rate): # Convert to wav file if not os.path.isdir('/tmp/'): os.makedirs('/tmp/') snd_file = tempfile.NamedTemporaryFile('w', prefix='/tmp/', suffix='.wav', delete=False) snd_file.close() convert2wav(fn, snd_file.name, rate) self.snd_fn = snd_file.name self.rm_flag = True else: self.snd_fn = fn self.rm_flag = False self.fp = Sndfile(self.snd_fn, 'r') self.num_channels = self.fp.channels self.rate = self.fp.samplerate self.num_frames = self.fp.nframes self.duration = self.num_frames / float(self.rate) self.k = 0 self.pad = pad_start if seek is not None and seek > 0: num_frames = int(seek * self.rate) self.fp.read_frames(num_frames) else: seek = 0 if duration is not None: self.duration = min(duration, self.duration - seek) self.num_frames = int(self.duration * self.rate) if rotation is not None: assert self.num_channels > 2 # Spatial audio assert -np.pi <= rotation < np.pi c = np.cos(rotation) s = np.sin(rotation) rot_mtx = np.array([ [1, 0, 0, 0], # W' = W [0, c, 0, s], # Y' = X sin + Y cos [0, 0, 1, 0], # Z' = Z [0, -s, 0, c] ]) # X' = X cos - Y sin self.rot_mtx = rot_mtx else: self.rot_mtx = None
def __init__(self, filename, write=False, format='wav', rate=None, channels=None): """ Open audiofile for writing or reading Parameters ---------- filename : mixed Input wav file. String if a real file, `sys.stdin` for standard in. write: boolean Set true for writing to a file rate : int Sample rate. Only required for writing channels : int Number of Channels. Only required for writing Notes ----- * The data is assumed to be a numpy array of floats, normalized between -1 and 1. """ try: from scikits.audiolab import Format, Sndfile except: raise RuntimeError('You must have scikits.audiolab installed') if filename is sys.stdin: filename = '-' if write is True and (rate is None or channels is None): raise ValueError('You must provide sampling rate and ' 'number of channels for writing file.') if write is False: self.f = Sndfile(filename, 'r') self.channels = self.f.channels self.rate = self.f.samplerate else: format = Format(format) self.f = Sndfile(filename, 'w', format, channels, rate) self.channels = channels self.rate = rate
def signal(self): if not hasattr(self, '_signal'): if self.offset_seconds is None: self._signal = Sndfile(self.path, mode='r').read_frames( self.nframes, dtype=numpy.dtype(theanoconfig.floatX).type) else: self._signal = Sndfile(self.path, mode='r').read_frames( self.nframes_extended, dtype=numpy.dtype(theanoconfig.floatX).type) self._signal = \ self._signal[self.nframes_extended - self.nframes:] self.normalize() return self._signal
def load(filename): """ Load a wave file and return the signal, sample rate and number of channels. Can be any format supported by the underlying library (libsndfile or SciPy) """ if wav_loader == 'pysoundfile': sf = SoundFile(filename) signal = sf.read() channels = sf.channels sample_rate = sf.samplerate samples = len(sf) file_format = sf.format_info + ' ' + sf.subtype_info sf.close() elif wav_loader == 'scikits.audiolab': sf = Sndfile(filename, 'r') signal = sf.read_frames(sf.nframes) channels = sf.channels sample_rate = sf.samplerate samples = sf.nframes file_format = sf.format sf.close() elif wav_loader == 'scipy.io.wavfile': sample_rate, signal = read(filename) try: channels = signal.shape[1] except IndexError: channels = 1 samples = signal.shape[0] file_format = str(signal.dtype) return signal, sample_rate, channels
def savenoise(samps: np.ndarray, nhours: int, ofn: Path, fs: int, nsec: int, wavapi: str): if not ofn: return ofn = Path(ofn).expanduser() f: Any if wavapi == "raw": if ofn.is_file(): # delete because we're going to append ofn.unlink() with ofn.open("a+b") as f: for _ in range(int(nhours * 3600 / nsec)): f.write(samps) elif wavapi == "scipy": # pragma: no cover from scipy.io import wavfile wavfile.write(ofn, fs, samps) elif wavapi == "skaudio": # pragma: no cover from scikits.audiolab import Format, Sndfile fmt = Format("flac") f = Sndfile(ofn, "w", fmt, 1, 16000) # scikit-audio does not have context manager f.write_frames(samps) f.close() else: raise ValueError(f"I do not understand write method {wavapi}")
def load_pcm(path): wave = Sndfile(path, "r") pcm = wave.read_frames(wave.nframes) wave.close() if wave.channels is not 1: pcm = pcm[:, 0] return (pcm, wave.samplerate)
def __create_feature(self, input_path, speaker_name, feature_filename, mode): speaker_featurepath = os.path.join(self.features_rootpath, speaker_name) if not os.path.exists(speaker_featurepath): os.mkdir(speaker_featurepath) output_path = os.path.join(speaker_featurepath, feature_filename) f = Sndfile(input_path) n = f.nframes rate = f.samplerate data = f.read_frames(n) original_data = data * pow(2, 15) extractor = bob.bio.spear.extractor.Cepstral(win_length_ms=25, n_filters=27, n_ceps=13, with_energy=False, mel_scale=True, features_mask=np.arange( 0, 39)) preprocessor = bob.bio.spear.preprocessor.Energy_Thr() __, __, labels = preprocessor((rate, original_data)) feature = extractor([rate, original_data, labels]) out_file = bob.io.base.HDF5File(output_path, 'w') extractor.write_feature(feature, out_file) out_file.close()
def bin_calculate(sent_file, bpm, stepsize): sound_file = Sndfile('{0}'.format(sent_file), 'r') sec_bin_size = ((60000/float((bpm*(stepsize/4)))))*0.001 sixfour_sec_bin_size = ((60000/float((bpm*((stepsize*2)/4)))))*0.001 bin_size = sec_bin_size*(sound_file.samplerate) shunt_window = int(sixfour_sec_bin_size*(sound_file.samplerate)) return bin_size, shunt_window
def file_to_specgram(path, specgrammode=None): if specgrammode == None: # default is to do a "normal" spectrogram right here if fftsize != framelen: raise ValueError("this mode requires normal fftsize") if not os.path.isfile(path): raise ValueError("path %s not found" % path) sf = Sndfile(path, "r") if sf.channels != 1: raise Error( "ERROR in spemptk: sound file has multiple channels (%i) - mono audio required." % sf.channels) if sf.samplerate != fs: raise Error("ERROR in spemptk: wanted srate %g - got %g." % (fs, sf.samplerate)) chunksize = 4096 pcm = np.array([]) while (True): try: chunk = sf.read_frames(chunksize, dtype=np.float32) pcm = np.hstack((pcm, chunk)) except RuntimeError: break spec = stft(pcm).T else: raise ValueError("specgrammode not recognised: %s" % specgrammode) spec = spec[specfreqbinrange[0]:specfreqbinrange[1], :] mags = abs(spec) phasifiers = spec / mags if specgrammode == None: mags = np.log(mags) return (mags, phasifiers)
def read_wav(self, sample_path): sample = Sndfile(cwd + sample_path, 'r') sampling_rate = sample.samplerate channels = sample.channels encoding = sample.encoding frames_count = sample.nframes frames = sample.read_frames(frames_count, dtype=np.float32) sample.close() del sample if channels == 1: text_type = 'mono' sample_type = 0 elif channels == 2: text_type = 'stereo' sample_type = 0b01100100 else: text_type = '{0}-channels'.format(channels) if OPTIONS['verbose'] > 1: print "*", encoding, text_type, 'sample "', sample_path, '"', 4 * frames_count, 'kB' if OPTIONS['play_sound']: play(frames.astype(np.float64).T, sampling_rate) self.update({ 'sample_data': frames, 'sample_type': sample_type, 'channels': 2, 'sample_bittype': 4 })
def steg(data1, S1, T1, data2, S2, T2, theta): d1count = len(data1) d2count = len(data2) expectedcount = d1count + d2count target_split = splitArray(data2, S2, theta) gap_in_s = int(numpy.floor(T1 * theta / T2)) gap = gap_in_s * S1 i = 0 j = gap while len(target_split): data1[j:0] = target_split[0] # print "####################################################################" # print "New sample sequence: data1[", j-2, ":", j+len(target_split[0])+2, "] : ", data1[j-2:j+len(target_split[0])+2] j += gap + len(target_split[0]) target_split.pop(0) data1[0] = T1 ^ len(data1) data1[1] = S2 ^ len(data1) data1[2] = T2 ^ len(data1) data1[3] = theta size = len(data1) # print "\td1: ", d1count, "\n\td2: ", d2count, "\n\tec: ", expectedcount, "\n\tsize: ", size op = numpy.asarray(data1) format = Format('wav') f = Sndfile("stego_file.wav", 'w', format, 1, S1) f.write_frames(op) f.close() return size
def file_to_features(self, wavpath): "Reads through a mono WAV file, converting each frame to the required features. Returns a 2D array." if verbose: print("Reading %s" % wavpath) if not os.path.isfile(wavpath): raise ValueError("path %s not found" % wavpath) sf = Sndfile(wavpath, "r") #if (sf.channels != 1) and verbose: print(" Sound file has multiple channels (%i) - channels will be mixed to mono." % sf.channels) if sf.samplerate != fs: raise ValueError("wanted sample rate %g - got %g." % (fs, sf.samplerate)) window = np.hamming(framelen) features = [] while(True): try: chunk = sf.read_frames(framelen, dtype=np.float32) if len(chunk) != framelen: print("Not read sufficient samples - returning") break if sf.channels != 1: chunk = np.mean(chunk, 1) # mixdown framespectrum = np.fft.fft(window * chunk) magspec = abs(framespectrum[:framelen/2]) # do the frequency warping and MFCC computation melSpectrum = self.mfccMaker.warpSpectrum(magspec) melCepstrum = self.mfccMaker.getMFCCs(melSpectrum,cn=True) melCepstrum = melCepstrum[1:] # exclude zeroth coefficient melCepstrum = melCepstrum[:13] # limit to lower MFCCs framefeatures = melCepstrum # todo: include deltas? that can be your homework. features.append(framefeatures) except RuntimeError: break sf.close() return np.array(features)
def readAudioFromFile(self): with contextlib.closing(Sndfile(self.filename)) as f: self.channels = f.channels self.sample_count = f.nframes self.sample_rate = f.samplerate self.samples = f.read_frames(f.nframes, dtype=numpy.dtype('int16')) return True
def load_dict(filename): """ Load a wave file and return the signal, sample rate and number of channels. Can be any format supported by the underlying library (libsndfile or SciPy) """ soundfile = {} if wav_loader == 'pysoundfile': sf = SoundFile(filename) soundfile['signal'] = sf.read() soundfile['channels'] = sf.channels soundfile['fs'] = sf.samplerate soundfile['samples'] = len(sf) soundfile['format'] = sf.format_info + ' ' + sf.subtype_info sf.close() elif wav_loader == 'scikits.audiolab': sf = Sndfile(filename, 'r') soundfile['signal'] = sf.read_frames(sf.nframes) soundfile['channels'] = sf.channels soundfile['fs'] = sf.samplerate soundfile['samples'] = sf.nframes soundfile['format'] = sf.format sf.close() elif wav_loader == 'scipy.io.wavfile': soundfile['fs'], soundfile['signal'] = read(filename) try: soundfile['channels'] = soundfile['signal'].shape[1] except IndexError: soundfile['channels'] = 1 soundfile['samples'] = soundfile['signal'].shape[0] soundfile['format'] = str(soundfile['signal'].dtype) return soundfile
def removeFiles(self): if self.logs: self.logs.write('removing temp file') start_time = time.time() if '.flac' in self.filename: #if flac convert to wav if not self.removeFile: if self.logs: self.logs.write('file was flac: creating wav copy') try: format = Format('wav') f = Sndfile(self.localfilename+".wav", 'w', format, self.channs, self.sample_rate) f.write_frames(self.original) f.close() os.remove(self.localfilename) self.localfilename = self.localfilename+".wav" except: if self.logs : self.logs.write("Rec.py : error creating wav copy : "+self.localfilename) return False if self.removeFile: if self.logs: self.logs.write('removing tmeporary file '+self.localfilename) if os.path.isfile(self.localfilename): os.remove(self.localfilename) if self.logs : self.logs.write("Rec.py : removed temporary file:" + str(time.time() - start_time)) return True
def extractData(file_names): data = [] targets = [] for k, v in file_names.items(): for f_name in v: source_fname = k + "/" + f_name target_fname = k + "/" + f_name.split(".")[0] + ".TXT" source_fname = "./TIMIT" + source_fname[1:] target_fname = "./TIMIT" + target_fname[1:] audio_file = Sndfile(source_fname, "r") sr = audio_file.samplerate audio = audio_file.read_frames(audio_file.nframes) datum = mfcc(audio, samplerate=sr, nfilt=64, numcep=40) #datum = logfbank( audio, samplerate=sr, nfilt=64 ) datum = preprocessing.scale(datum) data.append(datum) audio_file.close() with open(target_fname, "r") as text_file: target_txt = ' '.join(text_file.read().lower().strip().replace( ".", "").split()[2:]) target_txt = filter(lambda x: x not in special_chars, target_txt) target_txt = target_txt.replace(' ', ' ').split(' ') target = np.hstack( ['<space>' if x == '' else list(x) for x in target_txt]) target = np.asarray( [ 0 if x == '<space>' else ord(x) - ( ord('a') - 1 )\ for x in target ] ) targets.append(target) return data, targets
def convert(filename, name): f = Sndfile(filename, 'r') fs = f.samplerate nc = f.channels enc = f.encoding data = f.read_frames(f.nframes) new_name = '/home/bitnami/apps/django/django_projects/Project/sonic_bar_code/static/newfile.wav' format = Format('wav') # f = Sndfile(new_name, 'w', format, 1, fs) f = Sndfile(new_name, 'w', format, nc, fs) f.write_frames(data) f.close() return new_name
def file_to_features(self, wavpath): sf = Sndfile(wavpath, "r") window = np.hamming(framelen) features = [] while (True): try: chunk = sf.read_frames(framelen, dtype=np.float32) if len(chunk) != framelen: print("Not read sufficient samples - returning") break if sf.channels != 1: chunk = np.mean(chunk, 1) # mixdown framespectrum = np.fft.fft(window * chunk) magspec = abs(framespectrum[:framelen / 2]) # do the frequency warping and MFCC computation melSpectrum = self.mfccMaker.warpSpectrum(magspec) melCepstrum = self.mfccMaker.getMFCCs(melSpectrum, cn=True) melCepstrum = melCepstrum[1:] # exclude zeroth coefficient melCepstrum = melCepstrum[:13] # limit to lower MFCCs framefeatures = melCepstrum features.append(framefeatures) except RuntimeError: break sf.close() return np.array(features)
def __init__(self, file_name): self.sf = Sndfile(file_name) self.file_format = self.sf.format self.nchans = self.sf.channels self.sr = self.sf.samplerate self.length = self.sf.nframes self.audio = self.sf.read_frames(self.length)
def duration(filename): """ return duration of wav file in second :param filename: :return: """ f = Sndfile(filename, 'r') return f.nframes / float(f.samplerate)
def save_file(device, sound, name_append=None, prefix=(BASE_PATH + "/../recordings/")): file = Sndfile( prefix + now_stamp() + ("__" + name_append if name_append else "") + ".wav", 'w', Format('wav', 'pcm16'), device.channels, device.rate) file.write_frames((1.0 / 32678) * sound) file.close()
def convertAndRemoveVoice(inputfile): #print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) song_folder = os.getcwd() + '/' print inputfile[-3:] mp3_file = song_folder + inputfile wav_file = song_folder + inputfile[:-4] + '.wav' #'song.wav' command = "ffmpeg " + "-i " + '"' + mp3_file + '"' + " -y " + " -ac 2 " + " -ar 44100 " + '"' + wav_file + '"' #print '\n' print command try: p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) output = p.communicate()[0] #lyricfileess = song_folder + inputfile.replace('.mp3','_beatSynced.json') #origfileess = song_folder + inputfile.replace('.mp3','_original.json') except: print 'wav conversion problem' return 0 original_wav = Sndfile(wav_file, 'r') audio = original_wav.read_frames(original_wav.nframes) #return audio #audio /= float(np.max(abs(audio))) # normalize audio #outputAudio = np.zeros(original_wav.nframes) #print type(outputAudio) #for idx,frame in enumerate(audio): #print idx #print frame outputAudio = (audio[:, 0] - audio[:, 1]) / 2 #print len(audio) print 2 new_filename = wav_file.replace('.wav', '_VocalRemoved.wav') print new_filename output_wav = Sndfile(new_filename, 'w', original_wav.format, 1, original_wav.samplerate) output_wav.write_frames(outputAudio) output_wav.close() #original_wav.close() return 1
def load_sound(filename): """ load a sound file and return a numpy array INFO: The values are normalized between -1 and 1 :param filename: :return: numpy array with (sound_lenght, channels) shape """ f = Sndfile(filename, 'r') data = f.read_frames(f.nframes, dtype=np.float64) return data, f.samplerate
def save_record(): global data n_channels, fmt = 1, Format('flac', 'pcm16') caller_id = agi.get_variable("CALLERID(num)") file_name = 'TmpSpeechFile_' + caller_id + '.flac' _, temp_sound_file = mkstemp(file_name) flac_file = Sndfile(temp_sound_file, 'w', fmt, n_channels, SAMPLE_RATE) flac_file.write_frames(data) flac_audio = AudioSegment.from_file(temp_sound_file, "flac") flac_audio.export(PATH + FILE_NAME, format="mp3")