How to add label List to project?

Audio software developers forum.
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
Post Reply
Rinin
Posts: 10
Joined: Thu Aug 14, 2014 4:26 pm
Operating System: Please select

How to add label List to project?

Post by Rinin » Thu Aug 14, 2014 4:37 pm

I'm currently trying to add some features for audacity. Generally I need to comunicate with another programm and record some sound. It would be nice to add label track and some labels. And I'll be pleased if someone point me out to needed functions. Now I got own wxTimer handler in application and using:

Code: Select all

AudacityProject* pProject = GetActiveProject();
pProject->OnRecord();
And sorry for my skills in english =)

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

Re: How to add label List to project?

Post by steve » Thu Aug 14, 2014 5:03 pm

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

Edgar
Forum Crew
Posts: 2043
Joined: Thu Sep 03, 2009 9:13 pm
Operating System: Windows 10

Re: How to add label List to project?

Post by Edgar » Thu Aug 14, 2014 11:24 pm

I think I understand that you want to have your "another program" create a list of labels (consisting of a string and an insertion point for each label) then have that program communicate with Audacity so that Audacity can actually create a label track (if needed) and add all of these labels at the appropriate insertion point in time.

If that is the case then you will need to come up with a method for the two programs to communicate. There have been some experimental attempts at this in the past; one of those (no longer included in the mainstream Audacity source code) is mod-script-pipe (It was removed sometime after SVN revision 11364).

After you figure out a way to get the two programs to communicate changing Audacity to do what you want should be fairly easy. Take a look at int LabelTrack::AddLabel(double t, double t1, const wxString &title) at or near line number 2514 in file srcLabelTrack.cpp:

Code: Select all

int LabelTrack::AddLabel(double t, double t1, const wxString &title)
{
   LabelStruct *l = new LabelStruct();
   l->t = t;
   l->t1 = t1;
   l->title = title;
   mCurrentCursorPos = title.length();
   mInitialCursorPos = mCurrentCursorPos;

   int len = mLabels.Count();
   int pos = 0;

   while (pos < len && mLabels[pos]->t < t)
      pos++;

   mLabels.Insert(l, pos);

   mSelIndex = pos;
   mDrawCursor = true;

   return pos;
}
(I removed the long comment for brevity).

Rinin
Posts: 10
Joined: Thu Aug 14, 2014 4:26 pm
Operating System: Please select

Re: How to add label List to project?

Post by Rinin » Fri Aug 15, 2014 12:14 am

Edgar wrote:I think I understand that you want to have your "another program" create a list of labels (consisting of a string and an insertion point for each label) then have that program communicate with Audacity so that Audacity can actually create a label track (if needed) and add all of these labels at the appropriate insertion point in time.
It's not exactly what I need, but close. Another programm (lets call it Alice) comunicate with audacity and sending a signal to start record. After that Alice keep checking hardware system state and generate labels on the fly. Somthing like "In this point super strong EMP generating equipment was turned on, recorded signal should be corrupted" Thats why I also need to get cureent timestamp somehow.
Edgar wrote:If that is the case then you will need to come up with a method for the two programs to communicate. There have been some experimental attempts at this in the past; one of those (no longer included in the mainstream Audacity source code) is mod-script-pipe (It was removed sometime after SVN revision 11364).
Communication shouldn't be a problem because it's limited and independent from audacity at all. So it'll be only my own code, and main problem for me is "reading" big new project like audacity
Edgar wrote:After you figure out a way to get the two programs to communicate changing Audacity to do what you want should be fairly easy. Take a look at int LabelTrack::AddLabel(double t, double t1, const wxString &title) at or near line number 2514 in file srcLabelTrack.cpp:
Thanks for pointing this out, as I understand I need firstly add LabelTrack to AudacityProject and then call AddLabe for this LabelTrack instance, calling function - not a problem, creating LabelTrack instance also shouldn't be a problem, but I still can't found how to "connect" this new LabelTrack to AudacityProject

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

Re: How to add label List to project?

Post by steve » Fri Aug 15, 2014 12:59 am

Edgar wrote:There have been some experimental attempts at this in the past; one of those (no longer included in the mainstream Audacity source code) is mod-script-pipe (It was removed sometime after SVN revision 11364).
It's still in SVN (in "/lib-src").
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Edgar
Forum Crew
Posts: 2043
Joined: Thu Sep 03, 2009 9:13 pm
Operating System: Windows 10

Re: How to add label List to project?

Post by Edgar » Fri Aug 15, 2014 3:56 am

steve wrote:
Edgar wrote:There have been some experimental attempts at this in the past; one of those (no longer included in the mainstream Audacity source code) is mod-script-pipe (It was removed sometime after SVN revision 11364).
It's still in SVN (in "/lib-src").
I see, however, it is no longer linked to the Windows project.

Edgar
Forum Crew
Posts: 2043
Joined: Thu Sep 03, 2009 9:13 pm
Operating System: Windows 10

Re: How to add label List to project?

Post by Edgar » Fri Aug 15, 2014 4:12 am

Rinin wrote: I still can't found how to "connect" this new LabelTrack to AudacityProject
There are couple of places in the code where label tracks are created:

Code: Select all

Find all "LabelTrack(", Whole word, Subfolders, Find Results 2, "Entire Solution", "*.cpp"
  D:AudacitySVNsrcLabelDialog.cpp(317):      LabelTrack *newTrack = new LabelTrack(mDirManager);
  D:AudacitySVNsrcLabelDialog.cpp(539):         LabelTrack *lt = new LabelTrack(mDirManager);
  D:AudacitySVNsrcLabelDialog.cpp(609):   LabelTrack *lt = new LabelTrack(mDirManager);
  D:AudacitySVNsrcLabelTrack.cpp(83):   return new LabelTrack(mDirManager);
  D:AudacitySVNsrcLabelTrack.cpp(2295):   *dest = new LabelTrack(GetDirManager());
  D:AudacitySVNsrcMenus.cpp(3686):         t = new LabelTrack(mDirManager);
  D:AudacitySVNsrcMenus.cpp(4682):      LabelTrack *newTrack = new LabelTrack(mDirManager);
  D:AudacitySVNsrcMenus.cpp(5457):   LabelTrack *t = new LabelTrack(mDirManager);
  D:AudacitySVNsrcMenus.cpp(5552):      lt = new LabelTrack(mDirManager);
  Matching lines: 12    Matching files: 3    Total files searched: 348
I think that the ones in Menus.cpp might be the most interesting for you to look at especially the one at line 4656:

Code: Select all

void AudacityProject::OnImportLabels()
{
   […]
      LabelTrack *newTrack = new LabelTrack(mDirManager);
mDirManager is a member of a AudacityProject:

Code: Select all

class AUDACITY_DLL_API AudacityProject:  […]
{
 […]
   DirManager *mDirManager;

so using your project variable:

Code: Select all

 
LabelTrack *newTrack = new LabelTrack(pProject->mDirManager);

Rinin
Posts: 10
Joined: Thu Aug 14, 2014 4:26 pm
Operating System: Please select

Re: How to add label List to project?

Post by Rinin » Fri Aug 15, 2014 10:28 am

Edgar wrote:

Code: Select all

LabelTrack *newTrack = new LabelTrack(pProject->mDirManager);
Edgar, thanks a lot for this example, it's exactly what I need. I'll be pleased if you could help me with obtaining of current time of record, but it's not a big problem anyway, since I could count time in another programm (Alice).

Rinin
Posts: 10
Joined: Thu Aug 14, 2014 4:26 pm
Operating System: Please select

Re: How to add label List to project?

Post by Rinin » Fri Aug 15, 2014 11:04 am

And my final code is

Code: Select all

		
AudacityProject* pProject;
LabelTrack* Labels_track;
TrackList* All_Tracks;

pProject = GetActiveProject();
Labels_track = new LabelTrack(pProject->GetDirManager());
Labels_track->AddLabel(0,15,L"Hello world");

All_Tracks = pProject->GetTracks();		
All_Tracks->Add(Labels_track);
I hope it could help someone.

Edgar
Forum Crew
Posts: 2043
Joined: Thu Sep 03, 2009 9:13 pm
Operating System: Windows 10

Re: How to add label List to project?

Post by Edgar » Fri Aug 15, 2014 4:34 pm

Just FYI, One of the Audacity Developers has written a comment in the code that

Code: Select all

GetActiveProject();
might not be 100% reliable and might return NULL even when a project exists (sorry, I don't have time to search for the specific citation). GetActiveProject is used all throughout the Audacity source code and, to the best of my knowledge, no other developer has experienced a problem with it. Nevertheless, if this is mission-critical code it might be a smart idea to wrap it in a test for a NULL return value:

Code: Select all

      
AudacityProject* pProject;
LabelTrack* Labels_track;
TrackList* All_Tracks;

pProject = GetActiveProject();
if (pProject) {
   Labels_track = new LabelTrack(pProject->GetDirManager());
   Labels_track->AddLabel(0,15,L"Hello world");

   All_Tracks = pProject->GetTracks();      
   All_Tracks->Add(Labels_track);
}
else [SOMETHING BAD HAPPENED – fail gracefully]

Post Reply