LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can the sox command handle variables in shell scripts (https://www.linuxquestions.org/questions/programming-9/can-the-sox-command-handle-variables-in-shell-scripts-902660/)

RedNeck-LQ 09-12-2011 06:12 PM

Can the sox command handle variables in shell scripts
 
Hi,

I have a simple shell script using the sox command to set a maximum gain value to the audio without clipping or distortion.

For example,I type from a terminal

sox audiofile.wav louder.wav stat -v

The value return is 1.059, which means I can safely increase the volume by that value. The louder.wav can be any name. Sox needs a temporary file to write to.

To commit the change

sox -v 1.059 audiofile.wav tmp/audiofile.wav

Note: The tmp/audiofile.wav is the louder version


Anyway, I put this in a shell script like so

Code:

#!/bin/bash

for i in *.wav

do
# this line gets a stat value for each wav file and stores it in the stat variable
stat=$(sox "$i" louder.wav stat -v)
# this line commits the chances and writes the louder versions to tmp with the same filename..
sox -v $stat "$i" tmp/"$i"
done

I gave the script a test run in a directory and I get this error.

1.059
sox FAIL sox: Volume value `' is not a number
Probably text, not sound

It seems like sox doesn't understand the $stat variable I have set. I even tried sox "$stat" to no effect.

I have many experience writing shells scripts with more complex variables, pattern substitution and expressions and command line programs like ffmpeg, mplayer. mencoder and others understand them.

Does anyone has any ideas or this a bug in sox?

PS: In case you're thinking of telling me to use the normalize command, I did so in the pass. Normalize is a good tool, but sometimes it distorts the audio.

macemoneta 09-12-2011 06:35 PM

The problem is that sox writes the gain value to stderr, not stdout. Your script is capturing a null, which is why it's complaining about an invalid value. Use:
Code:

stat=$(sox "$i" louder.wav stat -v 2>&1)

RedNeck-LQ 09-12-2011 06:54 PM

Quote:

Originally Posted by macemoneta (Post 4470104)
The problem is that sox writes the gain value to stderr, not stdout. Your script is capturing a null, which is why it's complaining about an invalid value. Use:
Code:

stat=$(sox "$i" louder.wav stat -v 2>&1)

Thanks for the prompt reply.

I added the 2>&1 as you said and got this error

sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN sox: `Internet radio show.wav' balancing clipped 1 samples; decrease volume?
sox FAIL sox: Volume value `sox' is not a number
sox FAIL sox: Volume value `Samples' is not a number

Very weird...

I am using sox v14.3.0

macemoneta 09-12-2011 07:06 PM

Those errors are from the following sox invocation. You may want to echo the stat value and file being processed so that you can see which of the files is having a problem.

RedNeck-LQ 09-12-2011 07:07 PM

I am going to continue experimenting. If sox can't handles variables, I'll have to use the normalize command instead.

RedNeck-LQ 09-12-2011 07:21 PM

Quote:

Originally Posted by macemoneta (Post 4470119)
Those errors are from the following sox invocation. You may want to echo the stat value and file being processed so that you can see which of the files is having a problem.

The wav files are OK, because I play them and there are no errors.

In my first post I did the sox gain manually by typing it in the terminal. like so.
sox "Internet radio show.wav" louder.wav stat -v
1.059

And got that value 1.059, next I applied the gain by tying

sox -v 1.059 "Internet radio show.wav" tmp/"Internet radio show.wav"

And it works. Only when I type it directly in the terminal.

But it does not work in a shell script. Which I don't understand way

macemoneta 09-12-2011 07:27 PM

As I said, you won't know until you echo the stat value and file being processed so that you can see which of the files is having a problem.

RedNeck-LQ 09-12-2011 08:01 PM

Quote:

Originally Posted by macemoneta (Post 4470133)
As I said, you won't know until you echo the stat value and file being processed so that you can see which of the files is having a problem.

I added the echo statement before the the sox command to see what it is doing on each file like so
echo sox -v $stat "$i" tmp/"$i"

The first file "Internet Radio Show.wav" went without any problems.
However, the file louder.wav was the culprit. This is the temp file sox uses to write to.

So, instead of having louder.wav in the same directory with the wav files. I tell sox to write louder.wav elsewhere like so

stat=$(sox "$i" ../louder.wav stat -v 2>&1)

I want to thank you for those helpful suggestions. A reputation will be added to you.
:)

BTW, I saw this sox usage from this website. Linux Cookbook
http://dsl.org/cookbook/cookbook_29.html

Scroll down to Changing the Amplitude of a Sound File


All times are GMT -5. The time now is 12:14 AM.