Add-on for transferring audio edits between Blender and Audacity

The resulting add-on:

Original thread title: “Bug: Importing Labels through command line fails”

I working on adding Audacity as an optional assistant application to Blender, so audio strips from the Video Sequencer can be round tripped in Audacity.

I need to mark the area of the audio file used in the Blender(not always the full file duration) in Audacity, so I wanted to use an Audacity label for this.

So I’ll need to open both an audio file and an audacity label file through the command line. Like this:

C:\Program Files (x86)\Audacity>audacity "C:\Users\45239\Desktop\Billy.wav" "C:\Users\45239\Desktop\label.txt"

Label file:

126.348822	1758.354445	Billy.wav

But the label file fails at import. "Audacity did not recognize the file type of… "

Is this a bug, an unsupported feature or is there some way I can work around this?

It would be nice if Blender and Audacity could benefit from each other this way.

(Btw. opening “List of Files…” also seems to fail.)
(Btw. btw. Label import through command line also fails in Audacity 3.0 beta)

If I recall correctly that is an unsupported feature (I’ll need to check).

Regarding a workaround, do you have any experience with Python? There’s probably a Python solution (again I’d need to check).

I do have experience with python, but I’ll need to limit the interaction from the Blender side to a command line.

I’ve checked and yes it is not a feature. Only audio files can be passed as arguments.

Do you only need one label ?
Do you only need a “start” position, or an “end” position, or “start and end”?
How are you creating the label track?

There’s probably a way to do what you want, but nothing obvious is coming to mind. If you can provide a clearer picture of what you are wanting to do, that may help me to find a solution.

If the user ex. needs to do some noise reduction on an audio strip in Blender, then the user could select “Send to Audacity”.
Internally Blender would then make a copy of that file(newer use the original), and switch to it internally. Then Audacity should be opened with the file, including markings of the range of the clip used in Blender, so the user can do the noise reduction, and save the file. And when updating the file in Blender it will be with the Audacity noise reduced file.

From the Blender side it would be really simple to just open the file in Audacity through a command line and including some marker file.

If txt as extension is also used for audio files, there is no way Audacity can know if it is a marker file or not. So I guess if Audacity marker files had a different extension it would be easier to open them correct though the command line? I’ve seen some also use subtitle formatting ex. srt:

00:00:54,319 --> 00:02:53,822
My edit

Or csv:

00:00:54.319,00:02:53.822,My edit

A range or In and out would work, but just one set for each file.

The new pipe stuff looks quite powerful, but it’ll demand of users to install and enable things, and that maybe is too much to ask for the common user, and not how “assistant application”/“Open in…” is working in most softwares.

But on the other hand if it is a one-off setting up and enabling, and then if files(load and save), labels and playhead position can be controlled from Blender, that would super cool. So you would have both programs running in parallel, and you would be able to edit whatever audio file in Audacity, more or less instantly.

How much do you know about Blender?
I don’t know a lot, but I’ve heard that it has an embedded Python interpreter. Is it possible to use that to communicate with Audacity via mod-script-pipe? The central requirement is to be able to write to a “named pipe”.

I have been working on the video editor part of Blender including add-ons for a couple of years now. Everything within python should be possible, but I don’t know much about piping. Stuff I’ve done and collected about the VSE:

What I was original thinking was a built-in solution like the video and image options here:

Where you can set up a path to an app which will open ex. an image like this:

If it needs to be in python, it is possible to make an add-on for that, but add-ons are rarely used.

The closer to a turn-key solution the more will use it. The audio options of Blender are really, really basic, so there is a great need for a better solution.

There has been some discussion about adding an option to configure Audacity for use as an add-on editor for other apps, but currently Audacity is not ideal for that kind of use. Audacity does not directly edit files, it copies the audio data from a file into the “project” and exports from the project to create a new file. Command line options for Audacity are minimal (as you have discovered).

Imo it’s looking like the best option, but I know nothing about Python add-ons in Blender.
If you want to do a feasibility study, you could try creating a minimal test add-on, perhaps just launching Audacity, loading a file and then send the “Play:” command via a named pipe.

Some information about mod-script-pipe and the scripting API:

The first script seems to be working, but the next two makes Blender hang. I don’t know why.

Here’s the first one:

I have a bit of a hard time to get my head around how this would work for editing sound for video. Do we need to need to export the entire timeline to Audacity, and make the Blender playhead control the Audacity playhead(like jack), or do we have to mirror the UI from Audacity, or how is this feature envisioned?

In the specific case of noise reduction I guess this feature would make it possible to do everything from Blender while Audacity windowless would do all of the hard work, though the user will have a better time doing the tweaking in Audacity. Is there no chance of the Audacity Labels getting a specific extension name, so they can be distinguished from other text files and opened through the Audacity command line?

Great :smiley:

You can take it as far as you like, but I initially envisioned it as simply:

  1. Launch Audacity and import the file
  2. Select the required section of audio
  3. (optional) add a label to the selected section.

It’s a somewhat frustrating experience, Blender is shutting do down over and over, without any info.
Seems like Audacity forces Blender to end if there ex. is an error with a path string.
And running the same script twice will make Blender crash:
And labels are not working for me, what is the correct ‘do_commands’ for setting a range and adding a label to it?

Log for run twice:

  1. run:
Test folder: C:\Users\45239\Desktop\, running on windows
Write to  "\\.\pipe\ToSrvPipe"
Read from "\\.\pipe\FromSrvPipe"
-- Both pipes exist.  Good.
-- File to write to has been opened
-- File to read from has now been opened too

Send: >>> Help: CommandName=Help
Rcvd: <<< 
{ "id":"Help", "name":"Help", "params":
      { "key":"Command", "type":"string", "default":"Help" },
      { "key":"Format", "type":"enum", "default":"JSON", "enum":
          [ "JSON", "LISP", "Brief" ] } ],
  "tip":"Gives help on a command." }
BatchCommand finished: OK

Send: >>> Import2: Filename=C:\Users\45239\Desktop\Salig.wav
Rcvd: <<<
BatchCommand finished: OK
  1. run:
Test folder: C:\Users\45239\Desktop\, running on windows
Write to  "\\.\pipe\ToSrvPipe"
 ..does not exist.
    Ensure Audacity is running with mod-script-pipe.

Blender shuts down.

To select from 10 to 15 seconds and add a label:

SelectTime:End="15" RelativeTo="ProjectStart" Start="10"

Note that the commands for Python scripting are the same as Macro commands:

If you’re launching Audacity from Python, you will probably need your script to pause while Audacity launches before sending the pipe commands.

Note also that is only intended as a quick demo, not as practical code. For practical code you need to ensure that the Python script exits gracefully on error.

For practical code you need to ensure that the Python script exits gracefully on error.

Do you have an example of how to do this? Or can the pipe be closed(so Blender won’t crash every 2. time I run the script)? Or not start a new pipe if one is already existing?

I’m using Linux, so I can describe what happens here. I assume that it is pretty much the same on other platforms.

The pipes are created by Audacity when Audacity launches if:
a) mod-script-pipe is enabled in Audacity preferences
b) the pipes do not already exist.

I don’t know why Blender is hanging, but one possible “gotcha” in is that “get_response” can block if it is unable to read from the “FROMFILE” pipe.

If you look at you will see that reading and writing occur in a separate thread from the main code. That prevents the code from hanging indefinitely in the event of failure to read from the “read” pipe.

If Blender requires an exit code when the script completes, ensure that your code provides an exit code when it quits for any reason.

Thank you. Making a bit of progress:

I still need to reopen Blender for each test. Pretty annoying. And I haven’t digging into your treading stuff yet. But “from Blender to Audacity” now works. :slight_smile:

Cool :sunglasses:
Good work.

For returning the file into Blender, I’m thinking about using a file dialogue in Blender, so users can decide if they want to overwrite the original file(will change all of the instances used in the edit), or if they want to save it as something else, and the this new file will be swapped with the old file.

I know very little of how Audacity works, after adding filters and then exporting, is it necessary to play/record to a new track and then export that(like in your examples), or can the file be exported directly?

Is it possible to export with the same settings as the original file, or is it endorsed to change the settings?

How do I detect when the export is finished, so the file can be swapped/refreshed in Blender?

I got receiving working, and recording(sick!):

The Audacity scripting opens amazing doors I never thought was even possible!

I’m sorry for all of my questions, but now I’m attempting to recreate all of the audio edits from Blender in Audacity and I need to be able to import trimmed audio files into specific spots and tracks.
Normally you would do this with the Time Shift tool, but can this also be done with the do_commands?

I can import, select range and trim everything outside of the selection, but how do I move it?