Hi! Im trying to create a function which plays a beep sound (square signal) of a specified frecuency during a specified time.
This code compiles but I hear nothing:
What can I do to solve this?
Any help is appreciated, thanks in advance.
BTW: I'm using Allegro 5.1 under MacOSX 10.6.8.
I didn't look closely, but time/1000 is always 0 so that would be one reason that code could never play sound.
Thanks for your answer.
I replaced this line:
al_rest(time/1000);
with this:
al_rest(float(time)/1000.0);
but I still get no sound.
Well, the sample you produce has the first half filled with silence (you set bytes to 0/100) and the second half unmodified. What you probably could try is something like:
int16_t *ptr = al_get_sample_data(beep); for (i = 0; i < samples; i += 2) { ptr[i] = -32768; ptr[i + 1] = 32767; }
Did you look at ex_audio_timer? [1]
It should be
int16_t *
not unsigned char *
I think.
Also 100 is a small number.
Also, I'm not fully sure, but wouldn't 0 -> 100 -> 0 -> 100 -> 0 -> 100 (or -32768 -> 32767 -> ...) just create a triangle wave? A square wave would have to be bottom -> bottom -> top -> top -> ...
Not to mention that, from what I'm understanding in the code, that would generate a wave with a wavelength of two samples. Which would create a triangle wave with the highest pitch/frequency possible. Can humans even hear the output?
He sets the playback frequency really low so it would indeed create a square wave. The more logical way would be to keep the frequency at 48k Hz and create a normal square wave of course... but his way should work.
[Edit] Actually, if interpolation is on you're right, it should end up as a triangle wave instead.