#!/usr/bin/perl

# ------------------------------------------------------------------- FILES ---
$input_file = "/snd/Public_Sounds/steeldrums.aiff";
$input_chan = 0;                    # numbered from 1; 0: use all input chans
$begintime = 0.38;
$endtime = 0.65;                    # 0 means end of file

$output_file = "/snd/myuserid/transpose_by_function.snd";

# ---------------------------------------------------------------- ANALYSIS ---
$fft_length = 1024;
$frames_per_second = 200;

# -------------------------------------------------- SPECTRUM MODIFICATIONS ---
# Frequency shift in Hz
$frequency_shift = 0;

# Pitch transposition in semitones, using a function file.
$trans_file = "pvc_trans_curve";
$pitch_transposition = $trans_file;

# Define the transposition curve, and write it to the file referenced above.
# Modify only the numbers in the middle, after `2000' and before `>'. These
# numbers work like the time/value pairs in an RTcmix setline.
`gen1 -L2000   0 -6  1 7  2 -10   > $trans_file`;

# Resynthesize only those frequency bins with power above the given threshold,
# in dBFS. This can reduce processing time. Try -60 to -70.
$osc_resynth_threshold = -70;

# ------------------------------------------------------------- RESYNTHESIS ---
$gain = 0;                           # Gain change in dB

# Time compression / expansion factor
$time_scale_factor = 15.0;

# ----------------------------------------------------------------- DISPLAY ---
$print_amp_stats = 1;                # 0: no, 1: yes
$amp_stats_time_interval = .25;


#==============================================================================
# COMMAND LINE SETUP -- OFFICE USE ONLY

# Adjust frame rate by scale factor, but can't be > sampling rate.
$frames_per_second = $time_scale_factor * $frames_per_second;
if ($frames_per_second > 44100) {
   $frames_per_second = 44100;
}

# Build argument string from vars defined above.
$pvflags = 
" -N$fft_length" .
" -M0" .
" -w2" .
" -D$frames_per_second" .
" -I$time_scale_factor" .
" -a$frequency_shift" .
" -P$pitch_transposition" .
" -A$gain" .
" -C$input_chan" .
" -t$osc_resynth_threshold" .
" -b$begintime" .
" -e$endtime" .
" -p$print_amp_stats" .
" -i$amp_stats_time_interval";

# Build command line.
$cmd = "plainpv $pvflags $input_file $output_file";

# Print command line for reference.
print $cmd, "\n\n";

# Execute command.
`$cmd`;

# Delete the function files.
unlink $trans_file;
unlink $rate_file;

