Commit 78e05e62 authored by Arnaud Blanchard's avatar Arnaud Blanchard

Impove bash scripts

parent 7602b89c
......@@ -21,7 +21,7 @@ blaar_program_name=$(basename $0)
#Create a temporary directory, essencially for logs
TMP_DIR="$(mktemp -d -t $blaar_program_name)"
trap 'error=$?; [ $? = 0 ] || { echo; echo "BLAAR: error $error executing $blaar_program_name $*"; echo; exit $error }; rm -rf "$TMP_DIR"' EXIT INT HUP TERM
trap 'error=$?; rm -rf "$TMP_DIR"; [ $error = 0 ] || { echo; echo "BLAAR: error $error executing $blaar_program_name $*"; echo; exit $error; }' EXIT INT HUP TERM ABRT
calc(){
echo $(echo "$@"|bc -l)
......@@ -29,26 +29,20 @@ calc(){
test_run() {
"$@" || {
error=$?
echo executing: "$@"
exit $?
exit $error
}
}
blaar_module(){
usage(){
echo
echo "Try to execute <executable> otherwise explain you need the module <module>"
echo "usage: blaar_module <module> <executable>"
echo
}
[ $# -lt 2 ] && { echo "You need at least 2 arguments but you have $#:'$*'"; usage; exit 1; }
hash $2 2>/dev/null || { echo "You need to install https://gitlab.blaar.org/$1"; exit 2; }
shift
test_run "$@"
test_run "$@"
}
blaar_add_option(){
......
......@@ -14,15 +14,12 @@
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
set -o errexit #Exit on first error
set -o nounset #Error if a variable is used but not defined
set -o pipefail #Error if a pipe fail
source blaar.sh
blaar_init_options "Graph the frequency spectrum from the microphone or a sound file(-f)"
blaar_add_option device D "" "device_name" "device to use for the microphone"
blaar_add_option filename f "" "filename" "file to load the sound from"
blaar_add_option frequency F "" "frequency" "frequency of a pure sound"
blaar_add_option size s "" "integer" "number of itmes per sample" "4096"
blaar_add_option samplerate S "" "integer" "Samplerate of the sound acquisition" "44100"
blaar_add_option max M "" "real" "maximal intensity to display"
......@@ -30,12 +27,12 @@ blaar_add_option xmax X "" "real" "maximal values on absisca to display"
blaar_parse_args "$@"
[ $device ] && device_option="-D $device" || device_option=""
[ $filename ] && file_option="-f=$filename" || file_option=""
[ $filename ] && file_option="-f $filename" || file_option=""
[ $max ] && max_option="--max=$max" || max_option=""
[ $xmax ] && xmax_option="--xmax=$xmax" || xmax_option=""
[ $frequency ] && frequency_option="-F $frequency" || frequency_option=""
i_spectrum.sh -s$size $file_option $device_option -o.spectrum$$ |
i_spectrum.sh -s$size $file_option $device_option $frequency_option -o:spectrum$$ |
o_gnuplot --label-max=$(calc "$samplerate/2") --text="set xlabel 'Frequencies (Hz)';set ylabel 'Amplitude'" $max_option $xmax_option
#specify the output names allows you to erase them later
......
......@@ -14,10 +14,6 @@
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
set -o errexit #Exit on first error
set -o nounset #Error if a variable is used but not defined
set -o pipefail #Error if a pipe fail
source blaar.sh
blaar_init_options "Acquire the sound from microphone or sound file (-f filename)"
......@@ -33,19 +29,20 @@ blaar_parse_args "$@"
[ $output ] && output_option="--output=$output"|| output_option=""
if [ $filename ]; then
echo "Loading '$filename" >&2
[ $frequency ]&&{ echo "You cannot have file (-f) and frequecy (-F) at the same time"; exit 1; }
echo "Loading '$filename'" >&2
[ $frequency] &&{ echo "You cannot have file (-f) and frequecy (-F) at the same time"; exit 1; }
[ $device ] && { echo "You cannot have file (-f) and microphone (-D) at the same time"; exit 1; }
blaar_module sndfile i_sndfile --file=$filename --size=$size --samplerate=$samplerate $output_option
elif [ $frequency ]; then
echo "Generating sinusoïd at $frequency Hz" >&2
[ $device ] && { echo "You cannot have frequency (-F) and microphone (-D) at the same time"; exit 1; }
echo "Generating sinusoïd of $size items at $frequency Hz" >&2
i_oscillator --min=-1 --frequency=$frequency --size=$size --refresh=$(calc $size/$samplerate*1000) $output_option
elif [ $(uname) = "Darwin" ]; then #microphone
echo "Coreaudio microphone" >&2
echo "Coreaudio microphone sampling at $samplerate Hz in buffer of $size items" >&2
[ $device ] && echo "On Darwin you cannot precise microphone (-D)"
blaar_module coreaudio i_coreaudio --size=$size --samplerate=$samplerate $output_option
elif [ $(uname) = "Linux" ]; then
echo "ALSA microphone" >&2
echo "ALSA microphone sampling at $samplerate Hz in buffer of $size items" >&2
[ ! $device ] && device=default
blaar_module asound i_asound --device=$device --size=$size --samplerate=$samplerate $output_option
else
......
......@@ -14,15 +14,12 @@
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
set -o errexit #Exit on first error
set -o nounset #Error if a variable is used but not defined
set -o pipefail #Error if a pipe fail
source blaar.sh
blaar_init_options "Acquire the frequency spectrum from the microphone or a sound file(-f)"
blaar_add_option device D "" "device_name" "device to use for the microphone"
blaar_add_option filename f "" "filename" "file to load the sound from"
blaar_add_option frequency F "" "frequency" "frequency of a pure sound"
blaar_add_option output o "" "blc_channel|filename|-" "output of the data ('-' for stdout)"
blaar_add_option size s "" "integer" "number of items per sample" "4096"
blaar_add_option samplerate S "" "integer" "Samplerate of the sound acquisition" "44100"
......@@ -31,8 +28,9 @@ blaar_parse_args "$@"
[ $device ] && device_option="-D$device" || device_option=""
[ $filename ] && filename_option="-f$filename" || filename_option=""
[ $output ] && output_option="--output=$output" || output_option=""
[ $frequency ] && frequency_option="-F $frequency" || frequency_option=""
i_sound.sh -o:sound$$ -s$size -S$samplerate $device_option $filename_option|
i_sound.sh -o:sound$$ -s$size -S$samplerate $device_option $filename_option $frequency_option|
blaar_module fftw f_fftw_spectrum $output_option
blc_channels --unlink /sound$$
......
......@@ -29,15 +29,15 @@ blaar_parse_args "$@"
[ $filename ] && file_option="-f=$filename" || file_option=""
if [ $filename ]; then
echo "Recording '$filename" >&2
echo "to '$filename'" >&2
[ $device ] && { echo "You cannot have file (-f $filename) and speaker (-D $device) at the same time"; exit 1; }
blaar_module sndfile o_sndfile --file=$filename --size=$size --samplerate=$samplerate
elif [ $(uname) = "Darwin" ]; then #microphone
echo "Coreaudio speaker" >&2
blaar_module coreaudio o_coreaudio "$@"
echo "to coreaudio speaker" >&2
blaar_module coreaudio o_coreaudio
elif [ $(uname) = "Linux" ]; then
echo "ALSA speaker" >&2
blaar_module asound o_asound "$@"
echo "to ALSA speaker" >&2
blaar_module asound o_asound
else
echo "$blaar_program_name does not work on '$(uname)' platform"
exit 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment