Hi,
I'm experimenting with a little
applet that makes the keyboard function as a musical keyboard. I'm wondering what is the most effective way to do this, so to keep the latency to a minimum (i.e., the time between pushing a key and hearing the sound).
Presently, I have a waveform stored in an int[] array, and when I want to hear it, I just cycle through it. When I want to change the pitch, I cycle through at some fixed number of steps. The waveform is pre-rendered to a very high resolution so that this skipping through it doesn't produce noticable distortion.
My dataline is opened with a buffer of 2/10ths of a second worth of bytes, and there is a loop that writes to the buffer a total of 1/10th-sec of bytes with each iteration, copying into the buffer my waveform in 1/10th sec blocks.
If you have worked with sound, probably you can guess that this technique causes a bit of latency. If I try to make the buffers any smaller, though, the sounds get chopped up.
Somehow there needs to be a balance between size of buffer, which gives more continuity in the sound, and immediacy of response, so that the user can feel like he's playing an instrument.