Import WAV files into Audacity via programming

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?

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.




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

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: https://forum.audacityteam.org/t/au-files-format-specification/30195/1 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:

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,

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:

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.

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.

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.

<?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>

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!

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.

<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.