Commit c425a4ef authored by Ronald van Engelen's avatar Ronald van Engelen

fixed json indentation; start numbering cards in normal output at 1 insteads...

fixed json indentation; start numbering cards in normal output at 1 insteads of 0; fix samplerate unit to Hz instead of Khz in normal output
parent 652b1ea9
......@@ -44,7 +44,6 @@ PROFILE="${PROFILE:-}"
## (hopefully with some grace).
TESTFILE="${TESTFILE:-}"
### generic functions
function die() {
calling_function_key="$(( ${#FUNCNAME[@]} - 2 ))"
......@@ -101,7 +100,7 @@ function get_aplay_output() {
## returns the raw output of aplay or an error.
res=""
aplay_msg_nosoundcards_regexp="no[[:space:]]soundcards"
if [[ ! -z "${TESTFILE}" ]]; then
if [[ "${TESTFILE}x" != "x" ]]; then
if [[ ! -f "${TESTFILE}" ]]; then
# shellcheck disable=SC2059
printf 1>&2 "${MSG_APLAY_ERROR_NOSUCHTESTFILE}" \
......@@ -118,9 +117,7 @@ function get_aplay_output() {
return 1 )
fi
else
## run aplay to check for alsa errors or display audio cards
## check for alsa errors
# shellcheck disable=SC2059
## run aplay -l to check for alsa errors or display audio cards
res="$(${CMD_APLAY} -l 2>&1)" || \
(
printf "${MSG_APLAY_ERROR_GENERAL}\n" "${res}"
......@@ -134,20 +131,15 @@ function get_aplay_output() {
fi
## return the result to the calling function
printf "%s" "${res}"
}
function handle_doublebrackets() {
## return the name of the alsa card / device, even when they
## contain brackets.
string="$*"
[[ ${DEBUG} ]] && \
debug "(${LINENO}): using string: \`${string}'"
bracketcounter=0
for (( i=0; i<${#string}; i++ )); do
char="${string:$i:1}"
#printf "." 1>&2;
if [[ "${char}" = "[" ]]; then
(( bracketcounter++ ))
elif [[ "${char}" = "]" ]]; then
......@@ -164,15 +156,15 @@ function handle_doublebrackets() {
fi
fi
done
}
function print_human_output() {
function return_human_output() {
## print default output to std_err.
## called by fetch_alsa_outputinterfaces.
printf "%s\n" "${alsa_if_display_title}" 1>&2;
printf " - %-17s = %-60s\n" \
"${MSG_ALSA_DEVNAME}" "${alsa_dev_label}" 1>&2;
"${MSG_ALSA_DEVNAME}" \
"${alsa_dev_label}" 1>&2;
printf " - %-17s = %-60s\n" \
"${MSG_ALSA_IFNAME}" "${alsa_if_label}" 1>&2;
printf " - %-17s = %-60s\n" \
......@@ -190,7 +182,7 @@ function print_human_output() {
else
formatcounter=0
if [[ ! -z ${OPT_SAMPLERATES} ]]; then
MSG_ALSA_ENCODINGFORMATS="samplerates (kHz)"
MSG_ALSA_ENCODINGFORMATS="samplerates (Hz)"
fi
printf " - %-17s = " \
"${MSG_ALSA_ENCODINGFORMATS}" 1>&2;
......@@ -211,9 +203,12 @@ function print_human_output() {
}
function key_val_to_json() {
## returns a json "key": "val" pair.
key="$1"
val="$2"
## check if val is a number
if printf -v numval "%d" "${val}" 2>/dev/null; then
## it is
printf '"%s": %d' \
"${key}" "${numval}"
else
......@@ -224,8 +219,9 @@ function key_val_to_json() {
}
function ret_json_format() {
## returns the json formatted encoding format and possibly sample
## rates.
formats_raw="$1"
indent=" "
declare -a json_formats
if [[ "${formats_raw}" =~ ':' ]]; then
## sample rates included
......@@ -234,13 +230,13 @@ function ret_json_format() {
if [[ "${line}" =~ ${split_re} ]]; then
format=${BASH_REMATCH[1]}
IFS=" " samplerates=(${BASH_REMATCH[2]})
printf -v sr_out "\t\t\"%s\",\n" "${samplerates[@]}"
printf -v sr_out "\t\t\"%s\",\n" \
"${samplerates[@]}"
sr_out="${sr_out%,*}"
format_json=""
label_samplerates='"samplerates"'
output_line="\
{
$(key_val_to_json format "${format}"),
output_line="{
$(key_val_to_json format "${format// /}"),
${label_samplerates}: [
${sr_out}
]
......@@ -248,16 +244,18 @@ ${sr_out}
output_lines+=("${output_line}")
fi
done<<<"${formats_raw}"
printf -v json_formats "\t\t%s\n" "${output_lines[@]}"
printf -v json_formats "\t%s\n" "${output_lines[@]}"
## strip the continuation comma from the last element
json_formats="${json_formats%,*}"
else
## no sample rates included
IFS="," formats_res=(${formats_raw})
printf -v json_formats '\t\t"%s",\n' \
"${formats_res[@]}"
"${formats_res[@]// /}"
## strip the continuation comma from the last element
json_formats="${json_formats%,*}"
fi
printf "%s" "${json_formats// /}"
printf "%s" "${json_formats}"
}
function ret_json_card() {
......@@ -622,7 +620,7 @@ ${MSG_ERROR_UNEXPECTED}"
## construct the display title
printf -v alsa_if_display_title \
" %s) %s \`%s'" \
"${#ALSA_AIF_HWADDRESSES[@]}" \
"${cur_aif_no}" \
"${alsa_if_title_label}" \
"${alsa_if_hwaddress}"
## store the details of the current interface in global arrays
......@@ -635,10 +633,10 @@ ${MSG_ERROR_UNEXPECTED}"
ALSA_AIF_FORMATS="${alsa_if_formats[*]}"
ALSA_AIF_CHARDEVS+=("${alsa_if_chardev}")
fi
if [[ -z "${OPT_QUIET}" ]] && [[ "${OPT_JSON}x" == "x" ]]; then
## print the list to std_err
print_human_output
res="$(return_human_output)" || exit 1
printf 1>&2 "%s\n" "${res}"
fi
if [[ "${OPT_JSON}x" != "x" ]]; then
if [[ ${cur_aif_no} -lt ${#aplay_lines[@]} ]]; then
......@@ -795,7 +793,6 @@ using encoding_format \`${encoding_format}'."
rates+=("${rate}")
fi
done
declare -p rates 1>&2
## sort and retrun trhe newline separated sample rates
sort -u -n <(printf "%s\n" "${rates[@]}")
}
......
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