Quick tutorial for Windows 10 - Batch sample conversion using SoX

Lots of frowny faces in your flex machine sample folders? Here’s a quick script I wrote using the SoX audio processing library and Windows Powershell to copy an entire folder of wav files of any bit depth and sample rate, convert any non-16/44.1 or 24/44.1 to 16-bit 44.1 kHz wav, and paste all into a new folder that maintains the file naming and subfolder hierarchy.

First time setup

  1. Download and install SoX from http://sox.sourceforge.net/
  2. Find the folder where SoX is installed on your computer. For me it was: “C:\Program Files (x86)\sox-14-4-2”
  3. Hit Windows key and type “environment”. Click on “Edit environment variables for your account”.
  4. In the user variables list, highlight “Path”, then click “Edit…”
  5. Click “New”, and paste the path of your Sox folder. Then OK your way out of all this.
  6. Download the attached file, OTconvert.ps1. Save it anywhere.

OTconvert.ps1 (789 Bytes)

Usage

  1. In OTconvert.ps1, edit the $SRC and $DST lines to reflect your own existing source and desired destination folders. Your DST folder should not exist yet. Save the file.

WARNING: SRC and DST cannot be the same. Due to some less than ideal behavior in SoX, this will result in all of your original samples being overwritten with silence. DST also cannot be a subfolder of SRC. Make them completely separate.

  1. Inside the folder containing OTconvert.ps1, hold shift and right click, then click “Open Powershell window here”.
  2. Paste the following command and hit enter. That should be it!

powershell -ExecutionPolicy ByPass -File OTconvert.ps1

Notes

  1. This is my first time using any of this, so experts: please let me know if I got something wrong/stupid.
  2. This is non-destructive to your original samples. It creates an entirely new folder.
  3. The bit depth reduction (with dither) and sample rate conversion processes can result in clipping. The SoX flag “-G” is supposed to prevent this, but doesn’t always work completely. The next flag, “-v $gain”, reduces the sample volumes to $gain of the original as a further measure to prevent clipping. You can play around with this number or omit that part entirely. SoX will let you know if any of the samples clip.
3 Likes

Very nice, this should be useful to some folks. I’ve used OcenAudio to do some batch conversion of samples myself.

There are a few pieces of GUI software that will do it, but I couldn’t find any (free ones) that would maintain my naming and folder hierarchy.

Good start! Sox is the best audio conversion tool I know of, hands down.
Some things you might consider:

  • If the file is 16/24 bit and 44.1k, it does not need to be converted
  • If a file is 24 bit ideally it should be kept that way, as OT has no problem with 24 bit, and converting to 16 bit is lossy

I have never used powershell, but all my sample library organisation is done with sox and Ruby atm. I’ve got a kind of version control system going on, might post about it if I get time.

Sox4lyfe

2 Likes

Agree completely. This is my first cut and I haven’t yet learned all I need to know on sox/ps to make that happen.

Many ways to the same result, based on your powershell script. I wrote a one-liner for using sox to convert samples for Octatrack purposes. I think I totally lucked into the fact that the source and destination folders have to be different. I don’t think I’ve encountered any issues with dithering causing clipping, but maybe that’s because the bit rate hasn’t had to change (or at least not dramatically).

Do you have to manually specify the “dither” command? I thought it did that automatically?

When I’m on my macbook later, I’ll dig up my script for the command line. It’s very similar, and works great. The hardest part is remembering to create the destination folder and update the pathnames in the script before getting started. Cheers.

You’re probably right, I think dither is applied by default on bit depth reduction. Regarding the clipping, it’s also possible that some of the janky sound packs I’ve downloaded contain already-clipping files. I’ve read of other SoX users experiencing the same issue though.

1 Like

IIRC dither is applied by default, the -D flag is used to bypass it.

@alspacka some commands that might be useful:

sox --i -r input
returns samplerate

sox --i -b input
returns bitdepth

2 Likes

Thanks! I’ll get to work incorporating that when I get the chance. If I apply bit depth reduction and sample rate reduction serially, is that equivalent to doing it in one command as I have above?

1 Like

I’ve edited the code so it won’t convert samples that are already OT-compatible, though it will still gain-reduce them if you specify a $gain other than 1.0.

1 Like

Easiest way is just to get the SoX plugin for Foobar2000, that’s how I’ve been doing my sample rate conversion for years. Once you set up your templates for different formats it takes a few clicks to load all of your samples into a playlist and convert them.

Is it the main particular thing? I usually use Samplitude, I don’t know if it keeps subfolders…

Are you asking if that’s my primary reason for scripting my own solution? If so, yes.

1 Like