Show exstream.c syntax highlighted
/*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This program shows how to use the audio stream functions to transfer
* large blocks of sample data to the soundcard.
*/
#include "allegro.h"
#define BUFFER_SIZE 1024
int main()
{
AL_AUDIOSTREAM *stream;
int updates = 0;
int pitch = 0;
int val = 0;
int i;
allegro_init();
al_install_keyboard();
al_install_timer();
if (al_set_gfx_mode(AL_GFX_SAFE, 320, 200, 0, 0) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Unable to set any graphic mode\n%s\n", al_error);
return 1;
}
al_set_palette(al_desktop_palette);
al_clear_to_color(al_screen, al_make_color(255, 255, 255));
al_text_mode(al_make_color(255, 255, 255));
/* install a digital sound driver */
if (al_install_sound(DIGI_AUTODETECT, AL_MIDI_NONE, NULL) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error initialising sound system\n%s\n", al_error);
return 1;
}
/* create an audio stream */
stream = al_play_audio_stream(BUFFER_SIZE, 8, FALSE, 22050, 255, 128);
if (!stream) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error creating audio stream!\n");
return 1;
}
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, AL_SCREEN_H/3, al_make_color(0, 0, 0), "Audio stream is now playing...");
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, AL_SCREEN_H/3+24, al_make_color(0, 0, 0), "Driver: %s", digi_driver->name);
while (!al_key_pressed()) {
/* does the stream need any more data yet? */
unsigned char *p = al_get_audio_stream_buffer(stream);
if (p) {
/* if so, generate a bit more of our waveform... */
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, AL_SCREEN_H*2/3, al_make_color(0, 0, 0), "update #%d", updates++);
for (i=0; i<BUFFER_SIZE; i++) {
/* this is just a sawtooth wave that gradually increases in
* pitch. Obviously you would want to do something a bit more
* interesting here, for example you could fread() the next
* buffer of data in from a disk file...
*/
p[i] = (val >> 16) & 0xFF;
val += pitch;
pitch++;
if (pitch > 0x40000)
pitch = 0x10000;
}
al_free_audio_stream_buffer(stream);
}
}
al_stop_audio_stream(stream);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here