# ECMC PVC example "convolver5" is similar to "convolver2", but here there
# is no "cross-fade" between Sounds A (/sflib/string/vc.gs2) and B (a flute tone
# derived from an analysis of /sflib/wind/fl.c4, created by example "pvcanal2
# Instead, only the convolution is used, and remains fixed, so that only frequencies
# common to both source sounds are heard throughout the output sound:
#   convolution_panpot_position=0 #   int, float or FUNC
# The duration is stretched by a factor of 1.5
#  time_expansion_contraction_factor=1.5
# and the pitch is transposed up a minor seventh:
#  OUTPUT_SPECTRUM_pitch_transposition_in_semitones=10 # int, float or FUNC

#******************************************************
#................... CONVOLVER ........................
#******************************************************
     # ******ECMC CHANGES & ADDITIONS: ***************
#******** INPUT & OUTPUT SOUNDFILES *************
cd $SFDIR # all files will be created in your current working soundfile directory

Sound_A_input_soundfile=/sflib/string/vc.gs2.wav
Sound_B_input_analysis_file=fl.c4.pvc
outputsf=convolver5.wav # convolver2  # output soundfile
outputformat=1 # 0,1,2, or 3; 1 = 16 bit short ints; 2 = 32 bit floats; 3 = 24 bit ints
# 0 outputs 16 bit ints if samp. rate < 50000, 24 bit ints if samp rate > 50000
# ********************* #

   ##### Cmusic function file generator tempates #####

##### function file #####
# gen4 -Llength t1 v1 a1 ... tN vN
# function to make fl.c4 duration match  vc.gs2 duration
 gen4 -L1000 0 0 0 3.82 3.473 > $SFDIR/func1 
 # cross-fade function

   ##### End of gen routine function generator tempates #####

#........... OUTPUT CHANNEL(S) .......................

Sound_A_channel_1_to_max__0_all=0
Sound_B_channel_1_to_max__0_all=0

# (channels are numbered from 1-maximum,0 = all channels
# 0-0 configurations will match A and B channels
# 0 with a single channel will apply single channel to
#  all channels of other file 

#======================================================
#............ SOUND A BEGIN/END TIMES .................

Sound_A_begintime=0
Sound_A_endtime=0
# (0 end time defaults to end of file)

#======================================================
#............. SOUND B TIME ...........................
SOUND_B_data_access_mode__explicit_0__rate_1=1
SOUND_B_time_point=0
SOUND_B_rate_multiplier=1  # int, float or FUNC

# In rate mode, set time_point to initial position.
# In explicit mode, control time_point with a function.
#  Rate_multiplier is nonfunctional in explicit mode.

#......................................................

SOUND_B_lower_time_boundary=$SFDIR/func1 # int, float or FUN
SOUND_B_upper_time_boundary=$SFDIR/func1 # int, float or FUN
# upper time < 0 defaults to file duration
#......................................................

autostop_on_1__off_0=1
# If on, this will terminate synthesis when a boundary is crossed

#======================================================
#*** ANALYSIS PARAMETERS ******************************
window_type=2
    # window type: 0 = Hamming,1 = rectangular, 2 = Blackman (DEFAULT),
    #  3 = Bartlett triangular, 4-12 = Kaiser windows for alpha = 4-12
    # generally recommended: 2 or 8
windowsize=0    # default 0 sets windowsize (in samples) to 2 * FFT_length if
  #  samp. rate <= 48000 or to 4 * FFT_length if samp. rate > 48000

#======================================================
#*** RESYNTHESIS PARAMETERS ***************************

#.............OSCIL THRESHOLD ........................
oscillator_resynthesis_threshold_in_dB=-96

#****************** MODIFICATIONS *********************

#.................. TIME ..............................
time_expansion_contraction_factor=1.5

#.................. DECIBELS ..........................
OUTPUT_SPECTRUM_gain_in_decibels=10 # int, float or FUNC

#.................. PITCH .............................
OUTPUT_SPECTRUM_frequency_shift=0 # int, float or FUNC
OUTPUT_SPECTRUM_pitch_transposition_in_semitones=10 # int, float or FUNC

#............ AMPLITUDE RESPONSE ......................

release_in_seconds=0   # int, float or FUNC
attack_in_seconds=0    # int, float or FUNC

#======================================================
#*** CONVOLUTION PANPOT (-1 to 1) (-1 = A,  1 = B )****
convolution_panpot_position=0 #   int, float or FUNC

#.................. DECIBELS ..........................

Sound_A_gain_in_decibels=-0   #  int, float or FUNC
Sound_B_gain_in_decibels=-0   #  int, float or FUNC
Convolution_gain_in_decibels=0 # 20   #  int, float or FUNC

#............ PANPOT DOMAIN WARP ......................
   #(warp values: 0 and greater)
  # warp: 0 = linear, incresingly positive warp 
  # values distribute the convolution center into the 
  # Sound A or B's panpot domain. 
Sound_A_panpot_domain_warp=0
Sound_B_panpot_domain_warp=0


#(The domain warp allows you to expand the useful
# convolution range on either side of the full 
# convolution point at 0 by warping the panpot range on
# that side.  Use increasingly positive  warp values
#  to expand the convolution center into the
# Sound A or B's panpot domain.  )

#======================================================
#****** CONVOLUTION OUTPUT LOW/HIGH SHELF EQ **********
FILTER_LOW_SHELF_EQ_gain_in_decibels=-0
FILTER_LOW_SHELF_EQ_frequency=200

FILTER_HIGH_SHELF_EQ_gain_in_decibels=0
FILTER_HIGH_SHELF_EQ_frequency=1000
#======================================================
#........... RESCALE for floating point only ......
rescale_level_in_decibels=0
   # set to 1 to rescale to peak of input file; do not do this if input amplitude
   # is low

#======================================================
#********** AMPLITUDE STATISTICS ******************** 
print_amplitude_statistics_0_no__1_yes=1
amplitude_statistics_time_interval=.25

#====================================================
# COMMAND LINE SETUP -- OFFICE USE ONLY
#      (DO NOT WRITE BELOW THIS LINE)
#====================================================
# *****  TKLA CHANGES: ******* #
input_file=$Sound_A_input_soundfile ; output_file=$outputsf
output_data_format=outputformat
WORDSIZE=`/usr/local/bin/sfbits "${Sound_A_input_soundfile}" | awk '{print $1}'`
if ( [ "$WORDSIZE" == "24" ] ) ; then rm -f pvcin; input_file=pvcin; 
  24tofloat $Sound_A_input_soundfile pvcin ; output_data_format=2
fi
if  ( ( [ "$outputformat" == 3 ] ) || ( [ "$WORDSIZE" == "24" ] && [ "$outputformat" == 0 ] ) ) ; then
 rm -f pvcout; output_file=pvcout # temporary floating point output soundfile for 24-bit output
output_data_format=2
fi
#  ****** end of TKLA changes & additions ****** #
pvroutine=convolver 

PVFLAGS="\
\
-M$windowsize \
-w$window_type \
-I$time_expansion_contraction_factor \
\
-a$OUTPUT_SPECTRUM_frequency_shift \
-P$OUTPUT_SPECTRUM_pitch_transposition_in_semitones \
-A$OUTPUT_SPECTRUM_gain_in_decibels \
\
-C$Sound_A_channel_1_to_max__0_all \
-t$oscillator_resynthesis_threshold_in_dB \
\
-b$Sound_A_begintime \
-e$Sound_A_endtime \
\
-q$Sound_A_gain_in_decibels \
-B$Sound_B_gain_in_decibels \
-Z$Convolution_gain_in_decibels \
-L$release_in_seconds \
-l$attack_in_seconds \
-F$Sound_B_input_analysis_file \
\
-u$SOUND_B_data_access_mode__explicit_0__rate_1 \
-K$Sound_B_channel_1_to_max__0_all \
-Q$SOUND_B_time_point \
-Y$SOUND_B_rate_multiplier \
-g$SOUND_B_lower_time_boundary \
-G$SOUND_B_upper_time_boundary \
\
-S$convolution_panpot_position \
-j$Sound_A_panpot_domain_warp \
-J$Sound_B_panpot_domain_warp \
\
-H$FILTER_LOW_SHELF_EQ_gain_in_decibels \
-m$FILTER_LOW_SHELF_EQ_frequency \
\
-X$FILTER_HIGH_SHELF_EQ_gain_in_decibels \
-R$FILTER_HIGH_SHELF_EQ_frequency \
\
-y$autostop_on_1__off_0 \
\
-p$print_amplitude_statistics_0_no__1_yes \
-i$amplitude_statistics_time_interval \
\
-_$output_data_format \
-=$rescale_level_in_decibels \
\
"
echo "\n$pvroutine $PVFLAGS $Sound_A_input_file $output_file"

$pvroutine  $PVFLAGS $input_file $output_file   ; 
   # *****  TKLA CHANGES & ADDITIONS: ******* #
if (  ( test -f "pvcin" ) ) ; then   rm -f pvcin ; fi
if ( ( [ "$WORDSIZE" == "24" ] ) &&  ( [ "$output_file" == "pvcout" ] ) ) ; then
      echo "Converting temporary float output file pvcout to $outputsf"
      echo " -------------------------------------------------------"
      floatto24 pvcout $outputsf 2> /dev/null  ; rm -f pvcin pvcout 
   echo " -------------------------------------------------------"
else
   echo " -------------------------------------------------------"
    echo -e -n "Output soundfile: " ;  sfinfo -s $outputsf
   echo " -------------------------------------------------------"
fi
   #  ****** end of TKLA changes & additions ****** #
# If you have created any gen function files above delete them below
rm func1 


