File size: 6,758 Bytes
30ee64e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash

# Config file location
cfg_file=

effect="denoiser"
gpu="t4"
ulimit_val=0
sample_rate=16
output_sample_rate=48
batch_size=1
reset=
multi_input=""
frame_size=10;
enable_vad=
input_file=

args=()

declare -A gpu_map=( ["a100"]="sm_80"
                     ["a30"]="sm_80"
                     ["a2"]="sm_86"
                     ["a10"]="sm_86"
                     ["a16"]="sm_86"
                     ["a40"]="sm_86"
                     ["t4"]="sm_75"
                     ["v100"]="sm_70"
                    )

gpu_list=$(IFS=/; printf "%s" "${!gpu_map[*]}")

declare -A supported_effects=( ["denoiser"]="16 48"
                               ["dereverb"]="16 48"
                               ["dereverb_denoiser"]="16 48"
                               ["aec"]="16 48"
                               ["superres"]="8_to_16 16_to_48 8_to_48"
                             )

supported_effects_list=$(IFS=/; printf "%s" "${!supported_effects[*]}")

while [[ "$#" -gt 0 ]]; do
    args+=($1)
    case $1 in
        -e|--effect) effect="$2"; args+=($2); shift ;;
        -g|--gpu) gpu="$2"; args+=($2); shift ;;
        -u|--ulimit) ulimit_val="$2"; args+=($2); shift ;;
        -s|--sample_rate) sample_rate="$2"; args+=($2); shift ;;
        -o|--output_sample_rate) output_sample_rate="$2"; args+=($2); shift ;;
        -b|--batch_size) batch_size="$2"; args+=($2); shift ;;
        -c|--cfg-file) cfg_file="$2"; args+=($2); shift;;
        -r|--reset) multi_input="0,1,2,3,4" reset="reset 2 3 5";;
        -f|--frame_size) frame_size="$2"; args+=($2); shift;;
        -v|--enable_vad) enable_vad=1;;
        -i|--input_file) input_file="$2"; unset 'args[${#args[@]}-1]'; shift;;
        -h|--help) echo -e "-g, --gpu : $gpu_list [default=t4]"
                   echo -e "-e, --effect : effect to use: $supported_effects_list [default=denoiser]"
                   echo -e "-u, --ulimit : raise files open ulimit, this is used when running larger batch sizes that exceed the kernel maximum open files limit. Warning: Use this option with caution. [default=0(unusued)]"
                   echo -e "-s, --sample_rate : sample rate to use 8/16/48 (in kHz). [default=16]"
                   echo -e "-o, --output_sample_rate : output sample rate to use 16/48 (in kHz). [default=48]"
                   echo -e "-b, --batch_size : batch size to use. [default=1, max=1024]"
                   echo -e "-c, --cfg-file: configuration file to write to"
                   echo -e "-f, --frame_size: frame size to be used 10/20ms. [default=10]"
                   echo -e "-v, --enable_vad: Enable VAD. Refer to the Programming Guide for supported effects. [default=disabled]"
                   echo -e "-i, --input_file: file/folder to be used as input, instead of using sample input files (file must be in correct format)"
                   echo -e "-h, --help: print this help menu";exit;;
        *) echo "Unknown parameter passed: $1"; exit 1 ;;
    esac
    shift
done

# Verify if effect is supported
if [ "$effect" == "superres" ]; then
  rate_check="${sample_rate}_to_${output_sample_rate}"
else
  rate_check="$sample_rate"
fi

if [ -z "${supported_effects[$effect]:-}" ]; then
  echo "Invalid effect (got $effect, supported $supported_effects_list)"
  exit 1
fi

rate_supported="${supported_effects[$effect]}"
check_val="\b$rate_check\b"
if ! [[ "$rate_supported" =~ $check_val ]]; then
  if [ "$effect" == "superres" ]; then
    cv=${rate_supported// /, }
    echo "Sample rate combination '$sample_rate -> $output_sample_rate' is not supported for Superres (supported combinations: ${cv//_to_/ -> }"
  else
    echo "Sample rate '$rate_check' is not supported for this effect (supported: ${rate_supported// /, })"
  fi
  exit 1
fi

# Validate GPU
if [ -z "${gpu_map[$gpu]:-}" ]; then
  echo "Invalid GPU (got $gpu, supported $gpu_list)"
  exit 1
fi
arch="${gpu_map[$gpu]}"

model="../../models"
if [ "$effect" == "superres" ]; then
  model="${model}/${arch}/${effect}_${sample_rate}k_to_${output_sample_rate}k.trtpkg"
else
  model="${model}/${arch}/${effect}_${sample_rate}k.trtpkg"
fi

if [ ! -f "$model" ]; then
  echo "Model file not found (using '$model')"
  exit 1
fi

OUT_DIR="${effect}/${sample_rate}k"
mkdir -p "$OUT_DIR"
input_list=""
out_file=""
if [ ! -z "$input_file" ]; then
  if [[ ! (-f "$input_file" || -d "$input_file") ]]; then
    echo "Error: File/folder '$input_file' does not exist"
    exit 1
  fi
  if [ $(($batch_size)) -ne 1 ]; then
    echo "Error: Batch size must be 1 when input file/folder is specified (got $batch_size)"
    exit 1
  fi
  if [ -d "$input_file" ]; then
    if [ ! -z "$cfg_file" ]; then
      echo "Error: config file cannot be specified if input is a folder"
      exit 1
    fi
    # If folder, process file by file for all files in that folder
    for i in "${input_file}"/*.wav; do
      ./$0 "${args[@]}" -i "$i"
      if [ $? -ne 0 ]; then
        echo "Error processing file '$i'"
        exit 1
      fi
    done
    exit 0
  else
    input_list="input_wav_list $input_file"
    out_file="$OUT_DIR/$(basename $input_file)"
  fi
else
    if [ "$effect" == "aec" ]; then
        farend_wav=$(./list.sh ../input_files/aec/${sample_rate}k/farend ${batch_size} ${multi_input})
        if [ $? -ne 0 ]; then
            echo "Error generating input files list"
            echo "Command returned: $farend_wav"
            exit 1
        fi
        nearend_wav=${farend_wav//farend/nearend}
        input_list="input_wav_list $nearend_wav
input_farend_wav_list $farend_wav"
    else
        wav=$(./list.sh ../input_files/${effect}/${sample_rate}k/ ${batch_size} ${multi_input})
        if [ $? -ne 0 ]; then
            echo "Error generating input files list"
            echo "Command returned: $wav"
            exit 1
        fi
        input_list="input_wav_list $wav"
    fi

    for i in $(seq 1 $batch_size); do
        out_file="$out_file $OUT_DIR/${effect}_${sample_rate}k_$i.wav"
    done
fi

if [ -z "$cfg_file" ]; then
  cfg_file="/tmp/tmp_cfg.txt"
fi

echo "effect ${effect}
sample_rate ${sample_rate}000
model ${model}
real_time 0
intensity_ratio 1.0
$input_list
output_wav_list $out_file
frame_size ${frame_size}" > $cfg_file

if [ ! -z "$reset" ]; then
  if [ $batch_size -lt 5 ]; then
    echo "Batch size must be at least 5 for reset configs"
    exit 1
  fi
  echo "$reset" >> $cfg_file
fi

if [ ! -z "$enable_vad" ]; then
  echo "enable_vad 1" >> $cfg_file
fi

echo "Using the config file $cfg_file"

if [ "$ulimit_val" -ne "0" ]; then
   if [ "$ulimit_val" -le "1024" ]; then
      echo "You probably dont want to do this. ulimit is too low.";
      exit;
   fi
   echo "ulimit set to ${ulimit_val}";
   ulimit -n $ulimit_val
   exit;
fi

./effects_demo -c $cfg_file