#!/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