Label data to text file

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
Post Reply
steve
Site Admin
Posts: 80679
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Label data to text file

Post by steve » Mon Jun 11, 2012 5:05 pm

Following on from a request here.

This topic is about how to modify a plug-in that outputs labels, so that it also outputs a text file.

Writing to a text file can be quite problematic for it to work reliably, with error checking, with cross platform support, so we will take a short cut and limit the export folder to one "safe" location - the user's "home" folder.

To test small parts of code we can use the "Nyquist Prompt" effect.

For making and testing the plug-in, we will have the plug-in open in a text editor (such as Notepad++ on Windows) while we work. Changes can then be made and tested instantly in Audacity.

The example plug-in in this forum thread will be based on the SoundFinder plug-in.
For the sake of being able to run this plug-in in a Chain, the "type" of plug-in will need to be change from an Analyze plug-in to a "Process" plug-in,
Analyze plug-ins appear in the Audacity Analyze menu.
Process plug-ins appear in the Audacity Effect menu.

From Audacity 2.0.1 (currently in alpha, but due for release soon) Nyquist "process" plug-ins may be used in Chains http://manual.audacityteam.org/manual/h ... ssing.html


Making Sound Finder into a "process" type effect.
Make a copy of SoundFinder.ny and rename it SoundFinderText.ny
Save the file in the Audacity Plug-ins folder.

Open SoundFinderText.ny in a text editor and change line 3 from

Code: Select all

;type analyze
to:

Code: Select all

;type process
Change line 5 from:

Code: Select all

;name "Sound Finder..."
to:

Code: Select all

;name "Sound Finder to Text File..."
Restart Audacity and there should be a new effect (below the line) in the Effect menu called "Sound Finder to Text File..."
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Label data to text file

Post by steve » Mon Jun 11, 2012 5:32 pm

The "home" folder (Home directory).
This is the root of a users "user space". The exact location depends on the operating system, but in Nyquist it can be found using the following code (run in the Nyquist Prompt
effect)

Code: Select all

;;; home directory
(defun home ()
  (or (get-env "HOME")            ; Mac / Linux
      (get-env "UserProfile")))   ; Windows

(print (home))
To run the Nyquist Prompt effect, part of an audio track must be selected.

Explanation:
Line 1: This is a comment. Any line beginning with one or more semicolons is ignored by Nyquist - it is treated as a "comment".

Line 2: We are defining a function. The name of the function is "home"
Parameters (arguments) can be passed to a function. If we want to pass arguments to a function we would list the names of those parameters in brackets after the name of the function. In this case we are not passing any arguments, so the brackets are empty.

Lines 3 and 4: These lines read environmental variables from the operating system.
Linux and Mac have an environmental variable called "HOME".
Windows has an environmental variable called "UserProfile".

Lines 1 to 4: The function will look for an environmental variable called "HOME" and if that does not exist, for an environmental variable called "UserProfile". The value of the environmental variable is the path to the users "home" directory.

Line 6 calls the function (home) and prints the value.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Label data to text file

Post by steve » Mon Jun 11, 2012 6:00 pm

Writing a file to the home folder. (part 1)

First we create a fully qualified file name for the output file which will be written in the "home" folder.

Code: Select all

;;; home directory
(defun home ()
  (or (get-env "HOME")            ; Mac / Linux
      (get-env "UserProfile")))   ; Windows

; file separator - the right sort of "slash" as a string.
(setq slash (format nil "~a" *file-separator*)) 

; remove the file separator from the end of the path
; if present - works around some Windows weirdness.
(setq path (string-right-trim slash (home)))

(setq name "output.txt") ; a file name

; put the path and file name together into 
; one string and return to Audacity. 
(format nil "~a~a~a" path slash name)
Try this in the Nyquist Prompt - what output do you get?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

eddieRay
Posts: 118
Joined: Mon Jun 11, 2012 1:08 pm
Operating System: Please select

Re: Label data to text file

Post by eddieRay » Mon Jun 11, 2012 6:39 pm

I will be able to try this in about an hour, I will keep you posted. Thank you so much for the help

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

Re: Label data to text file

Post by steve » Mon Jun 11, 2012 6:43 pm

Looking at the soundfinder.ny code, it's a bit of a mess, so I've just tidied it up a bit so that we can see what we're doing.

I've changed the name of the variable "l" to "labels-list".
It's not generally a good idea to use single letters as variable names, particularly not a letter that can be easily mistaken for some other character. Much better to use a descriptive name.

I've also changed the "type" to "process" so that we can use it in a Chain.
The name of the effect is "Sound Finder to Text File..."

In order to add a file name, we can use a Text Input Widget.
As it says in that document, the Text Input Widget was introduced with "version 2" plug-ins, so we need to also change the plug-in version number to "version 2" (line 2).
The new code for adding a control for entering a file name is on line 13:

Code: Select all

;control name "File name" string "" "Output.txt"
Note that the "initial-string" is set to "Output.txt", so that is our default output file name.


We will also need the function for finding the home directory, so I've added that in at lines 34 to 37:

Code: Select all

;;; home directory
(defun home ()
  (or (get-env "HOME")            ; Mac / Linux
      (get-env "UserProfile")))   ; Windows
Download this file and replace your current SoundFinderText.ny file with it.
SoundFinderText.ny
(5.56 KiB) Downloaded 175 times
You should notice that the "Sound Finder to Text File..." effect now has a text input field for the file name (and much of the excess verbosity removed from the interface).
Other than that it should behave exactly the same as the usual SoundFinder effect.
Check that it does.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

eddieRay
Posts: 118
Joined: Mon Jun 11, 2012 1:08 pm
Operating System: Please select

Re: Label data to text file

Post by eddieRay » Mon Jun 11, 2012 8:01 pm

So far so good, it seems to be working for me, although the Nyquist Prompt Comes up with a blank screen and just the cursor blinking and an OK button to press. Where is Output.txt written to?
Last edited by eddieRay on Mon Jun 11, 2012 8:33 pm, edited 1 time in total.

eddieRay
Posts: 118
Joined: Mon Jun 11, 2012 1:08 pm
Operating System: Please select

Re: Label data to text file

Post by eddieRay » Mon Jun 11, 2012 8:29 pm

And also, once we have all of the times is it possible to find an average time of the even numbered labels as well as an average time of the odd numbered labels?

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

Re: Label data to text file

Post by steve » Mon Jun 11, 2012 8:59 pm

eddieRay wrote:Where is Output.txt written to?
So far there isn't a text file. That's what we are going to add.
Is the effect creating a label track and marking the sounds like sound finder does?
Have you used Sound Finder before?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Label data to text file

Post by steve » Mon Jun 11, 2012 9:12 pm

Writing a text file (part 2)

The code in Writing a file to the home folder. (part 1) should have produced a valid, fully qualified file name (full path and file name).
Did it? What did it produce?

This must be working before we can add to it.

We can then use the (open) function to open a file for writing.
http://www.audacity-forum.de/download/e ... ef-190.htm

Add this code after the code from part 1.

Code: Select all

(setq outputfile (format nil "~a~a~a" path slash name))
(setq fp (open outputfile :direction :output))
(format fp "Hello World")
(close fp)
(format nil "The output should be here:~%~a" outputfile)
What do you get?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Label data to text file

Post by steve » Mon Jun 11, 2012 9:20 pm

eddieRay wrote:And also, once we have all of the times is it possible to find an average time of the even numbered labels as well as an average time of the odd numbered labels?
It's possible to do just about anything, but first things first.
Let's have a look at the data that we currently have.

Have a read of this bit about "Return Values" http://wiki.audacityteam.org/wiki/Nyqui ... urn_Values
Q. What data do we expect from the variable "labels-list"?

Try changing the last line of SoundFinderText.ny from:

Code: Select all

labels-list
to

Code: Select all

(format t "~a" labels-list)
then run the plug-in but use the Debug button rather than the OK button.
What do you get?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply