On Thu, 2007-11-01 at 15:09 +0100, Jens M Andreasen wrote:
On Wed, 2007-10-31 at 18:48 +0530, Avadhoot Punde
wrote:
Hi all
As part of a bigger project, I need to develop a module which reads
PCM data from a file and feeds it to sound driver. Is it possible to
develop such a module without actually writing device driver, I mean
just by using existing functions such as snd_pcm_open and all that ?
Can someone throw more light on this ?
For an example of how to use ALSA, you may want to have a look at this
kit:
http://amirhirsch.com/ps3synth.tar
I don't know if Avadhoot is still around here? Anyway, although the ALSA
part in this archive (play.c) looks great and should solve your initial
question, at least sine.c is seriously broken. It will run for a few
seconds and then start living a random life of its own ...
An alternative approach could look like this:
--8<------------------------------------ sine.c -------
#include <unistd.h>
// 16 bit Taylor approximation of sin()
float fastsin(float x)
{
float x2 = x*x;
return x*(x2*(x2*(x2*(x2*(1.0/36288.0)
- (1.0/5040.0))
+ (1.0/120.0))
- (1.0/6.0))
+ 1.0);
}
int main()
{
short out[256][2];
float
x = 22000.0, // amplitude
y = 0.0,
a = 2.0 * fastsin(440 * 3.14159/44100);
int i = 0;
for(;;)
{
x -= a*y;
y += a*x;
if(++i == 256) {
write(1, out, sizeof(short) * 256 * 2);
i = 0;
}
out[i][0] = out[i][1] = x;
}
}
--8<--------------------------------------- EOF --------
.. which will run forever as long as you keep the frequency below 1/6 of
the samplerate. The beuty of this variant is that by letting sin and cos
alternate - slightly out of phase with each other - the generated errors
tend to take out each other, stabilizing the system. Well, at least this
is my understanding of the dicussion over at
http://www.musicdsp.org