Sunday, January 25, 2009

Mencoder v4l2 - Solving the crackling noise in audio when recording from tv card


One of the few things that I could not get right for many months on my Ubuntu Linux installation is the ability to record television programmes from my low end Hauppauge WinTV-PCI-FM card. Playback was excellent using tvtime, in fact it was better than playing back on Windows using the manufacturer's software. However, whenever I tried recording TV using mencoder (that comes with mplayer), an obvious crackling/clicking noise was always present in the audio stream. This was a shame as the video recording offered higher quality than what I could record using Windows. Furthermore, I was able to directly record into any format that mplayer was capable of playing.

The command I used is shown below, it outputs the tv card's composite video and audio into the file, test.avi. Thinking it was something to do with my audio sampling settings, for months I was twiddling with the command used for recording to no avail by adjusting the both the sampling rates (in red below) between 38400Hz, 44100Hz and 48000Hz. In short, I was recording using PAL-BG encoding from the television signal with sound coming from the first alsa sound driver device (adevice=plughw.0,0). Read more about the various options here

$> mencoder -tv norm=PAL-BG:driver=v4l2:width=768:height=576:input=2:alsa:amode=1:adevice=plughw.0,0:audiorate=48000:volume=0:immediatemode=0 -af resample=48000:volume=-5:1 -oac mp3lame -lameopts preset=medium:vol=0:highpassfreq=0:lowpassfreq=0 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1200 -vf harddup,crop=720:544:24:16,pp=lb -noskip -ffourcc divx -o test.avi tv://

No matter how many audio filters I applied, I could never remove the crackling noise from the audio stream. Finally, after talking to my dad about how crackling noises may enter recorded audio streams due to too high a sampling rate, I decided to lower the sampling rate from the raw audio input stream differently. Where previously I adjusted the sampling rates between only 38400Hz, 44100Hz and 48000Hz (the only values accepted by the lame audio encoder), I decided to sample the input audio stream at a slightly lower rate and then resample the stream to be acceptable to the lame audio encoder as shown below. The sampling of the audio input is set at 47950 but to be resampled to 48000 for the encoder.

$> mencoder -tv norm=PAL-BG:driver=v4l2:width=768:height=576:input=2:alsa:amode=1:adevice=plughw.0,0:audiorate=47950:volume=0:immediatemode=0 -af resample=48000:volume=-5:1 -oac mp3lame -lameopts preset=medium:vol=0:highpassfreq=0:lowpassfreq=0 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1200 -vf harddup,crop=720:544:24:16,pp=lb -noskip -ffourcc divx -o test.avi tv://

To my surprise, that was it. All the irritating crackling noise disappeared! I can now put the command into a bash script for scheduling the recording of my television programmes in better quality and more flexibility than I can on windows.

Sunday, January 4, 2009

Happy Holidays 2008/09


The happy holidays came and went. Before I know it, it's time to get back to work. There were many gatherings during Christmas, one of the more memorable was at my place.


Heating a pre-roasted turkey in my oven, I had to squish it in as it barely fitted


"Boiled vegetables like broccoli is more healthy!", quips my friend


Boiling the pasta


Add oil while and after boiling to prevent sticking


Getting the sauce ready


The roast turkey, we had a free upgrade to a larger size


The simple spread, not too much, not too little


The must have log cake that came free as well

After Christmas and counting down to the New Year, I spent my last weekend with my partner watching Ip Man, an excellent must watch movie, and tried Sakae's Teppanyaki for the first time. While the teppanyaki did taste great, the value for money was average.


Roasting garlic and frying eggs on the pan for the garlic fried rice


We took a 2 person set, it came with some maki and sashimi


Chef in garlic fried rice action


Garlic fried rice garnished with some miniature shrimp, loads of garlic went into it that kept me awake the whole night


Grilling the rest of the items. Unfortunately, not all belonged to us, guess what is in the dome?


It is actually Chawanmushi but in an onion instead of in a tea cup


Prawns almost done


Frying of vegetables


Lots of garlic in these


Chawanmushi done


Teriyaki chicken, I ate the teriyaki cod fish before I could snap it

Overall it was an overload of garlic, giving me insomnia the entire night. I recommend those going for Sakae's Teppanyaki to go for lunch or an early dinner. Somehow we ended up having Japanese food again for lunch the following day at Sun with Moon restaurant.


A set lunch containing a mushroom fire pot, with unagi on rice



Hokkaido miso ramen set complete with corn and butter. Finally, good ramen!


Comes with these variant of gyoza as well

That ends the happy holidays for 2008/09. Oh well, back to work on Monday!

Saturday, December 6, 2008

Watchmen


I finally completed a reading of "Watchmen", a well acclaimed DC comic written back in 1987. I never knew it existed till I saw the trailer for it's upcoming movie adaptation for March 2009. I must say it is one of the two best comics I ever read with the other being Kingdom Come. Here's the Watchmen trailer that got me interested. The movie adaptation is directed by Zack Snyder that previously did 300. I'm definitely catching this one.



Thursday, December 4, 2008

Hidden multiple argument BAT file execution with VBS


I have been writing a program that calls external programs using batch files in windows. Every time the call is made, the ugly command line box appears. After searching the net it seems that a simple way would be to use Visual Basic Script (VBS) to call the batch file in a special shell. Since the VBS interpreter is available on WinXP and above systems, I decided to try this vbs script that I found somewhere off the net.

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & WScript.Arguments(0) & Chr(34), 0
Set WshShell = Nothing


Put the above code in a file called "invi.vbs" and then call the following from command line:

$>wscript.exe "invi.vbs" "mybatchfile.bat"


The batch file runs without a window! Thinking that this was great, I tried to run a batch file with arguments. Now that fails somehow because the interpreter seems to think that the entire WScript.Arguments(0) is a program name, i.e. "mybatchfile.bat arg1" will be treated as a program name instead of a batch file with a single argument.

Having not written VB since eons ago, I used my limited knowledge to concatenate the WScript.Arguments into a string. Somehow it just worked after replacing "invi.vbs" with my edits below.

Set WshShell = CreateObject("WScript.Shell")
dim toexec
For Each x in WScript.Arguments
   toexec = toexec & x & " "
Next
WshShell.Run toexec, 0, 1
Set WshShell = Nothing


Then, running it without quotes for the batch file, i.e.:

$>wscript.exe "invi.vbs" mybatchfile.bat arg1 arg2

Monday, December 1, 2008

MinGW to MSVC -- the case of rint()


I have recently been working on shifting a project over from compiling with the MinGW implementation of the GNU C++ compiler to the Microsoft Visual C++ (MSVC) compiler. The most glaring difference is the lack of the rint() function in MSVC. rint(x) takes a double, x, and returns the double value of the closest integer to x. Being a lazy bum I opened the math.h file for MinGW to copy the rint() function to a global win32 compatibility header. Unfortunately, it is written using an assembly instruction and a bunch of macros. Not wanting to copy the macros over, I did the next lazy thing and used Google.

More unfortunately for me, I copied a version of rint() that was not compatible with the implementation in MinGW. I had:

double rint(double x)
{
     return floor(x < 0.0 ? x - 0.5 : x + 0.5);
}

It turns out, that this is wrong as -1.5 should be rounded to -1.0 and not -2.0. After wasting a few hours debugging, I finally realised it should be the much simpler:

double rint(double x)
{
     return floor(x + 0.5);
}

So much for being a lazy bum...