LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-12-2011, 07:12 PM   #1
RedNeck-LQ
Member
 
Registered: Jan 2011
Posts: 83

Rep: Reputation: 11
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.

Last edited by RedNeck-LQ; 09-12-2011 at 07:19 PM.
 
Old 09-12-2011, 07:35 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
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)
 
Old 09-12-2011, 07:54 PM   #3
RedNeck-LQ
Member
 
Registered: Jan 2011
Posts: 83

Original Poster
Rep: Reputation: 11
Quote:
Originally Posted by macemoneta View Post
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

Last edited by RedNeck-LQ; 09-12-2011 at 07:55 PM.
 
Old 09-12-2011, 08:06 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
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.
 
Old 09-12-2011, 08:07 PM   #5
RedNeck-LQ
Member
 
Registered: Jan 2011
Posts: 83

Original Poster
Rep: Reputation: 11
I am going to continue experimenting. If sox can't handles variables, I'll have to use the normalize command instead.
 
Old 09-12-2011, 08:21 PM   #6
RedNeck-LQ
Member
 
Registered: Jan 2011
Posts: 83

Original Poster
Rep: Reputation: 11
Quote:
Originally Posted by macemoneta View Post
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

Last edited by RedNeck-LQ; 09-12-2011 at 08:27 PM.
 
Old 09-12-2011, 08:27 PM   #7
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
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.
 
Old 09-12-2011, 09:01 PM   #8
RedNeck-LQ
Member
 
Registered: Jan 2011
Posts: 83

Original Poster
Rep: Reputation: 11
Quote:
Originally Posted by macemoneta View Post
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

Last edited by RedNeck-LQ; 09-12-2011 at 09:12 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripts Variables Test Hi_This_is_Dev Linux - General 5 08-28-2010 09:21 PM
[SOLVED] Variables between shell scripts Potatos Linux - Newbie 1 05-20-2010 10:32 AM
shell command logging for SOX compliance linuxguy99 Red Hat 1 05-03-2007 01:01 PM
about shell scripts global variables nesta Programming 2 01-05-2007 01:32 AM
Passing variables between shell scripts big_ginge21 Programming 3 09-26-2006 01:10 PM


All times are GMT -5. The time now is 03:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration