Page 2 of 2

Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Mon Jan 01, 2018 6:15 pm
Posted by steve
The "format" function prints an expression to the specified 'destination' (http://www.audacity-forum.de/download/e ... ef-121.htm)
If the 'destination' is NIL , a string is created and returned with the contents of the 'format'.

If a Nyquist script returns a string, then Audacity displays the string in a pop-up message window.

In LISP / Nyquist, the tilde "~" character in a string has a special meaning. In conjunction with the next character, it acts as a "format specifier".
Some common format directives are:
~A - Prints the expression as ASCII text. Strings are printed without quotes.
~S - Prints the expression as an s-expr. Strings are printed with quotes.
~% - prints a 'newline' control character.
Code: Select all
(format nil "~s ho ho" "o&&")

This will replace ~s with "o&&", forming a string:
"o&&" ho ho

If the ~A specifier is used,
Code: Select all
(format nil "~a ho ho" "o&&")

then the created string will be:
o&& ho ho

The returned string may be bound to a variable like this:
Code: Select all
(setf my-string (format nil "~a ho ho" "o&&"))


Because the destination is NIL, the format function doesn't print the string anywhere, but if the string is the final expression in a script, then the string will be the value that is returned by the script (the "return value") and Audacity will display it in a pop-up message.

Code: Select all
(setf my-string (format nil "~a ho ho" "o&&"))
(hzosc 440)

This code creates a string "o&& ho ho" and binds the value to the variable "my-string". The string is not printed because the destination is NIL.
The return value of the script is a 440 Hz sine wave.


Code: Select all
(setf my-string (format nil "~a ho ho" "o&&"))
(hzosc 440)
my-string

In this case, "my-string" is the final expression of the script, so it is returned to Audacity. The value of "my-string" is "o&& ho ho", so Audacity creates a message window and displays the text "o&& ho ho" in it.


If the 'destination' is T , the printing occurs to *standard-output*. For Nyquist plug-ins, the *standard-output* is the debug window.
Code: Select all
(setf my-string (format t "~a ho ho" "o&&"))
(hzosc 440)

This code creates a string "o&& ho ho" and binds the value to the variable "my-string". The string is printed to the debug output.
The return value of the script is a 440 Hz sine wave.


Code: Select all
(setf my-string (format t "~a ho ho" "o&&"))
(hzosc 440)
my-string

In this case, "my-string" is printed to the debug window.
"my-string" contains the string "o&& ho ho", but it also carries the "destination" as the debug output, so it is not returned to Audacity so there is no message box.

If we want the last example to return "my-string" to Audacity as well as printing to the debug window, we could write:
Code: Select all
(setf my-string (format nil "~a ho ho" "o&&"))
(format t "~a" my-string) ;print to debug
(hzosc 440)
my-string ;return value

Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Wed Jan 03, 2018 2:22 pm
Posted by DERF
Thank you.
My question concerns ask functions (dotimes and format).
(dotimes (sym expr [rexpr]) expr...)
symbol = a symbol
end-expr = an integer

(format stream fmt arg...)
fmt = a string of custody

Now, it is necessary to observe this code:
Code: Select all
(format t "Item ~a in a-list = ~a~%" count val)


In this case, we observe that the arguments are:"count" and "val"
But, "count" is a symbol.
My question is the following one: Can we use symbols as arguments of the function "format"?
Thank you for your answer.

Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Wed Jan 03, 2018 3:00 pm
Posted by steve
DERF wrote:In this case, we observe that the arguments are:"count" and "val"
But, "count" is a symbol.
My question is the following one: Can we use symbols as arguments of the function "format"?

If the symbol "count" has a value (if it is "bound"), and if that value can be represented as a string (most values can), then yes you can use it like that and the value bound to "count" will replace the associated "~a" in the string.

Example:
Code: Select all
(dotimes (count 4)
  (format t "Counter is ~a~%" count))

Debug output:
Code: Select all
Counter is 0
Counter is 1
Counter is 2
Counter is 3


Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Sat Jan 06, 2018 4:51 pm
Posted by DERF
Thank you.
I have a question. it concerns the following function: terpri.
Into the code
Code: Select all
(setf a-list (list "A" "B" "C" "D"))
 (setf num (length a-list))
(print num)
 (terpri)
the print function prints correctly the value 4.
It seems to me that the function terpri prints objects awaiting impression.
But, in this example, the value 4 is already printed.
Thus, my question is the following one: what is the utility to use here the function terpri?
Beforehand thank you for your answer.

Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Sat Jan 06, 2018 5:26 pm
Posted by steve
http://www.audacity-forum.de/download/e ... ef-279.htm
In short, TERPRI prints an EOL character.
(the name "terpri" comes from "terminate print")

Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Fri Jan 12, 2018 5:46 pm
Posted by DERF
Thank you.
I tried to use prin1 here
Code: Select all
(setq data1 (list 0 0 1 1 0 1 1 0 0 1 1 0 0 0 1 0 1 0 0 0))
    (setq intvl 5)
    (setq root 60)

    (abs-env
     (simrep (i 4)
      (let*
       ((vol (* 8 (nth (+ 1 (* i 5)) data1))))
(prin1  (* i 5))
(prin1 (+ 1 (* i 5)))  )))
But, there is a problem. LISP return 015610111516
To use terpri
Code: Select all
 (prin1  (* i 5)) (terpri)
(prin1 (+ 1 (* i 5)))    )))
does not solve the problem.
Code: Select all
0
15
610
1115
16

My question is this:
Why is the first evaluation 051015 mixed with second 161116?

Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Fri Jan 12, 2018 7:23 pm
Posted by steve
Code: Select all
(prin1  (* i 5)) (terpri)

That will print the result of (*i 5) and then print a new line.

Code: Select all
(prin1 (+ 1 (* i 5)))    )))

That will print the result of (+ 1 (* i 5)) and returns the result. There is no new line.

so when i = 0, the loop will print:
Code: Select all
0
1


when i = 1, the values 5 (new line) and 6 are printed directly after the "1" giving:
Code: Select all
0
15
6


then when i = 2, the values 10 (new line) and 11, giving:
Code: Select all
0
15
610
11

and so on.


If you use "print" instead of "prin1":
Code: Select all
(setq data1 (list 0 0 1 1 0 1 1 0 0 1 1 0 0 0 1 0 1 0 0 0))
(setq intvl 5)
(setq root 60)

(abs-env
  (simrep (i 4)
    (let ((vol (* 8 (nth (+ 1 (* i 5)) data1))))
      (print  (* i 5))
      (print (+ 1 (* i 5))))))


you will get:
Code: Select all
0
1
5
6
10
11
15
16


Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Sat Jan 13, 2018 12:53 pm
Posted by DERF
Thank you.
I tried to explain.
It is strange that LISP estimates first:
(0 X 5) - (1 + 0 X 5)
then (1 X 5) - (1 + (1 X 5) ...
But not:
(0 X 5) - (1 X 5) - (2 X 5) - 3 X 5)
then (1 + 0 X 5) - (1 + (1 X 5) and so on
It is strange because the code is first:
(print (* i 5) then (print (+ 1 (* i 5))
It was my badly explained question.
So, my question is:
Can we write a code the evaluation of which gives:
051015
161116

Beforehand, thank you for your answer.

Re: follow up How to generate arbitrary waveforms

Permanent link to this postPosted: Sat Jan 13, 2018 1:29 pm
Posted by steve
I don't know what you are trying to achieve (you've not said).

If you want to produce two lists, one containing the numbers 0 5 10 15 and the other containing the numbers 1 6 11 16, then this will do it:
Code: Select all
(let ((list-a ())
      (list-b()))
  (dotimes (i 4)
    (push  (* i 5) list-a)
    (push (+ 1 (* i 5)) list-b))
  (print (reverse list-a))
  (print (reverse list-b)))