Home noise detection

After 2,5 years and 1000+ entities with 62 million state changes later its time to give something back to community. I have been looking for a solution to monitor noises at my house but couldn’t find any. So decided to try it myself. This is more like proof of concept so feel free to suggest improvements.

Motivation and use cases

  • Identify fire alarm remotely (main case for me)
  • Identify any noises when away from home
  • Use it as supporting factor to decide if house is empty (everyone away)
  • Identify when some machine is running
  • Monitor baseline for “boiler room” and get notified if some machine is soon breaking down or malfunctioning (pitch changes)

The solution

  1. Windows scheduler running Power Shell script every minute
  2. Use ffmpeg to record 58 seconds of audio from microphone
  3. Use aubiopitch.exe from Aubio library to analyse pitch from recorded file
  4. Average and max pitch is calculated from aubiopitch output
  5. Use ffmpeg to analyse average and max volume
  6. Use mosquitto_pub.exe to publish the results to mqtt queue
  7. Use mqtt sensors on Home Assistant to get the results

Power Shell script

del C:\temp\temp.wav
del C:\temp\temp.txt

#$audio = "audio=Desktop Microphone (Microsoftr LifeCam HD-5000)"
$audio = "audio=@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{42903B81-B67E-4949-8255-5619CAB6FB17}";
$time = 58;
$volume = 2;

"Recording for $time seconds with volume $volume"

C:\path_to_files\ffmpeg.exe -f dshow -t $time -i $audio -filter_complex "volume=$volume" C:\temp\temp.wav 2>&1 | Out-Null
C:\path_to_files\aubiopitch.exe -i C:\temp\temp.wav -s -150 -B 1024 -H 1024 > C:\temp\temp.txt | Out-Null
C:\path_to_files\ffmpeg.exe -i C:\temp\temp.wav -filter:a volumedetect -f null nul 2>&1 |

foreach {

    if($_ -match 'mean_volume') {
        $parts = $_ -split ' ';
        $mean_volume = $parts[4];
    }
    if($_ -match 'max_volume') {
        $parts = $_ -split ' ';
        $max_volume = $parts[4];
    }
}

$table = ""
$table = Import-Csv C:\temp\temp.txt -Delimiter ' ' -Header "Time", "Tone"
$count = 0
$total = 0
[Double]$max = 0.0

foreach ($item in $table) {
    if($item.Tone -ne 0){
        $count++;
        [Double]$tone = [double]$item.Tone;
        $total += $tone;
        if($tone -gt $max) {
            $max = $item.Tone;
        }
    }
}

$avg = $total/$count
#$avg
#$max
$mean_v = 100+$mean_volume
$max_v = 100+$max_volume

$json = @"
{
\"max_tone\": $max,
\"avg_tone\": $avg,
\"avg_volume\": $mean_v,
\"max_volume\": $max_v
}
"@

#$json

& 'C:\Program Files\mosquitto\mosquitto_pub.exe' -h 192.168.1.123 -t "volume/tyohuone" -m $json

From Windows driver I have set microphone “sensitivity” to 100 %. Volume-parameter on ffmpeg should be high enough to get some meaningful data out of aubiopitch. Too low value result only zeroes. Average is calculated by skipping zero values.

Home Assistant configuration

sensor:
  - platform: mqtt
    name: Työhuone max tone
    state_topic: "volume/tyohuone"
    value_template: "{{ value_json.max_tone | round(1) }}"
    unit_of_measurement: "Hz"
    icon: mdi:microphone
  - platform: mqtt
    name: Työhuone avg tone
    state_topic: "volume/tyohuone"
    value_template: "{{ value_json.avg_tone | round(1) }}"
    unit_of_measurement: "Hz"
    icon: mdi:microphone
  - platform: mqtt
    name: Työhuone avg volume
    state_topic: "volume/tyohuone"
    value_template: "{{ value_json.avg_volume }}"
    icon: mdi:microphone
    unit_of_measurement: "dB"
  - platform: mqtt
    name: Työhuone max volume
    state_topic: "volume/tyohuone"
    value_template: "{{ value_json.max_volume }}"
    unit_of_measurement: "dB"
    icon: mdi:microphone

Outcomes

image

In above graph HVAC speed increases at 2:00 10 %. You can hardly hear it but this graph impressively catches it (1.). Child screams on the other end of the house 4:40 (2.).

Small nighttime spikes at max volume are likely fridge and freezer compressor starts about 6 meters from mic.

Next steps

  • As you might notice my PowerShell skills are limited. Feel free to suggest improvements
  • Would be nice to have this analysis done from live stream
  • Please suggest also other improvements, such as addtional analysis methods
  • Make some critical and other notifcations from combination of these sensors
  • Also another mic to “boiler room” is still pending

edit: typo

8 Likes

Thank you for sharing, this is best result I’ve seen but looks hard to achieve and very demanding with running windows
I’d like to reach similar results without running window machine

Found FFT library which should translate analog microphone to spectrum analyzer

flashed it to wemos d1 mini with max4466 microphone it returns this data:

Computed magnitudes:
0.000000Hz 18451.2604
1.562500Hz 8026.8306
3.125000Hz 87.0263
4.687500Hz 44.3263
6.250000Hz 21.0252
7.812500Hz 33.6955
9.375000Hz 15.6019
10.937500Hz 16.0835
12.500000Hz 39.2304
14.062500Hz 31.4193
15.625000Hz 38.8394
17.187500Hz 23.1039
18.750000Hz 25.7518
20.312500Hz 7.1066
21.875000Hz 13.7945
23.437500Hz 23.6252
25.000000Hz 36.0504
26.562500Hz 32.5109
28.125000Hz 11.4898
29.687500Hz 27.5495
31.250000Hz 43.1910
32.812500Hz 27.1569
34.375000Hz 12.5470
35.937500Hz 1.9484
37.500000Hz 12.6212
39.062500Hz 20.1076
40.625000Hz 12.2179
42.187500Hz 60.5007
43.750000Hz 45.6161
45.312500Hz 51.7855
46.875000Hz 109.2218
48.437500Hz 77.1940

now working on figuring how to send values to mqtt for some graphing

1 Like

That looks good. Just thinking how to transform that output to sensor data. I would probably try to calculate some averages from those frequencies: 1 sensor for 10-100 Hz, 1 sensor for 100-1000 Hz and 1 sensor for >1000 Hz.

What do you think about this?

The idea was to take code of led equalizer which will return value for each band, then graph them and see results but I’m having hard time with Arduino IDE so no success yet

Another thing I found is chip called MSGEQ7 which is “Graphic Equalizer Display Filter” seems it makes sound analyze more accurate than FFT

For now I’m only learning I’ll update when there will be any progress

@henri1 Do we have such integration in HACS?