本文整理汇总了Python中scikits.audiolab.Sndfile.read_frames方法的典型用法代码示例。如果您正苦于以下问题:Python Sndfile.read_frames方法的具体用法?Python Sndfile.read_frames怎么用?Python Sndfile.read_frames使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scikits.audiolab.Sndfile
的用法示例。
在下文中一共展示了Sndfile.read_frames方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_write
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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)
示例2: test_basic_io
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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)
示例3: load_soundfile
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
def load_soundfile(inwavpath, startpossecs, maxdursecs=None):
"""Loads audio data, optionally limiting to a specified start position and duration.
Must be SINGLE-CHANNEL and matching our desired sample-rate."""
framelen = 4096
hopspls = framelen
unhopspls = framelen - hopspls
if (framelen % wavdownsample) != 0:
raise ValueError("framelen needs to be a multiple of wavdownsample: %i, %i" % (
framelen, wavdownsample))
if (hopspls % wavdownsample) != 0:
raise ValueError("hopspls needs to be a multiple of wavdownsample: %i, %i" % (
hopspls, wavdownsample))
if maxdursecs == None:
maxdursecs = 9999
sf = Sndfile(inwavpath, "r")
splsread = 0
framesread = 0
if sf.channels != 1:
raise ValueError(
"Sound file %s has multiple channels (%i) - mono required." % (inwavpath, sf.channels))
timemax_spls = int(maxdursecs * sf.samplerate)
if sf.samplerate != (srate * wavdownsample):
raise ValueError(
"Sample rate mismatch: we expect %g, file has %g" % (srate, sf.samplerate))
if startpossecs > 0:
# note: returns IOError if beyond the end
sf.seek(startpossecs * sf.samplerate)
audiodata = np.array([], dtype=np.float32)
while(True):
try:
if splsread == 0:
chunk = sf.read_frames(framelen)[::wavdownsample]
splsread += framelen
else:
chunk = np.hstack(
(chunk[:unhopspls], sf.read_frames(hopspls)[::wavdownsample]))
splsread += hopspls
framesread += 1
if framesread % 25000 == 0:
print("Read %i frames" % framesread)
if len(chunk) != (framelen / wavdownsample):
print("Not read sufficient samples - returning")
break
chunk = np.array(chunk, dtype=np.float32)
audiodata = np.hstack((audiodata, chunk))
if splsread >= timemax_spls:
break
except RuntimeError:
break
sf.close()
return audiodata
示例4: test_simple
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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)
示例5: downsample
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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
示例6: file_to_specgram
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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)
示例7: read_sound
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
def read_sound(fp):
"""
create a normalized float array and datarate from any audo file
"""
if fp.endswith('mp3'):
try:
oname = 'temp.wav'
#cmd = 'lame --decode "{0}" {1}'.format( fp ,oname )
result = subprocess.call(['lame', '--decode', fp, oname])
assert(result is 0)
samplerate, data = wav.read(oname)
except:
print "couldn't run lame"
try:
import moviepy.editor as mpy
aud_clip = mpy.AudioFileClip(fp)
samplerate = aud_clip.fps
data = aud_clip.to_soundarray()
except:
print "moviepy not installed?"
if fp.endswith('aif'):
#sf = aifc.open(fp)
oname = fp
sf = Sndfile(fp, 'r')
sf.seek(0)
data = sf.read_frames(sf.nframes)
samplerate = sf.samplerate
if fp.endswith('wav'):
samplerate, data = wav.read(fp)
if len(data.shape)>1: data = data[:,0]
data = data.astype('float64')
data /= data.max()
return data, samplerate
示例8: read_wav
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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
})
示例9: file_to_features
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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)
示例10: _test_int_io
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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)
示例11: file_to_features
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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)
示例12: timeStretchAudio
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
def timeStretchAudio(inputAudio, outputAudio, outputDuration, writeOutput=1):
originalWav = Sndfile(inputAudio, 'r')
x = originalWav.read_frames(originalWav.nframes)
fs = originalWav.samplerate
nChannel = originalWav.channels
print fs
if nChannel >1:
x = x[0]
w = np.hamming(801)
N = 2048
t = -90
minSineDur = .005
maxnSines = 150
freqDevOffset = 20
freqDevSlope = 0.02
Ns = 512
H = Ns/4
tfreq, tmag, tphase = SM.sineModelAnal(x, fs, w, N, H, t, maxnSines, minSineDur, freqDevOffset, freqDevSlope)
inputDur = float(len(tfreq)*H/fs)
#timeScale = np.array([0.1,0.1, inputDur, inputDur*2])
timeScale = np.array([0,0, .4,outputDuration])
ytfreq, ytmag = trans.sineTimeScaling(tfreq, tmag, timeScale)
y = SM.sineModelSynth(ytfreq, ytmag, np.array([]), Ns, H, fs)
if writeOutput ==1:
outputWav = Sndfile(outputAudio, 'w', originalWav.format, originalWav.channels, originalWav.samplerate)
outputWav.write_frames(y)
outputWav.close()
else:
return y, fs, nChannel
示例13: test_bad_wavread
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
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)
示例14: get_fft_points
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
def get_fft_points(sound_filename, fps, fft_pixels, rate = 1, fourierwidth = 0.3):
"""TODO
will generate rate points per frame
Based on the script from
http://classicalconvert.com/2008/04/
how-to-visualize-music-using-animated-spectrograms-with
-open-source-everything/"""
f = Sndfile(sound_filename, 'r')
divisor = f.samplerate / (rate * fps) # should be integer
points = []
framepos = 0L
while framepos < f.nframes:
read_len = (
divisor if (framepos + divisor < f.nframes)
else f.nframes - framepos)
frames = f.read_frames(read_len)
buff = []
for frame in frames:
# is frame iterable or just one chan?
if getattr(frame, '__iter__', False):
fval = sum(frame) / len(frame)
else:
fval = frame
buff.append(fval)
# TODO: trim to 1024 or so?
outfft = fft(buff)
spectrum = [
(outfft[y].real
if y < len(outfft) else 0.0)
for y in xrange(fft_pixels)]
points.append(spectrum)
framepos += len(frames)
f.close()
# maximise
return points
示例15: test_read_wave
# 需要导入模块: from scikits.audiolab import Sndfile [as 别名]
# 或者: from scikits.audiolab.Sndfile import read_frames [as 别名]
def test_read_wave():
f = Sndfile("../fcjf0/sa1.wav", 'r')
data = f.read_frames(46797)
data_arr = np.array(data)
#print data_arr
pyplot.figure()
pyplot.specgram(data_arr)
pyplot.show()