Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some functions added #19

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
77 changes: 76 additions & 1 deletion pyrubberband/pyrb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import soundfile as sf


__all__ = ['time_stretch', 'pitch_shift', 'timemap_stretch']
__all__ = ['time_stretch', 'pitch_shift', 'timemap_stretch', 'frequency_multiply', 'change_tempo']

__RUBBERBAND_UTIL = 'rubberband'

Expand Down Expand Up @@ -255,3 +255,78 @@ def pitch_shift(y, sr, n_steps, rbargs=None):
rbargs.setdefault('--pitch', n_steps)

return __rubberband(y, sr, **rbargs)


def frequency_multiply(y, sr, X, rbargs=None):
'''Multiply the frequencies inside an audio time series.
The equivalent of the -f option.
Parameters
----------
y : np.ndarray [shape=(n,) or (n, c)]
Audio time series, either single or multichannel

sr : int > 0
Sampling rate of `y`

X : float
Shift magnitudes in spectrum by X
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this parameter have a more descriptive name? X doesn't convey much here, maybe factor or shift would be more appropriate.

The description of this parameter (and the function docstring itself) is also a bit confusing. I assume this means that energy at frequency f maps to frequency f*factor? I think it would be best to be explicit about what the exact calculation is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, factor seems a better name.

The rubberband-cli documentation just says Change frequency by multiple X. I was looking in the rubberband repository, and I guess by this line that this does f*factor. I think we could safely describe this parameter as you said.


rbargs
Additional keyword parameters for rubberband

See `rubberband -h` for details.

Returns
-------
y_shift : np.ndarray
frequency-multiplied audio
'''

if X == 0:
return y

if rbargs is None:
rbargs = dict()

rbargs.setdefault('--frequency', X)

return __rubberband(y, sr, **rbargs)


def change_tempo(y, sr, X, Y, rbargs=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this different from time_stretch?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not, it just saves the step of creating the coefficient between the two tempos.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i get it now. Thanks. LGTM

'''Multiply the frequencies inside an audio time series.
The equivalent of the --tempo <X>:<Y> option.
Parameters
----------
y : np.ndarray [shape=(n,) or (n, c)]
Audio time series, either single or multichannel

sr : int > 0
Sampling rate of `y`

X : float
Base tempo

Y : float
Final tempo

rbargs
Additional keyword parameters for rubberband

See `rubberband -h` for details.

Returns
-------
y_shift : np.ndarray
tempo-shifted audio
'''

if X == 0:
return y

if rbargs is None:
rbargs = dict()

rbargs.setdefault('--tempo', '%s:%s' % (X, Y))

return __rubberband(y, sr, **rbargs)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add unit tests for this new functionality?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me take a look at how do they work! For change_tempo and frequency_shift, right?