GNU Info

Info Node: (openal.info)Trivial

(openal.info)Trivial


Next: Error Handling Up: Example
Enter node , (file) or (file)node

Trivial Example
===============


     #include <AL/al.h>
     #include <AL/alc.h>
     #include <AL/alext.h>
     #include <AL/alkludge.h>
     #include <AL/alut.h>
     
     #include <time.h>
     #include <stdio.h>
     #include <stdlib.h>
     #include <sys/time.h>
     #include <sys/types.h>
     #include <unistd.h>
     #include <sys/stat.h>
     
     #define FILE "sample.wav"
     
     static void init(void);
     
     static ALuint moving_source = 0;
     
     static time_t start;
     static void *data = (void *) 0xDEADBEEF;
     
     static void *context_id;
     
     static void init( void ) {
             FILE *fh;
             ALfloat zeroes[] = { 0.0f, 0.0f,  0.0f };
             ALfloat back[]   = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
             ALfloat front[]  = { 0.0f, 0.0f,  1.0f, 0.0f, 1.0f, 0.0f };
             ALuint stereo;
             ALsizei size;
             ALsizei bits;
             ALsizei freq;
             ALsizei format;
             int filelen;
     	struct stat buf;
     
     	if( stat( FILE, &buf ) < 0 ) {
     		/* file does not exist */
                     fprintf( stderr, "Couldn't access %s\n", FILE );
     		exit(1);
     	}
     
             fh = fopen("sample.wav", "rb");
             if(fh == NULL) {
                     fprintf( stderr, "Couldn't open %s\n", FILE );
                     exit(1);
             }
     
     	filelen = buf.st_size;
     
             data = malloc( filelen );
     
             alListenerfv( AL_POSITION, zeroes );
             alListenerfv( AL_ORIENTATION, front );
     
             alGenBuffers( 1, &stereo);
     
             if( fread( data, filelen, 1, fh) != 1 ) {
     		fprintf( stderr, "Couldn't read %s\n", FILE );
     
     		free( data );
     
     		exit(1);
     	}
     
             fclose( fh );
     
             alBufferData( stereo, data, AL_FORMAT_WAVE_EXT, filelen, 0 );
     
     	free( data );
     
             alGenSources(1, &moving_source);
     
             alSource3f( moving_source, AL_POSITION, 0.0, 0.0, 4.0 );
             alSourcefv( moving_source, AL_VELOCITY, zeroes );
             alSourcei(  moving_source, AL_BUFFER, stereo );
             alSourcei(  moving_source, AL_LOOPING, AL_FALSE);
     
             return;
     }
     
     int main( int argc, char* argv[] ) {
     	ALCdevice *dev;
             int attrlist[] = { ALC_FREQUENCY, 22050,
                                 ALC_INVALID };
             time_t shouldend;
             ALint state = AL_INITIAL;
     
     	/* open device */
     	dev = alcOpenDevice( NULL );
     	if( dev == NULL ) {
     		return 1;
     	}
     
             /* create context. */
             context_id = alcCreateContext( dev, attrlist );
             if( context_id == NULL ) {
     		alcCloseDevice( dev );
     
                     return 1;
             }
     
     	alcMakeContextCurrent( context_id );
     
             /*
              * Setup LOKI extensions
              */
             fixup_function_pointers();
     
             init( );
     
             alSourcePlay( moving_source );
             do {
             	/* sleep for a bit */
             	micro_sleep( 500000 );
     
                     shouldend = time(NULL);
                     if((shouldend - start) > 10) {
                             /* After 10 seconds, we end */
                             alSourceStop( moving_source );
                     }
     
                     alSourcei( moving_source, AL_SOURCE_STATE, &state );
             } while(state != AL_STOPPED);
     
             cleanup();
     
             alcDestroyContext( context_id );
     	alcCloseDevice( dev );
     
             return 0;
     }


automatically generated by info2www version 1.2.2.9