Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm writing a script that uses sox to record me talking. Now I need sox to wait until it detects sound before it begins recording, and I do have that figured out. But I also need sox to exit once there has been silence for at least 3 seconds. As it is now, I have to manually kill sox once I finish talking, otherwise sox just waits again until I talk some more, appending to the output file (That's not what I want). Here is the command for recording I am using now:

rec /tmp/recording.flac rate 32k silence 1 0.1 3% -1 3.0 3%

Again, just to be clear, Sox should wait until I start talking, and then record until I stop talking, then the sox program should quit.

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Remove the negative sign from your original command:

rec /tmp/recording.flac rate 32k silence 1 0.1 3% 1 3.0 3%

When the "below count" is negative, the silence command will trim all silences from the middle of the file. When it's positive, it trims silence from the end of the file.

share|improve this answer
    
Exactly what I needed! Thanks a lot! –  Isaac Dec 12 '12 at 18:55
    
That is a much simpler solution than the one I came up with, LOL! –  Isaac Dec 12 '12 at 18:57
add comment

I found a kind of shaky way that sort of solves this problem.

I noticed that when silence is detected, sox naturally won't append anything more to the file until it hears more sound. So I decided I could try and take advantage of this fact.

First of all, I start up sox's sound recorder in the background:

rec /tmp/recording.flac rate 32k silence 1 0.1 3% -1 3.0 3% &

Next I get the PID of the recording process with: p=$!

Then I give the recording process a second to finish starting up, and then wait until the file starts to grow.

sleep 1
until [ "$var1" != "$var2" ]; do
    var1=`du "/tmp/recording.flac"`
    sleep 1
    var2=`du "/tmp/recording.flac"`
done

After this loop breaks, that means that sound has been detected. So now I create another loop to wait until data stops being appended to the file (This will happen once sox detects silence again).

echo "Sound Detected"
until [ "$var1" == "$var2" ]; do
    var1=`du "/tmp/recording.flac"`
    sleep 0.5
    var2=`du "/tmp/recording.flac"`
done

Now I just need to quit rec:

echo "Silence Detected"
kill $p

And that's it. Here is the completed script:

rec /tmp/recording.flac rate 32k silence 1 0.1 3% -1 3.0 3% &
p=$!
sleep 1
until [ "$var1" != "$var2" ]; do
    var1=`du "/tmp/recording.flac"`
    sleep 1
    var2=`du "/tmp/recording.flac"`
done
echo "Sound Detected"
until [ "$var1" == "$var2" ]; do
    var1=`du "/tmp/recording.flac"`
    sleep 0.5
    var2=`du "/tmp/recording.flac"`
done
echo "Silence Detected"
kill $p

Now I would prefer a better, more stable way to do this, but this will have to work for now.

share|improve this answer
add comment

This listens forever, records only the last sounds until it hears one second of silence, then writes the file, timestamps it, and restarts:

#!/bin/bash
/usr/bin/sox -t alsa default /home/default/working/recording.wav silence 1 0.1 5% 1 1.0 5%
DATE=$(date +%Y%m%d%H%M%S)
mv /home/default/working/recording.wav /home/default/waiting/$DATE.recording.wav
/home/default/startrecord.sh
share|improve this answer
    
Bash has while true loops –  Michael Mrozek Jul 2 '13 at 18:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.