1

i create this not working script,

#!/bin/bash

echo "Mic Check"
timeout 2 pacat -r ./testmic -d 2

FILENAME=./testmic
FILESIZE=$(stat -c%s "$FILENAME")

killall pacat

if (( $FILESIZE <= 100 )); then
    echo "Mic Muted..."
else
    echo "Mic not Muted..."
fi

my headphones have a mute button but no led to indicate this =[ im trying to use a script to alert me when the mic is open

I don't know if this is the best way to do this, but... this script creates a "testMic" file, when i press the mute button the file is created with nothing inside:

black

but when i not press to mute, the file is created with audio code: not black

i don't understand very well whats happening, but $FILESIZE aways return same value. any way to make this more effective?

tks guys

Edit1: The problem in this code is, always, if the testMic file is blank or if it has an audio code inside, the value of $FILESIZE is the same.

Edit2: I don't want to solve the problem by muting the mic in the system, I want to use the mute system from the headset itself

Edit3: I solved the problem with this code:

#!/bin/bash

echo "Mic Check"
timeout 2 pacat -r ./testmic -d 2

FILECONTENT=$(grep -L -z -e . testmic)

killall pacat

if [[ "$FILECONTENT" == "testmic" ]]; then
    echo "Mic Muted."
else
    echo "Mic not Muted."
fi
0110k011
  • 37
  • 4
  • What part of your code is actually _wrong_? What do you expect it to do instead? "[could be] more effective" is not a clear problem statement. – Charles Duffy May 02 '23 at 22:21
  • 1
    To be clear, it's normal for there to be analog noise on the line even with a hardware mute in place. If you want to _quantify_ that noise, you should be parsing the data (ideally, getting it as raw samples instead of any kind of encoded format, and then using tools that work with raw data; bash is not a good choice for processing binary data -- you'd be better off writing this in a different language) – Charles Duffy May 02 '23 at 22:22
  • ...indeed, there could _actually be_ a whole lot of NUL bytes indicating absolute silence in your file, but `cat` will never show them on the terminal, because NUL bytes are invisible when copied to a terminal. – Charles Duffy May 02 '23 at 22:24
  • 2
    for evaluating your file's contents as a human reader, consider using `xxd – Charles Duffy May 02 '23 at 22:25
  • (and in general, printing binary data in human-readable format is something `cat` is never expected to do) – Charles Duffy May 02 '23 at 22:28

1 Answers1

1

The file probably only gets written to disk after you kill pacat.

If that's a correct diagnosis, the minimal fix is to move the stat after the killall; but altogether a better solution is to only kill the correct process, and avoid upper case for your private variables.

#!/bin/bash

# Write diagnostics to standard error; include script name in message
echo "$0: Mic Check" >&2

pacat -r ./testmic -d 2 &
pacatid=$!

sleep 2
kill $pacatid

if grep -qz . testmic; then
    echo "$0: Mic not Muted." >&2
else
    echo "$0: Mic Muted." >&2
fi

Your script should probably set an exit code to reflect the result but I'm not clear on which scenario should count as success. Generally, exit 0 for success, and any other number for failure, but typically 1 if you don't have any additional information you want to provide in machine-readable form. (Also, in this scenario, perhaps provide an option to suppress the diagnostic output.)

A proper tool would also avoid dropping random files in the current directory; maybe look at mktemp and set a trap to clean up the temporary file in case of an error.

tripleee
  • 175,061
  • 34
  • 275
  • 318