How do I make a machine "blank screen" for a period of time (as a penalty) if certain noise levels are reached?

1 554

590

My kids (4 and 5) yell a lot when playing games on the computer. I found an effective cure for this. When I hear loud noises, I ssh into the game computer and do:

chvt 3;  sleep 15;  chvt 7 

This will turn off the screen for 15 seconds on Linux. I've told them that the computer doesn't like loud noises. They totally believe this and beg the computer for forgiveness. They became much quieter, but not to the level that I would be happy, and so I need to continue this educational process. However, I am not always around to do this manually.

Is it possible to automate this? A microphone is attached to the box. If the level of loudness passes some threshold then I want to run a command.

Leonid Volnitsky

Posted 2013-02-01T17:14:09.170

Reputation: 8 235

5Until they learn to press CTRL + ALT + F7 – Suici Doga – 2018-02-13T13:35:39.837

2@SuiciDoga Hey; they don't know what's going on! – wizzwizz4 – 2018-10-24T08:39:59.053

1Congrat for a technical solution. But I think, it is important to say always the truth to the children. – peterh - Reinstate Monica – 2019-01-30T11:44:57.003

Answers

647

Use sox from SoX to analyze a short audio sample:

sox -t .wav "|arecord -d 2" -n stat

With -t .wav we specify we process the wav type, "|arecord -d 2" executes the arecord program for two seconds, -n outputs to the null file and with stat we specify we want statistics.

The output of this command, on my system with some background speech, is:

Recording WAVE 'stdin' : Unsigned 8 bit, Rate 8000 Hz, Mono
Samples read:             16000
Length (seconds):      2.000000
Scaled by:         2147483647.0
Maximum amplitude:     0.312500
Minimum amplitude:    -0.421875
Midline amplitude:    -0.054688
Mean    norm:          0.046831
Mean    amplitude:    -0.000044
RMS     amplitude:     0.068383
Maximum delta:         0.414063
Minimum delta:         0.000000
Mean    delta:         0.021912
RMS     delta:         0.036752
Rough   frequency:          684
Volume adjustment:        2.370

The maximum amplitude can then be extracted via:

grep -e "RMS.*amplitude" | tr -d ' ' | cut -d ':' -f 2

We grep for the line we want, use tr to trim away the space characters and then cut it by the : character and take the second part which gives us 0.068383 in this example. As suggested by comments, RMS is a better measure of energy than maximum amplitude.

You can finally use bc on the result to compare floating-point values from the command-line:

if (( $(echo "$value > $threshold" | bc -l) )) ; # ... 

If you build a loop (see Bash examples) that calls sleep for 1 minute, tests the volume, and then repeats, you can leave it running in the background. The last step is to add it to the init scripts or service files (depending on your OS / distro), such that you do not even have to launch it manually.

tucuxi

Posted 2013-02-01T17:14:09.170

Reputation: 4 413

I have tried to build a shell script based on the solution above. Built it for mac using OSX Yosemite (10.10.2). Script can be found here: https://github.com/ohenrik/hush

– Ole Henrik Skogstrøm – 2014-12-27T23:59:25.947

284I would disrecommend taking the maximum amplitude. It's not nice for the kids when their screen goes blank just because someone clapped or something similar. Average seems more appropriate. – orlp – 2013-02-01T17:59:07.260

36Just a clarification, by "average" you mean RMS Amplitude right? The Mean Amplitude is going to be close to 0 if the noise is of a consistent loudness over the 2 seconds (the positive and negative halves will cancel each other out). – Luke – 2013-02-02T20:10:56.410

7A simple "energy" detector for a series of samples is to just add the value of all the peaks together. You wouldn't have to even average it if you didn't want to. A peak is just any point where sample[n]>sample[n-1]&&sample[n]>sample[n+1] I've used this as a rudimentary mechanism for measuring the energy of a song and it works quite well. Just search for a magic number at which you're happy with the volume level. – Kaslai – 2013-02-03T01:45:59.463

3I would like to see a sample output of your first command when it really comes to a kid yelling, for reference. – Alvin Wong – 2013-02-03T11:39:44.177

If you're sleeping for a minute each time and then testing only 2 seconds of audio, aren't you very likely to miss any (probably intermittent) yelling? I would suggest a slightly longer recording time, and drastically reduced rest time between tests. Maybe 5s sleep, 5s test? – John Lyon – 2013-02-04T05:08:46.753

1I would replace grep -e "RMS.*amplitude" | tr -d ' ' | cut -d ':' -f 2 with sed -En 's#^RMS[[:space:]]+amplitude:[[:space:]]+([\.[:digit:]]+)$#\1#p' – jcayzac – 2013-02-06T00:41:30.537

3For the described usage (start automatically + run every few minutes) a cron job is the right tool to use. Much simpler to setup and more robust than using init script + bash loop + sleep. – m000 – 2013-03-24T14:08:21.380

To add to @nightcracker's comment. You should first take a few sample recordings when the kids yell, so you get an idea of your threshold in terms of amplitude/energy from the microphone. Then when you run your monitoring program, do a matched filtering with that sample recording before you calculate the signal amplitude/energy, that should take care of the other noise source from triggering false alarm. – LWZ – 2013-02-11T16:56:49.247

133

Here's how it can be done with Pure Data:

Kid yell prevention using Pure Data

Metro is a metronome, and "metro 100" keeps banging each 100 ms.

The audio is coming from adc~, the volume is calculated by env~. "pd dsp 0" turns off the DSP when banged, "pd dsp 1" turns it on. "shell" executes the passed command in a shell, I use the Linux xrandr API to set the brightness to X, you need to adapt this for Wayland.

As you can see, grace period and locking takes up way more space than audio code does.

Making a solution with ring buffers and/or moving averages should be way easier than doing it with sox. So I don't think it's a bad idea to use Pure Data for this. But the screen blanking itself and the locking doesn't fit with the dataflow paradigm.

The PD file is at gist.github.com: ysangkok - kidsyell.pd.

Janus Troelsen

Posted 2013-02-01T17:14:09.170

Reputation: 1 958

@JanusTroelsen Including that PD file directly in your answer (with Ctrl+K or 4 spaces) wouldn't put you over the character limit. – wizzwizz4 – 2018-10-24T08:43:40.180

@wizzwizz4 since it's not really human readable, I'd prefer not to – Janus Troelsen – 2018-10-24T13:10:02.650

12very nice! You could make this to be quite responsive using this technique: track the average sound level over a minute, then use that as the baseline, so that when the kids go over 20 dB above the baseline, it triggers. Then it'll automatically adjust to the ambient sound level. – Hans-Christoph Steiner – 2013-02-05T16:33:37.290

1Yes, that makes sense @Hans-ChristophSteiner. But in a way, wouldn't the ambient noise level actually require the kids to yell louder, since they would make up a smaller proportion of the overall noise? That of course would only apply if the existing noise is white or pink or otherwise ignored. – Janus Troelsen – 2013-02-05T16:40:33.580

4if it was quieter than usual, like a weekend morning, then it would make it more sensitive, since it would always be 20 dB above the ambient level – Hans-Christoph Steiner – 2013-02-05T16:42:49.580

This is the extended PD? – nullpotent – 2013-02-09T15:09:42.113

@iccthedral: I used pd-extended to make it, but I don't know if I used any pd-extended specific constructs. – Janus Troelsen – 2013-02-10T12:56:51.400

I wrote almost the same solution - but I wasn't able to call the shell like you did. I ended up doing it with C bindings. So I guess that's specific to extended version. I prefer PD-vanilla as I often find myself porting PD patches to Android. However, I love this solution. PD is so powerful. – nullpotent – 2013-02-10T13:14:58.280

104

Check "How to detect the presence of sound/audio" by Thomer M. Gil.

Basically it records the sound every 5 seconds, than checks for the sound amplitude, using sox, and decides if trigger a script or not. I think you can easily adapt the ruby script for your children! Or you can choose to hack away on the Python script (using PyAudio) that he has provided as well.

Atropo

Posted 2013-02-01T17:14:09.170

Reputation: 1 513

6What about those outbursts less than 5 seconds that avoid detection? – RhysW – 2013-02-04T10:13:48.123

54

You can get information from the microphone by doing something like:

arecord -d1 /dev/null -vvv

You might have to play with the settings a little, such as:

arecord -d1 -Dhw:0 -c2 -fS16_LE /dev/null -vvv

From there on out, it's a simple matter of parsing the output.

cha0site

Posted 2013-02-01T17:14:09.170

Reputation: 712

46

This is one of the more fun questions that I've seen. I would like to thank tucuxi for such a fine answer; that I have set as a bash script

#!/bin/bash

threshold=0.001
# we should check that sox and arecord are installed
if [ $1 ]; then threshold=$1; fi
while [ 1 -gt 0 ]; do
 if(( $(echo "$(sox -t .wav '|arecord -d 2' -n stat 2>&1|grep -e 'RMS.*amplitude'|tr -d ' '|cut -d ':' -f 2 ) > $threshold"|bc -l) ))
 then
  chvt 3; sleep 5; chvt 7;
 fi
done

Alexx Roche

Posted 2013-02-01T17:14:09.170

Reputation: 760

8If you start this running by adding a line to /etc/rc4.d/S99rc.local and then change the input mic from unamplified to 100% you too can end up being thrown over to tty3 (you can jump back before the sleep is over with Ctrl+Alt+F7), and if your keyboard is too loud to open a terminal, to run sudo killall too_loud then Ctrl+Alt+F1 and log in there.) – Alexx Roche – 2013-02-10T12:45:36.137

42

My 2 cents for the C or C++ solution: maybe not the most effective approach, but on Linux, you can use the ALSA API (built-in audio handling library of Linux) and use some numerical technique (for example, calculating the average sound level each second) to obtain the level of noise.

Then you can check it in an infinite loop, and if it's greater than a preset treshold, you can use the X11 library to turn off the screen for some seconds, or alternatively (less elegant, but it works) invoke the chvt command using system("chvt 3; sleep 15; chvt 7 ");.

H2CO3

Posted 2013-02-01T17:14:09.170

Reputation: 1 014

2

If using command I would consider something different then chvt. ArchWiki has nice examples.

– A.D. – 2013-02-05T13:20:34.597