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 steve » Thu Aug 03, 2017 4:13 pm

PiTeRr wrote: is it possible to incorporate 0 dB amplification into this randomized function of amplification

Sure, you just need to modify
Code: Select all
(* -5 (+ 1 (random 10)))


Looking at the documentation: http://www.cs.cmu.edu/~rbd/doc/nyquist/ ... #index1629
(random n)
[LISP] – compute a random number between 0 and |n|-1 inclusive.
If n is 0, return 0.
n – the upper bound (integer)
returns – a random number

Can you work it out from that?
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 » Thu Aug 03, 2017 4:29 pm

I think I figured it out :)

Code: Select all
(* -5 (+ 0 (random 10))))))


Thanks a lot :)
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 » Thu Aug 03, 2017 4:39 pm

Yes that will give you a maximum value of "0", and the minimum (most negative) is -5 x 9 = -45 (I don't recall, did you want -45 or -50?) but you don't really need to add "0":
(* -5 (random 10))
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 » Thu Aug 03, 2017 4:56 pm

I wanted just -45 dB :) Thanks once again for all of your help :) I really want to make it up to you somehow :)
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 » Thu Aug 03, 2017 5:50 pm

Your welcome.
I think we're done so I'll close this topic now.
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

Particular number of repetitions of randomized tones

Permanent link to this post Posted by PiTeRr » Thu Aug 10, 2017 9:36 am

Audacity 2.1.3
Hi :) I'm deeply asking for help once again in reference to the topic from this post https://forum.audacityteam.org/viewtopi ... 83#p331683 . Is it possible to force in the code that all of this randomized tones (multiples of 5 dB) appear exactly 10 times each ?
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 » Thu Aug 10, 2017 10:11 am

[Topic re-opened and new topic merged]

PiTeRr wrote: Is it possible to force in the code that all of this randomized tones (multiples of 5 dB) appear exactly 10 times each ?

Do you mean, 10 occurrences any -45 dB, 10 at -40 dB ... and so on up to 0 dB, so 100 tones total?
If so, do you mean that there should be 10 sequences of 10 tones, in which each tone is at one of the defined dB levels, or do you mean 100 tones, 10 at each level, jumbled up into one random sequence of 100 tones?

It would probably help if you told us the purpose of this, then I might understand more clearly what you want.
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 » Thu Aug 10, 2017 11:02 am

steve wrote:Do you mean, 10 occurrences any -45 dB, 10 at -40 dB ... and so on up to 0 dB, so 100 tones total?
Exactly :)

steve wrote:do you mean 100 tones, 10 at each level, jumbled up into one random sequence of 100 tones?
That is what I exactly mean :)

steve wrote:It would probably help if you told us the purpose of this, then I might understand more clearly what you want.
Purpose of this is an experiment which I conduct with my academic and we measure reaction time from tones occurences. It's kind of student research group.

And I have one another question :) is it possible to incorporate in code randomized occurence of tones at 2 frequencies in this earlier given terms ?
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 » Thu Aug 10, 2017 12:08 pm

PiTeRr wrote:
steve wrote: do you mean 100 tones, 10 at each level, jumbled up into one random sequence of 100 tones?

That is what I exactly mean :)


OK, so that's not actually a "random sequence", it's a "permutation" of specified values, or in other words it is a given list that is "shuffled".
Rather than generating the tones and then shuffling them (which would involve moving around a lot of audio data), it's easier to just create a list to represent the levels, shuffle that list, then read the amplitudes sequentially from that shuffled list when we generate the audio.


So far we have code that looks something like this:
Code: Select all
;version 4
;type generate

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

(defun rest ()
"Generates between 1.8s and 2.8s silence"
  (s-rest (+ 1.8 (rrandom))))

(defun gain ()
"Random number 0 to -45 in steps of -5"
  (db-to-linear (* -5 (random 10))))

;; Generate a tone and repeat x200 with
;; randomised spacing and gain
(let ((tone (tone)))
  (seqrep (i 200)
    (seq (rest) (cue (mult (gain) tone)))))


Note that in the final line we have: (cue (mult (gain) tone))
We need to replace that with a tone that gets it's amplitude from a shuffled list, so for clarity we can create a new function to get the next tone. In fact, on reflection, it may be easiest to write a function that creates our "silence-tone" pair:

Code: Select all
(defun get-next-note ()
  ;some code here to generate the
  ;silence-tone pair
  )

;; Generate a tone and repeat x200 with
;; randomised spacing and gain
(let ((tone (tone)))
  (seqrep (i 200) (get-next-note)))


So, before we move on, what code do we need to put into the new "get-next-note" function to make it functionally the same as the code at the beginning of this post?
We could try something like this:
Code: Select all
(defun get-next-note ()
  (seq (rest)
       (cue (mult (gain) tone))))

but Nyquist will complain that the variable "tone" is not defined.
We defined the variable "tone" in our "let" statement, but it is currently local to that "let" block, so our "get-next-note" function does not know that it exists.
To solve that, we need to pass the sound "tone" to the "get-next-note" function, and we do that by supplying it as an "argument" of the function.
See here for details - this is a very important feature of LISP functions: http://www.audacity-forum.de/download/e ... ef-087.htm

Here's the rewritten code. Try it out and ensure that you understand how it works, then we can move on to the next part, which is replacing the "rrandom" function with a "shuffle" function. I think we will need to write the "shuffle" function ourselves as I don't think that Nyquist has one built in.

Code: Select all
;version 4
;type generate

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

(defun rest ()
"Generates between 1.8s and 2.8s silence"
  (s-rest (+ 1.8 (rrandom))))

(defun gain ()
"Random number 0 to -45 in steps of -5"
  (db-to-linear (* -5 (random 10))))

(defun get-next-note (tone)
"Generate a 'rest-tone' pair"
  (seq (rest)
       (cue (mult (gain) tone))))

;; Generate a sequence of 200 notes.
(let ((tone (tone)))
  (seqrep (i 200) (get-next-note tone)))

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 » Thu Aug 10, 2017 12:45 pm

Here's a long-winded implementation of the Fisher-Yates shuffle algorithm.
For details about the algorithm, see: https://en.wikipedia.org/wiki/Fisher%E2 ... es_shuffle

Try this out and see how it works. We can tidy this code later and make it a lot cleaner.

Code: Select all
;; Fisher-Yates_shuffle (modern algorithm)
;; From: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
;;
;; -- To shuffle an array a of n elements (indices 0..n-1):
;; for i from 0 to n-2 do
;;      j <- random integer such that i <= j < n
;;      exchange a[i] and a[j]


;Create a list
(setf amplitudes (list 0 1 2 3 4 5 6 7 8 9))
(print amplitudes)  ;print it
(terpri)            ;print empty line (terminate print)

(defun exchange (datalist i j)
"Swap the i and j elements in datalist"
  ;; Print out to debug what we are doing
  (format t "Swapping data[~a]:~a <-> data[~a]:~a~%"
          i (nth i datalist)
          j (nth j datalist))
  ;; Swap them
  (setf temp (nth i datalist))
  (setf (nth i datalist)(nth j datalist))
  (setf (nth j datalist) temp)
  ; print out what we've done
  (print datalist)(terpri))


;;Main program
;; (see Wikipedia for details of this algorithm)

(let ((n (length amplitudes)))
  (dotimes (index (- n 2))
    (setq jmin index)
    (setq jmax n)
    (setq jrange (- jmax jmin))
    (setq j (+ jmin (random jrange)))
    (exchange amplitudes index j)))

(format nil "Output is in debug window.")
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

PreviousNext

Return to Windows



Who is online

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