本文整理汇总了Python中scipy.io.wavfile.read方法的典型用法代码示例。如果您正苦于以下问题:Python wavfile.read方法的具体用法?Python wavfile.read怎么用?Python wavfile.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.io.wavfile
的用法示例。
在下文中一共展示了wavfile.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wavefile_to_waveform
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def wavefile_to_waveform(wav_file, features_type):
data, sr = sf.read(wav_file)
if features_type == 'vggish':
tmp_name = str(int(np.random.rand(1)*1000000)) + '.wav'
sf.write(tmp_name, data, sr, subtype='PCM_16')
sr, wav_data = wavfile.read(tmp_name)
os.remove(tmp_name)
# sr, wav_data = wavfile.read(wav_file) # as done in VGGish Audioset
assert wav_data.dtype == np.int16, 'Bad sample type: %r' % wav_data.dtype
data = wav_data / 32768.0 # Convert to [-1.0, +1.0]
# at least one second of samples, if not repead-pad
src_repeat = data
while (src_repeat.shape[0] < sr):
src_repeat = np.concatenate((src_repeat, data), axis=0)
data = src_repeat[:sr]
return data, sr
示例2: plotstft
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="gray", channel=0, name='tmp.png', alpha=1, offset=0):
samplerate, samples = wav.read(audiopath)
samples = samples[:, channel]
s = stft(samples, binsize)
sshow, freq = logscale_spec(s, factor=1, sr=samplerate, alpha=alpha)
sshow = sshow[2:, :]
ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel
timebins, freqbins = np.shape(ims)
ims = np.transpose(ims)
# ims = ims[0:256, offset:offset+768] # 0-11khz, ~9s interval
ims = ims[0:256, :] # 0-11khz, ~10s interval
#print "ims.shape", ims.shape
image = Image.fromarray(ims)
image = image.convert('L')
image.save(name)
示例3: read_file
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def read_file(filename):
"""
Read wave file as mono.
Args:
- filename (str) : wave file / path.
Returns:
tuple of sampling rate and audio data.
"""
fs, sig = read(filename=filename)
if (sig.ndim == 1):
samples = sig
else:
samples = sig[:, 0]
return fs, samples
示例4: triangle
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def triangle(t, randfunc=np.random.rand, t0_fac=None): # ramp up then down
height = (0.4 * randfunc() + 0.4) * np.random.choice([-1,1])
width = randfunc()/4 * t[-1] # half-width actually
t0 = 2*width + 0.4 * randfunc()*t[-1] if t0_fac is None else t0_fac*t[-1]
x = height * (1 - np.abs(t-t0)/width)
x[np.where(t < (t0-width))] = 0
x[np.where(t > (t0+width))] = 0
amp_n = (0.1*randfunc()+0.02) # add noise
return x + amp_n*pinknoise(t.shape[0])
# Prelude to read_audio_file
# Tried lots of ways of doing this.. most are slow.
#signal, rate = librosa.load(filename, sr=sr, mono=True, res_type='kaiser_fast') # Librosa's reader is incredibly slow. do not use
#signal, rate = torchaudio.load(filename)#, normalization=True) # Torchaudio's reader is pretty fast but normalization is a problem
#signal = signal.numpy().flatten()
#reader = io_methods.AudioIO # Stylios' file reader. Haven't gotten it working yet
#signal, rate = reader.audioRead(filename, mono=True)
#signal, rate = sf.read('existing_file.wav')
示例5: __init__
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def __init__(self, path, sr=44100, ):
super(FileEffect, self).__init__()
print(" FileEffect: path = ",path)
if (path is None) or (not glob.glob(path+"/Train/target*")) \
or (not glob.glob(path+"/Val/target*")) or ((not glob.glob(path+"/effect_info.ini"))):
print(f"Error: can't file target output files or effect_info.ini in path = {path}")
sys.exit(1) # Yea, this is fatal
self.sr = sr
# read the effect info config file "effect_info.ini"
config = configparser.ConfigParser()
config.read(path+'/effect_info.ini')
self.name = config['effect']['name']+"(files)" # tack on "(files)" to the effect name
#TODO: note that use of 'eval' below could be a potential security issue
self.knob_names = eval(config.get("effect","knob_names"))
self.knob_ranges = np.array(eval(config.get("effect","knob_ranges")))
try:
self.is_inverse = (True == bool(config['effect']['inverse']) )
self.name = "De-"+self.name
except:
pass # Ignore errors we don't require that 'inverse' be defined anywhere in the file
示例6: wavread
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def wavread(fn):
"""Emulate the parts of the matlab wavread function that we need.
y, Fs = wavread(fn)
y is the vector of audio samples, Fs is the frame rate.
Matlab's wavread is used by voicesauce to read in the wav files for
processing. As a consequence, all the translated algorithms assume the
data from the wav file is in matlab form, which in this case means a double
precision float between -1 and 1. The corresponding scipy function returns
the actual integer PCM values from the file, which range between -32768 and
32767. (matlab's wavread *can* return the integers, but does not by
default and voicesauce uses the default). Consequently, after reading the
data using scipy's io.wavfile, we convert to float by dividing each integer
by 32768.
"""
# For reference, I figured this out from:
# http://mirlab.org/jang/books/audiosignalprocessing/matlab4waveRead.asp?title=4-2%20Reading%20Wave%20Files
# XXX: if we need to handle 8 bit files we'll need to detect them and
# special case them here.
Fs, y = wavfile.read(fn)
return y/numpy.float64(32768.0), Fs
示例7: test_read_1
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def test_read_1():
for mmap in [False, True]:
warn_ctx = WarningManager()
warn_ctx.__enter__()
try:
warnings.simplefilter('ignore', wavfile.WavFileWarning)
rate, data = wavfile.read(datafile('test-44100-le-1ch-4bytes.wav'),
mmap=mmap)
finally:
warn_ctx.__exit__()
assert_equal(rate, 44100)
assert_(np.issubdtype(data.dtype, np.int32))
assert_equal(data.shape, (4410,))
del data
示例8: fbank
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def fbank(wav_path, flat=True):
""" Currently grabs log Mel filterbank, deltas and double deltas."""
(rate, sig) = wav.read(wav_path)
if len(sig) == 0:
logger.warning("Empty wav: {}".format(wav_path))
fbank_feat = python_speech_features.logfbank(sig, rate, nfilt=40)
energy = extract_energy(rate, sig)
feat = np.hstack([energy, fbank_feat])
delta_feat = python_speech_features.delta(feat, 2)
delta_delta_feat = python_speech_features.delta(delta_feat, 2)
all_feats = [feat, delta_feat, delta_delta_feat]
if not flat:
all_feats = np.array(all_feats)
# Make time the first dimension for easy length normalization padding
# later.
all_feats = np.swapaxes(all_feats, 0, 1)
all_feats = np.swapaxes(all_feats, 1, 2)
else:
all_feats = np.concatenate(all_feats, axis=1)
# Log Mel Filterbank, with delta, and double delta
feat_fn = wav_path[:-3] + "fbank.npy"
np.save(feat_fn, all_feats)
示例9: detect_MIDI_notes
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def detect_MIDI_notes(self):
"""
The algorithm for calculating midi notes from a given wav file.
"""
(framerate, sample) = wav.read(self.wav_file)
# We need to change the 2 channels into one because STFT works only
# for 1 channel. We could also do STFT for each channel separately.
monoChannel = sample.mean(axis=1)
duration = getDuration(self.wav_file)
midi_notes = []
# Consider only files with a duration longer than 0.2 seconds.
if duration > 0.18:
frequency_power = self.calculateFFT(duration, framerate, monoChannel)
filtered_frequencies = [f for (f, p) in frequency_power]
#self.plot_power_spectrum(frequency_power)
#self.plot_power_spectrum_dB(frequency_power)
f0_candidates = self.get_pitch_candidates_remove_highest_peak(frequency_power)
midi_notes = self.matchWithMIDINotes(f0_candidates)
return midi_notes
示例10: get_data
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def get_data():
"""The data files come from the TSP dataset 16k set"""
_, man = wavfile.read('./wav_data/MA02_04.wav')
rate, woman = wavfile.read('./wav_data/FA01_03.wav')
man = man.astype('float32')
woman = woman.astype('float32')
man_max = np.max(man)
woman_max = np.max(woman)
man /= man_max
woman /= woman_max
shortest = min(len(man), len(woman))
woman = woman[:shortest]
man = man[:shortest]
np.random.seed(101)
noise = np.random.uniform(-1, 1, len(man))
sources = np.stack((woman, man, noise))
A = np.random.uniform(-1, 1, (3, 3))
linear_mix = np.dot(A, sources)
pnl_mix = linear_mix.copy()
pnl_mix[0] = np.tanh(pnl_mix[0])
pnl_mix[1] = (pnl_mix[1] + pnl_mix[1]**3) / 2
pnl_mix[2] = np.exp(pnl_mix[2])
return linear_mix, pnl_mix, A, sources
示例11: mainParkinson
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def mainParkinson():
general_feature_list = []
general_label_list = []
folder = raw_input('Give the name of the folder that you want to read data: ')
if(folder == 'PD'):
healthyCases = os.listdir('/home/gionanide/Theses_2017-2018_2519/Gkagkos/Audio_Files/PD')
for x in healthyCases:
wav = '/'+folder+'/'+str(x)
mfcc_features,inputWav = mfcc_features_extraction(wav)
mean_features(mfcc_features,inputWav,folder,general_feature_list,general_label_list)
folder = raw_input('Give the name of the folder that you want to read data: ')
if(folder == 'HC'):
parkinsonCases = os.listdir('/home/gionanide/Theses_2017-2018_2519/Gkagkos/Audio_Files/HC')
for x in parkinsonCases:
wav = '/'+folder+'/'+str(x)
mfcc_features,inputWav = mfcc_features_extraction(wav)
mean_features(mfcc_features,inputWav,folder,general_feature_list,general_label_list)
#print general_feature_list, general_label_list
#writeFeatures(general_feature_list,general_label_list,wav,folder)
classifyPHC(general_feature_list,general_label_list)
示例12: wavfile_to_examples
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def wavfile_to_examples(wav_file):
"""Convenience wrapper around waveform_to_examples() for a common WAV format.
Args:
wav_file: String path to a file, or a file-like object. The file
is assumed to contain WAV audio data with signed 16-bit PCM samples.
Returns:
See waveform_to_examples.
"""
try:
wav_file = BytesIO(wav_file)
sr, wav_data = wavfile.read(wav_file)
except IOError:
print("Error reading WAV file!")
print("The specified WAV file type is not supported by scipy.io.wavfile.read()")
sys.exit(1)
if wav_data.dtype != np.int16:
raise TypeError('Bad sample type: %r' % wav_data.dtype)
samples = wav_data / 32768.0 # Convert to [-1.0, +1.0]
return waveform_to_examples(samples, sr)
示例13: load_wav
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def load_wav(input_wav_file):
# Load the inputs that we're given
fs, audio = wav.read(input_wav_file)
assert fs == 16000
print('source dB', db(audio))
return audio
示例14: load
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def load(path):
bps, data = wav.read(path)
if len(data.shape) != 1:
data = data[:,0] + data[:,1]
return bps, data
示例15: load
# 需要导入模块: from scipy.io import wavfile [as 别名]
# 或者: from scipy.io.wavfile import read [as 别名]
def load(path):
bps, data = wav.read(path)
if len(data.shape) != 1:
data = data[:,0] + data[:,1]
return bps, data