Yuri Burkov
Yuri Burkov

Reputation: 141

How to Split Audio using Sox Depending on Input Variables

I want to split a stereo audio into two mono files. To run my program I am passing three arguments. ./program.sh /folder_path KHZ_TYPE C12_or_L2R and depending on the variables I want to split the audio file into two mono files and name them either output_ch1.wav and output_ch2.wav or output_left.wav and output_right.wav. Below is my code.

But I keep getting syntax error near unexpected token `then'. Also How do I save the result files into a directory called output_files?

DIR=$1
KHZ=$2
TYPE=$3
CMD_1=""
CMD_2=""

for filename in $DIR/*; do
    filename="${filename##*/}"
    echo $filename
    fname="${filename// /_}"
    fname="${fname%.*}"

    if[$KHZ = "8KHZ"] && [$TYPE = "L2R"]; then
        output_left = "${fname}_left.wav"
        output_right = "${fname}_right.wav"
        $CMD_1 = "sox -r 8000 -b 16 $filename $output_left remix 1"
        $CMD_2 = "sox -r 8000 -b 16 $filename $output_right remix 2"
    elif[$KHZ = "8KHZ"] && [$TYPE = "C12"]; then
        output_ch1 = "${fname}_ch1.wav"
        output_ch2 = "${fname}_ch2.wav"
        $CMD_1 = "sox -r 8000 -b 16 $filename $output_ch1 remix 1"
        $CMD_2 = "sox -r 8000 -b 16 $filename $output_ch2 remix 2"
    elif[$KHZ = "16KHZ"] && [$TYPE = "C12"]; then
        output_ch1 = "${fname}_ch1.wav"
        output_ch2 = "${fname}_ch2.wav"
        $CMD_1 = "sox -r 16000 -b 16 $filename $output_ch1 remix 1"
        $CMD_2 = "sox -r 16000 -b 16 $filename $output_ch2 remix 2"
    elif[$KHZ = "16KHZ"] && [$TYPE = "L2R"]; then
        output_left = "${fname}_left.wav"
        output_right = "${fname}_right.wav"
        $CMD_1 = "sox -r 16000 -b 16 $filename $output_left remix 1"
        $CMD_2 = "sox -r 16000 -b 16 $filename $output_right remix 2"
done

Upvotes: 0

Views: 157

Answers (1)

user2849202
user2849202

Reputation:

You were close, but do inspect the differences, esp. those in whitespace, double quoting, and $ removed in LHS variables.

Here's a simpler version. Please double check that I'm doing the correct thing as far as the rates go:

#!/bin/bash

DIR="$1"
KHZ="$2"
TYPE="$3"
CMD_1=""
CMD_2=""

for filename in "$DIR"/*; do
    filename="${filename##*/}"
    echo "filename=$filename"
    fname="${filename// /_}"
    fname="${fname%.*}"
    echo "fname=$fname"

    output_left="${fname}_left.wav"
    output_right="${fname}_right.wav"

    if [ "$KHZ" = "8KHZ" ] && [ "$TYPE" = "L2R" ]; then
        rate="8000"
    elif [ "$KHZ" = "8KHZ" ] && [ "$TYPE" = "C12" ]; then
        rate=8000
    elif [ "$KHZ" = "16KHZ" ] && [ "$TYPE" = "C12" ]; then
        rate=16000
    elif [ "$KHZ" = "16KHZ" ] && [ "$TYPE" = "L2R" ]; then
        rate=16000
    fi

    echo "rate=$rate"
    CMD_1="sox -r $rate -b 16 $filename $output_left remix 1"
    CMD_2="sox -r $rate -b 16 $filename $output_right remix 2"

    # using parenthesis means: run in subshell
    # when subshell exits, we're back in the original 
    # directory, i.e. no need to cd back to it :-)
    (
        cd "$DIR"
        $CMD_1
        $CMD_2
    )

done

Upvotes: 1

Related Questions