|
[A5] Beep sound function. |
joecarl
Member #14,983
March 2013
|
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: 1void PlaySound(int nota, int time, int octava) {
2 int frec=(int) (440.0*pow(2.0,(float) (octava-3.0+(nota-10.0)/12.0)));
3
4 unsigned int samples = float(time)/1000.0 * frec;
5 unsigned long sample_size=al_get_channel_count(ALLEGRO_CHANNEL_CONF_1) *al_get_audio_depth_size(ALLEGRO_AUDIO_DEPTH_INT16);
6 unsigned long bytes = samples * sample_size;
7 void *buff = NULL;
8 buff = al_malloc(bytes);
9
10 ALLEGRO_SAMPLE *beep = NULL;
11 beep = al_create_sample (buff,samples, frec,ALLEGRO_AUDIO_DEPTH_INT16,ALLEGRO_CHANNEL_CONF_1,true);
12 unsigned char* ptr;
13
14 if(beep!=NULL){
15 ptr = (unsigned char*) al_get_sample_data(beep);
16 for(int i=0;i<samples;i+=2){
17 ptr[i]=100;
18 ptr[i+1]=0;
19 }
20 al_play_sample (beep,1.0,0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);
21 al_rest(time/1000);
22 al_destroy_sample(beep);
23 }
24}
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. |
Elias
Member #358
May 2000
|
I didn't look closely, but time/1000 is always 0 so that would be one reason that code could never play sound. -- |
joecarl
Member #14,983
March 2013
|
Thanks for your answer. I replaced this line: with this: but I still get no sound. |
Elias
Member #358
May 2000
|
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] -- |
l j
Member #10,584
January 2009
|
It should be Also 100 is a small number.
|
André Silva
Member #11,991
May 2010
|
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?
|
Elias
Member #358
May 2000
|
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. -- |
|