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.

Monday, 23 May 2016

Some Nifty udev Rules and Examples

Dynamic device management made easy with udev
Connecting flash drives, hard disks, cameras or mobile phones to your Linux system has never been so easy and manageable, thanks to udev, developed by Greg Kroah-Hartman, Kay Sievers and Dan Stekloff. First implemented in Linux kernel 2.6, udev handles devices that are hot-plugged into a running system, as well as cold-plugged devices (connected before the system is powered on). In this article, we cover how udev dynamically adds device nodes to the /dev directory, and provide some examples of configurations for your use or amusement.
udev stands for “userspace implementation of devfs”. It includes a udevd daemon, configuration files and rule files, which are used to dynamically manage device files in the /dev directory in Linux, in response to uevents generated by the kernel. udev has successfully and completely replaced the older devfs since the Linux kernel 2.6 series.
Why did we need a totally new implementation of device mapping? Why has udev been such a success? The answer involves the history of the Linux device driver mapping scheme.
Each device file is assigned two 8-bit numbers: a major number and a minor number. Each device driver has a major device number; and all device files for devices controlled by that driver have the same major number. Minor device numbers distinguish between different devices controlled by the driver.
In earlier Linux kernel versions, /dev contained one static device file for each device that could be connected to the system (and controlled by a device driver). Unfortunately, this had problems: there weren’t sufficient numbers to handle all the possibilities, especially since the list of device drivers kept increasing. Also, over 18,000 device files consumed too much extra space on the disk. These problems were resolved by adding the ability to ignore the major and minor numbers in udev.
Particularly for hot-pluggable devices like USB hardware, there was no consistency in the naming/mapping of device files to the actual hardware device. For instance, on a system with two USB printers, one might be called /dev/usb/lp0 and the other /dev/usb/lp1 — but it was never certain which printer was referred to by which device node. That could change based on which one was powered on when the computer booted, or was connected first — or could change if they were connected to a USB hub instead of directly to the system USB ports. This often proved frustrating and confusing to users. udev provides a means to assign a persistent name to a device, using the udev rules feature.
Other udev features resolve many problems inherent to devfs:
  • udev works in user-space, reducing kernel code and complexity.
  • •udev provides a way to assign persistent names to devices, independent of the order of connection or location on a bus.
  • •udev dynamically populates the /dev directory with device nodes for only the devices that are actually present/connected to the system. It also enables the assignment of common names to devices via symbolic links to the actual device node.
  • •udev provides complete device information to user-space programs, removing the need to access kernel-space for the information.

How udev works

The udevd daemon listens to a netlink socket for uevents issued by the kernel on the connection or removal of a device. You can monitor these events with the command udevmonitor — run it, attach a USB device like a flash drive, and remove it. (In newer distributions, udevmonitor may not be available — in that case, use udevadm monitor instead of udevmonitor.)
At start-up, udev mounts a tmpfs filesystem for the /dev directory. It then copies device nodes from /lib/udev/device to /dev and begins collecting uevents from the kernel for cold-plugged devices. /etc/udev/rules.d is used to apply device parameters, to create symbolic links, and to take other action. For hot-plugged devices, udevd catches the uevent via D-Bus, and matches the attributes of the new device as exposed in the /sys directory, with udev rules — and then creates the device node in the /dev directory. udev can also load properly-written device drivers for detected hardware, using the modalias mechanism.

udev rules and examples

udev gives you the power to handle your devices by writing your own rules and configuration files. You can override the behaviour of package-supplied rules (usually found in/lib/udev/rules.d), or add custom and specific behaviour to suit your needs. You add your own rules file in /etc/udev/rules.d/ — the directory meant for local/custom rules.
Create your own rules (which assign the name, symlinks, permissions, etc, that you want) in this directory. To ensure your rule file takes precedence, begin the file name with a number that is lower than the rules file you want to override — for example, 10-local.rules.
Here are some sample rules with comments, which may help you write your own rules.
Disable root login until the admin connects his USB drive
BUS=="usb", SUBSYSTEM=="block", PROGRAM="/bin/enable_root_login"
To get this rule to work, you will need to code a program/script with the given name, which obtains the serial numbers of the USB devices connected to the system, and compares each with the serial number of the admin’s USB device, which is known. If matched, this program will delete a line from the /etc/pam.d/login file — auth requisite pam_deny.so so that root login is enabled. There will be no change in the login file if any other USB drive is connected to the system. Conversely, as soon as the USB drive is unplugged, this line will again be added to the login file.
This rule has been checked on RHEL 5.0 and works fine, but when using the su command, or when logging in to single-user mode on boot, this rule will not work. To lock down root login for the su command, you can:
  1. Edit /etc/security/access.conf and add root: ALL
  2. Edit /etc/pam.d/system-auth and add account required pam_access.so as the second line.
  3. Edit /etc/pam.d/su and make account include system_auth the first line of this file.
These tasks will, of course, be handled by the enable_root_login program. After checking if the attached USB device is the admin’s, your program should undo all the changes to the above files, and if the attached device is not the admin’s, then apply the changes to the files mentioned above.
This will still not work in single-user mode, so if you need that locked down, perhaps you can assign a GRUB password to prevent easy access to the single-user mode.
To retrieve information like the serial number, kernel name, vendor ID, manufacturer’s name, etc, for a particular device, you can run the following command:
udevinfo -a -p /sys/block/sdb
In newer distributions, udevinfo may not be available — in that case, substitute udevadm info instead of udevinfo.
Disable all USB ports
BUS=="usb", OPTIONS+="ignore_device"
The effect of this rule will be to disable all devices connected to all USB ports of your system — USB printers, keyboards and mouses will not work. So use it with care!
Disable all USB block devices
BUS=="usb", SUBSYSTEM=="block", OPTIONS+="ignore_device"
This particular rule prevents USB block storage devices from being recognised. This may be useful for information security or confidentiality within an organisation.
Assign a persistent name to your second IDE disk
KERNEL=="sdb", NAME="my_spare"
Adjust sdb if you want to apply this rule to another drive.
Ignore the second USB SCSI/IDE disk connected via USB
BUS=="usb", KERNEL=="sdb", OPTIONS+="ignore_device"
OR
BUS=="usb", KERNEL=="hdb", OPTIONS+="ignore_device"
Add a symbolic link to a specific USB mouse device’s name
SUBSYSTEM=="input", BUS=="usb", SYSFS{serial}=="0000:00:1d.0", SYMLINK+=="MY-USB-MOUSE"
Change device node name based on the manufacturer’s name
BUS=="usb", SYSFS{manufacturer}=="JetFlash", NAME="UNIVERSE"
This rule states that if the name of a USB drive’s manufacturer is JetFlash, the device’s node name will be ‘UNIVERSE’.
Selectively allow USB block devices via a custom program
BUS=="usb", SUBSYSTEM=="block", PROGRAM="/bin/usbc.jar", RESULT!="my", OPTIONS+="ignore_device"
If the output generated by the program is equal to the ‘my’ value that is specified, then allow the USB device; if the output is anything else, then ignore the device.
For further reference, visit: