Archive
Cheat Sheets
Visit http://www.cheat-sheets.org/ for an extensive list of cheat sheets for various programming languages and applications.
“All cheat sheets, round-ups, quick reference cards, quick reference guides and quick reference sheets in one page. The only one you need.“
Help in bash
Did you know that bash has its help too? Just simply type in:
help
It will show the version number and the list of its internal commands.
This thing is so trivial that in my opinion most people don’t even think of it. I just found it by accident too :)
Update (20110416)
If you only want to figure out the version number of bash, here are the alternatives:
bash --version- “\C-x\C-v”, i.e. CTRL+X CTRL-V (see
bind -Pfor full list of shortcuts)
Update the database of the command ‘locate’
Here is how to update manually the database of locate:
sudo /usr/bin/updatedb
updatedb is usually run daily by cron to update the default database but if you don’t want to wait, you can launch it manually too. For instance, once I installed a package and I wanted to figure out the path of a specific file. Of course, it was not yet in the database.
The tee command
Problem
You have a program/script that prints some output to the stdout. You redirect the output to a file but since the execution time is slow, you would like to see its progress too on the standard output. That is, redirect the output to a file and to the standard output too.
Solution
You’ll need the tee command. Example:
C:\tmp> date >d.txt C:\tmp> date | tee e.txt Wed Mar 30 22:40:22 EDT 2011 C:\tmp> cat e.txt Wed Mar 30 22:40:22 EDT 2011
It was a simpe example. As I mentioned, it is more useful when you run a program whose execution time is slow. This way you can see its partial results.
Troubleshooting:
It can happen that you see no output on the stdout. You know that the program should have printed something, but nothing appears on the stdout. It’s because of buffering. When you use a pipeline, the shell will do some buffering. If you don’t need that, you can use autoflush in your program/script.
Alternative way:
If you already launched the program (and redirected its output to a file) and you don’t want to restart it with tee, there is another solution. You can monitor the changes of the output file with “tail -f output.txt“. It will print the contents of the file till its end and it will be waiting. When some new lines are added to the file, “tail -f” will print them and wait, etc. Since it never quits, you can terminate it with CTRL+C.
Moving projects to GitHub
A few days ago I decided to learn Git. This is something that I’ve had in my mind for a long time. Also, I will move my projects to GitHub (here is my profile page).
Useful links
Start with:
- Pro Git book (Free, available in HTML. A very nice introduction.)
Read on:
- Git HQ (official home of Git)
- Learn.GitHub – Introduction To Git (with screencasts)
- A few of my Git tricks, tips and workflows (@reddit)
- Git Reference
- git ready » learn git one commit at a time
- Aha! Moments When Learning Git | BetterExplained
- Bart Trojanowski’s Git tutorial (“Git the basics”, video + PDF, held in July 2008)
- Hogyan használjuk a Git verziókezelő rendszert (in Hungarian)
Recording soundcard output
Problem
I wanted to capture and record any audio output that comes through my soundcard. It is especially useful when, for example, you listen to a radio station that has a Flash player.
I have had this problem in my head for years when recently I came across the article “Linux: Recording soundcard output using arecord” written by Karthik. However, it seems that Karthik’s blog is abandoned :( Let’s hope he is all right.
This post will consist of two parts. In Part 1, I will sum up the results of Karthik. In Part 2, which is my own contribution to the topic, I will present some scripts that can do all the necessary changes of Part 1.
Part 1
As I mentioned, this section is based on this article and its comments.
This solution works with ALSA drivers. We will use the commands “arecord” and “amixer“, they come with ALSA.
First, create the file $HOME/.asoundrc with the following contents:
pcm.copy {
type plug
slave {
pcm hw
}
route_policy copy
}
This specifies a PCM device called ‘copy’.
Execute the command “amixer contents“. It will display all controls applicable to the soundcard. We need the “Capture*” controls. Here is the relevant output on my machine:
numid=24,iface=MIXER,name='Capture Source' ; type=ENUMERATED,access=rw------,values=2,items=8 ; Item #0 'Mic' ; Item #1 'CD' ; Item #2 'Video' ; Item #3 'Aux' ; Item #4 'Line' ; Item #5 'Mix' ; Item #6 'Mix Mono' ; Item #7 'Phone' : values=0,0 numid=25,iface=MIXER,name='Capture Switch' ; type=BOOLEAN,access=rw------,values=1 : values=on numid=26,iface=MIXER,name='Capture Volume' ; type=INTEGER,access=rw---R--,values=2,min=0,max=15,step=0 : values=10,10 | dBscale-min=0.00dB,step=1.50dB,mute=0
The value of “Capture Source” is 0, i.e. recording is done from the microphone. “Capture Switch” is on, and “Capture Volume” is 10.
Here is how to set to record from the soundcard. On my machine, the value of ‘Mix’ is 5, thus I need to execute the following command:
amixer cset numid=24,iface=MIXER,name='Capture Source' 5
If “Capture Switch” is off, turn it on:
amixer cset numid=25,iface=MIXER,name='Capture Switch' on
Finally, lower the “Capture Volume”, otherwise the recorded output will be noisy. You will have to play with this to find the value that works best for you. For me a low value, about 20%-30% of the maximum, worked fine.
amixer cset numid=26,iface=MIXER,name='Capture Volume' 3
Also, do not set your player volume too high while recording since it will introduce noise into your recording. Set it to 50%-60% or even lower.
Now you are ready to start recording. Try this:
arecord -d 10 -c 2 -f S16_LE -r 44100 -t wav -D copy foobar.wav
It will record a 10 seconds long session in WAV format. If you don’t want to specify the time limit, use the switch “-d 0″ and stop the recording with CTRL + C. You can listen back the recording with “mplayer foobar.wav” for instance.
If it works fine, you can consider to save the output in a compressed form:
arecord -d 0 -c 2 -f S16_LE -r 44100 -t wav -D copy | oggenc -o foobar.ogg -
It will encode your recording to OGG format on the fly. Notice the “-” at the end of the command oggenc, it tells oggenc to read from the standard input. The command oggenc is part of the vorbis-tools package.
If you want to use Skype for instance, don’t forget to set back the recording to your microphone!!!
Thanks to Karthik for figuring all these things out :)
Part 2
As you have seen in Part 1, there are several commands and switches that you need to keep in mind to record your soundcard’s output. To facilitate life, I’ve made some scripts that can do all this job for you.
Download:
Visit https://github.com/jabbalaci/Record-Soundcard-Output.
I copy here the contents of the README file:
Recording soundcard output
==========================
Usage:
./01-verify.py
Verify your current settings. It is a good idea to write down
these settings to know what to restore later.
You can edit 03-Skype-restore-rec-to-microphone.py and specify
these values.
./02-set-for-rec-soundcard-output.py
Change settings for recording soundcard output.
Default values: 'Mix', 'on', and 3. You might want to
change these values in the source.
./04-start-rec-soundcard-output.py
Start recording. The script will ask the name of the
output file. If you press ENTER, the script will generate
a unique name. By default, the output will be stored in the
"output" folder.
./03-Skype-restore-rec-to-microphone.py
Restore settings for recording the microphone. Edit the
source and set those values that you wrote down at the
beginning.
Troubleshooting
Well, it might happen that you have no ‘Capture Source’. It simply means that you had no luck :( I don’t know how to record the soundcard output in this case. Maybe this kind of recording is not supported by your soundcard? I tried this method on four Dell machines with a success rate of 50%. Two of them have no ‘Capture Source’ at all…
You can check the section “Related links” below, one article addresses a similar problem.
Related links
- How to redirect the ALSA output to a file? (He had no ‘Mix’ in the ‘Capture Source’ but he states he could record the soundcard output.)
[ @reddit ]
Lord Raiden’s case with the guitar
Android 16 was programmed in BASIC
Android #16 (a.k.a. C16) is a robot in the Dragon Ball Z series, created by the evil Dr. Gero. While trying to protect #17 and #18 from Cell, he suffers serious damage. While waiting for the Cell Games, Bulma and her father repair him.
In episode 168, we can see Bulma and her father working on him. Bulma manages to access the source code of the programming of #16. From this episode I could catch a screenshot from the screen of Bulma. Hmm, it seems #16 was programmed in BASIC. Was Dr. Gero, the brilliant scientist, the Mastermind behind the Red Ribbon Army a BASIC programmer?
Well, he must have used some low-level methods too to operate the hardware of #16. Thus the programming of #16 must also contain (lots of) assembly. But we also know that Dr. Gero created several androids, so it’s very likely that he developed a robot programming framework that he reused for each of his androids.
Text file encoding
Detect the encoding of a text file:
$ file all.txt all.txt: ISO-8859 text
Get more verbosity:
$ file --mime all.txt all.txt: text/plain; charset=iso-8859-1
Change the encoding of a text file:
iconv --from-code=UTF-8 --to-code=ISO-8859-2 file.txt >tmp.txt
This latter tip is from here.
Update (20110918)
You can also use “chardet” for detecting charcter encoding. Usage:
$ chardet test.txt test.txt: utf-8 (confidence: 0.99)
