Professional Documents
Culture Documents
Cod Pyhon para Tom
Cod Pyhon para Tom
/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Effects
=======
hpss
harmonic
percussive
time_stretch
pitch_shift
Miscellaneous
-------------
.. autosummary::
:toctree: generated/
remix
trim
split
preemphasis
deemphasis
"""
import numpy as np
import scipy.signal
__all__ = [
"hpss",
"harmonic",
"percussive",
"time_stretch",
"pitch_shift",
"remix",
"trim",
"split",
]
Parameters
----------
y : np.ndarray [shape=(n,)]
audio time series
Returns
-------
y_harmonic : np.ndarray [shape=(n,)]
audio time series of the harmonic elements
See Also
--------
harmonic : Extract only the harmonic component
percussive : Extract only the percussive component
librosa.decompose.hpss : HPSS on spectrograms
Examples
--------
>>> # Extract harmonic and percussive components
>>> y, sr = librosa.load(librosa.ex('choice'))
>>> y_harmonic, y_percussive = librosa.effects.hpss(y)
"""
Parameters
----------
y : np.ndarray [shape=(n,)]
audio time series
kwargs : additional keyword arguments.
See `librosa.decompose.hpss` for details.
Returns
-------
y_harmonic : np.ndarray [shape=(n,)]
audio time series of just the harmonic portion
See Also
--------
hpss : Separate harmonic and percussive components
percussive : Extract only the percussive component
librosa.decompose.hpss : HPSS for spectrograms
Examples
--------
>>> # Extract harmonic component
>>> y, sr = librosa.load(librosa.ex('choice'))
>>> y_harmonic = librosa.effects.harmonic(y)
"""
# Remove percussives
stft_harm = decompose.hpss(stft, **kwargs)[0]
return y_harm
Parameters
----------
y : np.ndarray [shape=(n,)]
audio time series
Returns
-------
y_percussive : np.ndarray [shape=(n,)]
audio time series of just the percussive portion
See Also
--------
hpss : Separate harmonic and percussive components
harmonic : Extract only the harmonic component
librosa.decompose.hpss : HPSS for spectrograms
Examples
--------
>>> # Extract percussive component
>>> y, sr = librosa.load(librosa.ex('choice'))
>>> y_percussive = librosa.effects.percussive(y)
"""
# Remove harmonics
stft_perc = decompose.hpss(stft, **kwargs)[1]
return y_perc
Parameters
----------
y : np.ndarray [shape=(n,)]
audio time series
Returns
-------
y_stretch : np.ndarray [shape=(round(n/rate),)]
audio time series stretched by the specified rate
See Also
--------
pitch_shift : pitch shifting
librosa.phase_vocoder : spectrogram phase vocoder
pyrubberband.pyrb.time_stretch : high-quality time stretching using RubberBand
Examples
--------
Compress to be twice as fast
>>> y, sr = librosa.load(librosa.ex('choice'))
>>> y_fast = librosa.effects.time_stretch(y, 2.0)
"""
if rate <= 0:
raise ParameterError("rate must be a positive number")
return y_stretch
Parameters
----------
y : np.ndarray [shape=(n,)]
audio time series
res_type : string
Resample type. By default, 'kaiser_best' is used.
Returns
-------
y_shift : np.ndarray [shape=(n,)]
The pitch-shifted audio time-series
See Also
--------
time_stretch : time stretching
librosa.phase_vocoder : spectrogram phase vocoder
pyrubberband.pyrb.pitch_shift : high-quality pitch shifting using RubberBand
Examples
--------
Shift up by a major third (four steps if ``bins_per_octave`` is 12)
>>> y, sr = librosa.load(librosa.ex('choice'))
>>> y_third = librosa.effects.pitch_shift(y, sr, n_steps=4)
Shift up by 3 quarter-tones
Parameters
----------
y : np.ndarray [shape=(t,) or (2, t)]
Audio time series
align_zeros : boolean
If ``True``, interval boundaries are mapped to the closest
zero-crossing in ``y``. If ``y`` is stereo, zero-crossings
are computed after converting to mono.
Returns
-------
y_remix : np.ndarray [shape=(d,) or (2, d)]
``y`` remixed in the order specified by ``intervals``
Examples
--------
Load in the example track and reverse the beats
>>> y, sr = librosa.load(librosa.ex('choice'))
Compute beats
y_out = []
if align_zeros:
y_mono = core.to_mono(y)
zeros = np.nonzero(core.zero_crossings(y_mono))[-1]
# Force end-of-signal onto zeros
zeros = np.append(zeros, [len(y_mono)])
if align_zeros:
interval = zeros[util.match_events(interval, zeros)]
y_out.append(y[tuple(clip)])
return y_out
def _signal_to_frame_nonsilent(
y, frame_length=2048, hop_length=512, top_db=60, ref=np.max
):
"""Frame-wise non-silent indicator for audio input.
Parameters
----------
y : np.ndarray, shape=(n,) or (2,n)
Audio signal, mono or stereo
Returns
-------
non_silent : np.ndarray, shape=(m,), dtype=bool
Indicator of non-silent frames
"""
# Convert to mono
y_mono = core.to_mono(y)
Parameters
----------
y : np.ndarray, shape=(n,) or (2,n)
Audio signal, can be mono or stereo
Returns
-------
y_trimmed : np.ndarray, shape=(m,) or (2, m)
The trimmed signal
Examples
--------
>>> # Load some audio
>>> y, sr = librosa.load(librosa.ex('choice'))
>>> # Trim the beginning and ending silence
>>> yt, index = librosa.effects.trim(y)
>>> # Print the durations
>>> print(librosa.get_duration(y), librosa.get_duration(yt))
25.025986394557822 25.007891156462584
"""
non_silent = _signal_to_frame_nonsilent(
y, frame_length=frame_length, hop_length=hop_length, ref=ref, top_db=top_db
)
nonzero = np.flatnonzero(non_silent)
if nonzero.size > 0:
# Compute the start and end positions
# End position goes one frame past the last non-zero
start = int(core.frames_to_samples(nonzero[0], hop_length))
end = min(y.shape[-1], int(core.frames_to_samples(nonzero[-1] + 1,
hop_length)))
else:
# The signal only contains zeros
start, end = 0, 0
Parameters
----------
y : np.ndarray, shape=(n,) or (2, n)
An audio signal
Returns
-------
intervals : np.ndarray, shape=(m, 2)
``intervals[i] == (start_i, end_i)`` are the start and end time
(in samples) of non-silent interval ``i``.
"""
non_silent = _signal_to_frame_nonsilent(
y, frame_length=frame_length, hop_length=hop_length, ref=ref, top_db=top_db
)
Parameters
----------
y : np.ndarray
Audio signal
The default (0.97) matches the pre-emphasis filter used in the HTK
implementation of MFCCs [#]_.
.. [#] http://htk.eng.cam.ac.uk/
zi : number
Initial filter state. When making successive calls to non-overlapping
frames, this can be set to the ``zf`` returned from the previous call.
(See example below.)
return_zf : boolean
If ``True``, return the final filter state.
If ``False``, only return the pre-emphasized signal.
Returns
-------
y_out : np.ndarray
pre-emphasized signal
zf : number
if ``return_zf=True``, the final filter state is also returned
Examples
--------
Apply a standard pre-emphasis filter
Apply pre-emphasis in pieces for block streaming. Note that the second block
initializes ``zi`` with the final state ``zf`` returned by the first call.
See Also
--------
deemphasis
"""
b = np.asarray([1.0, -coef], dtype=y.dtype)
a = np.asarray([1.0], dtype=y.dtype)
if zi is None:
# Initialize the filter to implement linear extrapolation
zi = 2 * y[..., 0] - y[..., 1]
zi = np.atleast_1d(zi)
if return_zf:
return y_out, z_f
return y_out
Parameters
----------
y : np.ndarray
Audio signal
The default (0.97) matches the pre-emphasis filter used in the HTK
implementation of MFCCs [#]_.
.. [#] http://htk.eng.cam.ac.uk/
zi : number
Initial filter state. If inverting a previous preemphasis(), the same value
should be used.
Examples
--------
Apply a standard pre-emphasis filter and invert it with de-emphasis
>>> y, sr = librosa.load(librosa.ex('trumpet'))
>>> y_filt = librosa.effects.preemphasis(y)
>>> y_deemph = librosa.effects.deemphasis(y_filt)
>>> np.allclose(y, y_deemph)
True
See Also
--------
preemphasis
"""
b = np.asarray([1.0, -coef], dtype=y.dtype)
a = np.asarray([1.0], dtype=y.dtype)
if zi is None:
zi = ((2 - coef) * y[0] - y[1]) / (3 - coef)
y[0] -= zi
y_out = scipy.signal.lfilter(a, b, y)
return y_out