Adding randomized silence (Windows 10, Audacity 2.1.3)

Help for Audacity 2.x.x on Windows.

ImageThis forum is for Audacity 2.x.x on Windows.

  • Please state which version of Windows you are using, the exact three-section version of Audacity from Help > About Audacity and whether you obtained the .exe installer or the zip.

  • Audacity 1.2.x and 1.3.x are obsolete and no longer supported. If you still have those versions, please upgrade at https://www.audacityteam.org/download/.
    The old forums for those versions are now closed, but you can still read the archives of the 1.2.x and 1.3.x forums.

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by PiTeRr » Thu Aug 10, 2017 6:53 pm

Thanks a lot :) I think I get it. Merging a silence and tone into pair make it the easiest to shuffle that merged list. I think I also understand the Fisher-Yates shuffle algorithm. But I have also another question : is it possible to incorporate in that code occurence of tones at 2 frequencies in given terms ? I mean in one track 100 tones at frequency 1000 Hz, 10 at each level and 100 tones at frequency 4000 Hz, 10 at each level, all of this shuffled ?
PiTeRr
 
Posts: 26
Joined: Thu Jul 27, 2017 6:15 pm
Operating System: Windows 10

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by PiTeRr » Mon Aug 14, 2017 12:28 pm

Hi :) could I count on help with my issue :) ?
PiTeRr
 
Posts: 26
Joined: Thu Jul 27, 2017 6:15 pm
Operating System: Windows 10

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by steve » Mon Aug 14, 2017 3:07 pm

PiTeRr wrote: is it possible to incorporate in that code occurence of tones at 2 frequencies in given terms ? I mean in one track 100 tones at frequency 1000 Hz, 10 at each level and 100 tones at frequency 4000 Hz, 10 at each level, all of this shuffled ?

Sure that would be possible, but probably best not to take too much on at once. By splitting the task into functions, we are making the program modular, so it should be fairly straightforward to extend the program by adding new "modules".

Did you see this post? https://forum.audacityteam.org/viewtopi ... 39&t=96715
How are you going to incorporate that into your program?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44692
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by steve » Tue Aug 15, 2017 1:49 pm

You could write the list of 'gains' (that will be shuffled) by hand as
(setf gainlist (list 0 -5 -10 -15 -20 -25 -30 -35 -40 -45 0 -5 -10....))
but as we're writing a program, why not just tell the computer to generate the list for us:
Code: Select all
;; Make a list of 100 numbers for gain levels where
;; 10 are at each is multiple of 5 between 0 and -45.

(defun gainlist (&aux glist)
  (do ((gain 0 (- gain 5)))
      ((= gain -50) glist)
    (dotimes (i 10)
      (push gain glist))))

;; Test it
(print (gainlist))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44692
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by PiTeRr » Fri Aug 18, 2017 10:21 am

Thanks a lot :) this is the code I put into the Nyquist Prompt but I still get errors
Code: Select all
;version 4
;type generate

(defun tone ()
  (mult (osc (hz-to-step 1000) 0.25)
        (pwlv 0 0.02 1 0.23 1 0.25 0)))

(defun rest ()
  (s-rest (+ 1.8 (rrandom))))

(defun gain ()
  (db-to-linear (* -5 (random 10))))

(defun gainlist (&aux glist)
  (do ((gain 0 (- gain 5)))
      ((= gain -50) glist)
    (dotimes (i 10)
      (push gain glist))))

(print (gainlist))

(defun shuffle (gain)
  (let ((n (length gain)))
    (do* ((i (1- n) (1- i))
          (j (random (1+ i))(random (1+ i))))
         ((= i 1) gainlist)
      (swap (nth i gain) (nth j gain)))))

(defmacro swap (x y)
  `(let ((temp ,x))
      (setf ,x ,y)
      (setf ,y temp)))

(let ((input (format nil "~a" gainlist)))  ;keep a copy so we can print it
  (format nil "Input:\t ~a~%Output:\t ~a"
              input
              (shuffle gainlist)))

(defun get-next-note (tone)
  (seq (rest)
       (cue (mult (gain) tone))))

(let ((tone (tone)))
  (seqrep (i 200) (get-next-note tone)))


And the list of errors
Code: Select all
error: unbound variable - GAINLIST
if continued: try evaluating symbol again
Function: #<FSubr-LET: #4286980>
Arguments:
  ((INPUT (FORMAT NIL "~a" GAINLIST)))
  (FORMAT NIL "Input:\t ~a~%Output:\t ~a" INPUT (SHUFFLE GAINLIST))
1> GET-NEXT-NOTE
1> #<Sound: #562ec38>
1>


The list of gains is printed. I know that I do here something wrong, but I can't figure out what. I am wondering also if that loop at the end is needed when we created the list of those 100 ampitudes.
PiTeRr
 
Posts: 26
Joined: Thu Jul 27, 2017 6:15 pm
Operating System: Windows 10

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by PiTeRr » Sat Aug 19, 2017 4:30 pm

Hi :) could I get some help ? I'm a little bit helpless with programming. Big thanks in advance :)
PiTeRr
 
Posts: 26
Joined: Thu Jul 27, 2017 6:15 pm
Operating System: Windows 10

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by steve » Sat Aug 19, 2017 6:13 pm

PiTeRr wrote:error: unbound variable - GAINLIST

You have used "GAINLIST" as both a variable and a function name (which is perfectly valid, though can be a bit confusing).

In the statement:
Code: Select all
(print (gainlist))

you are referring to the function. You can see that it is a reference to a function because the name "gainlist" is immediately after a "("


In other places, for example:
Code: Select all
(let ((input (format nil "~a" gainlist)))  ;keep a copy so we can print it

you are referring to a "variable" called "gainlist.

The error message is complaining about your use of a variable called "gainlist" and is saying that the variable is used without being bound to a value (you are attempting to use the variable before you have set it to a value).

So let's look at the places where you have used it:
Code: Select all
(defun shuffle (gain)
  (let ((n (length gain)))
    (do* ((i (1- n) (1- i))
          (j (random (1+ i))(random (1+ i))))
         ((= i 1) gainlist)
      (swap (nth i gain) (nth j gain)))))

Code: Select all
(let ((input (format nil "~a" gainlist)))  ;keep a copy so we can print it
[/code]
Code: Select all
  (format nil "Input:\t ~a~%Output:\t ~a"
              input
              (shuffle gainlist)))


and where do you set the value of "gainlist"?
I don't see that you have set it anywhere.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44692
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by steve » Sat Aug 19, 2017 6:34 pm

Generally I like to define all of my functions before I write the main script, so my plug-ins usually have a structure in the form of:
  1. set global variables (if any)
  2. define functions
  3. run the code

We are not currently using any global variables, but we are defining a lot of functions (and one macro), so let's put those all together first:
Code: Select all
;version 4
;type generate

(defun tone ()
  (mult (osc (hz-to-step 1000) 0.25)
        (pwlv 0 0.02 1 0.23 1 0.25 0)))

(defun rest ()
  (s-rest (+ 1.8 (rrandom))))

(defun gain ()
  (db-to-linear (* -5 (random 10))))

(defun gainlist (&aux glist)
  (do ((gain 0 (- gain 5)))
      ((= gain -50) glist)
    (dotimes (i 10)
      (push gain glist))))

(defun shuffle (gain)
  (let ((n (length gain)))
    (do* ((i (1- n) (1- i))
          (j (random (1+ i))(random (1+ i))))
         ((= i 1) gainlist)
      (swap (nth i gain) (nth j gain)))))

(defmacro swap (x y)
  `(let ((temp ,x))
      (setf ,x ,y)
      (setf ,y temp)))

(defun get-next-note (tone)
  (seq (rest)
       (cue (mult (gain) tone))))


Then we can add the main program that uses those functions.

The question now is, "what do we want the program to do?"

The answer:
  1. Create a list of gain values.
  2. Shuffle that list of gain values
  3. Generate a tone
  4. Create a sequence that repeats the tone, amplifying each occurrence by successive gain values from the shuffled list

For the first three steps, a "LET" structure ("special form") looks like it will be suitable. However, the order in which we do things is important - we must (obviously) create the list of gain values before we can shuffle the list, so we must use "LET*" rather than "LET".
See the difference here:
LET http://www.audacity-forum.de/download/e ... ef-148.htm
LET* http://www.audacity-forum.de/download/e ... ef-149.htm
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44692
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by steve » Sat Aug 19, 2017 6:49 pm

steve wrote:
  1. Create a list of gain values.
  2. Shuffle that list of gain values


So let's try that bit first:

Code: Select all
(defun gainlist (&aux glist)
  (do ((gain 0 (- gain 5)))
      ((= gain -50) glist)
    (dotimes (i 10)
      (push gain glist))))

(defun shuffle (gains)
  (let ((n (length gains)))
    (do* ((i (1- n) (1- i))
          (j (random (1+ i))(random (1+ i))))
         ((= i 1) gains)
      (swap (nth i gains) (nth j gains)))))

(defmacro swap (x y)
  `(let ((temp ,x))
      (setf ,x ,y)
      (setf ,y temp)))

(let* ((gain-list (gainlist)) ;set gain-list to the list returned by the function (gainlist)
       (gain-list (shuffle gain-list))) ;set gain-list to a shuffled version of itself
  ;; have a look to see if this is working
  (print gain-list))


but actually we can simplify that by combining the first two lines of the LET* statement into a single line:
Code: Select all
(defun gainlist (&aux glist)
  (do ((gain 0 (- gain 5)))
      ((= gain -50) glist)
    (dotimes (i 10)
      (push gain glist))))

(defun shuffle (gains)
  (let ((n (length gains)))
    (do* ((i (1- n) (1- i))
          (j (random (1+ i))(random (1+ i))))
         ((= i 1) gains)
      (swap (nth i gains) (nth j gains)))))

(defmacro swap (x y)
  `(let ((temp ,x))
      (setf ,x ,y)
      (setf ,y temp)))
       
(let ((gain-list (shuffle (gainlist))))
  ;; have a look to see if this is working
  (print gain-list))

(Note that we can now use LET rather than LET*)

Are you following this so far? Does it make sense and is it working for you PiTeRr?

Code: Select all
;version 4
;type generate

(defun tone ()
  (mult (osc (hz-to-step 1000) 0.25)
        (pwlv 0 0.02 1 0.23 1 0.25 0)))

(defun gainlist (&aux glist)
  (do ((gain 0 (- gain 5)))
      ((= gain -50) glist)
    (dotimes (i 10)
      (push gain glist))))

(defun shuffle (gains)
  (let ((n (length gains)))
    (do* ((i (1- n) (1- i))
          (j (random (1+ i))(random (1+ i))))
         ((= i 1) gains)
      (swap (nth i gains) (nth j gains)))))

(defmacro swap (x y)
  `(let ((temp ,x))
      (setf ,x ,y)
      (setf ,y temp)))

(defun rest ()
  (s-rest (+ 1.8 (rrandom))))

(defun get-next-note (tone gain)
  (seq (rest)
       (cue (mult (db-to-linear gain) tone))))
       
(let ((gain-list (shuffle (gainlist)))
      (tone (tone)))
  (seqrep (i (length gain-list))
          (get-next-note tone (nth i gain-list))))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44692
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Adding randomized silence (Windows 10, Audacity 2.1.3)

Permanent link to this post Posted by PiTeRr » Sun Aug 20, 2017 3:01 pm

Yes, now I get it much better and I need to study more carefully all of the LISP structures to understand it well :) this code is working perfectly and that is what I meant :) huge thanks :) but my final issue is to create a sequence of 100 tones at frequency of 1000 Hz and 100 tones at frequency of 4000 Hz and all of this should be shuffled. This is going to complicate a code but I am really obligated to do it in this way. I would be tremendously greatful for your further help in that issue :)
PiTeRr
 
Posts: 26
Joined: Thu Jul 27, 2017 6:15 pm
Operating System: Windows 10

PreviousNext

Return to Windows



Who is online

Users browsing this forum: Bing [Bot] and 25 guests