Tuesday, December 23, 2008

User Timer in Linux/Unix


#include <sys/time.h >
#include
<signal.h >
#include <stdlib.h >
#include <stdio.h >

#define MAX_COUNT 100

/* This flag controls termination of the main loop. */
volatile sig_atomic_t counter = 0;

/* The signal handler just clears the flag and re-enables itself. */
void
catch_alarm (int sig)
{
counter++;
signal (sig, catch_alarm);
// printf("\nCatch an alarm!\n");
}


void do_stuff (void)
{
static unsigned long c = 0;
printf ("Doing stuff while waiting for alarm (myCounter = %u, Counter=%u) \r", ++c, counter);
}

void set_timer_val(struct itimerval *t, const unsigned long ival_usec, const unsigned int val_usec)
{
//period between successive timer interrupts
t->it_interval.tv_usec = ival_usec % 1000000;
t->it_interval.tv_sec = ival_usec / 1000000;
//period between now and the first timer interrupt
t->it_value.tv_usec = val_usec % 1000000;
t->it_value.tv_sec = val_usec / 1000000;
}

int main (void)
{
struct itimerval ival, oval;

/* Establish a handler for SIGALRM signals. */
signal (SIGALRM, catch_alarm);

/* Set an alarm to go off in a little while. */
// one-shot timer
set_timer_val(&ival, 1000000, 500000);
setitimer (ITIMER_REAL, &ival, &oval);

/* Check the flag once in a while to see when to quit. */
while (counter < MAX_COUNT)
do_stuff ();

printf("\n");
return EXIT_SUCCESS;
}

Sunday, December 21, 2008

Creative XtremeGamer on Linux is Ready

The XFi driver is now available from Creative website. It's the first version (I think it is the first collaboration result between CreativeLabs and ALSA community). Although it supports very limited (very basic features: 2.0 speakers, no enhanced audio processing etc.) right now. At least, my OpenSUSE can spit out sound!

Get it from here.
extract it, but don't make it yet. Modify ctdrv.h. In my case, it shows like below:


#ifndef CTDRV_H
#define CTDRV_H

#define PCI_VENDOR_CREATIVE 0x1102
#define PCI_DEVICE_CREATIVE_20K1 0x0005
#define PCI_DEVICE_CREATIVE_20K2 0x000B
#define PCI_SUBVENDOR_CREATIVE 0x1102
#define PCI_SUBSYS_CREATIVE_SB0760 0x0024
//#define PCI_SUBSYS_CREATIVE_SB0880 0x0041
#define PCI_SUBSYS_CREATIVE_SB0880 0x6003

#define PCI_SUBSYS_CREATIVE_HENDRIX 0x6000
//#define PCI_SUBSYS_CREATIVE_HENDRIX 0x6003

#define CT_XFI_DMA_MASK 0xffffffffUL /* 32 bits */

That is because when I do:

Make a sound card as default in Linux

In the case that we have more than one sound-card, but to select one of them as the default sound-card, copy the following to ~/.asoundrc. In the example below, it makes the card 1 as the default (type "aplay -l" from shell to see all the cards installed and their associated card #).


pcm.!default {
type hw
card 1
}
ctl.!default {
type hw
card 1
}

Sunday, November 30, 2008

OS-X Bootcamp Missing or Corrupt Issue

I got my OS-X Leopard 10.5 a few days ago. After OSX installation, I went to Preference->Utilities->Boot Camp Assistant and then configured it. After reboot and installed the Windows XP, when my MacBook rebooted, it always failed and showed a message as below:

Windows could not start because the following file is missing or corrupt:
\system32\hal.dll
Please re-install a copy of the above file

I've tried various attempts with no success. I searched Google but nothing really helped solving the issue. Finally, I retried again by restoring the OSX into single partition and repartitioned it using The Bootcamp and chose 32 GB FAT32 partition for the Windows. During Windows Installation, I did not delete the partition created by the Bootcamp, but instead just chose NTFS Quick format. Out of my suprise, It worked.

So, the main issue before was that I should hadn't deleted the BOOTCAMP partition I shouldn't had used Windows' Partitioner) and just straightly reformat it and install the Windows.

It works now, except it couldn't find the audio driver (all the other devices were recognized and installed through the OSX CD).

Tuesday, November 25, 2008

AVI to MPEG mass conversion

Need a FFMPEG installed.

The following command will convert all *.AVI files into SVCD-format (with MP3 audio) files:


find . -iwholename *.avi -exec ffmpeg -target ntsc-svcd -acodec mp3 -y -i '{}' '{}.mpg' \;


To see all supported format on FFMPEG:


ffmpeg -formats

Wednesday, November 19, 2008

Using Vector template


#if defined(__cplusplus)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#else
error "must be CPP-enabled compiler to compile this program"
#endif

using namespace std;


class CPoint {
public:
CPoint(double a, double b) { x = a; y = b; };
friend std::ostream& operator<< (std::ostream& os, const CPoint& val);
double getX() { return x; };
double getY() { return y; };
private:
double x;
double y;
};


std::ostream& operator<< (ostream& os, const CPoint& pt)
{
return os << "(" << pt.x << ", " << pt.y << ")" ;
}


int main()
{
vector<CPoint> points;
vector<CPoint>::iterator it;
int i;

it = points.begin();

for (i=0; i<10; i++) {
it = points.insert( it, CPoint(10.5+double(i)/11, 22.7 - double(i)/13) );
}


cout << "Vector Demo" << endl << "-------------" << endl;
cout << "Vector size = " << points.size() << endl;

for (it = points.begin(), i=0; it < points.end(); it++,i++)
{
// cout << "points[" <<>x;
cout << " " << *it;
cout << endl;
}

for (i=0; i< points.size(); i++)
{
cout << "points[" << i << "] = " << points[i] << endl;
}

return 0;
}