Import WAV files into Audacity via programming

Audio software developers forum.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
freddythunder
Posts: 4
Joined: Sun May 13, 2018 1:30 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Import WAV files into Audacity via programming

Post by freddythunder » Sun May 13, 2018 1:55 am

Hi,

I was looking around this form and the rest of the interweb to find to see if there's an answer for what I'm trying to do.. Right to the point, here's what I'm doing. I have a library of a few hundred sounds. I'm planning on having a program attempt to make EDM songs by randomly selecting some of the sounds from the library in WAV files, and assemble them into a song. I would love to find something that does this and have run into ChucK which looks like almost what I need, but was unimpressed with the results on videos I've found. I was looking into Nyquist, but I couldn't find in the documentation much about automatically importing files into Audacity in certain places; looked to me like adding functionality while you're using Audacity. And I found some python-to-blender API stuff, but nothing about putting wav files in different places in the project.

I have found a couple projects out there of computers making random EDM songs, but both of the projects seem to have started, gotten really complicated in the planning phases, and fizzled out. I want to do some simple stuff to see how feasible it could be before I dump a ton of time and energy into it. I plan on actually making the songs using python or PHP to randomize and collect the types and places of samples, and then mix it automatically using sox and some very inefficient bash scripts. If something interesting is developed, then I can move to another more efficient workflow.

Ideally, I would want to be able to pull what's created by the computer into an Audacity project in case the song comes out good, but needs some human tweaking. I would love to be able to take the output of whatever was generated in code, develop the XML, convert the WAV files to this headerless 32 bit floating point PCM format that audacity uses, and then hope that Bob would be my uncle.

Here's what I have been successful at:
I was able to create a test project and duplicate one of the .au files, then go into the .aup XML and create another track using that duplicated file. When I open up the project again, I see the duplicated track's waveform. Great!

Here's my main roadblock. I haven't dug too deep yet, but I've tried making that format using avconv/ffmpeg and sox, but when I try any of the files I create, Audacity just crashes. Does anyone know of a way to convert a WAV to Audacity's .au format using command line so it can be done automatically?

kozikowski
Forum Staff
Posts: 41024
Joined: Thu Aug 02, 2007 5:57 pm
Operating System: OS X 10.9 Mavericks
Location: Los Angeles

Re: Import WAV files into Audacity via programming

Post by kozikowski » Sun May 13, 2018 3:47 am

Audacity opens up its AUP Project Manager file to find out what to do with all those little AU files in the _DATA folder.

This is a project.

Image


You got lucky on the first one. If the two aren't perfectly matched, it usually goes up in flames.

We probably need a senior elf for the rest of that.

Koz

freddythunder
Posts: 4
Joined: Sun May 13, 2018 1:30 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Re: Import WAV files into Audacity via programming

Post by freddythunder » Tue May 15, 2018 2:43 am

Hey Koz,

Yep, thanks for replying. I was able to update the <waveclip offset="0.0000000".. to a higher (or lower) value and it moves the clip in the appropriate direction. I would be able to position things in the proper place once I figure it out.

I tried setting the preference to use the wav files directly instead of saving the .au file, but it looks like it creates an "alias" file in the same place which is still binary so, kind of the same problem; maybe wordse.

I found this link: viewtopic.php?f=21&t=73428 from someone else trying to reverse engineer the .au files but takes a last stab at the format at the end of the thread. Not sure how accurate it is, but I would need to be able to format it using a different tool.

I cloned the source to my server and did a search for "AudacityBlockFile" since that's at the beginning of all the .au files. I found it's in BlockFile.cpp where I found this in the comments:

Code: Select all

A BlockFile represents a chunk of audio data.  These chunks are
assembled into sequences by the class Sequence.  These classes
are at the heart of how Audacity stores audio data.
So I'll try to find out how it breaks into it's chunks (which I think I already found) and how it creates and saves the file. So, cpp is not one of my languages so if anyone knows it, or anyone knows how I can create the same pcm file, I'm all ears.

Thanks,

steve
Site Admin
Posts: 47940
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Import WAV files into Audacity via programming

Post by steve » Tue May 15, 2018 9:52 am

The simple way to convert a WAV file into blockfiles is to simply import the WAV file.
From the command line, Audacity may be launched with:

Code: Select all

audacity "name-and-path-to-audio-file"
Which will launch Audacity and attempt to import the specified file.

Manipulating the AU or AUP files while Audacity is open is probably not going to work, because Audacity has complex tracking code to maintain a record of which blocks belong where, which are in use, and which are required for "Undo". Audacity also caches a lot of information in RAM, including peak and RMS information about the blockfiles, which obviously has to remain in synch with the data on disk.

Manipulating the AUP file while Audacity is not running should be possible. You just need to create valid XML that Audacity can understand, and reference blockfiles that exist where the XML says they exist.

I've got a feeling that this project will turn out a lot more complicated than you are expecting it to.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

freddythunder
Posts: 4
Joined: Sun May 13, 2018 1:30 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Re: Import WAV files into Audacity via programming

Post by freddythunder » Tue May 15, 2018 1:24 pm

Thanks Steve!

I didn't know you could do that from the command line so that would be very helpful, however there would be multiple, maybe hundreds of .au files that would need to be imported/hacked-in at once. I wouldn't expect to be able to manipulate the .aup while audacity is open; I would have to make everything intact before starting the program so it could load everything it needs into memory.

And you bet, it's already difficult! Most likely why none of the other people's attempts of what I'm trying to do exist. But, with most computer programming, I'd only have to figure it out once and then I can automate the process. Just need to figure out the format. Since I found the cpp that does it, it should be doable.

I did have another thought last night that would remove the need for a GUI but wouldn't be as user friendly. Whichever means I use for the random parts to be compiled together, I could have that program output a XML/YAML/JSON string with all of the chosen files and start/stop times and I can manipulate that and have the compiling program recreate any changes. Then I could just use a text editor or sed at the command line. But it sure would be neat to be able to open it up in a GUI like audacity.

steve
Site Admin
Posts: 47940
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Import WAV files into Audacity via programming

Post by steve » Tue May 15, 2018 2:15 pm

Something else that may be useful for you is that a project may contain other types of audio files, such as WAV or Ogg files.
Here's an example AUP file that imports two WAV files from its _data folder.

Code: Select all

<?xml version="1.0" standalone="no" ?>
<!DOCTYPE project PUBLIC "-//audacityproject-1.3.0//DTD//EN" "http://audacity.sourceforge.net/xml/audacityproject-1.3.0.dtd" >
<project xmlns="http://audacity.sourceforge.net/xml/" projname="test-project_data" version="1.3.0" audacityversion="2.3.0-alpha-May 14 2018" sel0="2.5721153846" sel1="5.3605769231" vpos="0" h="0.0000000000" zoom="83.2000000000" rate="44100.0" snapto="off" selectionformat="hh:mm:ss + milliseconds" frequencyformat="Hz" bandwidthformat="octaves">
	<tags/>
	<import filename="test.wav" offset="0.00000000" mute="0" solo="0" height="150" minimized="0" gain="1.0" pan="0.0"/>
	<import filename="test-2.wav" offset="2.57211538" mute="0" solo="0" height="150" minimized="0" gain="1.0" pan="0.0"/>
</project>

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

freddythunder
Posts: 4
Joined: Sun May 13, 2018 1:30 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Re: Import WAV files into Audacity via programming

Post by freddythunder » Thu May 17, 2018 12:51 pm

I have yet to try this Steve, but this looks to be very promising. Not sure how much control I'd have playing multiple wavs at the same time / importing multiple tracks. Do you know of some documentation that further explains this? I looked but couldn't find any. Thanks!

steve
Site Admin
Posts: 47940
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Import WAV files into Audacity via programming

Post by steve » Thu May 17, 2018 1:33 pm

freddythunder wrote:Do you know of some documentation that further explains this?
I don't think there is any.
It's not covered in the user manual as editing the AUP file manually is strongly discouraged in normal use of the application.

The format of the AUP file is just ordinary XML.

Code: Select all

<import filename="test.wav" offset="0.00000000" mute="0" solo="0" height="150" minimized="0" gain="1.0" pan="0.0"/>
<import .../> Tells Audacity to import something into a track.
filename="test.wav" The name of the file to import (paths are relative to the AUP file).
offset="0.00000000" The time position for the start of the imported file.
mute="0" solo="0" The state of the Mute and Solo buttons (1 = depressed).
height="150" The track height in pixels.
minimized="0" Whether the track is "collapsed".
gain="1.0" pan="0.0" Position of the track Pan and Gain sliders.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply