Introduction
I made simple sound surveillance system with sox and python on ubuntu linux. I tested this on 13.10 and 14.04. This script can record a sound from microphone of your computer only when there is no silence. You can also define which level is a silence. If silence lasts for a certain time, recording stops. There will be as many recorded files as events happen from sound to silence. I mean a event makes a result wav file. There is also log file for writing every event.
Before we start to see the script file, we have to study about a basic of digital audio file and how to record a voice with your computer.
Basic of WAV file
You need to know the basic concept of digital audio file. Following link has a rich information. Bit size, sample rate, and channels are so important that you should scrutinise them.
Volume adjustment for microphone
High volume of microphone makes lots of noise and you can hear nothing with its low volume, so you have to adjust the volume of your microphone. On ubuntu, you can control it with following process.
- Click Volume icon on your task bar
- Click
Sound Setting
- After opened
sound
window, ClickInput
tab. - Adjust the volume of microphone.
For lubuntu of my laptop, I can adjust microphone volume using alsamixer. But, my laptop’s built-in microphone has some problem. It is working but microphone volume is too low, and when I volume up there is much noise. So I used a headset with microphone.
Is a record function working on my PC?
Audacity is well known software to manipulate audio. You can check your recording hearing it and seeing sound chart. You can review following link to learn how to record a sound from your microphone.
sox
From web site of sox, they say that sox is the Swiss Army knife of sound processing programs. This is amazing tool to handle audio files on Windows, Mac, and Linux. In our final python script, we use sox to record a sound. It can detect both silence and sound, and make wav files whenever there is a sound.
Firstly, you need to install sox.
$ sudo apt-get install sox
I will show you basic examples of how to use sox.
// format changing
$ sox foo.aiff foo.wav
// r: sampling rate, s: signed, w: each sample is 2 bytes word
$ sox -r 44100 -s -w foo.raw foo.wav
// make volume double for output file
$ sox -v 2.0 foo.wav bar.wav
// record and play
$ sox -d rec.wav [rate 16k]
$ sox rec.wav -d
This examples will be used in our python script.
// You can record your voice after starting talking
$ rec rec.wav rate 32k silence 1 0.1 3%
// Recording starts after 2 sounds with duration of 0.1s and 3% threshold
$ rec rec.wav rate 32k silence 2 0.1 3%
// rate: Sample rate, 8k is enough for voice input, I think
// If input is lower than threshold 3% for 3 seconds, recording stops
$ rec rec.wav rate 8k silence 1 0.1 3% 1 3.0 3%
For more information about silence option, see this link.
http://digitalcardboard.com/blog/2009/08/25/the-sox-of-silence/
http://digitalcardboard.com/blog/2009/08/25/the-sox-of-silence/
Another nice tutorial for sox ishttp://billposer.org/Linguistics/Computation/SoxTutorial.html
python script
I used python version 2.7.5+.
This script generates log file called sound.log on your running folder. In addition, it saves wav files like
rec20140715-102108.wav
.
Example of log file follows:
// start time ~ end time (duration)
sox.py [22] 2014-07-14 21:44:52 ~ 2014-07-14 21:44:56 (4.081559)
sox.py [21] 2014-07-15 09:24:58 ~ 2014-07-15 09:25:15 (17.188328)
sox.py [21] 2014-07-15 09:26:53 ~ 2014-07-15 09:26:58 (4.736515)
sox.py [21] 2014-07-15 09:26:58 ~ 2014-07-15 09:27:20 (21.560936)
sox.py [21] 2014-07-15 09:27:20 ~ 2014-07-15 09:27:30 (9.875392)
sox.py [21] 2014-07-15 09:27:30 ~ 2014-07-15 09:28:08 (38.601679)
sox.py [21] 2014-07-15 09:28:08 ~ 2014-07-15 09:28:30 (21.885519)
This is python script. It is very simple^^
from subprocess import call
import logging
import datetime
# sensitivity of silence recognition
threshold = '3%'
if __name__ == "__main__":
logging.basicConfig(filename='sound.log', format='%(filename)s [%(lineno)d] %(message)s',
level=logging.INFO)
while (True):
start_now = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
start_time = datetime.datetime.now()
start = start_time.strftime('%Y-%m-%d %H:%M:%S')
output_filename = 'rec%s.wav' %start_now
cmd = ['rec', '-c', '1', output_filename, 'rate', '8k', 'silence', '1', '0.1', threshold, '1', '3.0', threshold]
call(cmd)
end_time = datetime.datetime.now()
end = end_time.strftime('%Y-%m-%d %H:%M:%S')
logging.info("%s ~ %s (%f)" %(start, end,(end_time-start_time).total_seconds()))
Note: I know there is a pysox, but it does not work in my environment. As another solution, pyaudio can be used to implement this function. But sox makes simple everything.
Conclusion
sox is very awesome tool. I have tried to make this software for more than a month. Before I knew sox, it was very difficult and I did not know where I started from. But, after knowing sox, it became easy. This simle script can be a useful tool to monitor you house or other place.
1 comment:
After you have used your Android smartphone or tab for a while, it accumulates a large volume of trash in its system. This large sum of trash causes frequent freezing, lagging and slowing down of the Android system.
This gives rise to a series of questions; how to delete trash android? Where are the deleted files? Doesn’t Android have a recycle bin? No. Unlike Windows and Mac computers, Android phones and tabs do not have a recycle bin.
There is nothing to be worried though. Android has its own set-up for you to find trashes and empty them without having to go through a recycle bin.
Post a Comment