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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
#!/bin/bash
set -e
verwendung() {
>&2 echo 'Verwendung: '"$(basename "${me}")"' [OPTIONEN]'
>&2 echo ''
>&2 echo ' -1, --noGain Lautstärke nicht normalisieren'
>&2 echo ' -a, --ausfuehrlich ausführliche Zwischenausgaben machen'
>&2 echo ' -c, --cd Dateien passend für CD-Tracks erzeugen'
>&2 echo ' -f, --force output.flac ggf. überschreiben'
>&2 echo ' -i, --input=inputDatei Quelle'
>&2 echo ' -n, --dummy nur auszuführenden Befehl anzeigen und nichts tun'
>&2 echo ' -o, --output=outputDatei Zieldatei'
>&2 echo ' -p, --points=pointsDatei Punktedatei'
>&2 echo ' -r, --reject=Kanal,Frequenz,Breite,Harmonische'
>&2 echo ' blocke in Kanal Frequenz, Frequenz*2 ...'
>&2 echo ' Frequenz*Harmonische, jeweils mit Breite (-3dB)'
>&2 echo ' -s, --sampleRate=Rate alternative Samplerate zum Abspeichern'
>&2 echo ' -t, --temp=tempDir alternatives temporäres Verzeichnis für sox'
>&2 echo \
'#HELPTEXT# #'
[ -z "$1" ] && exit 1 || exit $1
}
timetosample() {
local zeit
case "$2" in
*:*:*)
zeit="$2"
;;
*:*)
zeit="0:$2"
;;
*)
zeit="0:0:0$2"
;;
esac
bc <<< "( 0.5 + $1 * $(date "+%s.%N" -ud "1970-01-01 ${zeit}") ) / 1"
}
me="$(readlink -f "$0")"
eval set -- "$(
getopt -o 1acfi:no:p:r:s:t: \
--long noGain \
--long ausfuehrlich \
--long cd \
--long force \
--long help \
--long input: \
--long dummy \
--long output: \
--long points: \
--long reject: \
--long sampleRate: \
--long tempDir: \
--long version \
-n "$(basename "$0")" -- "$@" || \
echo verwendung
)"
noGain=false
ausfuehrlich=false
force=false
dummy=false
cdAusrichten=false
while true; do
case "$1" in
-1|--noGain)
noGain=true
;;
-a|--ausfuehrlich)
ausfuehrlich=true
;;
-c|--cd)
[ -n "${outRate}" ] && echo 'Ich kann nur eine Samplerate-Option verstehen.' && verwendung
outRate="44100"
cdAusrichten=true
;;
-f|--force)
force=true
;;
--help)
verwendung 0
;;
-i|--input)
shift
inputs[${#inputs[@]}]="$1"
;;
-n|--dummy)
dummy=true
;;
-o|--output)
shift
[ -n "${output}" ] && echo 'Ich kann nur eine Output-Datei-Option verstehen.' && verwendung
output="$1"
;;
-p|--points)
shift
[ -n "${points}" ] && echo 'Ich kann nur eine Point-Datei-Option verstehen.' && verwendung
points="$1"
;;
-r|--reject)
shift
tmp="$1"
kanal="${tmp%%,*}"
tmp="${tmp#*,}"
freq="${tmp%%,*}"
tmp="${tmp#*,}"
breite="${tmp%%,*}"
maxn="${tmp#*,}"
for ((i=1; i<=$maxn; i++))
do
rejects[${kanal}]="${rejects[${kanal}]} bandreject $[${i}*${freq}] ${breite}"
done
;;
-s|--sampleRate)
shift
[ -n "${outRate}" ] && echo 'Ich kann nur eine Samplerate-Option verstehen.' && verwendung
outRate="$1"
;;
-t|--tempDir)
shift
[ -n "${tempDir}" ] && echo 'Ich kann nur ein temporäres Verzeichnis verstehen.' && verwendung
tempDir="$1"
;;
--version)
>&2 echo '#VERSION#'
exit 0
;;
--)
shift
[ $# -gt 0 ] && echo 'Unbekannte Parameter: '"$#" && verwendung
break
;;
*)
>&2 echo 'Hups, das sollte nicht passieren könne, '"$1"' kenne ich doch nicht ...'
exit -1
;;
esac
shift
done
[ -z "${output}" ] && >&2 echo 'Output-Datei fehlt!' && verwendung
[ -z "${points}" ] && >&2 echo 'Points-Datei fehlt!' && verwendung
[ "${#inputs[@]}" -eq 0 ] && >&2 echo 'Input-Datei fehlt!' && verwendung
if ${cdAusrichten} && [[ ! "${output}" == *".wav" ]]
then
>&2 echo 'CD-Ausgabe sollte nur nach wav geschehen.'
exit 1
fi
[ ! -e "${points}" ] && >&2 echo "Die Schnittpunktedatei '${points}' existiert nicht!" && exit 1
for inp in "${inputs[@]}"
do
[ ! -e "${inp}" ] && >&2 echo "Die Inputdatei '${inp}' existiert nicht!" && verwendung
echo "${inp}" | grep -q "\s" && \
>&2 echo "Bitte keine Leerzeichen im input-Dateinamen!" && \
exit 1
done
echo "${output}" | grep -q "\s" && \
>&2 echo "Bitte keine Leerzeichen im output-Dateinamen!" && \
exit 1
echo "${points}" | grep -q "\s" && \
>&2 echo "Bitte keine Leerzeichen im points-Dateinamen!" && \
exit 1
[ -n "${tempDir}" ] && tempDir="--temp ${tempDir}"
if [ -e "${output}" ]
then
if ${force}
then
${dummy} || rm "${output}"
else
>&2 echo "Die Ausgabedatei '${output}' existiert bereits!" && exit 1
fi
fi
odn="$(dirname "$(readlink -f "${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
>&2 echo "Die Ausgabedatei '${output}' existiert bereits!" && exit 1
fi
fi
inRate="$(soxi -r "${inputs[0]}")"
[ -z "${outRate}" ] && outRate="${inRate}"
i=-1
absInTime=0
absOutTime=0
while read -r in
do
line="$(
echo "${in}" | \
sed 's|\s\+| |g' | \
sed 's/^ *//' | \
sed 's/ *\(\#\( .*\)\?\)\?$//'
)"
[ -z "${line}" ] && continue
teilEins="$(echo "${line}" | sed 's| *\(#.*\)\?$||')"
teilZwei="$(echo "${line}" | sed 's|^[^#]*\(# *\)\?||')"
if [ ${i} -eq -1 ]
then
firstline="${teilEins}"
outEndTime=$[ \
$(timetosample "${outRate}" $(echo "${firstline}" | awk '{print $2}' | tr '=' ' ')) \
- $(timetosample "${outRate}" $(echo "${firstline}" | awk '{print $1}' )) \
]
remixcmd="${teilZwei}"
if echo "${remixcmd}" | \
grep -q "rate "
then
>&2 echo 'FEHLER: "rate" explizit gesetzt, das bringt die Zeitmessung durcheinander - verwende statt dessen die Option "-s" bzw. "--sampleRate".'
verwendung
fi
i=0
continue
fi
if [ -z "${teilEins}" ]
then
if [ -n "${trackmods[${i}]}" ]
then
echo "Syntaxfehler in Track $[${i}+1]: habe bereits Modifikatoren gelesen ('${trackmods[${i}]}' -> '${teilZwei}')"
exit 1
fi
trackmods[${i}]=" ${teilZwei}"
inTimecorrections[${i}]=0
outTimecorrections[${i}]=0
while echo "${trackmods[${i}]}" | grep -q " trimsplice"
do
rest="${trackmods[${i}]#* trimsplice}"
end=$(timetosample ${inRate} $(echo "${rest}" | awk '{print $1}'))
if [ ! "${doverlaps[${i}]}" == "" ]
then
end=$[${end}+${doverlaps[${i}]}]
fi
inDiscard=$(timetosample ${inRate} $(echo "${rest}" | awk '{print $2}'))
outDiscard=$(timetosample ${outRate} $(echo "${rest}" | awk '{print $2}'))
inExcess=$(timetosample ${inRate} $(echo "${rest}" | awk '{print $3}'))
outExcess=$(timetosample ${outRate} $(echo "${rest}" | awk '{print $3}'))
leeway=$(timetosample ${inRate} $(echo "${rest}" | awk '{print $4}'))
trackmods[${i}]="$(
echo "${trackmods[${i}]}" | \
sed "s/ \+trimsplice\( \+[^ ]\+\)\{4\}/ trim 0s ${end}s ${inDiscard}s splice -t ${end}s,${inExcess}s,${leeway}s/"
)"
inTimecorrections[${i}]=$[${inTimecorrections[${i}]}+${inDiscard}+2*${inExcess}]
outTimecorrections[${i}]=$[${outTimecorrections[${i}]}+${outDiscard}+2*${outExcess}]
done
continue
fi
i=$[${i}+1]
tmp=$(timetosample ${inRate} "${teilEins}")
inTimes[${i}]=$[${tmp}-${absInTime}]
absInTime=${tmp}
tmp=$(timetosample ${outRate} "${teilEins}")
outTimes[${i}]=$[${tmp}-${absOutTime}]
absOutTime=${tmp}
newFile[${i}]=true
teilZwei="${teilZwei} "
while [ -n "${teilZwei}" ]
do
case "${teilZwei}" in
"overlap "*)
teilZwei="${teilZwei#* }"
overlaps[${i}]=$[$(timetosample ${inRate} ${teilZwei%% *})/2]
doverlaps[${i}]=$[2*${overlaps[${i}]}]
teilZwei="${teilZwei#* }"
;;
"noNewFile"*)
teilZwei="${teilZwei#* }"
newFile[${i}]=false
;;
" ")
break
;;
*)
>&2 echo "'${teilZwei}' ist unverständlich zwischen zwei Stücken ('${line}')"
exit 1
;;
esac
done
done < "${points}"
if ${ausfuehrlich}
then
echo "trackmods:"
for (( i=0; i<=${#inTimes[@]}; i++ ))
do
echo "${i}: ${trackmods[${i}]} ; ${inTimes[${i}]} ; ${overlaps[${i}]} ; ${doverlaps[${i}]} ; ${inTimecorrections[${i}]}"
done
fi
repcmd=""
abstime=0
for (( i=1; i<=${#inTimes[@]}; i++ ))
do
abstime=$[${abstime}+${inTimes[${i}]}]
[ -z "${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<=${#inTimes[@]}; i++ ))
do
if [ ${i} -gt 0 ]
then
[ -n "${inTimecorrections[$[${i}-1]]}" ] && \
inTimes[${i}]=$[${inTimes[${i}]}-${inTimecorrections[$[${i}-1]]}]
[ -n "${outTimecorrections[$[${i}-1]]}" ] && \
outTimes[${i}]=$[${outTimes[${i}]}-${outTimecorrections[$[${i}-1]]}]
fi
[ -n "${inTimes[$[${i}+1]]}" ] && abstime=$[${abstime}+${inTimes[$[${i}+1]]}]
[ -n "${doverlaps[${i}]}" ] && abstime=$[${abstime}+${doverlaps[${i}]}]
[ "${trackmods[${i}]}" == "${trackmods[$[${i}+1]]}" ] && continue
if [ ${i} -eq ${#inTimes[@]} ]
then
trackmodcmd="${trackmodcmd}${trackmods[${i}]} "
else
trackmodcmd="${trackmodcmd}trim 0s ${abstime}s${trackmods[${i}]} : "
fi
abstime=0
done
for (( i=0; i<=${#inTimes[@]}; i++ ))
do
[ -n "${outTimecorrections[${i}]}" ] && \
outEndTime=$[${outEndTime}-${outTimecorrections[${i}]}]
done
splicecmd=""
abstime=0
for (( i=0; i<=${#inTimes[@]}; i++ ))
do
[ -n "${inTimes[${i}]}" ] && abstime=$[${abstime}+${inTimes[${i}]}]
[ -z "${overlaps[${i}]}" ] && continue
splicecmd="${splicecmd} ${abstime}s,${overlaps[${i}]}s,0s"
abstime=$[${abstime}+${doverlaps[${i}]}]
done
[ -n "${splicecmd}" ] && splicecmd="splice -t${splicecmd}"
if ${cdAusrichten}
then
diff=0
align2=294
for (( i=1; i<=${#outTimes[@]}; i++ ))
do
nDiff=$(bc <<< "(${outTimes[${i}]} + ${diff} + ${align2}) % (2 * ${align2}) - ${align2}")
outTimes[${i}]=$(bc <<< "${outTimes[${i}]} + ${diff} - ${nDiff}")
diff="${nDiff}"
done
diff=$(bc <<< "(${outEndTime} + ${align2}) % (2 * ${align2}) - ${align2}")
outEndTime=$(bc <<< "${outEndTime} - ${diff}")
fi
trimcmd=""
for (( i=1; i<=${#outTimes[@]}; i++ ))
do
trimcmd="${trimcmd}trim 0s =${outTimes[${i}]}s : "
${newFile[${i}]} && trimcmd="${trimcmd}newfile : "
done
[ ${outRate} -ne ${inRate} ] && remixcmd="${remixcmd} rate -v ${outRate}"
if ${cdAusrichten}
then
output="-b 16 ${output}"
remixcmd="${remixcmd} trim 0 =${outEndTime}s"
extraIn=" -r ${inRate} -c ${#inputs[@]} -n"
else
extraIn=""
fi
if ! ${noGain}
then
firstline="${firstline} gain -b"
remixcmd="${remixcmd} gain -n"
fi
for ((i=0; i<=${#inputs[@]}; i++))
do
[ -z "${rejects[${i}]}" ] && continue
inputs[${i}]="|sox ${inputs[${i}]} -p${rejects[${i}]}"
done
echo sox ${tempDir} -M "${inputs[@]}" -p trim ${firstline}
echo sox ${tempDir} -t sox - -p ${repcmd}
echo sox ${tempDir} -t sox - -p ${trackmodcmd}
echo sox ${tempDir} -t sox - ${extraIn} -p ${splicecmd} ${remixcmd}
echo sox ${tempDir} -t sox - ${output} ${trimcmd}
if ! ${dummy}
then
sox ${tempDir} -M "${inputs[@]}" -p trim ${firstline}| \
sox ${tempDir} -t sox - -p ${repcmd} | \
sox ${tempDir} -t sox - -p ${trackmodcmd} | \
sox ${tempDir} -t sox - ${extraIn} -p ${splicecmd} ${remixcmd} | \
sox ${tempDir} -t sox - ${output} ${trimcmd}
fi
|