本文整理汇总了Python中resampy.resample方法的典型用法代码示例。如果您正苦于以下问题:Python resampy.resample方法的具体用法?Python resampy.resample怎么用?Python resampy.resample使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类resampy
的用法示例。
在下文中一共展示了resampy.resample方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convolve
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def convolve(self, impulse_segment, allow_resample=False):
"""Convolve this audio segment with the given impulse segment.
Note that this is an in-place transformation.
:param impulse_segment: Impulse response segments.
:type impulse_segment: AudioSegment
:param allow_resample: Indicates whether resampling is allowed when
the impulse_segment has a different sample
rate from this signal.
:type allow_resample: bool
:raises ValueError: If the sample rate is not match between two
audio segments when resample is not allowed.
"""
if allow_resample and self.sample_rate != impulse_segment.sample_rate:
impulse_segment.resample(self.sample_rate)
if self.sample_rate != impulse_segment.sample_rate:
raise ValueError("Impulse segment's sample rate (%d Hz) is not "
"equal to base signal sample rate (%d Hz)." %
(impulse_segment.sample_rate, self.sample_rate))
samples = signal.fftconvolve(self.samples, impulse_segment.samples,
"full")
self._samples = samples
示例2: test_resampy
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def test_resampy():
""" Compare matlab and librosa resample : FAILING """
from resampy import resample
from pystoi.stoi import FS
import matlab_wrapper
matlab = matlab_wrapper.MatlabSession()
matlab.put('FS', float(FS))
RTOL = 1e-4
for fs in [8000, 11025, 16000, 22050, 32000, 44100, 48000]:
x = np.random.randn(2*fs,)
x_r = resample(x, fs, FS)
matlab.put('x', x)
matlab.put('fs', float(fs))
matlab.eval('x_r = resample(x, FS, fs)')
assert_allclose(x_r, matlab.get('x_r'), atol=ATOL, rtol=RTOL)
示例3: test_nnresample
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def test_nnresample():
""" Compare matlab and nnresample resample : FAILING """
from nnresample import resample
from pystoi.stoi import FS
import matlab_wrapper
matlab = matlab_wrapper.MatlabSession()
matlab.put('FS', float(FS))
RTOL = 1e-4
for fs in [8000, 11025, 16000, 22050, 32000, 44100, 48000]:
x = np.random.randn(2*fs,)
x_r = resample(x, FS, fs)
matlab.put('x', x)
matlab.put('fs', float(fs))
matlab.eval('x_r = resample(x, FS, fs)')
assert_allclose(x_r, matlab.get('x_r'), atol=ATOL, rtol=RTOL)
示例4: resample_musicnet
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def resample_musicnet(file_in, file_out, frame_rate, frame_rate_out):
ratio = frame_rate_out / float(frame_rate)
print('.. resampling {} ({}Hz) into {} ({}Hz)'.format(
file_in, frame_rate, file_out, frame_rate_out))
print('.. sampling with ratio {}'.format(ratio))
resampled_data = {}
with open(file_in, 'rb') as f_in:
data_in = numpy.load(file_in)
n_files = len(data_in.keys())
for i, key in enumerate(data_in):
print('.. aggregating {} ({} / {})'.format(key, i, n_files))
data = data_in[key]
data[0] = resample(data[0], frame_rate, frame_rate_out)
resampled_intervals = []
for interval in data[1]:
resampled_begin = int(interval.begin * ratio)
resampled_end = int(interval.end * ratio)
resampled_interval = Interval(
resampled_begin, resampled_end, interval.data)
resampled_intervals.append(resampled_interval)
data[1] = IntervalTree(resampled_intervals)
resampled_data[key] = data
print('.. saving output')
with open(file_out, 'wb') as f_out:
numpy.savez(f_out, **resampled_data)
示例5: resample
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def resample(self, target_sample_rate, filter='kaiser_best'):
"""Resample the audio to a target sample rate.
Note that this is an in-place transformation.
:param target_sample_rate: Target sample rate.
:type target_sample_rate: int
:param filter: The resampling filter to use one of {'kaiser_best',
'kaiser_fast'}.
:type filter: str
"""
self._samples = resampy.resample(
self.samples, self.sample_rate, target_sample_rate, filter=filter)
self._sample_rate = target_sample_rate
示例6: load_wav
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def load_wav(fname, rate=None):
fp = Sndfile(fname, 'r')
_signal = fp.read_frames(fp.nframes)
_signal = _signal.reshape((-1, fp.channels))
_rate = fp.samplerate
if _signal.ndim == 1:
_signal.reshape((-1, 1))
if rate is not None and rate != _rate:
signal = resampy.resample(_signal, _rate, rate, axis=0, filter='kaiser_best')
else:
signal = _signal
rate = _rate
return signal, rate
示例7: main
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def main():
parser = get_parser()
args = parser.parse_args()
logfmt = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s"
if args.verbose > 0:
logging.basicConfig(level=logging.INFO, format=logfmt)
else:
logging.basicConfig(level=logging.WARN, format=logfmt)
logging.info(get_commandline_args())
with kaldiio.ReadHelper(
args.rspecifier, segments=args.segments
) as reader, file_writer_helper(
args.wspecifier,
filetype=args.filetype,
write_num_frames=args.write_num_frames,
compress=args.compress,
compression_method=args.compression_method,
) as writer:
for utt_id, (rate, array) in reader:
array = array.astype(numpy.float32)
if args.fs is not None and rate != args.fs:
array = resampy.resample(array, rate, args.fs, axis=0)
if args.normalize is not None and args.normalize != 1:
array = array / (1 << (args.normalize - 1))
spc = spectrogram(
x=array,
n_fft=args.n_fft,
n_shift=args.n_shift,
win_length=args.win_length,
window=args.window,
)
writer[utt_id] = spc
示例8: main
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def main():
parser = get_parser()
args = parser.parse_args()
logfmt = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s"
if args.verbose > 0:
logging.basicConfig(level=logging.INFO, format=logfmt)
else:
logging.basicConfig(level=logging.WARN, format=logfmt)
logging.info(get_commandline_args())
with kaldiio.ReadHelper(
args.rspecifier, segments=args.segments
) as reader, file_writer_helper(
args.wspecifier,
filetype=args.filetype,
write_num_frames=args.write_num_frames,
compress=args.compress,
compression_method=args.compression_method,
) as writer:
for utt_id, (rate, array) in reader:
array = array.astype(numpy.float32)
if args.fs is not None and rate != args.fs:
array = resampy.resample(array, rate, args.fs, axis=0)
if args.normalize is not None and args.normalize != 1:
array = array / (1 << (args.normalize - 1))
lmspc = logmelspectrogram(
x=array,
fs=args.fs,
n_mels=args.n_mels,
n_fft=args.n_fft,
n_shift=args.n_shift,
win_length=args.win_length,
window=args.window,
fmin=args.fmin,
fmax=args.fmax,
)
writer[utt_id] = lmspc
示例9: waveform_to_examples
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def waveform_to_examples(data, sample_rate):
# Convert to mono.
if len(data.shape) > 1:
data = np.mean(data, axis=1)
# Resample to the rate assumed by VGGish.
if sample_rate != vggish_params.SAMPLE_RATE:
data = resampy.resample(data, sample_rate, vggish_params.SAMPLE_RATE)
# Compute log mel spectrogram features.
log_mel = mel_features.log_mel_spectrogram(
data,
audio_sample_rate=vggish_params.SAMPLE_RATE,
log_offset=vggish_params.LOG_OFFSET,
window_length_secs=vggish_params.STFT_WINDOW_LENGTH_SECONDS,
hop_length_secs=vggish_params.STFT_HOP_LENGTH_SECONDS,
num_mel_bins=vggish_params.NUM_MEL_BINS,
lower_edge_hertz=vggish_params.MEL_MIN_HZ,
upper_edge_hertz=vggish_params.MEL_MAX_HZ)
# Frame features into examples.
features_sample_rate = 1.0 / vggish_params.STFT_HOP_LENGTH_SECONDS
example_window_length = int(round(
vggish_params.EXAMPLE_WINDOW_SECONDS * features_sample_rate))
example_hop_length = int(round(
vggish_params.EXAMPLE_HOP_SECONDS * features_sample_rate))
log_mel_examples = mel_features.frame(
log_mel,
window_length=example_window_length,
hop_length=example_hop_length)
return log_mel_examples
示例10: waveform_to_examples_subtract_bg
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def waveform_to_examples_subtract_bg(data, sample_rate, bg):
# Convert to mono.
if len(data.shape) > 1:
data = np.mean(data, axis=1)
# Resample to the rate assumed by VGGish.
if sample_rate != vggish_params.SAMPLE_RATE:
data = resampy.resample(data, sample_rate, vggish_params.SAMPLE_RATE)
# Compute log mel spectrogram features.
log_mel = mel_features.log_mel_spectrogram_subtract_bg(
data,
bg,
audio_sample_rate=vggish_params.SAMPLE_RATE,
log_offset=vggish_params.LOG_OFFSET,
window_length_secs=vggish_params.STFT_WINDOW_LENGTH_SECONDS,
hop_length_secs=vggish_params.STFT_HOP_LENGTH_SECONDS,
num_mel_bins=vggish_params.NUM_MEL_BINS,
lower_edge_hertz=vggish_params.MEL_MIN_HZ,
upper_edge_hertz=vggish_params.MEL_MAX_HZ)
# Frame features into examples.
features_sample_rate = 1.0 / vggish_params.STFT_HOP_LENGTH_SECONDS
example_window_length = int(round(
vggish_params.EXAMPLE_WINDOW_SECONDS * features_sample_rate))
example_hop_length = int(round(
vggish_params.EXAMPLE_HOP_SECONDS * features_sample_rate))
log_mel_examples = mel_features.frame(
log_mel,
window_length=example_window_length,
hop_length=example_hop_length)
return log_mel_examples
示例11: test_quality_sine
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def test_quality_sine(sr_orig, sr_new, fil, rms):
FREQ = 512.0
DURATION = 2.0
x = make_tone(FREQ, sr_orig, DURATION)
y = make_tone(FREQ, sr_new, DURATION)
y_pred = resampy.resample(x, sr_orig, sr_new, filter=fil)
idx = slice(sr_new // 2, - sr_new//2)
err = np.mean(np.abs(y[idx] - y_pred[idx]))
assert err <= rms, '{:g} > {:g}'.format(err, rms)
示例12: test_quality_sweep
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def test_quality_sweep(sr_orig, sr_new, fil, rms):
FREQ = 8192
DURATION = 5.0
x = make_sweep(FREQ, sr_orig, DURATION)
y = make_sweep(FREQ, sr_new, DURATION)
y_pred = resampy.resample(x, sr_orig, sr_new, filter=fil)
idx = slice(sr_new // 2, - sr_new//2)
err = np.mean(np.abs(y[idx] - y_pred[idx]))
assert err <= rms, '{:g} > {:g}'.format(err, rms)
示例13: test_shape
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def test_shape(axis):
sr_orig = 100
sr_new = sr_orig // 2
X = np.random.randn(sr_orig, sr_orig, sr_orig)
Y = resampy.resample(X, sr_orig, sr_new, axis=axis)
target_shape = list(X.shape)
target_shape[axis] = target_shape[axis] * sr_new // sr_orig
assert target_shape == list(Y.shape)
示例14: test_bad_sr
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def test_bad_sr(sr_orig, sr_new):
x = np.zeros(100)
resampy.resample(x, sr_orig, sr_new)
示例15: test_bad_rolloff
# 需要导入模块: import resampy [as 别名]
# 或者: from resampy import resample [as 别名]
def test_bad_rolloff(rolloff):
x = np.zeros(100)
resampy.resample(x, 100, 50, filter='sinc_window', rolloff=rolloff)