MushcatShiro's Blog

Snore Detector

This is a page about »Snore Detector«.

Snoring can be annoying, especially if one is accused. To put one in clear, it would be helpful to have a snoring detector. This can be easily done using Unihiker’s M10 board.

Implementation Details

Looking into the official documentation’s example 1, it is quite close to what a snore detector is supposed to do. However in the spirit of tinkering, let’s look at the two packages used pinpong and unihiker. They are found under /usr/local/lib/python3.7/dist-packages/unihiker, after ls through all the directories from python -m site. There are two main API that matters are Audio and Gui. To start recording is simple as,

 1# record.py
 2from unihiker import Audio
 3
 4audio = Audio()
 5
 6def log(fname: Path, val: int):
 7  with open(fname, "a") as wf:
 8    wf.write(f"{val}\n")
 9
10fname = str(dt.datetime.now())
11
12while True:
13  log(fname, audio.sound_level())
14  time.sleep(1)

And as for visualizing the sound level recorded,

 1# ui.py
 2vals = []
 3with open(fname, "r") as rf:
 4  vals.append(float(line.strip()))
 5
 6import matplotlib.pyplot as plt
 7plt.plot(container)
 8plt.title(fname)
 9plt.savefig("test.png", dpi=50)
10
11import tkinter as tk
12from PIL import Image, ImageTk
13
14root = tk.Tk()
15root.geometry("240x320")
16im = Image.open("test.png")
17imm = im.rotate(90, expand=1)
18imm = ImageTk.PhotoImage(image=imm)
19cvs = tk.Label(root, image=imm)
20cvs.pack(side="bottom", fill="both", expand="yes")
21root.mainloop()

The GUI could have been much straightforward by calling the Gui API but at the point of development the author was interested to learn how to build simple desktop apps with tkinter. The apps was rather simple, by running the record.py prior to sleep and kill off upon awake. Then run ui.py to see the plot.

plot

From the looks, there is some snoring early into the sleep and not too long before awake. There are some limitations with the current implementation,

Future enhancements should consider merging them into a single script, making sure that there is a dark mode that turns off the lcd screen (perhaps with the A/B buttons) and the ability to select log file to plot real time instead of fixed latest log file.

#Single Board Computer