本文整理汇总了Python中librosa.resample方法的典型用法代码示例。如果您正苦于以下问题:Python librosa.resample方法的具体用法?Python librosa.resample怎么用?Python librosa.resample使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类librosa
的用法示例。
在下文中一共展示了librosa.resample方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: doFileStuff
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [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: sample_rate
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def sample_rate(self):
"""
``int``
Sample rate associated with this object. If audio was read from a file, the sample
rate will be set to the sample rate associated with the file. If this object was
initialized from an array then the sample rate is set upon init. This property is
read-only. To change the sample rate, use :func:`resample`.
Notes:
This property is read-only and cannot be set directly. To change
See Also:
* :func:`resample` to change the sample rate and resample data in :attr:`sample_rate`.
* :func:`load_audio_from_array` to read audio from an array and set the sample rate.
* :var:`nussl.constants.DEFAULT_SAMPLE_RATE` the default sample rate for *nussl*
if not specified
"""
return self._sample_rate
示例3: transform
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def transform(self, y, sr):
'''Transform an audio signal
Parameters
----------
y : np.ndarray
The audio signal
sr : number > 0
The native sampling rate of y
Returns
-------
dict
Data dictionary containing features extracted from y
See Also
--------
transform_audio
'''
if sr != self.sr:
y = resample(y, sr, self.sr)
return self.merge([self.transform_audio(y)])
示例4: read_audio
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def read_audio(path, target_fs=None):
"""Read 1 dimension audio sequence from given path.
Args:
path: string, path of audio.
target_fs: int, resampling rate.
Returns:
audio: 1 dimension audio sequence.
fs: sampling rate of audio.
"""
(audio, fs) = soundfile.read(path)
if audio.ndim > 1:
audio = np.mean(audio, axis=1)
if target_fs is not None and fs != target_fs:
audio = librosa.resample(audio, orig_sr=fs, target_sr=target_fs)
fs = target_fs
return audio, fs
示例5: readWave
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def readWave(audio_path, start_frame, end_frame, mono=True, sample_rate=None, clip=True):
snd_file = SoundFile(audio_path, mode='r')
inf = snd_file._info
audio_sr = inf.samplerate
start_read = max(start_frame, 0)
pad_front = -min(start_frame, 0)
end_read = min(end_frame, inf.frames)
pad_back = max(end_frame - inf.frames, 0)
snd_file.seek(start_read)
audio = snd_file.read(end_read - start_read, dtype='float32', always_2d=True) # (num_frames, channels)
snd_file.close()
# Pad if necessary (start_frame or end_frame out of bounds)
audio = np.pad(audio, [(pad_front, pad_back), (0, 0)], mode="constant", constant_values=0.0)
# Convert to mono if desired
if mono:
audio = np.mean(audio, axis=1, keepdims=True)
# Resample if needed
if sample_rate is not None and sample_rate != audio_sr:
res_length = int(np.ceil(float(audio.shape[0]) * float(sample_rate) / float(audio_sr)))
audio = np.pad(audio, [(1, 1), (0,0)], mode="reflect") # Pad audio first
audio = librosa.resample(audio.T, audio_sr, sample_rate, res_type="kaiser_fast").T
skip = (audio.shape[0] - res_length) // 2
audio = audio[skip:skip+res_length,:]
# Clip to [-1,1] if desired
if clip:
audio = np.minimum(np.maximum(audio, -1.0), 1.0)
return audio, audio_sr
示例6: resample
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def resample(self, new_sample_rate, **kwargs):
"""
Resample the data in :attr:`audio_data` to the new sample rate provided by
:param:`new_sample_rate`. If the :param:`new_sample_rate` is the same as :attr:`sample_rate`
then nothing happens.
Args:
new_sample_rate (int): The new sample rate of :attr:`audio_data`.
kwargs: Keyword arguments to librosa.resample.
"""
if new_sample_rate == self.sample_rate:
warnings.warn('Cannot resample to the same sample rate.')
return
resampled_signal = []
for channel in self.get_channels():
resampled_channel = librosa.resample(
channel, self.sample_rate, new_sample_rate, **kwargs)
resampled_signal.append(resampled_channel)
self.audio_data = np.array(resampled_signal)
self.original_signal_length = self.signal_length
self._sample_rate = new_sample_rate
##################################################
# Channel Utilities
##################################################
示例7: read_audio
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def read_audio(audio_path, target_fs=None):
(audio, fs) = soundfile.read(audio_path)
if audio.ndim > 1:
audio = np.mean(audio, axis=1)
if target_fs is not None and fs != target_fs:
audio = librosa.resample(audio, orig_sr=fs, target_sr=target_fs)
fs = target_fs
return audio, fs
示例8: mel
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def mel(features,path,dataset=None):
"""
This function extracts mel-spectrogram from audio.
Make sure, you pass a dictionary containing all attributes
and a path to audio.
"""
fsx=features['fs'][0]
n_mels=features['n_mels'][0]
#print n_mels
fmin=features['fmin'][0]
fmax=features['fmax'][0]
mono=features['mono'][0]
hamming_window=features['hamming_window'][0]
noverlap=features['noverlap'][0]
detrend=features['detrend'][0]
return_onesided=features['return_onesided'][0]
mode=features['mode'][0]
wav, fs = read_audio('librosa',path,dataset)
#fsx = librosa.resample(wav,fs, 44100)
#wav, fs = librosa.load(path)
wav=convert_mono(wav,mono)
if fs != fsx:
raise Exception("Assertion Error. Sampling rate Found {} Expected {}".format(fs,fsx))
ham_win = np.hamming(hamming_window)
[f, t, X] = signal.spectral.spectrogram(wav,fs, window=ham_win, nperseg=hamming_window, noverlap=noverlap, detrend=detrend, return_onesided=return_onesided, mode=mode )
X = X.T
# define global melW, avoid init melW every time, to speed up.
if globals().get('melW') is None:
global melW
melW = librosa.filters.mel( fs, n_fft=hamming_window, n_mels=n_mels, fmin=fmin, fmax=fmax )
melW /= np.max(melW, axis=-1)[:,None]
X = np.dot( X, melW.T )
X = X[:, 0:]
X=feature_normalize(X)
return X
示例9: __call__
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def __call__(self, x, uttid=None, train=True):
if not train:
return x
x = x.astype(numpy.float32)
if self.accept_uttid:
ratio = self.utt2ratio[uttid]
else:
ratio = self.state.uniform(self.lower, self.upper)
# Note1: resample requires the sampling-rate of input and output,
# but actually only the ratio is used.
y = librosa.resample(x, ratio, 1, res_type=self.res_type)
if self.keep_length:
diff = abs(len(x) - len(y))
if len(y) > len(x):
# Truncate noise
y = y[diff // 2 : -((diff + 1) // 2)]
elif len(y) < len(x):
# Assume the time-axis is the first: (Time, Channel)
pad_width = [(diff // 2, (diff + 1) // 2)] + [
(0, 0) for _ in range(y.ndim - 1)
]
y = numpy.pad(
y, pad_width=pad_width, constant_values=0, mode="constant"
)
return y
示例10: compute_melspec
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def compute_melspec(filename, indir, outdir):
wav = np.load(indir + filename + '.npy')
wav = librosa.resample(wav, 44100, 22050)
melspec = librosa.feature.melspectrogram(wav,
sr=22050,
n_fft=1764,
hop_length=220,
n_mels=64)
logmel = librosa.core.power_to_db(melspec)
np.save(outdir + filename + '.npy', logmel)
示例11: _load_audio
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def _load_audio(self, path, center_timestamp, nearest_resample=False):
audio = np.zeros(self.audLen, dtype=np.float32)
# silent
if path.endswith('silent'):
return audio
# load audio
audio_raw, rate = self._load_audio_file(path)
# repeat if audio is too short
if audio_raw.shape[0] < rate * self.audSec:
n = int(rate * self.audSec / audio_raw.shape[0]) + 1
audio_raw = np.tile(audio_raw, n)
# resample
if rate > self.audRate:
# print('resmaple {}->{}'.format(rate, self.audRate))
if nearest_resample:
audio_raw = audio_raw[::rate//self.audRate]
else:
audio_raw = librosa.resample(audio_raw, rate, self.audRate)
# crop N seconds
len_raw = audio_raw.shape[0]
center = int(center_timestamp * self.audRate)
start = max(0, center - self.audLen // 2)
end = min(len_raw, center + self.audLen // 2)
audio[self.audLen//2-(center-start): self.audLen//2+(end-center)] = \
audio_raw[start:end]
# randomize volume
if self.split == 'train':
scale = random.random() + 0.5 # 0.5-1.5
audio *= scale
audio[audio > 1.] = 1.
audio[audio < -1.] = -1.
return audio
示例12: resample
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def resample(audio, orig_sr, new_sr):
return librosa.resample(audio.T, orig_sr, new_sr).T
示例13: resample
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def resample(self, target_fs, scale=True, res_type='kaiser_best'):
"""Resample audio data.
Parameters
----------
target_fs : int
Target sampling rate
scale : bool
Scale the resampled signal to have approximately equal total energy (see `librosa.core.resample`).
Default value True
res_type : str
Resample type (see `librosa.core.resample`)
Default value 'kaiser_best'
Returns
-------
self
"""
if target_fs != self.fs:
self._data = numpy.asfortranarray(self._data)
self._data = librosa.resample(
y=self._data,
orig_sr=self.fs,
target_sr=target_fs,
scale=scale,
res_type=res_type
)
self.fs = target_fs
return self
示例14: read_audio
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def read_audio(path, target_fs=None):
(audio, fs) = soundfile.read(path)
if audio.ndim > 1:
audio = np.mean(audio, axis=1)
if target_fs is not None and fs != target_fs:
audio = librosa.resample(audio, orig_sr=fs, target_sr=target_fs)
fs = target_fs
return audio, fs
示例15: preprocess_wav
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import resample [as 别名]
def preprocess_wav(fpath_or_wav: Union[str, Path, np.ndarray], source_sr: Optional[int]=None):
"""
Applies preprocessing operations to a waveform either on disk or in memory such that
The waveform will be resampled to match the data hyperparameters.
:param fpath_or_wav: either a filepath to an audio file (many extensions are supported, not
just .wav), either the waveform as a numpy array of floats.
:param source_sr: if passing an audio waveform, the sampling rate of the waveform before
preprocessing. After preprocessing, the waveform'speaker sampling rate will match the data
hyperparameters. If passing a filepath, the sampling rate will be automatically detected and
this argument will be ignored.
"""
# Load the wav from disk if needed
if isinstance(fpath_or_wav, str) or isinstance(fpath_or_wav, Path):
wav, source_sr = librosa.load(str(fpath_or_wav), sr=None)
else:
wav = fpath_or_wav
# Resample the wav
if source_sr is not None:
wav = librosa.resample(wav, source_sr, sampling_rate)
# Apply the preprocessing: normalize volume and shorten long silences
wav = normalize_volume(wav, audio_norm_target_dBFS, increase_only=True)
wav = trim_long_silences(wav)
return wav