I have recently been using Python to control Audacity, with Python version 3.9 and Audacity version 3.2.5. However, I encountered an error while trying to export data after plotting the spectrum. The error message I received was “Could not export to TXT format!”. Does anyone have any suggestions on how to fix this issue? Thanks.
How were you doing that? From your Python script? If so, please post your Python script, and list any steps that need to be followed so that we can reproduce the issue.
Perhaps you would like to share a screenshot showing this error message. BTW, the PlotSpectrum command doesn’t support any parameters. How are you invoking Export ?
Thank you for your reply.
My goal is to export the plot spectrum data using the Python code below.
import pyaudacity
#Step1 : Import wave file to audacity
audioPath = ‘D:\20K-100_50points_1s.wav’
pyaudacity.do(f’Import2: Filename=“{audioPath }”‘)
#Step2: Plot Spectrum
pyaudacity.do(‘SelectAll’)
pyaudacity.do(‘PlotSpectrum: Range=Full Project’)
#Step3: Export the frequnecy analys data
exportPath = ‘D:\spectrum.txt’
pyaudacity.do(f’Export2: Filename=“{exportPath}”’)
pyaudacity.do(‘Exit’)
After executing the script, I received an error message. I believe the problem may be related to using the ‘Export2’ command incorrectly, but I am not sure what the correct command is for exporting frequency analysis data. Can you suggest the correct command to use? Thank you.
I called the do
method and used the Export2
command, but I think this command may be incorrect. Unfortunately, I am unable to find the correct command to use for exporting the data. Could you please give me some advice on how to proceed? Thank you.
Yes. I don’t think this feature has been implemented. @steve may know differently.
You’re right, Export2
is for exporting audio.
Unfortunately there isn’t a command for exporting text from Plot Spectrum.
The full list of available commands is here: Scripting Reference - Audacity Manual
The most recent version of that page (the “alpha manual”) is here: https://alphamanual.audacityteam.org/man/Scripting_Reference
(Note that plug-ins can also be called, though some plug-ins may not have all of their functionality available. Nyquist plug-ins should all work correctly via scripting.)
There are two possible approaches that I can think of for obtaining FFT data from Audacity via scripting. Unfortunately both add a fair bit of complexity:
- Use a third part app such as AutoKeys or AutoHotKeys to automate clicking the Export button (this is probably the easiest option).
- Write a Nyquist plug-in to perform FFT analysis and export the data. This is the better “pure Audacity” solution, but doing FFT with Nyquist is definitely one of the more “advanced” (difficult) features in Nyquist.
I’d also recommend using pipeclient rather than pyaudacity.
Pipeclient is a bit “lower level” than pyaudacity, but it is threaded so as to be more robust and non-blocking, and it accepts any/all Audacity scripting commands (past or future). Use the latest documentation to find the available commands and their correct syntax. It can also be used interactively from the command line, which is an excellent way to test commands before or while coding. (Full disclosure: I wrote it).
There is also an updated but unofficial version of pipeclient here: pipeclient.py is a Python module for sending commands to Audacity from Python. This version is more recent than Audacity's version. · GitHub
Any bugs / issues regarding this version should be posted as comments on that page (probably requires a [free] GitHub account).
This version is likely to be updated and developed further (when I have time )
Regarding documentation, there may be more up to date documentation on the new Audacity support site, but if it is there I’ve not been able to find it. Perhaps @LWinterberg may know where the latest Scripting documentation is.
This topic was automatically closed after 30 days. New replies are no longer allowed.