new to SFML 2.4.2,and I did'nt find some good example about how to do this.
I found some topic about soundstream without full coding.
and some example from github like this.
https://github.com/slallement/SoundSynthIt works but not the way I want.
The note seems to be data fixed length and add to the buffer and can not continuous write data to the buffer.
What I need is something like bass audio library.
http://www.un4seen.com/they have a very good example called synth.
some code like below
stream=BASS_StreamCreate(info.freq,2,0,(STREAMPROC*)WriteStream,0); // create a stream (stereo for effects)
BASS_ChannelPlay(stream,FALSE); // start it
DWORD CALLBACK WriteStream(HSTREAM handle, short *buffer, DWORD length, void *user)
{
int k,c,s;
float omega;
memset(buffer,0,length);
for (k=0;k<KEYS;k++) {
if (!vol[k]) continue;
omega=2*M_PI*pow(2.0,(k+3)/12.0)*440.0/info.freq;
for (c=0;c<length/sizeof(short);c+=2) {
s=buffer[c]+sin(pos[k])*vol[k];
if (s>32767) s=32767;
else if (s<-32768) s=-32768;
buffer[c+1]=buffer[c]=s; // left and right channels are the same
pos[k]+=omega;
if (vol[k]<MAXVOL) {
vol[k]-=DECAY;
if (vol[k]<=0) { // faded-out
vol[k]=0;
break;
}
}
}
pos[k]=fmod(pos[k],2*M_PI);
}
return length;
}
All the work is in this thread called WriteStream
in the function,you just write and wave formular like sine wave,It just keep producing sound.
and according to the vol array outside,you can control which freq sound to play if your key is down.
What I need it how to write such a non stop, keep writing thread,to fill buffer and playing for ever.