Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to cut audio files into small samples of even length. What I need is basically a batch processor for audio that does it and names the slices numerically. I'm using OSX and have found Audio Splitter. The shortest possible sample length with it is 1 second though, too long for my purpose. Anyone know an alternative?

share|improve this question
up vote 4 down vote accepted

An excellent sound converter SoX (also named 'the Swiss Army knife of audio manipulation') works on OS X, so I hope this script will help you:

#!/bin/sh

N=0
START=0
LENGTH=8000
COUNT=100
INPUT=input.mp3
OUTPUT=output-X.wav

for i in `jot ${COUNT}`; do
    echo "Trimming ${START}+${LENGTH}"
    sox ${INPUT} `echo ${OUTPUT} | sed s/X/${N}/` trim ${START}s ${LENGTH}s
    N=`expr ${N} + 1`
    START=`expr ${START} + ${LENGTH}`
done

All lengths are in samples (you can also specify time in 'HH:MM:SS.ddd' format, check SoX manpage); it reads INPUT file and produces files named output-0.wav, output-1.wav and so on.

share|improve this answer
    
okay this sounds great, exactly what i need. i've installed SoX, however when i execute run the script i get "line 15: seq: command not found" (i'm a complete command line noob) – Jakob Jan 31 '10 at 3:20
    
nvm, it works now. thanks a lot! – Jakob Jan 31 '10 at 3:55
1  
"seq" is GNU. For OSX or BSD you want "jot". e.g. for i in `jot ${COUNT}`; do – Richard Hoskins Jan 31 '10 at 4:49
    
Fixed the script. – whitequark Jan 31 '10 at 15:12

Here is my solution.

#!/bin/bash

INPUT=input.wav
OUTPUT=output-X.wav

N=0
START=0
LENGTH=0.05 # length in ms (to use samples you should add s suffix to commandline trim Xs Ys and s flag for soxi)
TOTAL_LENGTH=`soxi -D ${INPUT}`

while [ $(echo "$START < $TOTAL_LENGTH"|bc) -eq 1 ]; do
    echo "Trimming from ${START} of ${TOTAL_LENGTH}"
    sox ${INPUT} `echo ${OUTPUT} | sed s/X/${N}/` trim ${START} ${LENGTH}
    N=$(($N+1))
    START=`echo "$START + $LENGTH"|bc`
done

The difference is:

  • Script works with ms rather than with samples
  • The amount of output files is automatically detected by INPUT length
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .