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

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

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

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.

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

Does the number printed match the number of labels in the project?

lastLabelIndex = index + 1
>

Is "index" in scope?

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

Yes, it’s ascending, but it is starting from “Label=0”, and that is the first label in the first label track.

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

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:

[ 
  [ 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:

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"

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

I’m not sure that all of those scripts work as intended. Some of them are quite old and need updating.


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.

Hi

I am sorry for being back late. Something new got into my way.
Your explanation was fine and I could solve my problem.
I just realized I did not say Thank you.

So. Thank you

Have a nice day
Enno

You’re welcome, and thanks for the update.
I’ll close this topic now as “solved”.

Feel free to start a new topic if you need help with anything else.