Thoughts of a random geek

Tuesday, November 18, 2008

Windows Sockets

MSDN on winsock

CodeProject C++ skeleton using winsock

Of note: An application must call the WSACleanup function for every successful time the WSAStartup function is called. This means, for example, that if an application calls WSAStartup three times, it must call WSACleanup three times. The first two calls to WSACleanup do nothing except decrement an internal counter; the final WSACleanup call for the task does all necessary resource deallocation for the task.

Also: If you include windows.h and winsock2.h, you have to put winsock2 first. Otherwise, windows.h will include winsock.h and you'll get a plethora of redefinition errors from winsock2.h.

Inter-process communication in Windows

MSDN on the many ways to do IPC, including links to each.

Abridged version -
  • memory mapped files are fast, but need wrapped in a mutex and can't be used over a network
  • WM_COPYDATA message is useful if you have a message pump. Windows automagically shares the address space for you.
  • Mailslots can broadcast to multiple receiving processes and work on a network
  • Pipes are guaranteed two-way communication and work over a network
  • Sockets are platform independent

There's more, but I don't have experience with DDE/COM.

Here's an awesome codeproject page discussing multiple ways to inject your code into another process, including how to subclass a control (like the Windows Start button, in the example it switches the left and right mouse buttons). The page is extremely informative and uses some IPC techniques.

Monday, November 10, 2008

High-precision timing in Windows, cont.

Building on what I found earlier, here are some more attempts to make a high precision, high accuracy, high performance timer in Windows.

This link explains another timer, and provides BSD-licensed code.

ByteFusion makes a proprietary timer.

SourceForge has a project called fasttime, but it doesn't support Windows.

Fasttime has a sister project, TSC-I2, which has some nice details.

Here is the documentation for the Windows NTP client, w32tm, the Windows Time Service.

An MSDN blog indicates that the Windows Time Service is intended to keep sync only to the extent that Kerberos requires, and cannot maintain sync down to the second boundary.

Friday, October 31, 2008

Android FTW

his is why I've been waiting for Android. I don't care how good or bad the phone is. I want to write programs for my cell phone. And I'm not the only one. There are MANY developers all over the world who have itches they want to scratch. Give them the power and you will see amazing things.


Take a picture of a barcode and the phone automatically looks for a better price online and in brick-and-mortar stores. It can show you which stores are closer to you, so you can get the best price/distance.

http://blogs.dailynews.com/bargain/2008/10/google-phone-finds-the-best-pr.html



Somebody already made my location-aware ringer. It's called Locale.

One project named GeoLife gives users a way to set to-do lists and get reminders on their phones. Walk by the market, and the device might buzz with a message that you're supposed to pick up milk. Another effort, named Flare, was designed to help small businesses like pizza shops cheaply track their drivers.

Then there was Locale, which lets users configure their phones to automatically adjust their settings when the devices detect themselves in certain zones. So you might set your phone to automatically go into vibrate mode in the office and silent mode at the movie theater, and ring everywhere else.

The other student projects included Re:Public, a social-networking program that helps people make new friends in their area. Loco offers a way to find events around town and invite other people. Snap guides users to interesting places in their vicinity.

And there was KEI, pronounced "key," because that's what it is: software that enables a cell phone to unlock your car. It was the lone entrant not to tap the location craze.


http://www.msnbc.msn.com/id/24582069/

Wednesday, October 29, 2008

10/29/1675 - Liebniz sums it all up

Gottfried Leibniz was a many of many talents. The number of fields he was interested in was quite vast. While you might thank Newton for the calculus, it turns out Leibniz worked with Newton for a while. In fact, it is Leibniz's notation, with the integral sign and the differential, which we use today. Wired has a nice summary of Leibniz up today.

Friday, October 24, 2008

Time Reversal and Frequency Response

Someone on comp.dsp wrote a paper about time reversal and the effect on frequency response (PDF). It's interesting and doesn't require expertise in mathematics, but it does expect you to understand calculus. Basically, the magnitude is the same but the phase is negated, and in the discrete case there's also a time-domain right-shift that creates a frequency-dependent term in the phase.

High-precision timing in Windows

MSDN's Johan Nilsson on timing

Turns out the Windows System Time (GetSystemTime, which is actually under the hood of the .NET DateTime class) only has an effective resolution of 10-15 milliseconds. Sure, the data returned by GetSystemTimeAsFileTime has units of 100 nanoseconds, but if you were to repeatedly call the function you would find that in practice it only updates after many milliseconds.

The solution? To use QueryPerformanceCounter to supplement the times returned by GetSystemTime(). But now we have to call two functions in series, so the scheduler could pre-empt us, there's interrupt overhead, etc. Dual processor systems can mitigate this problem by using SetThreadAffinityMask to control for those factors. Otherwise, this limitation will put something of a floor on maximum performance.

But there's a problem with that, too - QPC was designed to count clock cycles, not long periods of time. The oscillators in PCs have terrible drift; the author notes that the time reported by QPC differed from the System Time by one millisecond in less than two minutes. This is consistent with my own timing observations. The author handles this by compensating for errors in QueryPerformanceFrequency. One caveat that I should add is that the error in reported frequency will change with time. I once studied the drift and it changes with a period of 24 hours. I'm pretty sure it peaks during the day and troughs at night, but I may have that backwards.

It also turns out that QPC can jump forward unexpectedly under heavy PCI bus load. You also have to account for NTP potentially changing the System Time behind your back, though it (usually) sends a WM_TIMECHANGE message to all top-level windows when it does this.

Finally, the author mentions that in uniprocessor systems, QPC doesn't directly read the Pentium Time Stamp Counter (TSC), but in multi-processor systems it does. QPC therefore has "poor" performance in a uniprocessor system, and the author created an experimental version of the time_provider which directly reads the TSC which has much better performance.

Tuesday, October 21, 2008

Calculating logarithms and exponentials without multiplying or dividing

How to calculate a logarithm or an exponential without using multiplies

Pretty nifty stuff. The author iterates through a lookup table with addition, subtraction, and bit-shifts to compute 32-bit fixed point exponentials and logarithms for base e (can easily be converted into other bases too) . Additionally includes some notes on residual error.

There is some confusion in the section on logarithms. The C code example initializes y = 0xA65AF, which in 16-bit integer 16-bit fraction (16.16) is about 10.39721. Why? Because e^10.39721 is about 32,768, and the algorithm requires that we divide x by something to make it smaller than zero. Thus, it cannot compute ln(x) for x>32767.

The author also says that x is in 16.16 format, but then treats x as if it were in 1.31, and x is never divided by 32,768! Why? It turns out that treating a 16.16 number as if it were 1.31 is the same as dividing by 32,768!

Finally, by adjusting the lookup table, exponentials and logarithms for other bases (2, 10, pi, whatever) can be computed as well.

Saturday, October 18, 2008

FLV and other video editing

I recently found myself with the desire to cut and stitch FLV files. Google lead me to discover that one can use SUPER to change back and forth between FLV, AVI, and plethora of additional formats. The GUI interface is awkward but it does the job quite well.

RichFLV (I suggest googling to make sure you get the current version) requires Adobe AIR in order to run, but it's a good way to quickly cut/stitch/etc FLV files. It doesn't work well on files that are very large (100+ MB), so you should use FLV Parser to split them into manageable chunks