Linux‎ > ‎

sox - Sound Exchange

This will record as soon as non-silence is detected (with a 2% volume threshold). The recording will cease when silence is detected for 2 seconds or more.

rec recording.wav silence 1 5 2% 1 0:00:02 2%

Note that the program terminates when silence is detected, so if you wanted the program to listen for a second chunk of audio and continue the recording, you would have to call rec from a loop. This is fine, except that it will truncate the file it records to if you use the same file the next time.

One solution is to create a different file for each recording. This might be exactly what you want. If so, it's pretty easy to write a little script to do this. I added in recording for only an hour too, although it will continue to record if there is no silence after the last rec instance is started even if that goes over an hour.

#!/bin/bash

main () {
        # handle control-c better than just letting sox exit current rec.
        trap sighandler INT

        if [ $? -ne 0 ]; then
                usage 1
        fi

        if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
                usage 0
        fi

        n=1
        while [ $(( $SECONDS / 3600 )) -lt $1 ]; do
                file=recording_$(printf "%03d" $n).wav
                rec $file silence 1 5 2% 1 0:00:02 2%
                let n+=1
        done
}

usage () {
        cat << EOD
Usage:
  $0 hours

where hours is the time to record
EOD
        exit ${1:-0}
}

sighandler () {
        exit "That's all folks."
        exit 2
}

main "$@"

Repository

Git repository:
 git clone git://git.code.sf.net/p/sox/code sox

Modifications I want to make
  1. New file for each sample.
  2. File name indicting date and time.
  3. Add date & time tags to mettadata in recording
  4. auto erase the oldest file as space becomes tight





Comments