# Path: opensauce/reaper.py
# def creaper_pitch(wav_fn, reaper_path, frame_shift, max_pitch, min_pitch,
#                   high_pass, hilbert_transform, inter_mark):
#     """Return F0 vector estimated by REAPER (compiled C code) along with
#        corresponding times
# 
#     Args:
#         wav_fn            - WAV filename [string]
#         reaper_path       - Path to REAPER executable [string]
#         frame_shift       - Length of each frame in ms [integer]
#         max_pitch         - Maximum valid F0 allowed in Hz [integer]
#         min_pitch         - Minimum valid F0 allowed in Hz [integer]
#         high_pass         - Whether to apply high pass filter (80 Hz) to input
#                             [Boolean]
#         hilbert_transform - Whether to apply Hilbert transform that may reduce
#                             phase distortion [Boolean]
#         inter_mark        - Regular inter-mark interval to use in unvoiced
#                             pitchmark regions given in milliseconds [integer]
#     Returns:
#         F0_times          - Times corresponding to F0 estimates [NumPy vector]
#         F0                - F0 estimates [NumPy vector]
#     """
#     # Output file names
#     wav_dir = os.path.dirname(wav_fn)
#     reaper_f0_fn = os.path.join(wav_dir, 'reaper-f0.txt')
#     # XXX: We aren't using the output of these files for now
#     #      But they may be useful in the future
#     reaper_pitchmarks_fn = os.path.join(wav_dir, 'reaper-pitchmarks.txt')
#     reaper_corr_fn = os.path.join(wav_dir, 'reaper-corr.txt')
# 
#     # Run REAPER command
#     cmd = [reaper_path, '-i', wav_fn]
#     cmd.extend(['-f', reaper_f0_fn])
#     cmd.extend(['-p', reaper_pitchmarks_fn])
#     cmd.extend(['-c', reaper_corr_fn])
#     if hilbert_transform:
#         cmd.extend(['-t'])
#     if not high_pass:
#         cmd.extend(['-s'])
#     cmd.extend(['-e', str(frame_shift / 1000.0)])
#     cmd.extend(['-x', str(max_pitch)])
#     cmd.extend(['-m', str(min_pitch)])
#     cmd.extend(['-u', str(inter_mark / 1000.0)])
#     cmd.extend(['-a'])
# 
#     try:
#         return_code = subprocess.call(cmd, stdout=subprocess.PIPE)
#     except OSError:
#         raise OSError('Error while attempting to call REAPER.  Is REAPER path {} correct?'.format(reaper_path))
#     else:
#         if return_code != 0: # pragma: no cover
#             raise OSError('Error when trying to call REAPER')
# 
#     # XXX: I think flag is 1 when the measurement is in a voiced region,
#     #      and flag is 0 when the measurement is an unvoiced region
#     F0_times, flag, F0 = np.loadtxt(reaper_f0_fn, skiprows=7, unpack=True)
# 
#     # Replace invalid measurements with NaN
#     F0[F0 < 0] = np.nan
# 
#     os.remove(reaper_f0_fn)
#     os.remove(reaper_pitchmarks_fn)
#     os.remove(reaper_corr_fn)
# 
#     return F0_times, F0


import sys
import os
import glob
import numpy as np
from opensauce.reaper import creaper_pitch
# Script to generate raw REAPER samples from test wav files
# The data is used for comparison in unit tests

# Licensed under Apache v2 (see LICENSE)




def save_samples(data, fn, col_name, sample, out_dir):
    """Dump data in txt format using fn, col_name, and sample strings
       in file name
    """
    fn = os.path.splitext(os.path.basename(fn))[0]
    fn = '-'.join(('sample', fn, col_name, sample))
    fn = os.path.join(out_dir, fn) + '.txt'
    np.savetxt(fn, data)

def main(wav_dir, out_dir, reaper_path):
    # Find all .wav files in test/data directory
    wav_files = glob.glob(os.path.join(wav_dir, '*.wav'))

    # Generate Praat data for each wav file and save it to text files
    for wav_file in wav_files:
        wav_basename = os.path.basename(wav_file)

        print('Processing wav file {}'.format(wav_file))
        # Generate raw REAPER pitch samples
        # Use VoiceSauce default parameter values

# Path: opensauce/reaper.py
# def creaper_pitch(wav_fn, reaper_path, frame_shift, max_pitch, min_pitch,
#                   high_pass, hilbert_transform, inter_mark):
#     """Return F0 vector estimated by REAPER (compiled C code) along with
#        corresponding times
# 
#     Args:
#         wav_fn            - WAV filename [string]
#         reaper_path       - Path to REAPER executable [string]
#         frame_shift       - Length of each frame in ms [integer]
#         max_pitch         - Maximum valid F0 allowed in Hz [integer]
#         min_pitch         - Minimum valid F0 allowed in Hz [integer]
#         high_pass         - Whether to apply high pass filter (80 Hz) to input
#                             [Boolean]
#         hilbert_transform - Whether to apply Hilbert transform that may reduce
#                             phase distortion [Boolean]
#         inter_mark        - Regular inter-mark interval to use in unvoiced
#                             pitchmark regions given in milliseconds [integer]
#     Returns:
#         F0_times          - Times corresponding to F0 estimates [NumPy vector]
#         F0                - F0 estimates [NumPy vector]
#     """
#     # Output file names
#     wav_dir = os.path.dirname(wav_fn)
#     reaper_f0_fn = os.path.join(wav_dir, 'reaper-f0.txt')
#     # XXX: We aren't using the output of these files for now
#     #      But they may be useful in the future
#     reaper_pitchmarks_fn = os.path.join(wav_dir, 'reaper-pitchmarks.txt')
#     reaper_corr_fn = os.path.join(wav_dir, 'reaper-corr.txt')
# 
#     # Run REAPER command
#     cmd = [reaper_path, '-i', wav_fn]
#     cmd.extend(['-f', reaper_f0_fn])
#     cmd.extend(['-p', reaper_pitchmarks_fn])
#     cmd.extend(['-c', reaper_corr_fn])
#     if hilbert_transform:
#         cmd.extend(['-t'])
#     if not high_pass:
#         cmd.extend(['-s'])
#     cmd.extend(['-e', str(frame_shift / 1000.0)])
#     cmd.extend(['-x', str(max_pitch)])
#     cmd.extend(['-m', str(min_pitch)])
#     cmd.extend(['-u', str(inter_mark / 1000.0)])
#     cmd.extend(['-a'])
# 
#     try:
#         return_code = subprocess.call(cmd, stdout=subprocess.PIPE)
#     except OSError:
#         raise OSError('Error while attempting to call REAPER.  Is REAPER path {} correct?'.format(reaper_path))
#     else:
#         if return_code != 0: # pragma: no cover
#             raise OSError('Error when trying to call REAPER')
# 
#     # XXX: I think flag is 1 when the measurement is in a voiced region,
#     #      and flag is 0 when the measurement is an unvoiced region
#     F0_times, flag, F0 = np.loadtxt(reaper_f0_fn, skiprows=7, unpack=True)
# 
#     # Replace invalid measurements with NaN
#     F0[F0 < 0] = np.nan
# 
#     os.remove(reaper_f0_fn)
#     os.remove(reaper_pitchmarks_fn)
#     os.remove(reaper_corr_fn)
# 
#     return F0_times, F0


# Path: tools/generate_reaper_samples.py
import sys
import os
import glob
import numpy as np
from opensauce.reaper import creaper_pitch

# Script to generate raw REAPER samples from test wav files
# The data is used for comparison in unit tests

# Licensed under Apache v2 (see LICENSE)




def save_samples(data, fn, col_name, sample, out_dir):
    """Dump data in txt format using fn, col_name, and sample strings
       in file name
    """
    fn = os.path.splitext(os.path.basename(fn))[0]
    fn = '-'.join(('sample', fn, col_name, sample))
    fn = os.path.join(out_dir, fn) + '.txt'
    np.savetxt(fn, data)

def main(wav_dir, out_dir, reaper_path):
    # Find all .wav files in test/data directory
    wav_files = glob.glob(os.path.join(wav_dir, '*.wav'))

    # Generate Praat data for each wav file and save it to text files
    for wav_file in wav_files:
        wav_basename = os.path.basename(wav_file)

        print('Processing wav file {}'.format(wav_file))
        # Generate raw REAPER pitch samples
        # Use VoiceSauce default parameter values

        F0_times, F0 = creaper_pitch(wav_file, reaper_path=reaper_path,