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.