Creating two label tracks for a given wav file [SOLVED]

Questions, discussion and recipes for scripting and batch processing in Audacity.
See also the "Scripting" section of the Audacity manual.
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
ennoausberlin
Posts: 6
Joined: Thu Apr 15, 2021 9:53 am
Operating System: Linux *buntu

Creating two label tracks for a given wav file [SOLVED]

Post by ennoausberlin » Mon Apr 26, 2021 3:03 pm

Hello,

I have two label tracks as tab separated text files for a given .wav file. I load the wav file using commands over named pipe and additional I can load one of the label tracks by sending addLabel and setLabel: from inside a loop. That works. But if I want to load the second one additionally The first label track is messed up and the second label track has empty text labels and start and end is always the full span.

I guess, there is a problem with label index, but the label index is ascending from 0 and looks good.

This is the relevant code

Code: Select all

def import_label_track(file_name, title):
    global lastLabelIndex
    print("Last index", lastLabelIndex)
    doCommand('NewLabelTrack')
    doCommand("SetTrack: Name=" "\"" + title + "\"")
    with open(file_name, 'r') as label_file:
        lines = label_file.readlines()
        for index, each_line in enumerate(lines):
            fields = each_line.strip().split()
            doCommand("AddLabel")
            # time.sleep(1.5)
            command_string = "SetLabel: Label=" + str(lastLabelIndex) + " Text=\"" + fields[2] + "\"" + " Start=" + fields[0] +  " End=" + fields[1]
            # command_string = "SetLabel: Text=\"" + fields[2] + "\"" + " Start=" + fields[0] + " End=" + fields[1]
            print(command_string)
            doCommand(command_string)
    lastLabelIndex = index
Any ideas, whats wrong? I am using audacity git from 19. April 2021under Ubuntu 20.04

Thank you
Enno
Last edited by steve on Mon Apr 26, 2021 3:11 pm, edited 1 time in total.

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

Re: Creating two label tracks for a given wav file

Post by steve » Mon Apr 26, 2021 3:26 pm

ennoausberlin wrote:
Mon Apr 26, 2021 3:03 pm
But if I want to load the second one additionally The first label track is messed up and the second label track has empty text labels and start and end is always the full span.
The label index is for "all labels", not "labels in current track". So if your first label track has 8 labels (index 0 to 7), then the first label of the second track will be index 8.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

ennoausberlin
Posts: 6
Joined: Thu Apr 15, 2021 9:53 am
Operating System: Linux *buntu

Re: Creating two label tracks for a given wav file

Post by ennoausberlin » Tue Apr 27, 2021 6:04 am

steve wrote:
Mon Apr 26, 2021 3:26 pm
ennoausberlin wrote:
Mon Apr 26, 2021 3:03 pm
But if I want to load the second one additionally The first label track is messed up and the second label track has empty text labels and start and end is always the full span.
The label index is for "all labels", not "labels in current track". So if your first label track has 8 labels (index 0 to 7), then the first label of the second track will be index 8.
Hello Steve

You are right. The snippet I posted was not using the index. It was a leftover from tinkering around. But I tried this slightly changed code before and it does not work either. See the lastLabelIndex+index change. The ouput on the shell looks good. Every command gets an OK, but the tracks are messed up.

Code: Select all

def import_label_track(file_name, title):
    global lastLabelIndex
    print("Last index", lastLabelIndex)
    doCommand('NewLabelTrack')
    doCommand("SetTrack: Name=" "\"" + title + "\"")
    with open(file_name, 'r') as label_file:
        lines = label_file.readlines()
        for index, each_line in enumerate(lines):
            fields = each_line.strip().split()
            doCommand("AddLabel")
            # time.sleep(1.5)
            command_string = "SetLabel: Label=" + str(lastLabelIndex + index) + " Text=\"" + fields[2] + "\"" + " Start=" + fields[0] +  " End=" + fields[1]
            # command_string = "SetLabel: Text=\"" + fields[2] + "\"" + " Start=" + fields[0] + " End=" + fields[1]
            print(command_string)
            doCommand(command_string)
    lastLabelIndex = index + 1
What might be wrong?
Enno
Last edited by steve on Tue Apr 27, 2021 9:41 am, edited 1 time in total.

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

Re: Creating two label tracks for a given wav file

Post by steve » Tue Apr 27, 2021 9:44 am

ennoausberlin wrote:
Tue Apr 27, 2021 6:04 am

Code: Select all

print("Last index", lastLabelIndex)
Does the number printed match the number of labels in the project?

Code: Select all

    lastLabelIndex = index + 1
Is "index" in scope?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

ennoausberlin
Posts: 6
Joined: Thu Apr 15, 2021 9:53 am
Operating System: Linux *buntu

Re: Creating two label tracks for a given wav file

Post by ennoausberlin » Tue Apr 27, 2021 10:08 am

Hello Steve

This is the input file

0 37.171186 1
37.171186 38.058502 0
38.058502 154.308178 1
154.308178 155.664585 0
155.664585 255.32089 1
255.32089 255.886059 0
255.886059 261.7 1

and this is the corresponding output

Last index 0
Send: >>>
NewLabelTrack
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
SetTrack: Name="manual"
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
AddLabel
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
SetLabel: Label=0 Text="1" Start=0 End=37.171186
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
AddLabel
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
SetLabel: Label=1 Text="0" Start=37.171186 End=38.058502
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
AddLabel
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
SetLabel: Label=2 Text="1" Start=38.058502 End=154.308178
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
AddLabel
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
SetLabel: Label=3 Text="0" Start=154.308178 End=155.664585
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
AddLabel
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
SetLabel: Label=4 Text="1" Start=155.664585 End=255.32089
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
AddLabel
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
SetLabel: Label=5 Text="0" Start=255.32089 End=255.886059
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
AddLabel
Rcvd: <<<

BatchCommand finished: OK

Send: >>>
SetLabel: Label=6 Text="1" Start=255.886059 End=261.7
Rcvd: <<<

BatchCommand finished: OK

As you can see, the label index is ascending. I don't know whats wrong
steve wrote:
Tue Apr 27, 2021 9:44 am
ennoausberlin wrote:
Tue Apr 27, 2021 6:04 am

Code: Select all

print("Last index", lastLabelIndex)
Does the number printed match the number of labels in the project?

Code: Select all

    lastLabelIndex = index + 1
Is "index" in scope?

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

Re: Creating two label tracks for a given wav file

Post by steve » Tue Apr 27, 2021 10:24 am

ennoausberlin wrote:
Tue Apr 27, 2021 10:08 am
As you can see, the label index is ascending.
Yes, it's ascending, but it is starting from "Label=0", and that is the first label in the first label track.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

ennoausberlin
Posts: 6
Joined: Thu Apr 15, 2021 9:53 am
Operating System: Linux *buntu

Re: Creating two label tracks for a given wav file

Post by ennoausberlin » Tue Apr 27, 2021 11:05 am

Hello Steve,

Yes, because this is the log from the first label track I add. And even this does not work. If I omit the Label= parameter it works well for a single label track.
Did you try the snippet? The helper methods like doCommand - I did not post - are from pipeclient
steve wrote:
Tue Apr 27, 2021 10:24 am
ennoausberlin wrote:
Tue Apr 27, 2021 10:08 am
As you can see, the label index is ascending.
Yes, it's ascending, but it is starting from "Label=0", and that is the first label in the first label track.

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

Re: Creating two label tracks for a given wav file

Post by steve » Tue Apr 27, 2021 6:14 pm

Code: Select all

NewLabelTrack
SetTrack: Name="manual"
AddLabel
SetLabel: Label=0 Text="1" Start=0 End=37.171186
AddLabel
At this point, the labels are not as I think you expect.

You have two labels, one from 0.0 to 37.17... and the other from 0.0 to 0.0.

Your assumption is that the one that you added first will be index 0, and the one that you added second will be index 1, but that is not the case.
If you run "Extra menu > Scriptables II > GetInfo: Type=Labels" you will see:

Code: Select all

[ 
  [ 0,
    [ 
      [ 0, 0, "" ],
      [ 0, 37.1712, "1" ] ] ] ]
This shows that the "first" label is the one from time=0 to time=0.

What you need to do is:

Code: Select all

NewLabelTrack
SetTrack: Name="manual"
AddLabel
SetLabel: Label=0 Text="1" Start=0 End=37.171186
SelectTime: Start=37.171186 End=38.058502
AddLabel
SetLabel: Label=1 Text="0"
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

ennoausberlin
Posts: 6
Joined: Thu Apr 15, 2021 9:53 am
Operating System: Linux *buntu

Re: Creating two label tracks for a given wav file

Post by ennoausberlin » Tue Apr 27, 2021 8:31 pm

Hello Steve

I will try your suggested solution tomorrow. I hope it works that way, but looks not very intuitive to me.

In the audacity git repo are some named pipe scripts to create screenshots for documentation. One of them is a label script that creates labels like Intro or Chopin. I do not see a call to SelectTime there. What trick is used in this case?

I don’t want to steal your time, but I still don’t get the whole picture. I am confused

Thank you
Enno

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

Re: Creating two label tracks for a given wav file

Post by steve » Tue Apr 27, 2021 8:52 pm

ennoausberlin wrote:
Tue Apr 27, 2021 8:31 pm
In the audacity git repo are some named pipe scripts to create screenshots for documentation. One of them is a label script that creates labels like Intro or Chopin. I do not see a call to SelectTime there. What trick is used in this case?
I'm not sure that all of those scripts work as intended. Some of them are quite old and need updating.

ennoausberlin wrote:
Tue Apr 27, 2021 8:31 pm
I still don’t get the whole picture. I am confused
The label index is based on the start time of the label, not on the order that they were created.

By creating the labels at the required time position (by making a selection and then adding the label), you can deduce the index number of the label.
Alternatively:
If you create each label at time=0 and then so long as no other label has a start time of 0, the new label will be index number 0.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Locked