Wanted: a plugin to find synchronous zero crossings

Help for Audacity 2.x.x on Windows.
Forum rules
ImageThis forum is for Audacity 2.x.x on Windows.
Please state which version of Windows you are using,
and the exact three-section version number of Audacity from "Help menu > About Audacity".


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.
Ilya Kuligin
Posts: 7
Joined: Sun Jan 10, 2021 5:23 pm
Operating System: Windows 8 or 8.1

Wanted: a plugin to find synchronous zero crossings

Post by Ilya Kuligin » Sun Jan 10, 2021 6:03 pm

Hi
Need a plugin to highlight where are both stereo waveforms at very zero right at the same time. It's possible to find manually even in small selections but too long.

Select > Region > Ends to Zero Crossings function is inaccurate for me as it just suggests points with least clicks.

I don't want to use normalize/repair/fading effects as mentioned here , because they all change the waveform.

Versions: Audacity 2.4.2, Windows 8 x64

DVDdoug
Forum Crew
Posts: 8906
Joined: Fri Sep 10, 2010 11:30 pm
Operating System: Windows 10

Re: Wanted: a plugin to find synchronous zero crossings

Post by DVDdoug » Sun Jan 10, 2021 6:14 pm

I don't think that exists but it should be possible with Nyquist.

Note that exact-zero samples are rare so you'd be looking for places where a positive sample is followed by a negative sample or vice-versa.

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

Re: Wanted: a plugin to find synchronous zero crossings

Post by steve » Sun Jan 10, 2021 7:15 pm

Ilya Kuligin wrote:
Sun Jan 10, 2021 6:03 pm
Need a plugin to highlight where are both stereo waveforms at very zero right at the same time
What should the plug-in do if left and right do not have a zero crossing at the same time?

Example:

First Track000.png
First Track000.png (15.45 KiB) Viewed 192 times
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Wanted: a plugin to find synchronous zero crossings

Post by steve » Sun Jan 10, 2021 8:20 pm

This code, when run in the Nyquist Prompt (https://manual.audacityteam.org/man/nyquist_prompt.html) will create a label at the first zero crossing within the selected audio. If there are no zero crossings common to both channels, an error message is shown.

Note that this code can only be used on stereo tracks. On a mono track it will silently fail.
Note that if you select a long track and there are no zero crossings common to both tracks, it will be very slow - I'd strongly recommend that you only use it on short selections.

If you want to convert this into an installable plug-in, see: http://wiki.audacityteam.org/wiki/Nyqui ... _Reference

Code: Select all

(defun find-zero (sig)
  (let ((sign0  (plusp (snd-fetch (aref sig 0)))) ; left
        (sign1  (plusp (snd-fetch (aref sig 1)))) ; right
        newsign0
        newsign1
        zx0 ; zero crossing flag (left)
        zx1 ; zero crossing flag (right)
        (count 0.5))
    (do ((val0 (snd-fetch (aref sig 0)) (snd-fetch (aref sig 0)))
         (val1 (snd-fetch (aref sig 1)) (snd-fetch (aref sig 1))))
        ((not val0) nil)
      (setf newsign0 (plusp val0))
      (setf newsign1 (plusp val1))
      (setf zx0 (not (or (and (not sign0) (not newsign0))
                         (and sign0 newsign0))))
      (setf zx1 (not (or (and (not sign1) (not newsign1))
                         (and sign1 newsign1))))
      (cond
        ((and zx0  zx1)
          (return-from find-zero count))
        (t
          (setf sign0 newsign0)
          (setf sign1 newsign1)
          (incf count))))))


(setf z (find-zero *track*))
(if z
    (list (list (/ z *sound-srate*) "Z"))
    "Zero crossing not found")

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

Ilya Kuligin
Posts: 7
Joined: Sun Jan 10, 2021 5:23 pm
Operating System: Windows 8 or 8.1

Re: Wanted: a plugin to find synchronous zero crossings

Post by Ilya Kuligin » Mon Jan 11, 2021 12:44 pm

steve wrote:
Sun Jan 10, 2021 8:20 pm
This code, when run in the Nyquist Prompt (https://manual.audacityteam.org/man/nyquist_prompt.html) will create a label at the first zero crossing within the selected audio.
Thank you, but since an attack part of a sound is more short and visible than a release part, I always split waveforms just before the next sound to prevent ripping a note apart :lol: So it's more useful to detect crossing just before the split line, therefore the last possible in selection. What needs to be changed in your code?
Note that if you select a long track and there are no zero crossings common to both tracks, it will be very slow - I'd strongly recommend that you only use it on short selections.
I usually deal with a single bar of music per crossing and less. I guess it's not long.

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

Re: Wanted: a plugin to find synchronous zero crossings

Post by steve » Mon Jan 11, 2021 12:56 pm

Ilya Kuligin wrote:
Mon Jan 11, 2021 12:44 pm
Thank you, but since an attack part of a sound is more short and visible than a release part, I always split waveforms just before the next sound to prevent ripping a note apart :lol: So it's more useful to detect crossing just before the split line, therefore the last possible in selection.
If I were charging for this job, the price would now have doubled :lol:
Ilya Kuligin wrote:
Mon Jan 11, 2021 12:44 pm
What needs to be changed in your code?
Instead of "(return-from find-zero count)" when a zero crossing is found, it needs to set a "result" variable to the current "count". The loop will then process all samples in the selection (which will be slower because it can't terminate early). When all samples have been processed, return "result" or "nil".
(The boolean logic will probably also require some modification as we don't want to carry the flags beyond the current sample)

Do you want to try modifying it yourself?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Ilya Kuligin
Posts: 7
Joined: Sun Jan 10, 2021 5:23 pm
Operating System: Windows 8 or 8.1

Re: Wanted: a plugin to find synchronous zero crossings

Post by Ilya Kuligin » Mon Jan 11, 2021 4:10 pm

steve wrote:
Mon Jan 11, 2021 12:56 pm
Instead of "(return-from find-zero count)" when a zero crossing is found, it needs to set a "result" variable to the current "count". The loop will then process all samples in the selection (which will be slower because it can't terminate early). When all samples have been processed, return "result" or "nil".
(The boolean logic will probably also require some modification as we don't want to carry the flags beyond the current sample)

Do you want to try modifying it yourself?
I do, but I can't. Since I'm not a programmer, just understanding of what you wrote is harder than doing things as before. I wonder how nobody else needs it 20 years later.

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

Re: Wanted: a plugin to find synchronous zero crossings

Post by steve » Mon Jan 11, 2021 4:37 pm

Ilya Kuligin wrote:
Mon Jan 11, 2021 4:10 pm
I do, but I can't. Since I'm not a programmer
I wasn't a programmer either, but I needed modified versions of some plug-ins, so I used the documentation and had a go. I was fortunate to have help when I needed it (that was on a mailing list, as this forum had only just started). As with anything, the more you do, the more you learn ;)

Try this version. I've also added some comments to the code that explain some of the less obvious parts:
(a "comment" is text that is ignored by Nyquist and is for the benefit of anyone reading the code. Comments begin with a semicolon ";")

If you have any questions about this code, feel free to ask.

Code: Select all

(defun find-zero (sig)
  (let ((prev0  (plusp (snd-fetch (aref sig 0)))) ; is left positive
        (prev1  (plusp (snd-fetch (aref sig 1)))) ; is right positive
        new0  ;new sample (left)
        new1  ;new sample (right)
        rslt
        zx0 ; zero crossing flag (left)
        zx1); zero crossing flag (right) 
    (do ((val0 (snd-fetch (aref sig 0)) (snd-fetch (aref sig 0)))
         (val1 (snd-fetch (aref sig 1)) (snd-fetch (aref sig 1)))
         (count 0.5 (1+ count)))  ; rslt flag will be placed between previous and current samples
        ((not val0) rslt)
      (setf new0 (plusp val0))  ; is left still positive
      (setf new1 (plusp val1))  ; is right still positive
      ;; Previous and current sign both positive, or both "not positive".
      ;; When both are the same sign, we have not crossed zero.
      (setf zx0 (or (and prev0 new0)
                    (and (not prev0) (not new0))))
      (setf zx1 (or (and prev1 new1)
                    (and (not prev1) (not new1))))
      (when (and (not zx0) (not zx1))  ; left and right have crossed zero
        (setf rslt count))
      ;; Update previous sample flags
      (setf prev0 new0)
      (setf prev1 new1))))


(setf z (find-zero *track*))
(if z
    (list (list (/ z *sound-srate*) "Z"))
    "Zero crossing not found")

Ilya Kuligin wrote:
Mon Jan 11, 2021 4:10 pm
I wonder how nobody else needs it 20 years later.
I can only speak for myself:
When I'm doing a lot of detailed editing, I'm usually working with mono tracks, so this is a non-issue.
When working with stereo tracks, I'm usually only making a few edits, so it's no great difficulty to simply zoom in close (Ctrl + Mouse wheel) and select an edit point manually.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Ilya Kuligin
Posts: 7
Joined: Sun Jan 10, 2021 5:23 pm
Operating System: Windows 8 or 8.1

Re: Wanted: a plugin to find synchronous zero crossings

Post by Ilya Kuligin » Wed Jan 13, 2021 9:11 pm

steve wrote:
Mon Jan 11, 2021 4:37 pm

Try this version. I've also added some comments to the code that explain some of the less obvious parts:
(a "comment" is text that is ignored by Nyquist and is for the benefit of anyone reading the code. Comments begin with a semicolon ";")
Ok, thank you again. This code is ready to be converted, right? If it doesn't work properly for free, I'll return where I came from. It's faster than reading manuals, and I don't really edit a lot of songs in Audacity. Once I learned the very basics of php and javascript though. I have no great desire to walk this path again, because coding isn't my vocation, the music is.
steve wrote:
Mon Jan 11, 2021 4:37 pm
I can only speak for myself:
When I'm doing a lot of detailed editing, I'm usually working with mono tracks, so this is a non-issue.
When working with stereo tracks, I'm usually only making a few edits, so it's no great difficulty to simply zoom in close (Ctrl + Mouse wheel) and select an edit point manually.
The reason I can't work with mono is improving others' stereo tracks by soft-remixing them according to my feeling of beauty. Or making instrumental versions using original recordings of that songs, or something else.

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

Re: Wanted: a plugin to find synchronous zero crossings

Post by steve » Wed Jan 13, 2021 9:26 pm

Ilya Kuligin wrote:
Wed Jan 13, 2021 9:11 pm
This code is ready to be converted, right?
Does it do what you want it to do?

Ilya Kuligin wrote:
Wed Jan 13, 2021 9:11 pm
coding isn't my vocation, the music is.
Same here ;)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply