Exporting Selection Info
Forum rules
Audacity 1.2.x is now obsolete. Please use the current Audacity 2.1.x version.
The final version of Audacity for Windows 98/ME is the legacy 2.0.0 version.
Audacity 1.2.x is now obsolete. Please use the current Audacity 2.1.x version.
The final version of Audacity for Windows 98/ME is the legacy 2.0.0 version.
Exporting Selection Info
Does anyone know how to export the absolute time of a selection? The following Nyquist code will export the duration to a text file, but I can't figure out how to also export the absolute times of the start and end of the selection.
(setq fp (open "test.txt" :direction :output))
(setf duration (get-duration 1))
(get duration)
(print duration fp)
(close fp)
Also, does anyone know how to write a code so that the duration gets appended to a text file that already exists instead of writing over it?
Thanks!
(setq fp (open "test.txt" :direction :output))
(setf duration (get-duration 1))
(get duration)
(print duration fp)
(close fp)
Also, does anyone know how to write a code so that the duration gets appended to a text file that already exists instead of writing over it?
Thanks!
Re: Exporting Selection Info
From the Nyquist manual:buzzbuzz wrote:Does anyone know how to export the absolute time of a selection?
(snd-t0 sound)
Returns the time of the first sample of the sound.
I think that you have to read the text file back in, append it, then write it back. (at least, that's how I did it).buzzbuzz wrote:Also, does anyone know how to write a code so that the duration gets appended to a text file that already exists instead of writing over it?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
Re: Exporting Selection Info
Thanks for your suggestion, but I'm still having problems - sorry I'm new to this.
I tried the following Nyquist code to get Audacity to output the time of the first sample of my selection to a text file called "test.txt"
(setq fp (open "test.txt" :direction :output))
(setf time (snd-t0 s))
(print time fp)
(close fp)
My understanding is that "s" refers to the sound of the current selection. Thus, (snt-t0 s) will return the start time of the current selection, but maybe that's wrong or I'm using this command incorrectly.
I tried the following Nyquist code to get Audacity to output the time of the first sample of my selection to a text file called "test.txt"
(setq fp (open "test.txt" :direction :output))
(setf time (snd-t0 s))
(print time fp)
(close fp)
My understanding is that "s" refers to the sound of the current selection. Thus, (snt-t0 s) will return the start time of the current selection, but maybe that's wrong or I'm using this command incorrectly.
Re: Exporting Selection Info
Those "shocking" little faces make me laugh 
If you use the "Code" button (above the message edit area) your code will display correctly like this:
Yes you're correct - "s" is the selected audio that Audacity passes to Nyquist as "sound".
I've just tried it myself and I get the output 0 (zero) - I presume that is what you got?
I'm new to Nyquist as well, but thinking about it, "s" is the sound that is passed to Nyquist, so Nyquist will use that as it's reference. As far as Nyquist is concerned time=0 is at the beginning of the sound that is sent to it (s). It may not be possible for Nyquist to know what the absolute start time of the selection is (relative to Audacity's time line).
Why are you trying to get this value anyway?
BTW, if you are interested in Nyquist (which clearly you are), are you aware of the audacity-nyquist mailing list?
You can sign up to it here: https://lists.sourceforge.net/lists/lis ... ty-nyquist
If you use the "Code" button (above the message edit area) your code will display correctly like this:
Code: Select all
(setq fp (open "test.txt" :direction :output))
(setf time (snd-t0 s))
(print time fp)
(close fp)
I've just tried it myself and I get the output 0 (zero) - I presume that is what you got?
I'm new to Nyquist as well, but thinking about it, "s" is the sound that is passed to Nyquist, so Nyquist will use that as it's reference. As far as Nyquist is concerned time=0 is at the beginning of the sound that is sent to it (s). It may not be possible for Nyquist to know what the absolute start time of the selection is (relative to Audacity's time line).
Why are you trying to get this value anyway?
BTW, if you are interested in Nyquist (which clearly you are), are you aware of the audacity-nyquist mailing list?
You can sign up to it here: https://lists.sourceforge.net/lists/lis ... ty-nyquist
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
Re: Exporting Selection Info
When I ran your code, I didn't get an output of 0. The program gave me nothing - not NIL and not an error message. Just a blank text document. However, it should give zero like you said, because I agree (snd-t0 s) is just relative time. Seems like there should be some way to get the absolute time from the audacity time line since it displays it at the bottom.
The reason I want record when certain acoustic events occur is that I was hoping to use Audacity for a research project. I'm sure I can find a professional programs that can do this, but I thought it would've been simple to just write a code in Audacity to do this. It's funny that Audacity taunts me by displaying the information that I want but doesn't let me access it.
I also tried reading my text file like you said, but it doesn't seem to read the numbers in the file. Instead, I get an error #<File-Stream: #27fcbd0>. Not sure if that's because Audacity can't read a text file or if the path is specified wrong. Hmm...
Thanks for the link to the mailing list. Also, thanks for the tip on how to post code
The reason I want record when certain acoustic events occur is that I was hoping to use Audacity for a research project. I'm sure I can find a professional programs that can do this, but I thought it would've been simple to just write a code in Audacity to do this. It's funny that Audacity taunts me by displaying the information that I want but doesn't let me access it.
I also tried reading my text file like you said, but it doesn't seem to read the numbers in the file. Instead, I get an error #<File-Stream: #27fcbd0>. Not sure if that's because Audacity can't read a text file or if the path is specified wrong. Hmm...
Thanks for the link to the mailing list. Also, thanks for the tip on how to post code
Re: Exporting Selection Info
It wasn't MY code, I just copied it from your previous messagebuzzbuzz wrote:When I ran your code, I didn't get an output of 0. The program gave me nothing - not NIL and not an error message. Just a blank text document.
Strange that you got a different result (or rather "no" result). I presume that Nyquist in Audacity must have been updated - I'm using Audacity 1.3.5
If that's the case, then I'd suggest that you install Audacity 1.3.5. v.1.2.x will be obsolete soon as the new stable version (1.4.0) is due out soon. Audacity 1.4 will be based on 1.3 beta, so I expect it will share most of the additional features of 1.3.x
You can have Audacity 1.2 and 1.3 on the same machine, so no need to uninstall 1.2. The only thing to be aware of though, is that Audacity 1.2 projects can be opened in 1.3, but 1.3 projects cannot be opened in 1.2
Is there some reason that you cannot process the entire track? That would resolve the problem and give you times that match the Audacity time line.buzzbuzz wrote:The reason I want record when certain acoustic events occur is that I was hoping to use Audacity for a research project.
You may find it helpful to look at the code for the "Silence Finder" plug-in
http://easyspacepro.com/audacity/plugin ... eMarker.ny
It is certainly possible to read in text from a file - I've done it.buzzbuzz wrote:I also tried reading my text file like you said, but it doesn't seem to read the numbers in the file.
This is the code snippet that I used:
Code: Select all
(setq fp (open "test.dat" :direction :input))
(read fp)Code: Select all
(setq fp (open "test.txt" :direction :output))Note that Nyquist is really a separate program from Audacity. It is included (or at least a trimmed down version of Nyquist) with Audacity, and Audacity is able to pass some data to it such as the selected sound and parameters entered through the plug-in dialogue. The output from Nyquist can then be returned to Audacity if it is data that makes sense to Audacity, such as a sound or labels.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
Re: Exporting Selection Info
In Audacity 1.3.5buzzbuzz wrote:When I ran your code, I didn't get an output of 0. The program gave me nothing - not NIL and not an error message. Just a blank text document.
If you select some audio, then from the Effects menu choose "Nyquist prompt"
Enter
Code: Select all
(setf time (snd-t0 s))Nyquist will return the value 0.00000
It has been suggested to me that for developing complex Nyquist effects, it is better to use the standalone version of Nyquist, then adapt it for Audacity. The debugging of Nyquist in Audacity is rather limited and returns cryptic messages that tell you little more than that something is wrong.
The disadvantage of developing for Audacity in the stand alone version is that Audacity currently uses an old version of Nyquist, and it is a cut down version, so some features are not available.
For your project, the standalone version of Nyquist might be better than using Audacity at all (but I've not used the standalone version myself, so I can't say how easy to use it is).
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
Re: Exporting Selection Info
Aha! My problem is mostly solved.
First, I figured out that (snd-t0 s) works on mono but not stereo audio signals. To use stereo, you first have to split the channels into left and right. So now I get a result of 0 as expected. I still would like to get the absolute time from Audacity. I'll let you know if I ever figure it out.
Second, here's my code for recording the duration of selection into a text file. It's not pretty but does the job. It reads the data from "test.txt" and writes it to "test2.txt" (initially test.txt is a blank file). It then records the duration of the current selection to "test2.txt". Finally, it reads and writes the data from "test2.txt" into "test.txt" so that you can start all over again. Thus, if you run this every time you make a selection, the file "test.txt" will contain the durations of all the selections.
As for your other comments:
This works on both Audacity 1.2.6 and 1.3.5. For now, I prefer Audacity 1.2.6 because I like how it displays the current selection info (time/samples). Audacity 1.3.5 rounds the time to the nearest second, but I want millisecond precision. Fortunately, both export the data to the same precision, so I should be OK with the 1.4.0 upgrade.
I'm using Windows XP - the default directory is the desktop.
The reason why I can't process the entire track is that the acoustic signals that I'm looking for are not simply a matter of threshold. So the "Silence Finder" wouldn't help. In the future, I hope to write a more sophisticated code that can automatically detect these signals from noise. Like you said, that would resolve the problem of having my selection times match the Audacity time line. For now, however, it's easier to manually identify these signals by eye (or ear for that matter).
Thanks for your help!
First, I figured out that (snd-t0 s) works on mono but not stereo audio signals. To use stereo, you first have to split the channels into left and right. So now I get a result of 0 as expected. I still would like to get the absolute time from Audacity. I'll let you know if I ever figure it out.
Second, here's my code for recording the duration of selection into a text file. It's not pretty but does the job. It reads the data from "test.txt" and writes it to "test2.txt" (initially test.txt is a blank file). It then records the duration of the current selection to "test2.txt". Finally, it reads and writes the data from "test2.txt" into "test.txt" so that you can start all over again. Thus, if you run this every time you make a selection, the file "test.txt" will contain the durations of all the selections.
Code: Select all
(setq fx (open "test2.txt" :direction :output))
(setf duration (get-duration 1))
(do* ((fp (open "test.txt" :direction :input)) (ex (read fp) (read fp)))((null ex) nil)(print ex fx))
(print duration fx)
(close fp)
(close fx)
(setq fx (open "test.txt" :direction :output))
(do* ((fp (open "test2.txt" :direction :input)) (ex (read fp) (read fp)))((null ex) nil)(print ex fx))
(close fp)
(close fx)This works on both Audacity 1.2.6 and 1.3.5. For now, I prefer Audacity 1.2.6 because I like how it displays the current selection info (time/samples). Audacity 1.3.5 rounds the time to the nearest second, but I want millisecond precision. Fortunately, both export the data to the same precision, so I should be OK with the 1.4.0 upgrade.
I'm using Windows XP - the default directory is the desktop.
The reason why I can't process the entire track is that the acoustic signals that I'm looking for are not simply a matter of threshold. So the "Silence Finder" wouldn't help. In the future, I hope to write a more sophisticated code that can automatically detect these signals from noise. Like you said, that would resolve the problem of having my selection times match the Audacity time line. For now, however, it's easier to manually identify these signals by eye (or ear for that matter).
Thanks for your help!
Re: Exporting Selection Info
In Audacity 1.3.5 you can select various formats for the time display in the selection tool bar - just click on the little black arrow at the right hand side of the numbers.buzzbuzz wrote:I prefer Audacity 1.2.6 because I like how it displays the current selection info (time/samples). Audacity 1.3.5 rounds the time to the nearest second, but I want millisecond precision.
The time display on the time line will show decimals as you zoom in close enough for them to be relevant.
Ah, thanks. I've mostly been using Linux. I guess that will probably be the case on all Windows machines.buzzbuzz wrote:I'm using Windows XP - the default directory is the desktop.
I was suggesting looking at "Silence Finder" purely as an example of how another Nyquist plug-in handles events in relation to time.buzzbuzz wrote:So the "Silence Finder" wouldn't help.
I don't think that you need to use "Test2.txt", once the data has been read from test.txt it can stay in memory and then be written back directly into test.txt. Have a look at the "List Functions" in the Nyquist manual.buzzbuzz wrote:It reads the data from "test.txt" and writes it to "test2.txt" (initially test.txt is a blank file). It then records the duration of the current selection to "test2.txt". Finally, it reads and writes the data from "test2.txt" into "test.txt" so that you can start all over again.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)