dockthepod has asked for the wisdom of the Perl Monks concerning the following question:

I've started working (hacking) on a program but realized i have no idea how to do the next part, so i figured i ought to "seek wisdom" at perlmonks.

So here's the deal:

The program is a curses based drum machine, written in, of course, perl. I'm using the excellent CDK (Curses-DevKit) and have hacked up a pretty slick little matrix interface. The program will pipe it's output to CSound, which will handle the audio part.

Now what happens is you create patterns in a matrix and the program *should* step through it at whatever tempo we are going at, see if a note is going to be played, and if so send a little line to the csound program like:

i 1 0 2 3 4
or whatever.

Now this all happens in real time, so i need to have some kind of timer loop that polls the matrix at a regular interval, but doesn't steal all the cycles from the interface.

seems that this is your classic case for a fork with a sleeping child, but my concerns are: will the timing be accurate enough for an audio app ( <3ms or so), and how painful is IPC in perl. i haven't done any IPC programming for quite a while, and would rather avoid it if possible.

is there a magic module that i'm looking for? i looked at Time::HiRes but it didn't seem quite right. (maybe i'm just dumb;-)

Replies are listed 'Best First'.
Re: using timers in a music app
by kvale (Monsignor) on Jul 08, 2002 at 03:26 UTC
    To get high resolution sleep intervals on a Unix system, try Time::HiRes or BSD::Itimer, depending on your OS. To use Time::HiRes to sleep for an interval:
    use Time::HiRes qw( usleep ); usleep ($microseconds);
    Assuming Time::HiRes installs properly, you will get a highly accurate sleep interval. But latency and timing jitter depend on more than this. In a pre-emptive multitasking system with other processes running, it is very hard to predict when, say, Csound will read your command and start generating sound. I have seen jitters of up to 10 msec on my Linux machine. If you need low jitter, I would recommend dumping as many unneeded processes as possible and setting your desired processes to high priority.

    Even better is to have realtime control of your OS. Check out RTLinux for an elegant realtime version of the Linux kernel.

    -Mark