   # ECMC PVC example file spectwarper96gourd1
   # This example emphasizes the noise elements (rapidly changing frequencies)
   # within a 96k 24bit gourd shake soundfile
   # The most important parameters are:
# COMPRESSION_threshold_in_dB=-3 # default= 0; to emphasize noise try -3
# decibels_of_compression=-96 # default= 0; to emphasize noise try -96
# Note that the EXPANSION parameters, which tend to emphasize pitched elements,
# are at their default value of 0
# EXPANSION_threshold_in_dB=0 # default= 0; to emphasize pitch try -2
# decibels_of_expansion=0  # # default= 0; to emphasize pitch try -96
#COMPANDER_WINDOW_SIZE_in_octaves=1 # set to 0 for more noise, 1 for more pitch
 # Because of the 96k sampling rate and the manyu high frequencies within the guourd sound
 # the FFT size is increased from the default 1024 to 2048
# FFT_length=2048 # must be power-of-2, usually 1024, 512 or 2048
 # The frame rate is increased from the deafult 200 to 400 frames per second:
# frames_per_second=400   # generally 200, occasionally 400 or 600 when time stretching
# The pitch is tranposed down 3 half steps (a minor third)
# pitch_transposition_in_semitones=-3
# Because the resonant, sustained pitched frequencies are greatly attentuated
# it is necessary to increase the output gain:
# gain_in_decibels=6
#******************************************************
#..................... SPECTWARPER ......................
#******************************************************

#******************** OUTPUT **************************
#******** INPUT & OUTPUT SOUNDFILES *************
cd $SFDIR  # all files will be written to your current soundfile directory
#inputsf=/sflib/perc/marktree.lohi.wav
inputsf=/sflib96/perc/gourd/gourd1.shake.fast.wav
outputsf=spectwarper96gourd1.wav  # output soundfile
# ********************* #

   ##### Cmusic function file generator tempates #####
#   gen0  normalizes function files previously created with other gen routines
# gen0 -Llength  max < inputfuncfile > outputfuncfile
#   gen1 creates linear {straight line} segments, like Csound gen 7
# gen1 -Llength t1 v1 ... tN vN
#   gen2 generates harmonic waveforms from sine {a} & cosine {b} amps
# gen2 -Llength [-o (default) or -c] a1 ... aN b0 ... bM N
#  gen3 generates amp values & linear connections at equally spaced time points
# gen3 -Llength v1 v2 ... vN
#   gen4 generates exponenetial segments; "a" values determine shape &
#  depth of curve: 0 = linear, neg. = exponential, pos. = inverse expo.
# gen4 -Llength t1 v1 a1 ... tN vN
#  gen5 is like Csound gen 9 : harmonic1/amp/phase harmonic2/amp/phase
# gen5 -Llength h1 a1 p1 ... hN aN pN
#     gen6 generates a table of random numbers between +1 and -1
# gen6 -Llength
#    cspline: smooth curve {cubic spline} interpolator
# cspline len_flag [flags] x0 y0 x1 y1 ... xN yN
#   genraw reads in a previously created function file
# genraw -LN filename    (where N is the length of the output function.)
# For a usage summary of "reshape" type  "reshape"  with no arguments.
   ##### End of gen routine function generator tempates #####


output_data_format=0
   #  0=  Same as input file, 1 = integers 2 = rescaled floats

#........ BEGIN/END TIMES .............................
begintime=0
endtime=0        # (0 end time defaults to end of file)

#======================================================
#*** ANALYSIS PARAMETERS ******************************
FFT_length=2048 # must be power-of-2, usually 1024, 512 or 2048
      # do not set FFT_length lower than 1024 if samp. rate = 96000
window_type=12
    # window type: 0 to 12; default 12 gives good separation of noise and pitch
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
frames_per_second=400   # generally 200, occasionally 400 or 600 when time stretching
#======================================================
#*** RESYNTHESIS PARAMETERS ***************************

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

output_channel=1
# (channels are numbered from 1-maximum, 0 = all channels)

#.............OSCIL THRESHOLD ........................
oscillator_resynthesis_threshold_in_dB=-70

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

#.................. TIME ..............................
time_expansion_contraction_factor=1

#.................. DECIBELS ..........................
gain_in_decibels=6

#.................. PITCH .............................

frequency_shift=0
pitch_transposition_in_semitones=-3

#======================================================
#****** COMPRESSION/EXPANSION PARAMETERS **************

#.....COMPANDER FREQUENCY BAND ......................

low_cutoff_frequency=0 
high_cutoff_frequency=20000
octaves_of_rolloff=0

#( Choose to compress (noise/residue) or expand (pitch/resonance)
#  by setting the respective threshold and decibel level.  
#  Less interesting, though possible, is the selection of an 
#  amplitude band achieved by setting the expand threshold below 
#  the compress threshold, and giving each a substantial reduction level. )
###################################################################
# Change EITHER COMPRESSION..(NOISE) or EXPANSION..(PITCH) parameters:

#.....COMPRESSION..(NOISE)...........................
# To emphasize the NOISE components within the source sound
# try -3 COMPRESSION_threshold_in_dB with -96 decibels_of_compression
# to compress out pitch/resonance
COMPRESSION_threshold_in_dB=-3 # default= 0; to emphasize noise try -3
decibels_of_compression=-96 # default= 0; to emphasize noise try -96

#.....EXPANSION..(PITCH)...............................
# To emphasize the PITCH components within the source sound
# try EXPANSION_threshold_in_dB=-2 with decibels_of_expansion = -96
# to expand out residue and noise
EXPANSION_threshold_in_dB=0 # default= 0; to emphasize pitch try -2
decibels_of_expansion=0  # # default= 0; to emphasize pitch try -96
###################################################################

#......... COMPLEMENT SPECTRUM PROPORTION ..............

COMPLEMENT_SPECTRUM_proportion=0 # 
#( 0 = off tends to emphasize noise; 1 = one octave tends to emphasize noise)
#( 1 gives complement amp value, 0 off, .5 halfway between or 
#  simply, the original (no warp), etc.)
#( For example, if the settings below produced a predominantly
#  expanded spectrum highlighting the resonant parts
#  of the sound, then a value of 1  would produce its
#  compressed complement of residual-noise components. 
#  A 0-1 function would create a crossfade between the two.)

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

COMPANDER_WINDOW_SIZE_in_octaves=1 # set to 0 for more noise, 1 for more pitch
#( 0 = off tends to emphasize noise; 1 = one octave tends to emphasize pitch)
#( Compression/expansion is created by warping the amplitudes
# in a specific window relative to the peak amp in that window.
# When the window is not 0, this parameter determines the
# window size which is centered around each of the bins. 
# Otherwise their is only one peak.)
#( The larger the window, the more suppression there will
#  be in favor of the few strongest formants. Try 1 octave 
# windows. Very large windows are equivalent to 0 which  
# uses one window for the whole spectrum.)

#....PEAK FOLLOWING RESPONSE TIMES ....................

PEAK_FOLLOWING_attack_response_time_in_seconds=0
PEAK_FOLLOWING_release_response_time_in_seconds=0

#( In normal mode, the amplitudes are warped against the
#  peak amp in the current analysis frame, peaks that, in
# sucession represents the amplitude envelope of the signal. 
#  Setting non-zero response times for the "peak following"
#  delays the attack and decay features of this envelope which
#  in turn changes the warp of amplitudes by changing the
#  current peak. The best use is with non-zero release times. 
#  These perpetuate strong envelope peaks that  expand
#  out the decays after them. )

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

WARP_CURVE_INDEX=0

#( The compression/expansion is achieved using a linear gradient
#  when the warp curve index is 0.  Positive warp curve  
#  values  smooth the transition into the compression/expansion 
#  regions, negative values sharpen the transition.  )

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

COMPANDER_response_time_in_seconds=.05 # default .05
#( Use values from .01-.2 or more to remove the gurgle and 
#  roughness caused by  the oscillation of components
#  into and out of the compansion band.)

#======================================================
#*************** LOW/HIGH SHELF EQ *********************
LOW_SHELF_EQ_gain_in_decibels=-0
LOW_SHELF_EQ_frequency=1000

HIGH_SHELF_EQ_gain_in_decibels=-0
HIGH_SHELF_EQ_frequency=8000

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

#........... 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

#====================================================
# COMMAND LINE SETUP -- OFFICE USE ONLY
#      (DO NOT WRITE BELOW THIS LINE)
#====================================================
pvroutine=spectwarper  # ECMC
# *****  TKLA CHANGES: ******* #
cd $SFDIR 
 SR=`sfsr $inputsf | awk '{print $1}'`
if ( ( [ `expr "$SR" \> "48000"` == 1 ] ) &&  ( [ "$windowsize" == "0" ] ) ) ; then 
       # if SR is > 48000 & windowsize is set to 0 increase default windowsize
  windowsize=`expr $FFT_length \* 4`
fi
# Determine if input soundfile is 24 bit. If so, compile 32 bit float outputs, then 
# convert to 24 bit ints
WORDSIZE=`/usr/local/bin/sfbits "${inputsf}" | awk '{print $1}'`  
if [ "$WORDSIZE" == "24" ] ; then rm -f pvcin ; 24tofloat $inputsf pvcin ; input_file=pvcin
  if  [ "$output_data_format" != 2 ] ; then
    output_file=pvcout  # temporary floating point output soundfile for 24-bit inputs
    rm -f  pvcout;  output_data_format=2
  else  # 24 bit input but float output requested
    output_file=$outputsf ; fi
else # not 24 bit input ; 16 bit int or 32 bit float input
    input_file=$inputsf ;  output_file=$outputsf
fi
#  ****** end of TKLA changes & additions ****** #


#====================================================
# SYNTHESIS


PVFLAGS="\
\
-N$FFT_length \
-M$windowsize \
-w$window_type \
-D$frames_per_second \
-I$time_expansion_contraction_factor \
\
-a$frequency_shift \
-P$pitch_transposition_in_semitones \
-A$gain_in_decibels \
\
-C$output_channel \
-t$oscillator_resynthesis_threshold_in_dB \
\
-b$begintime \
-e$endtime \
\
\
-o$COMPRESSION_threshold_in_dB \
-O$decibels_of_compression \
-q$EXPANSION_threshold_in_dB \
-Q$decibels_of_expansion \
\
-W$WARP_CURVE_INDEX \
-S$COMPANDER_WINDOW_SIZE_in_octaves \
-r$COMPANDER_response_time_in_seconds \
\
-g$COMPLEMENT_SPECTRUM_proportion \
\
-c$low_cutoff_frequency \
-d$high_cutoff_frequency \
-f$octaves_of_rolloff \
\
-L$PEAK_FOLLOWING_release_response_time_in_seconds \
-l$PEAK_FOLLOWING_attack_response_time_in_seconds \
\
-H$LOW_SHELF_EQ_gain_in_decibels \
-m$LOW_SHELF_EQ_frequency \
\
-X$HIGH_SHELF_EQ_gain_in_decibels \
-R$HIGH_SHELF_EQ_frequency \
\
-p$print_amplitude_statistics_0_no__1_yes \
-i$amplitude_statistics_time_interval \
\
-_$output_data_format \
-=$rescale_level_in_decibels \
\
\
"
echo "\n$pvroutine $PVFLAGS $input_file $output_file"

$pvroutine  $PVFLAGS $input_file $output_file   ; 
   # *****  TKLA CHANGES & ADDITIONS: ******* #
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
    if (  ( test -f "pvcin" ) ) ; then   rm -f pvcin ; fi
   echo " -------------------------------------------------------"
fi
   #  ****** end of TKLA changes & additions ****** #

