本文整理汇总了Python中soundfile.read函数的典型用法代码示例。如果您正苦于以下问题:Python read函数的具体用法?Python read怎么用?Python read使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _load_sounds
def _load_sounds(self):
self._bar_audio_data, self._bar_audio_fs = soundfile.read(self._bar_file)
self._beat_audio_data, self._beat_audio_fs = soundfile.read(self._beat_file)
if self._division_file:
self._division_audio_data, self._division_audio_fs = soundfile.read(self._division_file)
else:
self._division_audio_data, self._division_audio_fs = None, None
开发者ID:trueneu,项目名称:metronomiconic,代码行数:7,代码来源:metronomiconic.py
示例2: main
def main():
#fs, bg_signal = wavfile.read(sys.argv[1])
if argv[1] == 'batch':
files = []
for f in os.listdir(argv[2]):
if os.path.splitext(f)[1] == ".flac":
files.append(f)
args = [(f, argv[2], argv[3]) for f in files]
pool = multiprocessing.Pool(12)
r = pool.map_async(compute_vad, args)
r.wait()
pool.close()
pool.join()
#for a in args:
# compute_vad(a)
else:
bg_signal, fs = soundfile.read(argv[1])
ltsd = LTSD_VAD()
bg_signal=bg_signal[:2000]
print(bg_signal)
ltsd.init_params_by_noise(fs, bg_signal)
signal, fs = soundfile.read(argv[1])
#vaded_signal = ltsd.filter(signal)
segments, sig_len = ltsd.segments(signal)
print(ltsd.segments(signal)[0])
开发者ID:jlep,项目名称:vad,代码行数:25,代码来源:ad-ltsd.py
示例3: test_write_non_seekable_file
def test_write_non_seekable_file(file_w):
with sf.SoundFile(file_w, 'w', 44100, 1, format='XI') as f:
assert not f.seekable()
assert f.frames == 0
f.write(data_mono)
assert f.frames == len(data_mono)
with pytest.raises(RuntimeError) as excinfo:
f.seek(2)
assert "unseekable" in str(excinfo.value)
with sf.SoundFile(filename_new) as f:
assert not f.seekable()
assert f.frames == len(data_mono)
data = f.read(3, dtype='int16')
assert np.all(data == data_mono[:3])
data = f.read(666, dtype='int16')
assert np.all(data == data_mono[3:])
with pytest.raises(RuntimeError) as excinfo:
f.seek(2)
assert "unseekable" in str(excinfo.value)
with pytest.raises(ValueError) as excinfo:
f.read()
assert "frames" in str(excinfo.value)
data, fs = sf.read(filename_new, dtype='int16')
assert np.all(data == data_mono)
assert fs == 44100
with pytest.raises(ValueError) as excinfo:
sf.read(filename_new, start=3)
assert "start is only allowed for seekable files" in str(excinfo.value)
开发者ID:bastibe,项目名称:PySoundFile,代码行数:34,代码来源:test_pysoundfile.py
示例4: test_read_into_non_contiguous_out
def test_read_into_non_contiguous_out(file_stereo_r):
out = np.empty(data_stereo.shape[::-1], dtype='float64')
if getattr(sys, 'pypy_version_info', (999,)) < (2, 6):
# The test for C-contiguous doesn't work with PyPy 2.5.0
sf.read(file_stereo_r, out=out.T)
else:
with pytest.raises(ValueError) as excinfo:
sf.read(file_stereo_r, out=out.T)
assert "C-contiguous" in str(excinfo.value)
开发者ID:dewabayu,项目名称:PySoundFile,代码行数:9,代码来源:test_pysoundfile.py
示例5: get_traindata
def get_traindata(gesfile, audio_f, dt,
audio_fargs=None, wavfile=None, ignore_f0=True):
"""Get input, output pairs for supervised learning training or testing.
Parameters
----------
dt : float
Sampling step size for the gesture and
gesfile : str
Path to a .ges gesture file (XML format).
audio_f : function
A function that will be applied to the audio stream
audio_fargs : dict, optional
Keyword arguments that will be provided to ``audio_f``.
By default, audio, sampling rate, and dt will be provided.
wavfile : str, optional
A .wav file that corresponds to the ``gesfile``.
If specified but the file does not exist, it will be generated.
If not specified, audio will be synthesized but not saved.
"""
gs = parse_ges(gesfile, ignore_f0=ignore_f0)
y = gs.trajectory(dt=dt)
if wavfile is None:
audio, fs = synthesize(gesfile)
elif not os.path.exists(wavfile):
synthesize(gesfile, wavfile)
audio, fs = sf.read(wavfile)
else:
audio, fs = sf.read(wavfile)
audio_fargs = {} if audio_fargs is None else audio_fargs.copy()
audio_fargs.update({'audio': audio, 'fs': fs, 'dt': dt})
x = audio_f(**audio_fargs)
# For some reason, the wav file size and the gesture trajectory size
# are often off by one or two. Here, we lengthen or shorten ``y``,
# assuming that VTL is doing it correctly.
# Not sure if that assumption is correct.
if x.shape[0] > y.shape[0]:
# Extend y by n timesteps
toadd = np.tile(y[np.newaxis, -1], (x.shape[0] - y.shape[0], 1))
y = np.concatenate((y, toadd))
if x.shape[0] < y.shape[0]:
# Shorten y by n timesteps
todelete = list(range(x.shape[0], y.shape[0]))
y = np.delete(y, todelete, 0)
assert x.shape[0] == y.shape[0], "Misaligned; %s %s" % (x.shape, y.shape)
return x, y, fs
开发者ID:tbekolay,项目名称:phd,代码行数:50,代码来源:__init__.py
示例6: show_file_hystogram
def show_file_hystogram(filename):
# data, sample_rate = sf.read(filename) # load the data
data, sample_rate = sf.read(filename) # load the data
#a = data.T[0] # this is a two channel soundtrack, I get the first track
b=[(ele/2**8.)*2-1 for ele in data] # this is 8-bit track, b is now normalized on [-1,1)
#print("vector more than 0 vals number: {0}".format(len(c)))
c = np.fft.fft(b) # calculate fourier transform (complex numbers list)
d = len(c)/2 - 1 # you only need half of the fft list (real signal symmetry)
#d = len(c) # you only need half of the fft list (real signal symmetry)
#print("complex val: {0} abs(complex val): {1}".format(c[0], abs(c[0])))
k = np.arange(d)
fs = 8000 # 8kHz
T = d/fs
frqLabel = k/T
c = c[:d]
c = abs(c)
c = [round(i, 1) for i in c]
print("vector dimensionality: {0}".format(d))
print("min: {0} max:{1}".format(min(c), max(c)))
plt.gca().set_ylim([min(c), 20])
plt.plot(frqLabel, c, 'g')
plt.show()
开发者ID:denis-plotnikov,项目名称:code,代码行数:26,代码来源:wav_hystogramm_sf.py
示例7: experimental_random_segmentation
def experimental_random_segmentation(audio_input, segments, options, sr):
"""
(branch mir-dev en Sonidos Mutantes)
Segmenta con valores aleatorios según opciones
"""
outputPath = options['outputPath']
min_dur,max_dur = options['duration']
try:
x = read(audio_input)[0]
for i in range(segments):
while(1):
pos = random.uniform(0.,2.) #posición en el archivo normalizada
dur = random.uniform(min_dur,max_dur)
durSamples = dur*sr
posSamples = int( pos*len(x) )
if posSamples+durSamples<len(x):
break
signalOut = x[pos:pos+durSamples]
baseName = os.path.splitext(filename)[0].split('/')[-1]
if not os.path.exists(outputPath):
os.makedirs(outputPath)
print("Creating samples directory")
time.sleep(4)
outputFilename = outputPath+'/'+baseName+'_sample'+str(i)+'.wav'
write_file(outputFilename,signalOut,sr)
print(("File generated: %s"%outputFilename))
time.sleep(1)
except Exception as e:
print(("Error: %s"%e))
开发者ID:MarsCrop,项目名称:apicultor,代码行数:31,代码来源:RandomSegmentation.py
示例8: _process_function
def _process_function(self, track, user_function, estimates_dir, evaluate):
# load estimates from disk instead of processing
if user_function is None:
track_estimate_dir = op.join(
estimates_dir,
track.subset,
track.filename
)
user_results = {}
for target_path in glob.glob(track_estimate_dir + '/*.wav'):
target_name = op.splitext(
os.path.basename(target_path)
)[0]
try:
target_audio, rate = sf.read(
target_path,
always_2d=True
)
user_results[target_name] = target_audio
except RuntimeError:
pass
else:
# call the user provided function
user_results = user_function(track)
if estimates_dir and not evaluate and user_function is not None:
self._save_estimates(user_results, track, estimates_dir)
if evaluate:
self._evaluate_estimates(user_results, track)
开发者ID:faroit,项目名称:dsdtools,代码行数:28,代码来源:__init__.py
示例9: test_buffer_write_with_bytes
def test_buffer_write_with_bytes(sf_stereo_w):
b = b"\x01\x00\xFF\xFF\xFF\x00\x00\xFF"
sf_stereo_w.buffer_write(b, 'short')
sf_stereo_w.close()
data, fs = sf.read(filename_new, dtype='int16')
assert np.all(data == [[1, -1], [255, -256]])
assert fs == 44100
开发者ID:dewabayu,项目名称:PySoundFile,代码行数:7,代码来源:test_pysoundfile.py
示例10: importRirs
def importRirs(downloadDir, insertIntoDbF):
url = "http://www.openslr.org/resources/13/RWCP.tar.gz"
filename = join(downloadDir, "rwcp.tar.gz")
unpackDir = join(downloadDir, "rwcp")
dl = util.FileDownloader(url, filename)
dl.download()
dl.unpackTo(unpackDir)
files = []
for root, dirnames, filenames in os.walk(join(unpackDir, "RWCP/micarray/MICARRAY/data1")):
for filename in filenames:
if filename[-2:] != ".1":
continue # we only use the front microphone
files.append(join(root, filename))
pattern = re.compile("(circle|cirline)\/(\w{3})\/imp(\d{3})")
bar = util.ConsoleProgressBar()
bar.start("Import RWCP")
for i, file in enumerate(sorted(files)): # we sort to get same identifiers cross-platform
m = pattern.search(file)
assert m, "Could parse room from path ({})".format(file)
room = m.group(2)
identifier = "{:04d}_{}_{}".format(i, room.lower(), m.group(3))
x, fs = sf.read(file, dtype="float32", **RawFormat)
x /= max(abs(x))
x = (3 ** 15 * x).astype(np.int16)
insertIntoDbF((x, fs), identifier, {"source": "RWCP", "room": room})
bar.progress(i / len(files))
bar.end()
开发者ID:Marvin182,项目名称:rir-database,代码行数:33,代码来源:Rwcp.py
示例11: test_rplus_append_data
def test_rplus_append_data(sf_stereo_rplus):
sf_stereo_rplus.seek(0, sf.SEEK_END)
sf_stereo_rplus.write(data_stereo / 2)
sf_stereo_rplus.close()
data, fs = sf.read(filename_new)
assert np.all(data[:len(data_stereo)] == data_stereo)
assert np.all(data[len(data_stereo):] == data_stereo / 2)
开发者ID:dewabayu,项目名称:PySoundFile,代码行数:7,代码来源:test_pysoundfile.py
示例12: test_process_multiple
def test_process_multiple(self):
keyword_file_names = ['alexa', 'americano', 'avocado', 'blueberry', 'bumblebee', 'caterpillar', 'christina',
'dragonfly', 'flamingo', 'francesca', 'grapefruit', 'grasshopper', 'iguana', 'picovoice',
'pineapple', 'porcupine', 'raspberry', 'terminator', 'vancouver']
keyword_file_paths = [
self._abs_path('../../resources/keyword_files/%s_%s.ppn' % (name, self._keyword_file_extension())) for name in keyword_file_names]
porcupine = Porcupine(
library_path=self._library_path(),
model_file_path=self._abs_path('../../lib/common/porcupine_params.pv'),
keyword_file_paths=keyword_file_paths,
sensitivities=[0.5] * len(keyword_file_paths))
audio, sample_rate = soundfile.read(
self._abs_path('../../resources/audio_samples/multiple_keywords.wav'),
dtype='int16')
assert sample_rate == porcupine.sample_rate
num_frames = len(audio) // porcupine.frame_length
results = []
for i in range(num_frames):
frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length]
result = porcupine.process(frame)
if result >= 0:
results.append(result)
self.assertEqual(results, [15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18])
porcupine.delete()
开发者ID:2vin,项目名称:pg_voice,代码行数:30,代码来源:test_porcupine.py
示例13: test_write_int_data_to_float_file
def test_write_int_data_to_float_file(file_inmemory):
"""This is a very uncommon use case."""
sf.write(file_inmemory, data_mono, 44100, format='WAV', subtype='FLOAT')
file_inmemory.seek(0)
read, fs = sf.read(file_inmemory, always_2d=False, dtype='float32')
assert np.all(read == data_mono)
assert fs == 44100
开发者ID:dewabayu,项目名称:PySoundFile,代码行数:7,代码来源:test_pysoundfile.py
示例14: play
def play(self, file_path):
if self.convert:
self.convert_mp3_to_wav(file_path_mp3=file_path)
data, fs = sf.read(file_path)
sd.play(data, fs)
sd.wait()
开发者ID:igorstarki,项目名称:kalliope,代码行数:7,代码来源:sounddeviceplayer.py
示例15: get_data
def get_data(rootdir = TIMIT_main_dir):
inputs = []
targets = []
for dir_path, sub_dirs, files in os.walk(rootdir):
for file in files:
if (os.path.join(dir_path, file)).endswith('.wav'):
wav_file_name = os.path.join(dir_path, file)
input_data, f_s = sf.read(wav_file_name)
# mfcc_feat = MFCC_input(mfcc(input_data,f_s))
mfcc_feat = mfcc(input_data,f_s)
#Delta features
delta_feat = mfcc_feat[:-1]-mfcc_feat[1:]
#Delta-Delta features
deltadelta_feat = delta_feat[:-1]-delta_feat[1:]
#Removing the first two frames
mfcc_feat = mfcc_feat[2:]
delta_feat = delta_feat[1:]
#Concatenating mfcc, delta and delta-delta features
full_input = np.concatenate((mfcc_feat,delta_feat,deltadelta_feat), axis=1)
inputs.append(np.asarray(full_input, dtype=theano.config.floatX))#Rakeshvar wants one frame along each column but i am using Lasagne
text_file_name = wav_file_name[:-4] + '.txt'
target_data_file = open(text_file_name)
target_data = str(target_data_file.read()).lower().translate(None, '!:,".;?')
# target_data = str(target_data_file.read()).lower().translate(str.maketrans('','', '!:,".;?'))
target_data = target_data[8:-1]#No '.' in lexfree dictionary
targets.append(target_data)
return inputs, targets
开发者ID:KGPML,项目名称:KGP-ASR,代码行数:31,代码来源:TIMIT_utils.py
示例16: do_segmentation
def do_segmentation(audio_input, audio_input_from_filename = True, audio_input_from_array = False, sec_len = 6, save_file = True):
lenght = int(sec_len) * 10
if audio_input_from_filename == True:
x = read(audio_input)[0]
if (audio_input_from_filename == False) and audio_input_from_array == True:
x = audio_input
retriever = MIR(x, 44100)
frame_size = 4096
hop_size = 1024
segments = [len(frame) / 44100 for frame in retriever.FrameGenerator()]
output = []
for segment in segments:
sample = int(segment*44100)
output.append(x[:sample*lenght]) #extend duration of segment
output = choice(output)
if save_file == True:
baseName = os.path.splitext(audio_input)[0].split('/')[-1]
outputFilename = 'samples'+'/'+baseName+'_sample'+'.wav'
write_file(outputFilename, 44100, output)
print(("File generated: %s"%outputFilename))
if save_file == False:
return output
开发者ID:MarsCrop,项目名称:apicultor,代码行数:31,代码来源:DoSegmentation.py
示例17: test_buffer_write
def test_buffer_write(sf_stereo_w):
buf = np.array([[1, 2], [-1, -2]], dtype='int16')
sf_stereo_w.buffer_write(buf, 'short')
sf_stereo_w.close()
data, fs = sf.read(filename_new, dtype='int16')
assert np.all(data == buf)
assert fs == 44100
开发者ID:dewabayu,项目名称:PySoundFile,代码行数:7,代码来源:test_pysoundfile.py
示例18: update_max_len
def update_max_len(file_path_list, max_len):
tmp_max_len = 0
# Update the max length based on the given dataset
signal_set = set()
for file_path in file_path_list:
file_list = open(file_path)
for line in file_list:
line = line.strip().split()
if len(line) < 2:
print 'Wrong audio list file record in the line:', line
continue
file_str = line[0]
if file_str in signal_set:
continue
signal_set.add(file_str)
signal, rate = sf.read(file_str) # signal: sample values,rate: sample rate
if len(signal.shape) > 1:
signal = signal[:, 0]
if rate != FRAME_RATE:
# up-sample or down-sample for predefined sample rate
signal = resampy.resample(signal, rate, FRAME_RATE, filter='kaiser_fast')
if len(signal) > tmp_max_len:
tmp_max_len = len(signal)
file_list.close()
if tmp_max_len < max_len:
max_len = tmp_max_len
return max_len
开发者ID:zhaoforever,项目名称:ASAM,代码行数:27,代码来源:config.py
示例19: _transform
def _transform(self, row):
if len(row) == 7:
path, channel, name, spkid, dataset, start_time, end_time = row
else:
path, channel, name, spkid, dataset = row[:5]
start_time = None
end_time = None
# ====== read audio ====== #
# for voxceleb1
if dataset == 'voxceleb1':
with open(path, 'rb') as f:
y, sr = sf.read(f)
y = pp.signal.resample(y, sr_orig=sr, sr_new=8000,
best_algorithm=True)
sr = 8000
# for sre, fisher and swb
elif (dataset[:3] == 'sre' or
dataset == 'swb' or
dataset == 'fisher'):
with open(path, 'rb') as f:
y, sr = sf.read(f)
y = pp.signal.resample(y, sr_orig=sr, sr_new=8000,
best_algorithm=True)
if y.ndim == 2:
y = y[:, int(channel)]
sr = 8000
# all other dataset: mix6, voxceleb2
else:
y, sr = pp.signal.anything2wav(inpath=path, outpath=None,
channel=channel,
dataset=dataset,
start=start_time, end=end_time,
sample_rate=Config.SAMPLE_RATE,
return_data=True)
# ====== error happen ignore file ====== #
if len(y) == 0:
return None
# ====== remove DC offset ====== #
y = y - np.mean(y, 0)
duration = max(y.shape) / sr
ret = {'raw': y, 'sr': sr, 'duration': duration, # in second
'path': path,
'spkid': spkid,
'name': name,
'dsname': dataset}
return ret
开发者ID:imito,项目名称:odin,代码行数:46,代码来源:feature_recipes.py
示例20: getData
def getData(self, params):
ticker = params['ticker']
import soundfile
sig, samplerate = soundfile.read(ticker + ".wav")
df = pd.Series({"filename": ticker, "length": len(sig),
"samplerate": samplerate})
df = df.to_frame().transpose()
return df
开发者ID:sequana,项目名称:presentations,代码行数:8,代码来源:myapp.py
注:本文中的soundfile.read函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论