Skip to content

Commit

Permalink
Merge a057102 into fe7f444
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcfee committed Nov 2, 2016
2 parents fe7f444 + a057102 commit d499ff6
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 7 deletions.
156 changes: 149 additions & 7 deletions librosa/effects.py
Expand Up @@ -30,15 +30,17 @@
"""

import numpy as np
import scipy.ndimage

from . import core
from . import decompose
from . import feature
from . import util
from .util.exceptions import ParameterError

__all__ = ['hpss', 'harmonic', 'percussive',
'time_stretch', 'pitch_shift',
'remix']
'remix', 'trim']


def hpss(y, **kwargs):
Expand Down Expand Up @@ -77,7 +79,7 @@ def hpss(y, **kwargs):
>>> y, sr = librosa.load(librosa.util.example_audio_file())
>>> y_harmonic, y_percussive = librosa.effects.hpss(y)
>>> # Get a more isolated percussive component by widening its margin
>>> # Get a more isolated percussive component by widening its margin
>>> y_harmonic, y_percussive = librosa.effects.hpss(y, margin=(1.0,5.0))
'''
Expand All @@ -94,6 +96,7 @@ def hpss(y, **kwargs):

return y_harm, y_perc


def harmonic(y, **kwargs):
'''Extract harmonic elements from an audio time-series.
Expand All @@ -117,14 +120,13 @@ def harmonic(y, **kwargs):
Examples
--------
>>> # Extract harmonic component
>>> # Extract harmonic component
>>> y, sr = librosa.load(librosa.util.example_audio_file())
>>> y_harmonic = librosa.effects.harmonic(y)
>>> # Use a margin > 1.0 for greater harmonic separation
>>> y_harmonic = librosa.effects.harmonic(y, margin=3.0)
'''

# Compute the STFT matrix
Expand All @@ -138,6 +140,7 @@ def harmonic(y, **kwargs):

return y_harm


def percussive(y, **kwargs):
'''Extract percussive elements from an audio time-series.
Expand All @@ -160,15 +163,14 @@ def percussive(y, **kwargs):
librosa.decompose.hpss : HPSS for spectrograms
Examples
--------
>>> # Extract percussive component
--------
>>> # Extract percussive component
>>> y, sr = librosa.load(librosa.util.example_audio_file())
>>> y_percussive = librosa.effects.percussive(y)
>>> # Use a margin > 1.0 for greater percussive separation
>>> y_percussive = librosa.effects.percussive(y, margin=3.0)
'''

# Compute the STFT matrix
Expand Down Expand Up @@ -374,3 +376,143 @@ def remix(y, intervals, align_zeros=True):
y_out.append(y[clip])

return np.concatenate(y_out, axis=-1)


def trim(y, top_db=60, ref_power=np.max, n_fft=2048, hop_length=512,
index=False):
'''Trim leading and trailing silence from an audio signal.
Parameters
----------
y : np.ndarray, shape=(n,) or (2,n)
Audio signal, can be mono or stereo
top_db : number > 0
The threshold (in decibels) below reference to consider as
silence
ref_power : number or callable
The reference power. By default, it uses `np.max` and compares
to the peak power in the signal.
n_fft : int > 0
The number of samples per analysis frame
hop_length : int > 0
The number of samples between analysis frames
index : bool
If `True`, return the start and end of the non-silent
region of `y` along with the trimmed signal.
If `False`, only return the trimmed signal.
Returns
-------
y_trimmed : np.ndarray, shape=(m,) or (2, m)
The trimmed signal
index : slice, optional
If `index=True` is provided, then this contains
the slice of `y` corresponding to the non-silent region:
`y_trimmed = y[index]`.
Examples
--------
>>> # Load some audio
>>> y, sr = librosa.load(librosa.util.example_audio_file())
>>> # Trim the beginning and ending silence
>>> yt = librosa.effects.trim(y)
>>> # Print the durations
>>> print(librosa.get_duration(y), librosa.get_duration(yt))
61.45886621315193 60.58086167800454
'''

# Convert to mono
y_mono = core.to_mono(y)

# Compute the MSE for the signal
mse = feature.rmse(y=y_mono, n_fft=n_fft, hop_length=hop_length)**2

# Compute the log power indicator and non-zero positions
logp = core.logamplitude(mse, ref_power=ref_power, top_db=None) > - top_db
nonzero = np.flatnonzero(logp)

# 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(len(y_mono),
int(core.frames_to_samples(nonzero[-1] + 1, hop_length)))

# Build the mono/stereo index
full_index = [slice(None)] * y.ndim
full_index[-1] = slice(start, end)

if index:
return y[full_index], full_index[-1]
else:
return y[full_index]


def split(y, top_db=60, ref_power=np.max, n_fft=2048, hop_length=512):
'''Split an audio signal into non-silent intervals.
Parameters
----------
y : np.ndarray, shape=(n,) or (2, n)
An audio signal
top_db : number > 0
The threshold (in decibels) below reference to consider as
silence
ref_power : number or callable
The reference power. By default, it uses `np.max` and compares
to the peak power in the signal.
n_fft : int > 0
The number of samples per analysis frame
hop_length : int > 0
The number of samples between analysis frames
Returns
-------
intervals : np.ndarray, shape=(m, 2)
`intervals[i] == (start_i, end_i)` are the start and end time
(in samples) if the `i`th non-silent interval.
'''

# Convert to mono
y = core.to_mono(y)

# Compute the MSE for the signal
mse = feature.rmse(y=y, n_fft=n_fft, hop_length=hop_length)**2

# Compute the log power indicator and non-zero positions
logp = (core.logamplitude(mse.squeeze(),
ref_power=ref_power,
top_db=None) > -top_db)

# Interval slicing, adapted from
# https://stackoverflow.com/questions/2619413/efficiently-finding-the-interval-with-non-zeros-in-scipy-numpy-in-python
# Find points where the sign flips
edges = np.flatnonzero(np.diff((~logp).astype(int)))

# Pad back the sample lost in the diff
edges = [edges + 1]

# If the first frame had high energy, count it
if logp[0]:
edges.insert(0, [0])

# Likewise for the last frame
if logp[-1]:
edges.append([len(logp)])

# Convert from frames to samples
edges = core.frames_to_samples(np.concatenate(edges),
hop_length=hop_length)

# Stack the results back as an ndarray
return edges.reshape((-1, 2))
80 changes: 80 additions & 0 deletions tests/test_effects.py
Expand Up @@ -139,3 +139,83 @@ def test_harmonic():
yh2 = librosa.effects.harmonic(y)

assert np.allclose(yh1, yh2)


def test_trim():

def __test(y, top_db, ref_power, index):

if index:
yt, idx = librosa.effects.trim(y, top_db=top_db,
ref_power=ref_power,
index=True)

# Test for index position
fidx = [slice(None)] * y.ndim
fidx[-1] = idx
assert np.allclose(yt, y[fidx])

else:
yt = librosa.effects.trim(y, top_db=top_db, ref_power=ref_power,
index=False)

# Verify logamp
rms = librosa.feature.rmse(librosa.to_mono(yt))
logamp = librosa.logamplitude(rms**2, ref_power=ref_power, top_db=None)

assert np.all(logamp >= - top_db)

# Verify duration
duration = librosa.get_duration(yt)
assert np.allclose(duration, 3.0, atol=1e-1), duration

# construct 5 seconds of stereo silence
# Stick a sine wave in the middle three seconds
sr = float(22050)
y = np.zeros((2, int(5 * sr)))
y[0, sr:4*sr] = np.sin(2 * np.pi * 440 * np.arange(0, 3 * sr) / sr)

for top_db in [60, 40, 20]:
for index in [False, True]:
for ref_power in [1, np.max]:
# Test stereo
yield __test, y, top_db, ref_power, index
# Test mono
yield __test, y[0], top_db, ref_power, index


def test_split():

def __test(hop_length, top_db):

intervals = librosa.effects.split(y,
top_db=top_db,
hop_length=hop_length)

int_match = librosa.util.match_intervals(intervals, idx_true)

print(idx_true)
print(intervals)
for i in range(len(intervals)):
i_true = idx_true[int_match[i]]

assert np.all(np.abs(i_true - intervals[i]) <= 2048), intervals[i]

# Make some high-frequency noise
sr = 8192

y = np.ones(10 * sr)
y[::2] *= -1

# Zero out all but two few intervals
y[:sr] = 0
y[2 * sr:3 * sr] = 0
y[4 * sr:] = 0

# The true non-silent intervals
idx_true = np.asarray([[sr, 2 * sr],
[3 * sr, 4 * sr]])

for hop_length in [256, 512, 1024]:
for top_db in [20, 60, 80]:
yield __test, hop_length, top_db

0 comments on commit d499ff6

Please sign in to comment.