Let me give an example:
(setf val 10)
(* 2 val)
(+ 1 val)
Question: What is the “return value” of this code?
Let’s look at the wrong reasoning first:
Line 1: We set VAL to 10
Line 2: We doubled VAL, producing 20
Line 3: We added 1, producing 21
The reason this is wrong is that in line 2, yes we applied the multiplication function to double the value of VAL, but we didn’t assign this value to anything and we have not modified VAL. VAL is still = 10.
So in line 3, we add 1 to 10, and the return value is 11.
You can prove this in the Nyquist Prompt.
If we actually want to get the answer 21, then there’s several ways we can do that:
- Assigned the result of (* 2 val) to a new variable, then apply the second function (addition) to the new variable:
(setf val 10)
(setf doubleval (* 2 val))
(+ 1 doubleval)
- Assign the result of the first function to the variable VAL, effectively updating the value of VAL:
(setf val 10)
(setf val (* 2 val))
(+ 1 val)
- Assign the result of the first function to a temporary “local” variable:
(setf val 10)
(let ((temp (* 2 val)))
(+ 1 val))
- Nest the functions so that the value returned from the multiplication is used directly in the second (addition) function. In this version, the innermost function “(* 2 val)” is evaluated first, and its return value is the second argument (second “parameter”) in the outer function:
(setf val 10)
(+ 1 (* 2 val))
OK, so you flipped a sound, but what did you do with that flipped sound? Did you assign it to a new variable? Did you updated the value of track? Did you assign the flipped sound to a temporary “local” variable?
The verbose way to handle this would be something like:
(setf filtered (lowpass2 *track* hz)) ;Assign filtered audio to a new variable
(setf firstflip (multichan-expand #'flip filtered ln)) ;Flip the filtered sound
(setf filtered (lowpass2 firstflip hz)) ;Filter flipped sound
(multichan-expand #'flip *track* ln) ;Return result of second flip
A more condensed way using temporary “local” variables:
;version 4
;type process
(defun flip (sig ln)
(let ((s-array (snd-fetch-array sig ln ln)))
(do ((i 0 (1+ i))
(j (1- ln) (1- j)))
((>= i j) (snd-from-array 0 *sound-srate* s-array))
(setf temp (aref s-array i))
(setf (aref s-array i) (aref s-array j))
(setf (aref s-array j) temp))))
(setf ln (truncate len))
(setf hz 4000)
(let* ((flipped (multichan-expand #'flip *track* ln))
(filtered (lowpass2 flipped hz)))
(lowpass2 (multichan-expand #'flip filtered ln) hz))