Nyquist Drum Replacer

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Paul2
Posts: 151
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Nyquist Drum Replacer

Post by Paul2 » Tue Jun 08, 2021 7:16 pm

Wow, fantastic info once again.

As regards the "beat.ny" code, makes perfect sense now, yes I agree that original code is a bit confusing.
Once I read your explanation and your code simplification, together with what is written in the page about return values:
When the return value is a character or string, a dialog window will appear with the data displayed as text.
Then further down....
If an appropriately formatted list is returned to Audacity, a label track will be created below the audio track(s).
Bingo!! The penny dropped.

The format is then "constructed" and "pushed" like: ((number "string") (number "string") ... )

I also really like your code for the tempo.
Was going to surprise you by adding BPM, but I arrived at it via another way.
If you look at the table of values I posted earlier:
Screen Shot 2021-06-08 at 9.06.56 PM.png
Screen Shot 2021-06-08 at 9.06.56 PM.png (17.7 KiB) Viewed 96 times
These values are in the "time" variable, so if we take the delta of any two consecutive values, invert
and multiply by 60, we get the BPM.

Taking the first two values:
(sorry, looks a bit convoluted as doing equations on the forum does not format too nicely)

0.744150 - 0.300150 = 0.440
1/0.440 = 2.2727
2.2727 * 60 = 136 BPM

i.e.

1/(t2-t1) * 60

Interesting project, just had my "din-dins" so going to carry on coding.

Paul2
Posts: 151
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Nyquist Drum Replacer

Post by Paul2 » Tue Jun 08, 2021 8:50 pm

Right...creating label tracks is not that difficult after all.
The code below....

Code: Select all

;type analyze
(setf sel-begin (get '*selection* 'start)) ; start offset
(setf sel-end (get '*selection* 'end))  ; end offset
(setf sel-dur (get-duration 1)) ; duration of selection

(setf end-mark (- sel-end sel-begin))
(setf start-mark (- end-mark sel-dur))

(setf labels NIL)

(push (list start-mark "START") labels)
(push (list end-mark "END") labels)
Produces a "START" and "END" label from a track selection.
Screen Shot 2021-06-08 at 11.11.18 PM.png
Screen Shot 2021-06-08 at 11.11.18 PM.png (25.42 KiB) Viewed 90 times
The only tricky part was getting to understand how Nyquist handles time.

steve
Site Admin
Posts: 81229
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Nyquist Drum Replacer

Post by steve » Tue Jun 08, 2021 11:51 pm

Paul2 wrote:
Tue Jun 08, 2021 7:16 pm
These values are in the "time" variable, so if we take the delta of any two consecutive values, invert
and multiply by 60, we get the BPM.
That's a very good way to do it, so long as you detect exactly one drum hit for each beat. It becomes rather complicated if the drum is playing some other rhythm, or if the beat detector misses some beats or adds in extra 'false' beats.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul2
Posts: 151
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Nyquist Drum Replacer

Post by Paul2 » Wed Jun 09, 2021 11:16 am

Yes, if there are false triggers or missing ones, then the BPM will be out.
To overcome this, could do three measurements at different times and take an average.
These measurements could be done at the beginning, middle and towards the end.
The timings will be in a list, which is essentially a formatted array, I'm assuming.

As for the drum sample/beat that will be replacing with, thought about something there as well.
The file option is not great due to the shortcomings of Nyquist.
Also, depending on the BPM of the song, it will have an effect on how long the decay of the drum can be.
So, even if the file option was OK, it would be difficult to visualize the length of the replacement drum sound.
Too long and it will be a mess.

So how about this:

Create a track, on this track put some candidate drum replacement sounds, with some silence between them.
With the original drum track underneath it, it's now easy to compare them.
We can also preview all the individual candidate drum samples and, once one is chosen, we can manipulate it.
Shorten it, adjust tonal qualities, etc.

Once done, we can use that sample as the replacement sound.

Now, the question is, what is involved in selecting that wanted sound, run a Nyquist plugin, it stores it in an array then
into *scratch*.
Similar idea to your PUNCH/ROLL plugin.

Now we run the "main" plugin and the sound gets put in a new audio track at the right times.

Do-able?

steve
Site Admin
Posts: 81229
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Nyquist Drum Replacer

Post by steve » Wed Jun 09, 2021 12:12 pm

Paul2 wrote:
Wed Jun 09, 2021 11:16 am
The file option is not great due to the shortcomings of Nyquist.
What's the problem?
Does this work for you?

Code: Select all

;type generate

;control filename "Select a mono WAV file" file "" "*default*/drum.wav" "WAV file|*.wav;*.WAV" "open,exists"

(defun get-sound (fname)
  (let ((fp (open fname)))
    (cond
      (fp (close fp)
          (s-read filename))
      (t  (format nil "Error.~%~s~%cannot be opened."
             fname)))))

(let ((drum-snd (get-sound filename)))
  (if (arrayp drum-snd)
      (format nil "Error.~%~s~%is stereo."
              filename)
      drum-snd))

See here about s-read: https://www.cs.cmu.edu/~rbd/doc/nyquist ... l#index662
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

steve
Site Admin
Posts: 81229
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Nyquist Drum Replacer

Post by steve » Wed Jun 09, 2021 12:27 pm

A very simple drum machine:

Code: Select all

;type generate
;name "Simple Drum Machine"

;control filename "Select a mono WAV file" file "" "*default*/drum.wav" "WAV file|*.wav;*.WAV" "open,exists"
;control number "Number of beats" int "" 8 1 32
;control tempo "Tempo" int "bpm" 120 30 300

(defun get-sound (fname)
  (let ((fp (open fname)))
    (cond
      (fp (close fp)
          (s-read filename))
      (t  (format nil "Error.~%~s~%cannot be opened."
             fname)))))

(defun do-sequence (d-snd)
  (let ((delay (/ 60.0 tempo)))
    (simrep (i number)
      (at (* i delay) (cue d-snd)))))

(let ((drum-snd (get-sound filename)))
  (if (arrayp drum-snd)
      (format nil "Error.~%~s~%is stereo."
              filename)
      (do-sequence drum-snd)))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

steve
Site Admin
Posts: 81229
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Nyquist Drum Replacer

Post by steve » Wed Jun 09, 2021 1:29 pm

steve wrote:
Wed Jun 09, 2021 12:27 pm
A very simple drum machine:
Some explanation:
steve wrote:
Wed Jun 09, 2021 12:27 pm
;type generate
Generator plug-ins will create a new mono track if there is no selection. It's not currently possible for a normal Nyquist plug-in to create a stereo track.
Generate type plug-ins count time in "seconds" (;type process and ;type analyze stretch time so that "1 unit" of time is the length of the selection).

Code: Select all

(defun get-sound (fname)
  ;;; Returns the audio file 'fname', else an
  ;;; error message if the file is not readable.
  (let ((fp (open fname)))  ;try opening the specified file
    (cond
      ;; If 'fp' exists (is not NIL), the file exists.
      ;; We have opened the file, so close it before doing
      ;; anything else.
      (fp (close fp)
          (s-read filename))  ;Read the audio file with default options.
      ;; 'Else' return an error message.
      (t  (format nil "Error.~%~s~%cannot be opened."
             fname)))))

Code: Select all

;;; Add the sound at specified intervals for the
;;; specified number of times, and return the result.
(defun do-sequence (d-snd)
  (let ((delay (/ 60.0 tempo)))
    (simrep (i number)
      (at (* i delay) (cue d-snd)))))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul2
Posts: 151
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Nyquist Drum Replacer

Post by Paul2 » Wed Jun 09, 2021 2:42 pm

Thank you Steve.
That works nicely.

Considering how many options have been discussed on this thread, perhaps it's best not to make one single plugin, but a few of them.
The general idea is to enable the creation of a simple song using only Audacity.

So one track for bass drum, another for hi-hats, cymbals etc.
Once those have been created, can mix them into a percussion stem (if need be), then move on to the synth track, guitar, vocals, etc.

What I have in mind then is:

- One plugin to detect the drums and just create labels and possibly a BPM count as well.
This is optional and only needed if one wants to add to an existing song, change it or play along with it.

- A variation on the above, but it inserts actual samples instead of just labels.
There is use for both types.

- Another plugin, your simple drum machine to create the percussion tracks, one at a time.
A nice addition is to be able to include a pattern, i.e. cymbals only every third beat, etc.
This can then be used to add different instrument samples.

steve
Site Admin
Posts: 81229
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Nyquist Drum Replacer

Post by steve » Wed Jun 09, 2021 2:49 pm

Paul2 wrote:
Wed Jun 09, 2021 2:42 pm
- A variation on the above, but it inserts actual samples instead of just labels.
I like the idea of being able to record myself tapping a rhythm on a table, and then replace the tap sounds with a drum. It would be perfectly sufficient for each recorded track to be one drum only, and has the advantage that I can then mix the tracks as required. I'm not aware of an existing plug-in to do that.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul2
Posts: 151
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Nyquist Drum Replacer

Post by Paul2 » Wed Jun 09, 2021 3:03 pm

Tapping will work and also, as you know, there is a click track which can be useful in certain situations.
It would be perfectly sufficient for each recorded track to be one drum only
Absolutely, sorry if my explanation was not clear.
What I meant was, using your drum machine to create separate tracks for each instrument.

Post Reply