NMS_Audio::NMS_Audio(ALfloat* pListenerPos,ALfloat* pListenerVel,ALfloat* pListenerOri) { // Initialize OpenAL and clear the error bit. alutInit(0, NULL); alGetError(); SetListenerValues(pListenerPos,pListenerVel,pListenerOri); }
int main(int argc, char *argv[]) { printf("MindCode's OpenAL Lesson 1: Single Static Source\n\n"); printf("Controls:\n"); printf("p) Play\n"); printf("s) Stop\n"); printf("h) Hold (pause)\n"); printf("q) Quit\n\n"); // Initialize OpenAL and clear the error bit. alutInit(NULL, 0); alGetError(); // Load the wav data. if(LoadALData() == AL_FALSE) { printf("Error loading data."); return 0; } SetListenerValues(); // Setup an exit procedure. atexit(KillALData); // Loop. ALubyte c = ' '; while(c != 'q') { c = getche(); switch(c) { // Pressing 'p' will begin playing the sample. case 'p': alSourcePlay(Source); break; // Pressing 's' will stop the sample from playing. case 's': alSourceStop(Source); break; // Pressing 'h' will pause the sample. case 'h': alSourcePause(Source); break; }; } return 0; }
/* * ALboolean LoadALData() * * This function will load our sample data from the disk using the alut * utility and send the data into OpenAL as a buffer. A source is then * also created to play that buffer. */ int LoadSoundBuffers() { int theerror=0; // Variables to load into. if (total_loaded_buffers==0) { fprintf(stderr,"No Sounds to load!\n"); return 0; } ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop; // Load wav data into buffers. alGenBuffers(total_loaded_buffers, Buffers); if(alGetError() != AL_NO_ERROR) { fprintf(stderr,"Error Generating Buffers!\n"); return 0; } for (unsigned int i=0; i<total_loaded_buffers; i++) { alutLoadWAVFile((ALbyte *)filenames[i], &format, &data, &size, &freq, &loop); theerror=alGetError(); if(theerror != AL_NO_ERROR) { fprintf(stderr,"Error (%u) loading Wav sound Buffer %u %s\n",theerror,i,filenames[i]); } alBufferData(Buffers[i], format, data, size, freq); theerror=alGetError(); if(theerror != AL_NO_ERROR) { fprintf(stderr,"Error (%u) loading Wav sound Buffer %u %s\n",theerror,i,filenames[i]); } alutUnloadWAV(format, data, size, freq); theerror=alGetError(); if(theerror != AL_NO_ERROR) { fprintf(stderr,"Error (%u) loading Wav sound Buffer %u %s\n",theerror,i,filenames[i]); } free(filenames[i]); /* Release memory for filename */ } // Bind buffers into audio sources. alGenSources(NUM_SOURCES, Sources); theerror=alGetError(); if(theerror != AL_NO_ERROR) { fprintf(stderr,"Error Generating Sources (%u)!\n",theerror); return 0; } for (unsigned int i=0; i<total_loaded_buffers; i++) { /*alGenSources( 1, &Sources[i] ); theerror=alGetError(); if(theerror != AL_NO_ERROR) { cout<<"Error Generating Sources ("<<theerror<<")!\n"; return false; } */ alSourcei (Sources[i], AL_BUFFER, Buffers[i] ); alSourcef (Sources[i], AL_PITCH, 1.0f ); alSourcef (Sources[i], AL_GAIN, 1.0f ); alSourcefv(Sources[i], AL_POSITION, SourcesPos[i]); alSourcefv(Sources[i], AL_VELOCITY, SourcesVel[i]); alSourcei (Sources[i], AL_LOOPING, AL_FALSE ); } // Bind buffers into audio sources. // Do another error check and return. if(alGetError() != AL_NO_ERROR) return 0; SetListenerValues(); return 1; }