Question

I've written a batch program in windows command-line to take a stereo wav file and split the right and left channel into separate FLAC files using sox v.14.4.0 audio program. Like so..

for /r %%n in (*.wav) do (
 C:\sox\sox.exe %%n -c 1 %%n.left.flac remix 1
 C:\sox\sox.exe %%n -c 2 %%n.right.flac remix 2)

However, this takes a bit longer than I'd like. I looked into this more and it appears that I can use SOX_OPTS and the --multi-thread option along with --buffer in order to perform two sox operations at the same time.

The Sox documentation states this:

The SOX_OPTS environment variable can be used to provide alternative default values for SoX’s global options. For example:

SOX_OPTS="−−buffer 20000 −−play−rate−arg −hs −−temp /mnt/temp"

Note that the way to set environment variables varies from system to system. Here are some examples: MS-DOS/MS-Windows:

set SOX_OPTS=−V −−no-clobber −−buffer BYTES, −−input−buffer BYTES

Set the size in bytes of the buffers used for processing audio (default 8192).
−−buffer applies to input, effects, and output processing;
−−input−buffer applies only to input processing (for which it overrides −−buffer if both are given).
Be aware that large values for −−buffer will cause SoX to be become slow to respond to requests to terminate or to skip the current input file.

Two questions.

  1. Is it possible to do split 1 input file to 2 output files simultaneously using --multi-thread as explained above or another way?
  2. If so, how? (note: I haven't been able to figure out the correct syntax to get SOX_OPTS from the documentation above to work on it's own never mind in a batch file)

Also: I've found some other stackoverflow questions similar to this but always using java, php, linux, etc. Unfortunately, I don't have any knowledge of those programs. Please be sympathetic of my programming ignorance.

Was it helpful?

Solution

No idea without getting more familiar than I'd prefer with your SoX...

However, I'd try this:

As a test, use a single .wav file

for /r %%n in (justone.wav) do (
 START "Left" C:\sox\sox.exe %%n -c 1 %%n.left.flac remix 1
 START "Right" C:\sox\sox.exe %%n -c 2 %%n.right.flac remix 2)

And perhaps it'll run two instances simultaneously on a multi-processor machine. (Window title is "Left" or "Right")

Now - if this works, then try

@ECHO OFF
SETLOCAL
for /r %%n in (*.wav) do CALL :runsox "%%n"
GOTO :eof

:runsox
CALL :wait
START "%~1.left" C:\sox\sox.exe %~1 -c 1 %~1.left.flac remix 1
CALL :wait
START "%~1.right" C:\sox\sox.exe %~1 -c 2 %~1.right.flac remix 2
GOTO :eof

:wait
FOR /f %%c IN ('tasklist^|find /i /c "sox.exe"') DO SET running=%%c
IF %running% GEQ 6 timeout /t 5 >nul&GOTO wait
GOTO :eof

What this will do is execute the :runsox procedure for each .wav encountered, passing the .wav` name.

The runsox procedure will call the wait procedure - which I'll come to. When the WAIT is finished, it'll put on the left sox, then wait again and put on the right, then return for the next filename until all's done. The %~1 in the names means 'the first parameter given to this routine, minus any enclosing quotes` - and that's the filename (%%n) provided by the CALL.

the wait procedure finds out how many SOX.EXE instances are running by passing a list of tasks from tasklist through a find filter to find the string sox.exe /i means regardless of case and /c means output a count.

Running therefore acquires a count of the instances of SOX in progress. If that is greater than or equal to 6 (an arbitrary number - choose it to suit yourself. Best to leave one or two processors for other tasks, so 6 id good for an 8-processor machine) then a timeout of 5 seconds is executed and after 5 seconds the tasklist is examined again. When there are fewer than 6 instances, the wait procedure exits and the next task is started.

So - the WAIT procedure waits until there are fewer than 6 SOX instances.

Change the 6 to suit your machinery and the 5 your aesthetics.

Edit: remove legacy closing-parenthesis

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top