# 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
# 
# def convert_boolean_for_praat(b):
#     """ Convert Python boolean for use in Praat
# 
#     Praat uses "yes"/"no" or 1/0 values instead of True/False.
#     Convert True to "yes", False to "no"
#     """
#     if b == True:
#         return "yes"
#     elif b == False:
#         return "no"
#     else:
#         raise ValueError('Input must be a Boolean')


import os
import numpy as np
from subprocess import call
from opensauce.helpers import round_half_away_from_zero, convert_boolean_for_praat
                      smooth - Whether to smooth pitch in post-processing,
                               using bandwidth specified by smooth_bandwidth
                               [Boolean]
                               (default = False)
            smooth_bandwidth - Bandwidth in Hz to use for smoothing if
                               smooth is set to True [integer]
                               (default = 5)

    Returns:
        F0 - F0 estimates (NumPy vector)

    Raw Praat estimates are at time points that don't completely match the time
    points in our measurement vectors, so we need to interpolate.  We use a
    crude interpolation method, that has precision set by frame_precision.

    For more information, see the Praat manual pages:
        http://www.fon.hum.uva.nl/praat/manual/Sound__To_Pitch__ac____.html
        http://www.fon.hum.uva.nl/praat/manual/Sound__To_Pitch__cc____.html
    """
    # Compute raw Praat F0 estimates
    t_raw, F0_raw = praat_raw_pitch(wav_fn, praat_path, frame_shift, method,
                                    min_pitch, max_pitch, silence_threshold,
                                    voice_threshold, octave_cost,
                                    octave_jumpcost, voiced_unvoiced_cost,
                                    kill_octave_jumps, interpolate, smooth,
                                    smooth_bandwidth)

    # Initialize F0 measurement vector with NaN
    F0 = np.full(data_len, np.nan)
    # Convert time from seconds to nearest whole millisecond

# 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
# 
# def convert_boolean_for_praat(b):
#     """ Convert Python boolean for use in Praat
# 
#     Praat uses "yes"/"no" or 1/0 values instead of True/False.
#     Convert True to "yes", False to "no"
#     """
#     if b == True:
#         return "yes"
#     elif b == False:
#         return "no"
#     else:
#         raise ValueError('Input must be a Boolean')


# Path: opensauce/praat.py
import os
import numpy as np
from subprocess import call
from opensauce.helpers import round_half_away_from_zero, convert_boolean_for_praat

                      smooth - Whether to smooth pitch in post-processing,
                               using bandwidth specified by smooth_bandwidth
                               [Boolean]
                               (default = False)
            smooth_bandwidth - Bandwidth in Hz to use for smoothing if
                               smooth is set to True [integer]
                               (default = 5)

    Returns:
        F0 - F0 estimates (NumPy vector)

    Raw Praat estimates are at time points that don't completely match the time
    points in our measurement vectors, so we need to interpolate.  We use a
    crude interpolation method, that has precision set by frame_precision.

    For more information, see the Praat manual pages:
        http://www.fon.hum.uva.nl/praat/manual/Sound__To_Pitch__ac____.html
        http://www.fon.hum.uva.nl/praat/manual/Sound__To_Pitch__cc____.html
    """
    # Compute raw Praat F0 estimates
    t_raw, F0_raw = praat_raw_pitch(wav_fn, praat_path, frame_shift, method,
                                    min_pitch, max_pitch, silence_threshold,
                                    voice_threshold, octave_cost,
                                    octave_jumpcost, voiced_unvoiced_cost,
                                    kill_octave_jumps, interpolate, smooth,
                                    smooth_bandwidth)

    # Initialize F0 measurement vector with NaN
    F0 = np.full(data_len, np.nan)
    # Convert time from seconds to nearest whole millisecond

    t_raw_ms = np.int_(round_half_away_from_zero(t_raw * 1000))