Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 4 Audio, playing given frequency

This thread is locked; no one can reply to it. rss feed Print
Allegro 4 Audio, playing given frequency
zvfccfsm
Member #16,659
April 2017

Hi. I dont know why my code doesn't work. Could you tell me the easiest way to generate sound by frequency?

#SelectExpand
1#include <allegro.h> 2 3int main() 4{ 5allegro_init(); 6install_keyboard(); 7 8set_color_depth(32); 9set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); 10 11install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, "" ); 12set_volume( 255, 255 ); 13 14BITMAP *buffer = create_bitmap(800, 600); 15clear_to_color(buffer, makecol(0,0,0)); 16 17play_audio_stream( 1024, 8, FALSE, 440, 255, 128 ); 18 19while (!key[KEY_ESC]) 20{ 21blit(buffer, screen, 0, 0, 0, 0, 800, 600); 22clear_to_color(buffer, makecol(0,0,0)); 23} 24 25destroy_bitmap(buffer); 26return 0; 27} 28END_OF_MAIN();

Polybios
Member #12,293
October 2010

You could have a look at the sine function from Allegro5's ex_synth.cpp (link).

In any case, you need to generate the sample data yourself. The freq parameter of A4's audio stream specifies the sample rate, not the frequency of the sound that is going to be played.

zvfccfsm
Member #16,659
April 2017

Ehh i dont like Allegro5.
I avoided my problem by making 1Khz sample in Audacity. I loaded it and then:

#SelectExpand
1stop_sample( dzwiek ); 2play_sample( dzwiek, 255, 128, 440, 1 );

I do it in timer loop 2 times per second.
But it's still not convenient way. :-/

Go to: