Tuesday, August 5, 2014

Very light http server: lighttpd

The first thing that I wanted to do after setting up an Access Point on my laptop's wifi (See the previous post) is to setup an http server which is extremely light. My intention was to display some text, maybe some nice picture or a file-list of a directory. So, here is the solution, with a punny name:

https://wiki.archlinux.org/index.php/lighttpd
pacman -S lighttpd



The config file is located at:  
/etc/lighttpd/lighttpd.conf
Check if config file's syntax is correct:  
lighttpd -t -f /etc/lighttpd/lighttpd.conf
Default location of index.html file, according to the default config:
/srv/http

To create the index.html file:
echo 'TestMe!' >> /srv/http/index.html
chmod 755 /srv/http/index.html


Starting and reloading:
systemctl start lighttpd
systemctl reload lighttpd


Enabling over boot:
systemctl enable lighttpd

For PHP, SSL etc, check out the documentation home page.

Creating an access point in your system

If you wish to share your LAN bandwidth through wifi or just share some file though your wifi in an ad-hoc manner (a reference to wInDoWs users), or I don't know, just to check things out, you might want to look at this project by OBLiQUE that makes it very simple: https://bbs.archlinux.org/viewtopic.php?id=162895

Download the create_ap directory from git or otherwise.
No NAT or bridge needed. The script has options to set them or not. There are important dependencies. Check README or the create_ap file to find out.

Running: 

systemctl disable netctl-auto@wlp6s0.service
# So that is does not conflict with "netctl-auto" managed wlan card wlp6s0

systemctl stop netctl-auto@wlp6s0.service

# So, disabling it stops it from happening over roboots
# Stopping it actually stops the service

ifconfig wlp6s0 up
# To bring the interface up (important)

Then traverse the directory and see help to set up aps.
For example, the share your LAN's (eth0) internet through wifi (wlp6s0) access point called "koboglo" with passphrase "lenovo1234", use:
./create_ap wlp6s0 eth0 koboglo lenovo1234 

I need to use --no-virt option to make it work depending on my wlan card. i.e.

./create_ap --no-virt wlp6s0 eth0 myaccesspt thebigpasswd

All about wifi

Most of the information presented here are taken from and are no richer than: https://wiki.archlinux.org/index.php/Wireless_network_configuration
But then again, it is a well known fact that setting up/reconfiguring the wifi on your laptop may be one of the most challenging tasks you had to go through at some point or the other in your life. At least I had to. For the first time when you get it working, you ask yourself, "How exactly did it work?" But it's the same as asking "How do magnets work?" and the answer always is "Magic".

So, when you are in the illusion that you have isolated the minimum number of steps to get it working again, you do not bother making them generic or don't care whether they work on other computers or wlan cards. You just stop asking questions, write the steps down and upload it to a place like a blog or something similarly eternal, hoping that you won't have to go through the same nightmare all over again. Until you buy a new laptop.

Anyways, the moral here is: life is too short to rediscover the steps to get your wifi working again.


Vague Theory:

Notation: We are using Arch Linux. The wlan card is: wlp6s0, the profile you want to connect to is: profile.

The program "netctl-auto" automates the connecting process.
"wifi-menu" creates the profile and keeps it in /etc/netctl.
"netctl-auto start/enable profile" attaches the profile to netctl and finally, 
"netctl-auto start/enable netctl-auto@wlp6s0.service" forces wlp6s0 to connect to the internet using the profile. 

This eases up several jobs: 


Setting up wifi for the first time:  
wifi-menu to create a profile,  
dhcpcd wlp6s0 to get ip through dhcp,  
systemctl enable dhcpcd.service to make it work over reboots.  

systemctl start netctl-auto@wlp6s0 to let netctl-auto handle the automatic connection,

systemctl enable netctl-auto@wlp6s0 to make it work over reboots. netctl-auto enable profile to make the profile work over reboots. 

Deleting a profile, while it is already enabled: 

The first step is to tell netctl-auto to stop using the profile. 

netctl-auto disable profile  
should do that. But unfortunately, it still uses the profile somehow. You can verify it by issuing "systemctl restart netctl-auto@wlp6s0" and see that it is again connecting to the same profile. The solution is to delete the profile. 

rm /etc/netctl/profile  
netctl-auto disable profile
systemctl restart netctl-auto@wlp6s0 
and "ifconfig" to verify that the wlp6s0 is free. 

Note that: there is no such method to stop the profile using a single netctl-auto command. (Or, is there?)

Setting up a new profile:

To set up the profile, issue  
wifi-menu

If you get error (maybe through journalctl -xn) that "the interface is already up", it means that the profile still exists somehow. Follow the previous steps of "Deleting a profile while it is already enabled". 

After wifi-menu finishes creating the profile, if "systemctl enable/start netctl-auto@wlp6s0" is already issued, you should be able to connect. Otherwise, issue
systemctl start netctl-auto@wlp6s0 
systemctl enable netctl-auto@wlp6s0 

Optionally, use 
netctl-auto enable profile
to make it an automatic profile. 

Switching to a new profile: 

Use wifi-menu to create a new profile and use  
netctl-auto switch-to new_profile.

Saturday, March 29, 2014

From sound recording to voice/clap activated 9gag scrolling

Record from the microphone in wav format:
arecord out.wav
Or, more conveniently,
rec out.wav

The following commands require the package "sox".

The package sox provides almost everything regarding audio handling. Here is how you can monitor your microphone input without recording it (with a dB monitor, that too, stereo):
rec -n stat

Without the dB monitor:
rec -q -n stat

Show it for 5 seconds then stop:
rec -q -n stat trim 0 5

Repeat after every 5 milliseconds:
while true; do rec -q -n stat trim 0 0.05; done

Repeat after every 5 milliseconds, but only show the Maximum amplitude in that 5ms time interval:
while true; do echo $(rec -q -n stat trim 0 0.05 2>$1 | awk '/^Maximum amplitude/ { print $3}') ; done

Keep recording from standard output and echo 1 if the volume level is above certain threshold   (0.15ms), and return 0 otherwise:
while true; do echo $(rec -n stat trim 0 0.5 2>&1 | awk '/^Maximum amplitude/ { print $3 < .15 ? 0 : 1 }'); done

Following similar command pattern, the following code mutes the master audio level if the input level is more than the threshold, otherwise unmutes:
while true; do amixer set Master $(rec -n stat trim 0 0.5 2>&1 | awk '/^Maximum amplitude/ { print $3 < .15 ? 0 : 1 }'); done

Returns "high" if the amplitude is higher than the threshold, or returns "low":
while true; do ampli=$(rec -q -n stat trim 0 0.25 2>&1 | awk '/^Maximum amplitude/ { print $3 < 0.15 ? 0 : 1 }'); if [ $ampli -eq 1 ]; then echo "high"; else echo "low"; fi; done
(Self-note: Very mysteriously, $3 > 0.15 ? 1 :0 does not work. Why?)

Simulating key stokes:
Package called "xdotool" can send keystroked to X11 like this:
xdotool key k

So, here is the code to simulate a keypress of "j" simulated ny a tap on the mic or a clap:
while true; do ampli=$(rec -q -n stat trim 0 0.25 2>&1 | awk '/^Maximum amplitude/ { print $3 < 0.15 ? 0 : 1 }'); if [ $ampli -eq 1 ]; then xdotool key j; fi; done

This can be used while endlessly scrolling 9gag/facebook page without using hands. Best tool for a forever alone guy.

Friday, March 14, 2014

Splitting a long mp3 and tricks learned in the process

If you have a video, which contains a long audio that you want to split into multiple mp3 files using their time marker (created by you or found anywhere in a form of list), then you might want to proceed in the following way.

First, you have extract the audio in mp3 format by:
ffmpeg -i input_video_file -f mp3 out.mp3

Now the you have out.mp3 that has to be chopped up into pieces. For this, you need a package called mp3splt. Install that. Now you can chop it up manually if using the time markers. See manpage for details. But we are more interested in a more organised way. Generally, there are CUE files corresponding to audio CDs that can be found on the Internet. If you do not have a cue file, but you have the list of time markers and filename list (or song names) corresponding to them, then you can generate your cue sheet from the following website:
http://cuegenerator.net/

There, you need to insert the Tracklist (song name). Optionally, you can enter in  "songname - perfomer" format if you have the performer names also. It should look like:

songname1
songname2 - singer1
songnaem3 - singer2
songname4
songname5 - singer3


The Timings field has to be filled up in the suggested way (found in the "Help" link next to it). I followed the format: (for example)

Marker 01       00:00:00:00
Marker 02       00:04:12:00
Marker 03       00:09:56:00
.
.
.
Marker 22       01:12:56:00

Here, the time format aa:bb:cc:dd is given by hour:minute:second:milisecond. There are TABs after Marker and marker number, not spaces.

Creating strings following this certain formatting took a lot of time. I copied a list found in the about section of an youtube video in the following format:

time -- song_name -- optional_performer

I copied that into vim and these vim tricks came handy:

Creating visual blocks: Ctrl + v
Aligning text w.r.t. the character --:     :Align --

You need to have the plugin Align installed to use this command. It can be found here: http://www.vim.org/scripts/script.php?script_id=294

The installation instruction is given there. Just download the gz file. Then do vim file.gz. Then you are in vim file browser mode. do :so % and quit.

Insert a string str in front of every line in a visual block:

1. Select the visual block
2. Press I. It will take you to the beginning of the first string of the first line of the visual block.
3. Enter the string str.
4. Press Esc

Similarly, making any change in every line of a visual block involves similar steps.

~ When you have the strings organised in the desired way, copy them to the website to generate the cue file.

Once you enter the information, you can download the .cue file. Let's call it list.cue.

Now, we want to create a directory named inside which all the files will be stored with the following filenames:
-.mp3

I'm not interested in performers since that might increase the length of the filename.

So, we issue the following command in terminal:

mp3splt -c list.cue -o @b/@N-@t out.mp3

Here, -o sets the output format.
@b = Title of the album (director named @b will be created automatically)
@N = Track Number
@t = Song title

More formatting options are in man mp3splt.

Wednesday, March 5, 2014

Password protecting text files in VIM

To encrypt a file while creating:
vim -x filename
(You will be asked to enter a password twice)

To open the file:
vim filename
(You will be asked to enter the password)

To edit the file and save it with the same password:
vim filename
When finished editing, enter :wq!

To change the password:
vim +X filemane

Tip: to verify that the file is encrypted, always check by cat filename

Detect filetype in terminal even without the extension

The command is:

file filename

It will give you useful information about the filetype of filename using its magic numbers. Whatever that means. Of course, man will give you other usage.

Monday, February 24, 2014

Viewing msword, pdf, ps files in linux terminal

The program antiword strips down the formatting and other fancy stuff off the file and shows a pain text in the terminal. Perfect for quick viewing and preserves the spirit as the name suggests.

Sunday, January 26, 2014

Canon RAW files(CR2) handling in Linux

Since I'm planning to shift to Arch Linux, handling raw files is one factor I would like to be fluent with.

Here are the necessary packages:

Converting CR2 to jpg or other readily viewable format: dcraw
Conveniently viewing the thumbnail or jpg files: feh
Merging to HDR: luminancehdr
Major Editing: gimp, ufraw, dcraw-gimp

Since I have no graphical file browser, viewing the raw files in a directory is out of question. Once I have copied all the CR2 files to a directory, I would like to view them in jpeg format.

Extracting Thumbnail:

The command dcraw -e *.CR2 extracts a jpg from the raw file and saves it as IMG_####.thumb.jpg in the same directory. We would now like to view these jpg files in a simple image viewer and find out the raw file(s) we would like to edit.

feh *.jpg opens a single window with all the images in its filelist. Here are the most frequently used commands which also can be found in man feh:

  • Zoom In, Zoom Out: Up_arrow, Down_arrow
  • Zoom to fit: /
  • Zoom to 100%: *
  • Moving the picture: Ctrl + Arrow_keys
  • Locking the zoom and offset value: k
  • Toggle Filename: d
  • Toggle EXIF: e
  • Next, previous image: Left_arrow, Right_arrow, n, p
  • Jump to 5% of the filelist: pageup, pagedown
  • Go to first, last image: Home, End
  • Rotate Image: <, >
  • Toggle slideshow: h
  • Increase, Decrese reload time: +, -

From the image preview, looking at the filenames, we now know which files to edit.

HDR:

luminance-hdr is a graphical HDR creating software. It is pretty much automated and used KDE-like file browser in its file list. So, naturally it is bulky with respect to Arch Linux's lightweight-ness. An alternative is enfuse which I am willing to try.

Alternative: In magiclantern, in the shoot menu, there is an option to generate a post-script corresponding to a bracketing sequence. This script file can be collected and  ran in linux to merge raw files into HDR using enfuse tools. This script can reduce the work load since it contains the list of photos clicked in the bracketing sequence. This makes the previous step of extracting thumbnails useless, provided magiclantern is used properly.

Editing:

Since GIMP cannot handle CR2 files, we need to install some plugins to convert the raw into usable format. ufraw, dcraw-gimp (decide which one is applicable/ useful) are the necessary plugins. If you have used lumance-hdr to generate a gimp-usable file, these plugins are not required.

Tuesday, January 21, 2014

Installation of Arch Linux


Without being generic, I'm explaining the steps I used to install archlinux in my laptop. While applying to other hardware, most of it must be unchanged except the nvidia driver part.
Completeness:
I haven't covered the installation of GRUB since I use the grub of another linux installed in my computer. Also, some steps about local time etc were omitted since they do not affect me much. There might be some other steps which are specific to my laptop or my preferences which can be omitted or modified when if desired.
Process:
Here is the link to download the zip file of these 6 files together: instseq.zip

In case the link s unavailable, here is the merged content of these files, separated by a long array of hashes. They should be saved in different files called 1, 2, ..., 5 (say) and ran in the natural order. Sometimes, a file ends echoing a command which have to performed manually. It is strongly advised to edit and modify the files before running to avoid deletion or alteration of important data.




After the installation, the linux should have a working awesome window manager. For personalisation, I use the following files:
1. bashrc
2. prettify

Update: Broadcom wifi drivers are working properly after updating. But gtk2 ran into an error while updating apart from some conflicts which I resolved. Apparently, gtk2 was installed by firefox, not by the standards package installation hence there was an ownership problem, it seemed. Finally the documentation of Arch Linux saved me. Since gtk2 was the only package having problems while updating, a simple pacman -Syu --force gtk2 fixed everything.(It gives pacman the right to replace existing files of the corresponding packages which was the problem to begin with.)

Update: The following codes in the ~/.inputrc will give you incremental history search facility in xterm which means you can type a few characters of your command and press up/down arrow to navigate through your terminal history with matching initial characters: