Analyze amplitude contour and output to .txt

Hello again!

I am trying to output the amplitude contour of selected audio to a .txt file. Basically, I seek the info for a new pwlv envelope determined by the sounds amplitude. I don’t want to actually apply it to anything though, I just want to output the info to .txt for later use or whatever.

First, I thought Sample Data Export was the way, but I’m completely ignorant about what the linear or db output is actually describing. I tried graphing the outputs within ranges like -1 to 1 and -60 to 0 but the result didn’t resemble the sounds shape at all. Then, I was hoping something like this would work…

(setq step 100)
(setq ampenvelope 
(snd-avg *track* step step op-peak))

(setdir "C:/")
(setq file (open "test.txt"  :direction :output))
(format file ampenvelope)
(close file)

but no luck.

I like to describe what I’ve tried so you don’t think I just come on here hoping someone will do all the work for me. :slight_smile:

I’m using window 7, audacity 2.1.2 and used the .exe installer.

Sample Data Export analyzes the audio sample by sample. The “samples” are the individual dots that you see if you zoom in really close on the waveform:


The vertical ruler on the left end of the track shows amplitude on a linear scale.
If you change to “Waveform (dB)” view, (Audio Track Dropdown Menu - Audacity Manual) then the scale is in “dB relative to full scale” (Decibel - Wikipedia). The dB scale goes from 0 dB (maximum amplitude, equivalent to +/- 1.0 on the linear scale), to -infinity (minus infinity - equivalent to 0.0 on the linear scale).

That’s quite close, but not quite right.

If you run that code on a mono track using the Debug button, you will see an error something like:

error: bad argument type - #<Sound: #afb08450>
Function: #<Subr-FORMAT: #9e67874>
Arguments:
  #<File-Stream: #9e438b0>
  #<Sound: #afb08450>
1> NIL
1>

Nyquist error messages can be rather cryptic. but what this is telling you is that somewhere in your code you are passing a “sound” to a function that does not accept “sounds” as arguments (function “arguments” are the parameters that you send to the function).

In this case, the problem is:

(format file ampenvelope)

“ampenvelope” is a “sound”.
You created “ampenvelope” as (snd-avg track step step op-peak)
(see in the Nyquist manual Nyquist Functions)

The “format” function is expecting to be passed a “string” (text), not a “sound”.

So what you need to do is to convert each sample of “ampenvelope” into a numeric value, and output the numeric values as text.
You could use something like this:

(setq maxsamples 100000) ;protect against stupidly large text output
(dotimes (i (min maxsamples (snd-length ampenvelope ny:all)))
  (format file "~a~%" (snd-fetch ampenvelope)))

See here for more information about the “format” command: XLISP format

Brilliant! Works like a charm. Thanks for your help again, Steve. I appreciate it! :smiley: