I programmed a small morse program which uses wave files for the beeps, but to save space, I want to create the beeps in realtime using fmod. I searched the forum for a solution, and this was the most promising code I found:
float freq = 1020.0;
signed char F_CALLBACKAPI streamcallback(FSOUND_STREAM *stream, void *buff, int len, int param)
static float t = 0; // time
signed short *buffer = (signed short *)buff;
float speed = freq / (float)FSOUND_GetOutputRate() * 6.28;
for (count=0; count<len>>1; count++)
*buffer++ = (signed short)(sin(t) * 32767.0);
t += speed;
However, I quickly was plagued with timing problems. I needed to fiddle with Sleep(); functions and separate threads a lot to create blocking and non-blocking versions, but actually, I haven’t found a good solution for making functions like:
CreateNonBlockingBeep(int duration, float frequency);
CreateBlockingBeep(int duration, float frequency);
My program is Borland C++ based. Does anyone has an idea? Thanks a lot.
I have a problem with the latency of the stream method. When the user presses a specific key, the program must instantly generate a beep.
I can reduce some of the latency by starting the stream at the program startup, and fill the buffer with a sin wave instantly, when the user presses the beep key. However, with this method I still have too much latency.
My current solution with playing wav files is much faster but of course using wav files results in an unnecessary program file size.
How can I reduce the latency of the stream system so that it is as fast as: FSOUND_PlaySound()
I meant that when I call this function:
Then the beep starts. When I want the beep to stop after one second, then I need to use this code:
But now the code is blocking. When I want a non-blocking version, I need to put that code in another thread. I was wondering whether I can put some code inside the callback function to specify how long the beep should play.
I have another problem. After the sound is playing for a minute or so, then suddenly the frequency of the sound changes… Also when the beep stops because of the Stop function, then the speaker makes a plop. Is it possible to fade the sound in and out?
I am not sure what the callback function actually does. It changes a variable which represents a sine at runtime, right?
Please login first to submit.