本文整理汇总了Python中soundfile.write方法的典型用法代码示例。如果您正苦于以下问题:Python soundfile.write方法的具体用法?Python soundfile.write怎么用?Python soundfile.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类soundfile
的用法示例。
在下文中一共展示了soundfile.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: doFileStuff
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def doFileStuff(line,isSlow):
myobj = gTTS(text=line, lang='en', slow=isSlow)
myobj.save("placeholder.mp3")
y, sr = librosa.load("placeholder.mp3")
data = librosa.resample(y, sr, SAMPLE_RATE)
librosa.output.write_wav('placeholder.wav', data, SAMPLE_RATE)
d, sr = sf.read('placeholder.wav')
sf.write('placeholder.wav', d, sr)
y, sr = librosa.load("placeholder.mp3")
lowData = librosa.resample(y, sr, SAMPLE_RATE*LOW_FACTOR)
librosa.output.write_wav('lowPlaceholder.wav', lowData, SAMPLE_RATE)
d, sr = sf.read('lowPlaceholder.wav')
sf.write('lowPlaceholder.wav', d, sr)
return data
示例2: get_num_frames_writer
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def get_num_frames_writer(write_num_frames: str):
"""get_num_frames_writer
Examples:
>>> get_num_frames_writer('ark,t:num_frames.txt')
"""
if write_num_frames is not None:
if ":" not in write_num_frames:
raise ValueError(
'Must include ":", write_num_frames={}'.format(write_num_frames)
)
nframes_type, nframes_file = write_num_frames.split(":", 1)
if nframes_type != "ark,t":
raise ValueError(
"Only supporting text mode. "
"e.g. --write-num-frames=ark,t:foo.txt :"
"{}".format(nframes_type)
)
return open(nframes_file, "w", encoding="utf-8")
示例3: wavefile_to_waveform
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [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
示例4: prepro_audio
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def prepro_audio(source_path, target_path, format=None, sr=None, db=None):
"""
Read a wav, change sample rate, format, and average decibel and write to target path.
:param source_path: source wav file path
:param target_path: target wav file path
:param sr: sample rate.
:param format: output audio format.
:param db: decibel.
"""
sound = AudioSegment.from_file(source_path, format)
if sr:
sound = sound.set_frame_rate(sr)
if db:
change_dBFS = db - sound.dBFS
sound = sound.apply_gain(change_dBFS)
sound.export(target_path, 'wav')
示例5: output
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def output(self, filename, format=None):
"""
Write the samples out to the given filename.
Parameters
----------
filename : str
The path to write the audio on disk.
This can be any format supported by `pysoundfile`, including
`WAV`, `FLAC`, or `OGG` (but not `mp3`).
format : str
If provided, explicitly set the output encoding format.
See `soundfile.available_formats`.
"""
sf.write(filename, self.raw_samples.T, int(self.sample_rate), format=format)
示例6: to_wav_file
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def to_wav_file(self, filepath, dtype='float32'):
"""Save audio segment to disk as wav file.
:param filepath: WAV filepath or file object to save the
audio segment.
:type filepath: basestring|file
:param dtype: Subtype for audio file. Options: 'int16', 'int32',
'float32', 'float64'. Default is 'float32'.
:type dtype: str
:raises TypeError: If dtype is not supported.
"""
samples = self._convert_samples_from_float32(self._samples, dtype)
subtype_map = {
'int16': 'PCM_16',
'int32': 'PCM_32',
'float32': 'FLOAT',
'float64': 'DOUBLE'
}
soundfile.write(
filepath,
samples,
self._sample_rate,
format='WAV',
subtype=subtype_map[dtype])
示例7: compute_scaler
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def compute_scaler(args):
"""Compute and write out scaler from already packed feature file. Using
scaler in training neural network can speed up training.
"""
workspace = args.workspace
feat_type = args.feat_type
# Load packed features.
t1 = time.time()
packed_feat_path = os.path.join(workspace, "packed_features", feat_type, "train.p")
[x_list, _, _] = cPickle.load(open(packed_feat_path, 'rb'))
# Compute scaler.
x_all = np.concatenate(x_list)
scaler = preprocessing.StandardScaler(with_mean=True, with_std=True).fit(x_all)
print(scaler.mean_)
print(scaler.scale_)
# Save out scaler.
out_path = os.path.join(workspace, "scalers", feat_type, "scaler.p")
create_folder(os.path.dirname(out_path))
pickle.dump(scaler, open(out_path, 'wb'))
print("Compute scaler finished! %s s" % (time.time() - t1,))
示例8: process
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def process(wavefile, outdir, pattern=''):
_, base = os.path.split(wavefile)
if pattern:
if pattern not in base:
return
# print base
raw_in = os.path.join(outdir, base.replace('.wav','.raw'))
raw_out = os.path.join(outdir, base.replace('.wav','_norm.raw'))
logfile = os.path.join(outdir, base.replace('.wav','.log'))
wav_out = os.path.join(outdir, base)
data, samplerate = sf.read(wavefile, dtype='int16')
sf.write(raw_in, data, samplerate, subtype='PCM_16')
os.system('%s -log %s -q -lev -26.0 -sf %s %s %s'%(sv56, logfile, samplerate, raw_in, raw_out))
norm_data, samplerate = sf.read(raw_out, dtype='int16', samplerate=samplerate, channels=1, subtype='PCM_16')
sf.write(wav_out, norm_data, samplerate)
os.system('rm %s %s'%(raw_in, raw_out))
示例9: copy_synth_SSRN_GL
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def copy_synth_SSRN_GL(hp, outdir):
safe_makedir(outdir)
dataset = load_data(hp, mode="synthesis")
fnames, texts = dataset['fpaths'], dataset['texts']
bases = [basename(fname) for fname in fnames]
mels = [np.load(os.path.join(hp.coarse_audio_dir, base + '.npy')) for base in bases]
lengths = [a.shape[0] for a in mels]
mels = list2batch(mels, 0)
g = SSRNGraph(hp, mode="synthesize"); print("Graph (ssrn) loaded")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
ssrn_epoch = restore_latest_model_parameters(sess, hp, 'ssrn')
print('Run SSRN...')
Z = synth_mel2mag(hp, mels, g, sess)
for i, mag in enumerate(Z):
print("Working on %s"%(bases[i]))
mag = mag[:lengths[i]*hp.r,:] ### trim to generated length
wav = spectrogram2wav(hp, mag)
soundfile.write(outdir + "/%s.wav"%(base), wav, hp.sr)
示例10: write_audio_file
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def write_audio_file(filepath, v_signal, fs, norm=0.98):
'''
norm: If None, no normalisation is applied. If it is a float number,
it is the target value (absolute) for the normalisation.
'''
# Normalisation:
if norm is not None:
v_signal = norm * v_signal / np.max(np.abs(v_signal)) # default
# Write:
sf.write(filepath, v_signal, fs)
return
#------------------------------------------------------------------------------
# data_type: 'magnitude', 'phase' or 'zeros' (for zero padding), 'complex'
示例11: __setitem__
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def __setitem__(self, key: str, value):
rate, signal = value
assert isinstance(rate, int), type(rate)
assert isinstance(signal, np.ndarray), type(signal)
if signal.ndim not in (1, 2):
raise RuntimeError(f"Input signal must be 1 or 2 dimension: {signal.ndim}")
if signal.ndim == 1:
signal = signal[:, None]
wav = self.dir / f"{key}.{self.format}"
wav.parent.mkdir(parents=True, exist_ok=True)
soundfile.write(str(wav), signal, rate)
self.fscp.write(f"{key} {wav}\n")
# Store the file path
self.data[key] = str(wav)
示例12: pipe_wav
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def pipe_wav(tmp_path):
p = tmp_path / "wav.scp"
soundfile.write(
tmp_path / "a.wav",
np.random.randint(-100, 100, (160000,), dtype=np.int16),
16000,
)
soundfile.write(
tmp_path / "b.wav",
np.random.randint(-100, 100, (80000,), dtype=np.int16),
16000,
)
with p.open("w") as f:
f.write(f"a {tmp_path / 'a.wav'}\n")
f.write(f"b {tmp_path / 'b.wav'}\n")
return str(p)
示例13: test_SoundScpReader_normalize
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def test_SoundScpReader_normalize(tmp_path: Path):
audio_path1 = tmp_path / "a1.wav"
audio1 = np.random.randint(-100, 100, 16, dtype=np.int16)
audio_path2 = tmp_path / "a2.wav"
audio2 = np.random.randint(-100, 100, 16, dtype=np.int16)
audio1 = audio1.astype(np.float64) / (np.iinfo(np.int16).max + 1)
audio2 = audio2.astype(np.float64) / (np.iinfo(np.int16).max + 1)
soundfile.write(audio_path1, audio1, 16)
soundfile.write(audio_path2, audio2, 16)
p = tmp_path / "dummy.scp"
with p.open("w") as f:
f.write(f"abc {audio_path1}\n")
f.write(f"def {audio_path2}\n")
desired = {"abc": (16, audio1), "def": (16, audio2)}
target = SoundScpReader(p, normalize=True)
for k in desired:
rate1, t = target[k]
rate2, d = desired[k]
assert rate1 == rate2
np.testing.assert_array_equal(t, d)
示例14: test_mwf
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def test_mwf(self):
true_mwf, fs = sf.read('wavs/mwf.wav')
out_mwf = MWF_Oracle(self.mix, self.nn, self.spk, frame_len=self.frame_len, frame_step=self.frame_step)
sf.write('wavs/test_out.wav', out_mwf, self.fs)
out_mwf, fs = sf.read('wavs/test_out.wav')
np.testing.assert_equal(out_mwf, true_mwf)
# def test_bfi(self):
#
# out_bfi = BeamformIt(self.mix).astype('float64')
#
# sf.write('wavs/tout.wav', out_bfi, 8000)
#
# true_bfi, fs = sf.read('wavs/bfi.wav')
# out_bfi, fs = sf.read('wavs/tout.wav')
#
# np.testing.assert_equal(out_bfi, true_bfi)
示例15: get_num_frames_writer
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import write [as 别名]
def get_num_frames_writer(write_num_frames: str):
"""get_num_frames_writer
Examples:
>>> get_num_frames_writer('ark,t:num_frames.txt')
"""
if write_num_frames is not None:
if ':' not in write_num_frames:
raise ValueError('Must include ":", write_num_frames={}'
.format(write_num_frames))
nframes_type, nframes_file = write_num_frames.split(':', 1)
if nframes_type != 'ark,t':
raise ValueError(
'Only supporting text mode. '
'e.g. --write-num-frames=ark,t:foo.txt :'
'{}'.format(nframes_type))
return open(nframes_file, 'w', encoding='utf-8')