summaryrefslogtreecommitdiff
path: root/doit
blob: 5822ca791aee8ec5f970e7108617e68ff67364f2 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash

# doit [ -f ] [ -ng ] input_1 input_2 ... input_n points output


eval set -- "$(getopt -o hf1ni:p:o:s: --long help,force,noGain,dummy,input:,points:,output:,sampleRate: -n "$(basename "$0")" -- "$@")"

force=false
noGain=false
dummy=false

while true; do
  case "$1" in
    -h|--help)
      echo "$0"' [ -f | --force ] [ -1 | --noGain ] [ -n | --dummy ] { ( -i | --input= ) input$i.flac } ( -o | --output= ) output.flac ( -p | --points= ) points'
      echo ' Mischen und Schneiden:'
      echo '  -f | --force:        output.flac ggf. überschreiben'
      echo '  -1 | --noGain:       Lautstärke nicht normalisieren'
      echo '  -n | --dummy:        nur auszuführenden Befehl anzeigen und nichts tun'
      echo '  -s | --sampleRate=:  alternative Samplerate zum Abspeichern'
      echo '  -i | --input=:       Quelle'
      echo '  -o | --output=:      Ziel'
      echo '  -p | --points=:      Punktedatei'
      echo "$0"' [ -h | --help ]'
      echo ' Hilfe anzeigen'
      exit 0
      ;;
    -f|--force)
      force=true
      ;;
    -1|--noGain)
      noGain=true
      ;;
    -n|--dummy)
      dummy=true
      ;;
    -i|--input)
      shift
      inputs[${#inputs[@]}]="$1"
      ;;
    -o|--output)
      shift
      [ -n "${output}" ] && echo 'Ich kann nur eine Output-Datei-Option verstehen.' && exit 1
      output="$1"
      ;;
    -p|--points)
      shift
      [ -n "${points}" ] && echo 'Ich kann nur eine Point-Datei-Option verstehen.' && exit 1
      points="$1"
      ;;
    -s|--sampleRate)
      shift
      [ -n "${outRate}" ] && echo 'Ich kann nur eine Samplerate-Option verstehen.' && exit 1
      outRate="$1"
      ;;
    --)
      shift
      [ $# -gt 0 ] && echo 'Unbekannte Parameter: '"$#" && exit 1
      break
      ;;
    *)
      echo 'Hups, das sollte nicht passieren könne, '"$1"' kenne ich doch nicht ...'
      exit -1
      ;;
  esac
  shift
done

[ -z "${output}" ] && echo 'Output-Datei fehlt!' && exit 1
[ -z "${points}" ] && echo 'Points-Datei fehlt!' && exit 1
[ "${#inputs[@]}" -eq 0 ] && echo 'Input-Datei fehlt!' && exit 1

dir="$(dirname $0)"

[ ! -e "${points}" ] && echo "Die Schnittpunktedatei ${points} existiert nicht!" && exit 1
for inp in "${inputs[@]}"
do
  [ ! -e "${inp}" ] && echo "Die Inputdatei ${inp} existiert nicht!" && exit 1
  [ $(echo "${inp}" | grep -c " ") -gt 0 ] && echo "Bitte keine Leerzeichen im input-Dateinamen!" && exit 1
done

[ $(echo "${output}" | grep -c " ") -gt 0 ] && echo "Bitte keine Leerzeichen im output-Dateinamen!" && exit 1
[ $(echo "${points}" | grep -c " ") -gt 0 ] && echo "Bitte keine Leerzeichen im points-Dateinamen!" && exit 1

if [ -e "${output}" ]
then
  if ${force}
  then
    ${dummy} || rm "${output}"
  else
    echo "Die Ausgabedatei ${output} existiert bereits!" && exit 1
  fi
fi
odn="$(dirname "${output}")"
obn="$(basename "${output}")"
if [ $(ls -1 "${odn}" | grep "$(echo "${obn}" | sed "s/%[0-9]\?n/.*/g")" | wc -l) -gt 0 ]
then
  if ${force}
  then
    ${dummy} || rm ${odn}/$(echo "${obn}" | sed "s/%[0-9]\?n/*/g")
  else
    echo "Die Ausgabedatei ${output} existiert bereits!" && exit 1
  fi
fi

samplerate=$(soxi -r "${inputs[0]}")
[ -z ${outRate} ] && outRate=${samplerate}
i=-1
abstime=0
while read -r in
do
  line="$(echo "${in}" | sed "s/^ *//" | sed "s/ *\(\# .*\)\{0,1\}$//")"
  [ "${line}" == "" ] && continue
  if [ $i -eq -1 ]
  then
    firstline="$(echo "${line}" | sed "s/ *\#.*$//")"
    remixcmd="$(echo "${in}" | sed "s/^[^\#]*\#//" | sed "s/ *\(\# .*\)\{0,1\}$//")"
    i=0
    continue
  fi
  if [ $(echo "${line}" | grep -c "^\#") -eq 1 ]
  then
    trackmods[${i}]=" $(echo "${line}" | sed "s/^.*\#//")"
    timecorrections[${i}]=0
    while [ $(echo "${trackmods[${i}]}" | grep -c " trimsplice") -eq 1 ]
    do
      tmp="$(echo "${trackmods[${i}]}" | sed "s/X/x/g" | sed "s/trimsplice/X/g" | sed "s/^[^X]* X  *//" | sed "s/  / /g")"
      end=$(${dir}/timetosample ${samplerate} $(echo "${tmp}" | awk '{print $1}'))
      if [ ! "${doverlaps[${i}]}" == "" ]
      then
        end=$[${end}+${doverlaps[${i}]}]
      fi
      discard=$(${dir}/timetosample ${samplerate} $(echo "${tmp}" | awk '{print $2}'))
      excess=$(${dir}/timetosample ${samplerate} $(echo "${tmp}" | awk '{print $3}'))
      leeway=$(${dir}/timetosample ${samplerate} $(echo "${tmp}" | awk '{print $4}'))
      trackmods[${i}]="$(echo "${trackmods[${i}]}" | sed "s/ *trimsplice  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*/ trim 0s ${end}s ${discard}s splice -t ${end}s,${excess}s,${leeway}s/")"
#      trackmods[${i}]="$(echo "${trackmods[${i}]}" | sed "s/ *trimsplice  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*/ trim 0s ${end}s ${discard}s/")"
      timecorrections[${i}]=$[${timecorrections[${i}]}+${discard}+2*${excess}]
    done
    continue
  fi
  i=$[${i}+1]
  tmp=$(${dir}/timetosample ${samplerate} $(echo "${line}" | sed "s/ *\(\#.*$\)\{0,1\}$//"))
  times[${i}]=$[${tmp}-${abstime}]
  abstime=${tmp}

  newFile[${i}]=true
  tmp="$(echo "${line}" | sed "s/^[^\#]*#\?//" | sed "s/\s\./ 0./g")"
  while [ -n "${tmp}" ]
  do
    if [[ "$tmp" = "overlap "* ]]
    then
      overlaps[${i}]=$[$(${dir}/timetosample ${samplerate} $(echo "${tmp}" | sed "s/^overlap \(\S\+\)\(\s.*\)\?$/\1/"))/2]
      doverlaps[${i}]=$[2*${overlaps[${i}]}]
      tmp="$(echo "${tmp}" | sed "s/^\S\+\s\+\S\+\s*//")"
      continue
    fi
    if [[ "$tmp" = "noNewFile"* ]]
    then
      newFile[${i}]=false
      tmp="$(echo "${tmp}" | sed "s/^\S\+\s*//")"
      continue
    fi
    echo "'${tmp}' ist unverständlich zwischen zwei Stücken"
    exit 1
  done
done < "${points}"

#for (( i=0; i<=${#times[@]}; i++ ))
#do
#  echo "${i} ; ${trackmods[${i}]} ; ${times[${i}]} ; ${overlaps[${i}]} ; ${doverlaps[${i}]} ; ${timecorrections[${i}]}"
#done
#exit 0

repcmd=""
abstime=0
for (( i=1; i<=${#times[@]}; i++ ))
do
  abstime=$[${abstime}+${times[${i}]}]
  [ "${doverlaps[${i}]}" == "" ] && continue
  repcmd="${repcmd} trim 0s $[${abstime}-${doverlaps[${i}]}]s : trim 0s ${doverlaps[${i}]}s repeat :"
  abstime=0
done

trackmodcmd=""
abstime=0
for (( i=0; i<=${#times[@]}; i++ ))
do
  if [ ${i} -gt 0 ]
  then
    [ "${timecorrections[$[${i}-1]]}" == "" ] || times[${i}]=$[${times[${i}]}-${timecorrections[$[${i}-1]]}]
  fi
  [ "${times[$[${i}+1]]}" == "" ] || abstime=$[${abstime}+${times[$[${i}+1]]}]
  [ "${doverlaps[${i}]}" == "" ] || abstime=$[${abstime}+${doverlaps[${i}]}]
  [ "${trackmods[${i}]}" == "${trackmods[$[${i}+1]]}" ] && continue
  if [ ${i} -eq ${#times[@]} ]
  then
    trackmodcmd="${trackmodcmd}${trackmods[${i}]} "
  else
    trackmodcmd="${trackmodcmd}trim 0s ${abstime}s${trackmods[${i}]} : "
  fi
  abstime=0
done

splicecmd=""
abstime=0
for (( i=0; i<=${#times[@]}; i++ ))
do
  [ "${times[${i}]}" == "" ] || abstime=$[${abstime}+${times[${i}]}]
  [ "${overlaps[${i}]}" == "" ] && continue
  splicecmd="${splicecmd} ${abstime}s,${overlaps[${i}]}s,0s"
  abstime=$[${abstime}+${doverlaps[${i}]}]
done
[ "${splicecmd}" == "" ] || splicecmd="splice -t${splicecmd}"

trimcmd=""
for (( i=1; i<=${#times[@]}; i++ ))
do
  if [ ${outRate} -eq ${samplerate} ]
  then
    trimcmd="${trimcmd}trim 0s =${times[${i}]}s : "
  else
    trimcmd="${trimcmd}trim 0s =$[$[${times[${i}]}*${outRate}+${samplerate}/2]/${samplerate}]s : "
  fi
  ${newFile[${i}]} && trimcmd="${trimcmd}newfile : "
done

if [ ! ${outRate} -eq ${samplerate} ]
then
  remixcmd="${remixcmd} rate -v ${outRate}"
fi

if ! ${noGain}
then
  firstline="${firstline} gain -b"
  remixcmd="${remixcmd} gain -n"
fi

echo sox -M "${inputs[@]}" -p trim ${firstline}
echo sox -t sox - -p ${repcmd}
echo sox -t sox - -p ${trackmodcmd}
echo sox -t sox - -p ${splicecmd} ${remixcmd}
echo sox -t sox - ${output} ${trimcmd}

if ! ${dummy}
then
  sox -M "${inputs[@]}" -p trim ${firstline}| \
      sox -t sox - -p ${repcmd} | \
      sox -t sox - -p ${trackmodcmd} | \
      sox -t sox - -p ${splicecmd} ${remixcmd} | \
      sox -t sox - ${output} ${trimcmd}
fi

#if $noGain
#then
#  echo sox -M "${@:1:$[$#-2]}" -p trim ${firstline}
#  echo sox -t sox - -p ${repcmd}
#  echo sox -t sox - -p ${trackmodcmd}
#  echo sox -t sox - -p ${splicecmd} ${remixcmd}
#  echo sox -t sox - ${output} ${trimcmd}
#
#  sox -M "${@:1:$[$#-2]}" -p trim ${firstline}| \
#      sox -t sox - -p ${repcmd} | \
#      sox -t sox - -p ${trackmodcmd} | \
#      sox -t sox - -p ${splicecmd} ${remixcmd} | \
#      sox -t sox - ${output} ${trimcmd}
#else
#  echo sox -M "${@:1:$[$#-2]}" -p trim ${firstline} gain -b
#  echo sox -t sox - -p ${repcmd}
#  echo sox -t sox - -p ${trackmodcmd}
#  echo sox -t sox - -p ${splicecmd} ${remixcmd}
#  echo sox -t sox - -p gain -n
#  echo sox -t sox - ${output} ${trimcmd}
#
#  sox -M "${@:1:$[$#-2]}" -p trim ${firstline} gain -b | \
#      sox -t sox - -p ${repcmd} | \
#      sox -t sox - -p ${trackmodcmd} | \
#      sox -t sox - -p ${splicecmd} ${remixcmd} | \
#      sox -t sox - -p gain -n | \
#      sox -t sox - ${output} ${trimcmd}
#fi