# Path: opensauce/helpers.py
# def round_half_away_from_zero(x):
#     """Rounds a number according to round half away from zero method
# 
#     Args:
#         x - number [float]
# 
#     Returns:
#         q - rounded number [integer]
# 
#     For example:
#        round_half_away_from_zero(3.5) = 4
#        round_half_away_from_zero(3.2) = 3
#        round_half_away_from_zero(-2.7) = -3
#        round_half_away_from_zero(-4.3) = -4
# 
#     The reason for writing our own rounding function is that NumPy uses the
#     round-half-to-even method. There is a Python round() function, but it
#     doesn't work on NumPy vectors. So we wrote our own
#     round-half-away-from-zero method here.
#     """
#     q = np.int_(np.sign(x) * np.floor(np.abs(x) + 0.5))
# 
#     return q


import numpy as np
from scipy.fftpack import fft
from scipy.interpolate import interp1d
from opensauce.helpers import round_half_away_from_zero
    """
    # XXX the octave code produces 201 output points given a datalen
    # of 200.  Presumably a bug in the matlab code.  But we'll emulate it.
    datalen += 1
    kw = {}
    # XXX This is awkward, fix it in refactoring later.
    if len(list(filter(None, (min_pitch, max_pitch)))) == 1:
        raise ValueError('none or both of min_pitch, max_pitch must be specified')
    elif min_pitch:
        kw['F0MinMax'] = (min_pitch, max_pitch)
    if window_length is not None:
        kw['frame_length'] = window_length
    if frame_shift is not None:
        kw['timestep'] = frame_shift
    if shr_threshold is not None:
        kw['SHR_Threshold'] = shr_threshold
    f0_time, f0_value, shr_value, f0_candidates = shrp(wav_data, fps, **kw)

    # "Postprocess subharmonic-harmonic ratios and f0 tracks"

    # "Initialize F0 and subharmonic-harmonic ratio values"
    F0 = np.full(datalen, np.nan)
    SHR = np.full(datalen, np.nan)

    # "time locations rounded to nearest ms"
    #
    # VoiceSauce uses Matlab, and Matlab's round function uses the
    # round-half-away-from-zero method.  However, NumPy uses the
    # round-half-to-even method.  So we use our own round-half-away-from-zero
    # method here.

# Path: opensauce/helpers.py
# def round_half_away_from_zero(x):
#     """Rounds a number according to round half away from zero method
# 
#     Args:
#         x - number [float]
# 
#     Returns:
#         q - rounded number [integer]
# 
#     For example:
#        round_half_away_from_zero(3.5) = 4
#        round_half_away_from_zero(3.2) = 3
#        round_half_away_from_zero(-2.7) = -3
#        round_half_away_from_zero(-4.3) = -4
# 
#     The reason for writing our own rounding function is that NumPy uses the
#     round-half-to-even method. There is a Python round() function, but it
#     doesn't work on NumPy vectors. So we wrote our own
#     round-half-away-from-zero method here.
#     """
#     q = np.int_(np.sign(x) * np.floor(np.abs(x) + 0.5))
# 
#     return q


# Path: opensauce/shrp.py
import numpy as np
from scipy.fftpack import fft
from scipy.interpolate import interp1d
from opensauce.helpers import round_half_away_from_zero

    """
    # XXX the octave code produces 201 output points given a datalen
    # of 200.  Presumably a bug in the matlab code.  But we'll emulate it.
    datalen += 1
    kw = {}
    # XXX This is awkward, fix it in refactoring later.
    if len(list(filter(None, (min_pitch, max_pitch)))) == 1:
        raise ValueError('none or both of min_pitch, max_pitch must be specified')
    elif min_pitch:
        kw['F0MinMax'] = (min_pitch, max_pitch)
    if window_length is not None:
        kw['frame_length'] = window_length
    if frame_shift is not None:
        kw['timestep'] = frame_shift
    if shr_threshold is not None:
        kw['SHR_Threshold'] = shr_threshold
    f0_time, f0_value, shr_value, f0_candidates = shrp(wav_data, fps, **kw)

    # "Postprocess subharmonic-harmonic ratios and f0 tracks"

    # "Initialize F0 and subharmonic-harmonic ratio values"
    F0 = np.full(datalen, np.nan)
    SHR = np.full(datalen, np.nan)

    # "time locations rounded to nearest ms"
    #
    # VoiceSauce uses Matlab, and Matlab's round function uses the
    # round-half-away-from-zero method.  However, NumPy uses the
    # round-half-to-even method.  So we use our own round-half-away-from-zero
    # method here.

    t = round_half_away_from_zero(f0_time)