Program For Converting a Ton of Samples

Yes.

Probably hundreds of directories with thousands of samples.

Well, so long as you have enough disk space making a deep copy of your samples shouldn’t be hard.

At the commmand line

  cp -r my-original-samples copy-of-mysamples
1 Like

Yeah, I mean duplicate your entire Sample Library top-level folder as Sample Library (2) and run the script on that, if you’ve got the HD space.

1 Like

I see. I am still unsure of the commands that I would need to enter to do the conversion on more than just the current directory though :-/

Let me see if i can knock up a one liner …

2 Likes

Ok, it would go something like

find copy-of-mysamples -name \*.wav | xargs -I@ bash -c "sox @ @"

but instead of just ‘sox’ you would put in your full sox command. So if the command you were doing manually was “sox -r 8k -u -b 8 -c 1 my-sample.wav my-sample.wav” (which converts my-sample.wav in place) then the command above would become

find copy-of-mysamples -name \*.wav | xargs -I@ bash -c "sox -r 8k -u -b 8 -c 1  @ @"

The @'s there are the file name you’re converting. You should type the @'s exactly as they are, when this command runs, the right filenames will be plugged in for you.

You might want to try that out on a small hierarchy first to check it’s doing what you want.

Link: Read through all the files and apply a command in Linux - Stack Overflow

2 Likes

So awesome, thanks so much, I will give it a shot and report back here!

OK, I am in the directory that has the folder in it with the copy of my samples named WAV-copy and I am putting this into the terminal:

find WAV-copy -name *.wav | xargs -I@ bash -c “sox @ -b 16 @ rate -v 48k”

and getting errors like these:

sox FAIL formats: can’t open input file `808’: No such file or directory

where 808 etc are the deepest directories where the wav files actually are (that could be inside another directory like “Bass Drum” or something).

I’m just here to pour out a 40 oz. for Bias Peak. The best 2-track audio editor ever, and it had an absolute killer batch processor to boot. RIP brother, we miss you.

I use Max or afconvert on my Mac now.

3 Likes

ahhhh … I bet some of those files or directory names have spaces in them don’t they ? Like, the directory below ‘808’ is ‘Bass Drum’ with a space ?

If I’m right, you’ll want to do this instead

find WAV-copy -name *.wav -print0 | xargs -0 -I@ bash -c “sox @ -b 16 @ rate -v 48k”

Note the added -print0 and -0 .

1 Like

Yep some of them have spaces. I tried the new command and still it is giving me the same errors…

Some files are also numbered like “01. Kits” with a period in it if that matters (its the only other thing I can think of)… Oh, and I did see one with an & too (none of this naming was my doing lol)…

Thanks so much again you are a life saver!

Edit: OK, so I put ‘@’ instead of @ so it looks like this now:

find WAV-copy -name *.wav -print0 | xargs -0 -I@ bash -c “sox ‘@’ -b 16 ‘@’ rate -v 48k”

Now I am just getting a bunch of:
sox WARN wav: Premature EOF on .wav input file

And all the converted wavs are like 44bytes and don’t play anything. Which sounds like I am doing something wrong with sox and not a problem with the command… like maybe sox doesn’t support the output file being the same as the input file…

1 Like

not sure if I’m missing something, but could you just use a free program like Switch? I use it to convert files for various samplers all the time, it’s never given me any trouble. It even preserves the folder/file structure if you want it to.

Sounds like you picked one but I use To Audio Converter Lite for Mac. The lite version is a freebie and it keeps your file structure intact if you don’t feel like scripting anything up. Although learning Unix is fun so there is that.

I’m sorry. I’ve successfully done this for myself, but always with code, never with a one-line solution. I can’t come up with something simple that also copes with files and directories that have spaces in them. I should have listened to my own advice when I said

If I can come up with something I will post it here, but you might be better off looking something other than sox or ffmpeg for your set of samples.

You may be right.

1 Like

No problem, you have been super helpful, I am sure if I work more on it I can figure something out. I will keep everyone updated so in the future anyone who wants to do it will have an easier time.

Maybe I can just write a short script mostly using what you already gave me :slight_smile:

1 Like

Sox does not support the input and output files being the same iirc. Here’s something I wrote up a while ago to do the exact thing you want to do, but unfortunately it’s Windows only (Sox is called from Powershell). Still, you might find some useful info in there.

1 Like

Sox definitely does support input and output being the same, 100%.

Try this one, I just tested it myself:

for f in **/**; do if [ -d "$f" ]; then mkdir -p "FIN/$f"; fi; if [[ $f == *.wav ]]; then sox "$f" -b 16 "FIN/$f"; fi; done

You don’t have to make a copy of your library. It copies the directory structure into a folder named FIN (or whatever you choose). The sox command is on the penultimate line (bash is delineated by ;).

3 Likes

Freakin’ awesome, works great! I was slowly heading towards a solution but you saved me a ton of time. I did learn quite a bit about unix today though lol.

It looks like the sox conversion is giving me some clipping on a few samples, but I should be able to figure that out.

Thanks all of you, this forum rocks, I wish I could give you all more than just one heart lol!

4 Likes

No worries. Using the -G flag automatically adjusts gain to avoid clipping

1 Like

Great, the -G gets rid of most of the clipping. There are still a few files giving me a clipping error, but I am not sure it is worth tracking down which ones and trying to fix them (edit: adding the dither -s option eliminated the remaining clipping errors). I think @alspacka had the same problem.

So for anyone interested in doing the same thing as I did with sox in macOS the final terminal command I used was:

for f in **/**; do if [ -d "$f" ]; then mkdir -p "CONV/$f"; fi; if [[ $f == *.wav ]]; then sox -G "$f" -b 16 "CONV/$f" rate -v 48k dither -s; fi; done

it converts the audio to 16 bit (-b 16) and 48khz using the very high quality algorithm (rate -v 48k) and avoids clipping (-G). The dither -s (dither with shibata noise shaping) was recomended over the default for 16bit and it also got rid of any remaining clipping warnings thet -G did not eliminate.

The command stores the converted files in a folder called CONV and preserves the folder structure. If you want to do a different bit depth just change the number after the -b, and for a different sample rate change the 48k to whatever you want.

Whew, now I can get back to making music!

5 Likes