samp2.lsp

Jules Rawlinson adapted Mike’s original samp1 instrument to include filters, lfos and waveshaping. Now I have adapted it to work in slippery chicken.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; File:             samp2.lsp
;;;
;;; Purpose:          A 'sampling' instrument: performs high-quality
;;;                   sampling-rate conversion (transposition)  of a sound
;;;                   file.  
;;;
;;;                   When duration is nil the instrument automatically plays
;;;                   the whole input file either forwards (or backwards: if
;;;                    is t).  
;;;
;;;                   If output duration > input duration then the whole input
;;;                   file will be played forwards then backwards until the
;;;                   duration is used up. 
;;;
;;;                   See the instrument parameters for more possibilities.
;;;
;;;                   This instrument was extended by Jules Rawlinson to
;;;                   include High and Low Pass filtering using the Butterworth
;;;                   filter (cload "mus:clm-3;butterworth.cl") and also
;;;                   supports various lfo modulations including amplitude,
;;;                   filter modulation, vibrato (pitch), and a hanning window
;;;                   for grain enveloping. Look for the hidden waveshaping!
;;;
;;;                   This was extended again in Feb 2013 by Michael Edwards to
;;;                   allow for multi-channel input file processesing.  If a
;;;                   mono input file is passed, we use locsig to locate in
;;;                   n-channel output space.  If the input has 2+ channels
;;;                   then the original spacing is retained and the same
;;;                   processing is applied to each channel successively.
;;;
;;; Authors:          Michael Edwards - m@michael-edwards.org
;;;                   Jules Rawlinson - j@pixelmechanics.com
;;;                   Adapted for use in slippery chicken by
;;;                   DJR - mr.danielross@gmail.com
;;;
;;;
;;; $$ Last modified: Fri 10 Feb 2017 16:46:04 GMT
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(in-package :clm)
(setf *read-default-float-format* 'single-float)
;;; You must compile and load butterworth before doing the same with this ins;
;;; loading alone is not enough, e.g.
(load (compile-file "/Applications/slippery-chicken-OSX.app/Contents/Resources/clm-4/butterworth.cl"))

(definstrument samp2
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; Beginning of Instrument Parameters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    (file ;; Input file path
     time ;; Point of output in file in seconds
     &key ;; the following parameters are optional
     ;; MDE Mon Nov  4 10:08:45 2013 -- just for
     ;; compatibility with clm-play i.e. not used
     frequency
     (duration nil)         ;; Output file duration; when nil then the whole
     ;; input file will be played. 
     ;; In Lisp, Yes/No or True/False (so-called
     ;; Booleans) are indicated by T and Nil.
     (reflect nil)          ;; When duration is nil, whether the input file
     ;; should play forwards then backwards.
     (reverse nil)          ;; Begin going backwards?
     (start 0)              ;; Input file start time (seconds).
     (end 0)                ;; Input-file end time (seconds).
     (srt 1.0)              ;; Sampling-rate Conversion: 1 = no 
     ;; transposition, 2 = oktave high, 0.5 = oktave
     ;; lower etc.
     (width 5)              ;; How many samples to convolve with the sinc table
     ;; (the higher the better but also the slower the
     ;; processing).
     (srt-env '(0 0 100 0)) ;; Sampling-rate Conversion Envelope (glissando);
     ;; when the y value is 0 there is no transposition
     ;; beyond that of srt-scaler.
     (srt-scaler 1.0)		     ;; Scaler for srt-env.
     (amp 1.0)			     ;; Amplitude, usually > 0.0  0.0 = ml end-sample) 
                   (setf (mus-increment src-gen) (- srt)))
               ;; similarly, if we're before the first input sample, start going
               ;; forwards. 
                 (when ( 0 - 1 = -1 > abs 1 * our input = unchanged input
               ;; -- in the case of 100% lfo amount...
               ;; 1 * 1 = 1 > 1 - 1 = 0 > abs 0 * input = 0 output (if amp lfo)
                 (setf lpflt-lfo (abs (- lpflt-lfo 1.)))
               ;; finally update freq from freq-env * lfo output
                 (set-butterlp lpflt (* (env lpflt-frq-env) lpflt-lfo))

               ;; do it all again for the hipass filter
                 (setf (mus-frequency hpflt-lfo-osc) (env hpflt-lfo-frq-env)
                       hpflt-lfo (* (oscil hpflt-lfo-osc)
                                    (env hpflt-lfo-amt-env))
                       hpflt-lfo (abs hpflt-lfo)
                       hpflt-lfo (abs (- hpflt-lfo 1.)))
                 (set-butterhp hpflt (* (env hpflt-frq-env) hpflt-lfo))

               ;; now do it all again for the amplitude lfo
                 (setf (mus-frequency amp-lfo-osc)(env amp-lfo-frq-env)
                       amp-lfo (* (oscil amp-lfo-osc) (env amp-lfo-amt-env))
                       amp-lfo (abs amp-lfo)
                       amp-lfo (abs (- amp-lfo 1.)))

               ;; pitch-mod stage
                 (setf (mus-frequency pm-osc)(env pm-frq-env)
                       pm-sig (* (* (oscil pm-osc) pm-max) (env pm-amt-env)))

               ;; calculate our output sample
                 (setf out-sig (src src-gen (+ (env senv) pm-sig)))

               ;; amp-mod stage
                 (setf (mus-frequency am-osc)(env am-frq-env)
                       am-sig (* (oscil am-osc) (env am-amt-env))
                       am-sig (+ am-sig (- 1 (env am-amt-env)))
                       out-sig (* out-sig am-sig))

               ;; ring-mod stage
                 (setf (mus-frequency rm-osc)(env rm-frq-env)
                       rm-sig (* (oscil rm-osc) (env rm-amt-env))
                       rm-sig (- 1 rm-sig)
                       out-sig (* out-sig rm-sig))

               ;; scaling stage

               ;; waveshaping
                 (if wv-shp-amt
                     (setf out-sig (/ (* out-sig (+ (abs out-sig) wv-shp-amt)) 
                                      (+ (expt out-sig 2) 
                                         (* 
                                          (- wv-shp-amt 1) (abs out-sig)) 1))))
           
               ;; are we graining by hanning
                 (if hanning
                     ;; Y - scale via hanning function
                     (setf out-sig 
                           (* out-sig
                              (* 0.5 (- 1.0 (cos (/ (* (* pi 2) i) 
                                                    (- (floor (* *srate* dur))
                                                       1)))))))
                     ;; N - scale it by the amp-env
                     (setf out-sig (* out-sig (env ampf)))
                     )

               ;; scale the output sample by the amp lfo
                 (setf out-sig (* out-sig amp-lfo))
               ;; filter stage
               ;; lowpass filter the output
                 (setf out-sig (butterlp lpflt out-sig))
               ;; lopass second stage
                 (if lp-two 
                     (setf out-sig (butterlp lpflt out-sig)))
               ;; hipass filter the output
                 (setf out-sig (butterhp hpflt out-sig))
               ;; hipass second stage
                 (if hp-two 
                     (setf out-sig (butterhp hpflt out-sig)))
               ;; output in stereo space
                 (if mono-in
                     (locsig loc i out-sig)
                     (out-any i out-sig chan))
               ;; END NEW FUNCTIONS
                 ))
           ;; close our input file
           (close-input f)))))

    ;;;;;;;;;;;;;;;;;;;;;;;;
    ;; End of definistrument
    ;;;;;;;;;;;;;;;;;;;;;;;;

#|

;; Example usage in clm-play

(clm-play +mini+ 1 'pno 'default-group
	      :pitch-synchronous t
	      :check-overwrite nil
	      :duration-run-over t
              :output-name-uniquifier "samp2-"
	      :snd-selector #'(lambda (sflist pitch event)
				(declare (ignore event))
				(get-nearest-by-freq
				 (frequency pitch) (data sflist)))
	      :pan-min-max '(30 60)
	      :rev-amt .05
	      :src-scaler 0.25
	      :amp-env '(0 0 5 1 70 1 100 0)
	      :output-name-uniquifier "samp2-"
	      :clm-ins #'clm::samp2
	      :clm-ins-args '(:lpflt-frq 60
			      :lpflt-frq-env (0 200 50 250 100 200)
			      :lpflt-lfo-frq-env (0 100 100 100)
			      :lpflt-lfo-amt-env (0 .2 50 .8 100 .2)
			      :srt-env (0 0 50 1 100 -1)
			      :rev-env (0 0 70 1 100 1)))
|#

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s