NyquistPrompt Python Script Command Not Working

Hi all,

I’m trying to write a Python script for Audacity 2.3.2 and I can’t seem to be able to use the NyquistPrompt command as seen on this page: https://alphamanual.audacityteam.org/man/Scripting_Reference. I’m able to execute other commands without issue, so I don’t think it’s a problem with the script pipe. I made sure to set the version to 4, but that doesn’t seem to change anything. Here’s the line I’m trying to execute:

do("NyquistPrompt: Command=(get '*selection* 'rms) Version=4")

When I try

get '*selection* 'rms

in the Nyquist Prompt in Audacity, it works perfectly fine. I can’t to figure out why it won’t work in the script.

Any help would be appreciated!

I presume that you mean, when you try:

(get '*selection* 'rms)



The Command: should be quoted, but as we’re already in a quoted string, the quotes need to be escaped:

do("NyquistPrompt: Command=\"(get '*selection* 'rms)\" Version=4")

If that doesn’t work for you, post enough of your Python script so that I can reproduce what you are doing.

Yes, sorry, that’s what I meant.

I tried putting it in quotes but it did not succeed. Here’s what I have for code:

import os
import sys
import time

if( sys.platform  == 'win32' ):
	print( "recording-test.py, running on windows" )
	toname = '\\\\.\\pipe\\ToSrvPipe'
	fromname = '\\\\.\\pipe\\FromSrvPipe'
	EOL = '\r\n\0'
else:
	print( "recording-test.py, running on linux or mac" )
	toname = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
	fromname = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
	EOL = '\n'

print( "Write to  \"" + toname +"\"" )
if not os.path.exists( toname ) :
	print( " ..does not exist.  Ensure Audacity is running with mod-script-pipe." )
	sys.exit();
    
print( "Read from \"" + fromname +"\"")
if not os.path.exists( fromname ) :
	print( " ..does not exist.  Ensure Audacity is running with mod-script-pipe." )
	sys.exit();

print( "-- Both pipes exist.  Good." )

tofile = open( toname, 'wt+' )
print( "-- File to write to has been opened" )
fromfile = open( fromname, 'rt')
print( "-- File to read from has now been opened too\r\n" )

def sendCommand( command ) :
	print( "Send: >>> "+command )
	tofile.write( command + EOL )	
	tofile.flush()

def getResponse() :
	result = ''
	line = ''
	while line != '\n' :
		result += line
		line = fromfile.readline()
	#print(" I read line:["+line+"]")
	return result

def doCommand( command ) :
	sendCommand( command )
	response = getResponse()
	print( "Rcvd: <<< " + response )
	return response

def do( command ) :
	return doCommand( command )
	
do("SelectTracks: Track=0 TrackCount=1 Mode=Set")
do("SelTrackStartToEnd")
do("NyquistPrompt: Command=\"(get '*selection* 'rms)\" Version=4")

This should select the first track in the project then measure the RMS of that track.

I needed to modify your Python code a bit to get it to run with Python 3 on Linux, but the underlying problem is that the “NyquistPrompt:” command is broken (doesn’t work).

An alternative approach that does work, is to install the “Measure RMS” plug-in. If you’ve not already got it, you can download it from here (direct link): https://raw.githubusercontent.com/audacity/audacity/master/plug-ins/rms.ny

You can then run the plug-in with:

do("MeasureRms:")

That worked perfectly. Thanks for your help!

In case it’s any use to you, this was my version of your script (works with Python 3 on Linux). I think it should work with Python 3 on other platforms, but I’ve not tested. The main differences are:

  • Line 28
TOFILE = open(TONAME, 'w')
  • Some indentations were tabs rather than spaces.
  • Global variables in upper case
  • Removed excess spaces within parentheses.
  • Replaced final “do” command.


import os
import sys
import time

if(sys.platform  == 'win32'):
    print("recording-test.py, running on windows")
    TONAME = '\\\\.\\pipe\\ToSrvPipe'
    FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
    EOL = '\r\n\0'
else:
    print("recording-test.py, running on linux or mac")
    TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
    FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
    EOL = '\n'

print("Write to  \"" + TONAME +"\"")
if not os.path.exists(TONAME) :
    print(" ..does not exist.  Ensure Audacity is running with mod-script-pipe.")
    sys.exit();

print("Read from \"" + FROMNAME +"\"")
if not os.path.exists(FROMNAME) :
    print(" ..does not exist.  Ensure Audacity is running with mod-script-pipe.")
    sys.exit();

print("-- Both pipes exist.  Good.")

TOFILE = open(TONAME, 'w')
print("-- File to write to has been opened")
FROMFILE = open(FROMNAME, 'rt')
print("-- File to read from has now been opened too\r\n")

def sendCommand(command) :
    print("Send: >>> "+command)
    TOFILE.write(command + EOL)	
    TOFILE.flush()

def getResponse() :
    result = ''
    line = ''
    while line != '\n' :
        result += line
        line = FROMFILE.readline()
    #print(" I read line:["+line+"]")
    return result

def doCommand(command) :
    sendCommand(command)
    response = getResponse()
    print("Rcvd: <<< " + response)
    return response

def do(command) :
    return doCommand(command)
    
do("SelectTracks: Track=0 TrackCount=1 Mode=Set")
do("SelTrackStartToEnd")
#do("NyquistPrompt: Command=\"(get '*selection* 'rms)\" Version=4")
do("MeasureRms:")