Re: [linux-audio-dev] programming question (w/code)

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: Re: [linux-audio-dev] programming question (w/code)
From: Peter Hanappe (peter_AT_hanappe.com)
Date: Sat Nov 24 2001 - 23:52:01 EET


Paul Coccoli wrote:
> Hello all
>
> I'm trying to learn audio programming the hard way (by just doing it), so
> I wrote a simple tone generating program that reads MIDI input from
> /dev/midi00. The problem is that there's a long delay between when I hit
> a key on my MIDI controller and when the sound actually comes out of my
> speakers. My debugging statements show up right away, however (indicating
> which MIDI message was received). I've tried adjusting the size of the
> buffer that I write to before writing to the soundcard, but nothing seems
> to change it. I can make the buffer as small as 16 bytes and I still have
> the same problem. Perhaps it has something to do with the soundcard's
> buffer?
>
> [cut rest of mail]

Hi Paul,

That's a nice little starter application you're writing and it surely
is the best way to get started. However, there are some improvements
you can make. Others on this list are of better help than I am but
here's my 2 centimes.

The main problem is the read() on the midi file descriptor. In
general, read() is blocking, meaning that your application will just
sleep until there's something to be read. There are system calls that
tell you whether there's any data available to be read. Check out the
man pages for poll or select. (I'd say poll is easier to use than
select.)

So you replace the read() with something like:

poll()
if (midi_data_available) {
   read();
}

My guess is that this is the main problem of you're code. The rest of
my comments are about optimizations that you can make.

The first optimization is to calculate a buffer of samples instead of
one sample at a time. Something like:

while (1) {

   poll()
   if (midi_data_available) {
     read(midifd);
   }

   for (i = 0; i < BUF_SIZE; i++) {
     buffer[i] = sine();
   }
   write(buffer);

}

This will avoid doing a poll() on every sample, which really is an
overkill.

An other optimization is the sinewave calculation. There are a number
of ways of calculating sinewave that avoid a call to the sin()
function. An easy way is to use a table in which you store one period
of a sinewave. There are others techniques that use a recursive
algorithm deduced from oscillating filters. I've you want to embark on
these DSP issues I suggest to take a look at the music-dsp archive and
music-dsp mailing list archive at http://shoko.calarts.edu/musicdsp

Oh, one more thing! A new set of audio driver are developped that are
a Free replacement of the OSS drivers. You might want to check out
http://www.alsa-project.org for more information.

Keep writing music software!

Peter


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : Sat Nov 24 2001 - 22:33:31 EET