Tuesday 24 May 2016

Simple way to record sounds detecting silence

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.
  1. Click Volume icon on your task bar
  2. Click Sound Setting
  3. After opened sound window, Click Input tab.
  4. 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/

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.

How to Check if Your Linux System is 32-bit or 64-bit

00_lead_image_ubuntu_desktop_XXbit
It’s always a good idea to know some basics about the operating system you’re running on your computer. For example, you may need to know whether you’re running a 64-bit or 32-bit system so you know which file to download for a program you want to install.
We will show you several different ways of checking whether your Ubuntu system is 32-bit or 64-bit. Some provide additional information beyond whether the system is 32-bit or 64-bit.
The first two methods involves the “uname” command, which prints system information to the screen. If you want more information than just whether your system is 32-bit or 64-bit, type the following command and press Enter.
uname –a
The following information is printed to the screen in the following order: kernel name, network node hostname, kernel release, kernel version, machine hardware name, processor type, hardware platform, operating system. You can find out what the Linux kernel is and what it doesat How-To Geek.
The machine hardware name lists whether your system is 32-bit (“i686” or “i386”) or 64-bit (“x86_64”). Notice that the processor type and hardware platform also indicates 32-bit or 64-bit.
01_uname_a_command
To use the “uname” command to only find out whether your system is 32-bit or 64-bit, type the following command and press Enter.
uname –m
This displays only the machine hardware name and indicates, as above, whether your system is 32-bit (“i686” or “i386”) or 64-bit (“x86_64”).
02_uname_m_command
The “arch” command is similar to the “uname -m” command and prints to the screen whether your system is 32-bit (“i686”) or 64-bit (“x86_64”). Type the following command and press Enter.
arch
03_arch_command
You can also use the “file” command with a special argument (“/sbin/init”) to find out whether your system is 32-bit or 64-bit. Type the following command and press Enter.
file /sbin/init
The following output is printed to the screen. The text outlined in red indicates whether your system is 32-bit or 64-bit.
04_file_sbin_init_command
If you would rather use a graphical tool to find out whether your system is 32-bit or 64-bit, you can use the “System Settings.” Click the “System” menu button (gear button) in the upper-right corner of the screen and select “System Settings” from the drop-down menu.
05_selecting_system_settings
On the “System Settings” dialog box, click “Details” in the “System” section, as shown below.
06_clicking_details
The “Details” screen displays. On the “Overview” screen, the “OS type” is listed as either “64-bit” or “32-bit,” along with other basic information about your Ubuntu system.
07_closing_settings
Done.

How to Install and Manage Snap Packages on Ubuntu 16.04 LTS

Ubuntu 16.04 LTS introduced “Snap” packages, which are a great new way of installing apps. Snaps require different terminal commands–apt-get and dpkg will only allow you to install .deb packages the old way, not Snaps.
Snaps–which have the “.snap” extension–are more similar to containers. Applications in Snaps are self-contained, include all the libraries they need to function, and are sandboxed. They’ll install to their own directory and they won’t interfere with the rest of your system.
Not all apps are available as snaps just yet, but if you come across one that is, here’s how to install it.
RELATED ARTICLES
Ubuntu 16.04 Makes Ubuntu Exciting Again
Ubuntu hasn’t had the best reputation among Linux users over the past few years–with some even going so far as... [Read Article]
How to Move the Unity Desktop’s Launcher to the Bottom of Your Screen on Ubuntu 16.04
Ubuntu 16.04 LTS includes a long-awaited feature: You can now move the Unity desktop’s launcher to the bottom of your screen.... [Read Article]
Search for Available Snap Packages
To see a list of all available packages in the store, open a terminal and run the following command:
snap find
To search for a specific package by name, just add your search term to the end of the snap find command:
snap find name
For a more complete search–searching package descriptions as well as package names–just pipe the output of the snap find command through the grep filtering tool, like so:
snap find | grep search

How to Install a Snap Package

To install a Snap package, use the following command, specifying the package by name. Because this makes changes to the system, you have to add a sudo before the command to run it with root privileges.
sudo snap install package-name
The snap command will download and install the snap package you specified, displaying the progress in the terminal window.
You can launch the application you installed like any other application. If it’s a graphical application, it should appear in your desktop’s applications menu. Otherwise, just start typing the application’s name at the terminal and press the “Tab” key to automatically complete it. You can then press Enter to launch the application or run the command you installed.

How to Update Snaps

To update an installed Snap package, run the following command, specifying the package’s name. If a new version of the Snap is available, it will be downloaded and installed.
sudo snap refresh package-name
There doesn’t appear to be a command that updates all installed Snaps at the moment, but we wouldn’t be surprised to see one added in the future.

How to List Your Installed Snaps

To list your installed Snap packages, run the following command
snap list
You can use this command to search your installed packages, too–just pipe the output through grep again:
snap list | grep search

How to Remove a Snap Package

To remove an installed Snap package from your computer, run the following command:
sudo snap remove package-name

View Recent Changes

Run the following command to view a list of system changes. This displays a list of the Snap packages you’ve recently installed refreshed (updated), and removed, along with the times those operations took place.
snap changes

See More Operations

To see more snap command operations, view the snap command’s manual with the following command. Use the arrow and page up/down keys to scroll through the manual. press the “q” key to quit when you’re done.
man snap
Ubuntu’s developers will likely continue working on the Snap package format and associated tools, so we’ll likely see more command-line options for working with Snap packages in the future.

If you’re interested in creating your own .snap packages, consult Ubuntu’s Snap documentation for more details.